home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Programming / Atmel / INCCT_Programmer / Src / gadget.c < prev    next >
C/C++ Source or Header  |  2000-08-04  |  11KB  |  469 lines

  1. /***************************************************************************/
  2. /* INCCT GUI V1.00                                                         */
  3. /* A quickly put together GUI for the Atmel in circuit programmer.         */
  4. /* 1999 LJS                                                                */
  5. /***************************************************************************/
  6.  
  7. #define INTUI_V36_NAMES_ONLY
  8.  
  9. #include <exec/types.h>
  10. #include <exec/io.h>
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <devices/parallel.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <libraries/gadtools.h>
  17.  
  18. #include <clib/exec_protos.h>
  19. #include <clib/graphics_protos.h>
  20. #include <clib/intuition_protos.h>
  21. #include <clib/gadtools_protos.h>
  22. #include <clib/alib_protos.h>
  23.  
  24. #include <stdio.h>
  25.  
  26. #include "globdefs.h"
  27.  
  28. #define MYGAD_STRING1   (1)
  29. #define MYGAD_FILENAME  (2)
  30. #define MYGAD_BUTTON    (3)
  31. #define MYGAD_PROGRAM   (4)
  32. #define MYGAD_LOCK      (5)
  33. #define MYGAD_READF     (6)
  34. #define MYGAD_EEPROM    (7)
  35. #define MAXGADS 8           /*number of gadgets + 1*/
  36.  
  37. void errorMessage(STRPTR error, int Err);
  38. VOID handleGadgetEvent(struct Window *win, struct Gadget *gad, UWORD code,
  39.     struct Gadget *my_gads[]);
  40. VOID handleVanillaKey(struct Window *win, UWORD code,
  41.     struct Gadget *my_gads[]);
  42. int createAllGadgets(struct Gadget **glistptr, void *vi,
  43.     UWORD topborder, struct Gadget *my_gads[]);
  44. VOID process_window_events(struct Window *mywin, struct Gadget *my_gads[]);
  45. VOID gadtoolsWindow(VOID);
  46. void StartProgram(BYTE X);
  47. void StartRead(void);
  48. void FreeParallel(void);
  49. int SaveParallel(void);
  50.  
  51. extern char FileName[];
  52. char Lock=0;
  53.  
  54. char Vers[]="$VER:INCCT AVR Programmer "VERSION;
  55.  
  56. struct EasyStruct myES = {
  57.     sizeof(struct EasyStruct),
  58.     0,
  59.     "Error",
  60.     "%s %ld",
  61.     "OK"
  62. };
  63.  
  64. struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0, };
  65.  
  66. struct Library      *IntuitionBase;
  67. struct Library      *GfxBase;
  68. struct Library      *GadToolsBase;
  69. struct MsgPort      *ParallelMP=NULL; 
  70. struct IOExtPar     *ParallelIO=NULL; 
  71.  
  72. struct Screen   *mysc;      /* The screen we are on */
  73.  
  74. int main(int argc, char *argv[])
  75. {
  76.  
  77.   FileName[0]=0;
  78.     
  79. if (NULL == (IntuitionBase = OpenLibrary("intuition.library", 37)))
  80.   printf("Can't open intuition.library V37 !!!!\n");
  81.    /*What else can we do?*/
  82. }
  83. else
  84. {
  85.   if(!create_timer())
  86.     errorMessage( "Can't open timer.device",0);
  87.   else
  88.   {
  89.     if(!SaveParallel())
  90.       errorMessage("Can't open parallel.device",0);    
  91.     else
  92.     {
  93.       if (NULL == (GfxBase = OpenLibrary("graphics.library", 37)))
  94.         errorMessage( "Requires V37 graphics.library",0);
  95.       else
  96.       {
  97.         if (NULL == (GadToolsBase = OpenLibrary("gadtools.library", 37)))
  98.           errorMessage( "Requires V37 gadtools.library",0);
  99.         else
  100.         {
  101.           if(argc>1)
  102.           {
  103.             Program_It(argc,argv);
  104.           }
  105.           else
  106.           {
  107.             gadtoolsWindow();
  108.           }
  109.           CloseLibrary(GadToolsBase);
  110.         }
  111.         CloseLibrary(GfxBase);
  112.       }
  113.       CloseDevice((struct IORequest *)ParallelIO);
  114.       FreeParallel();
  115.     }
  116.     delete_timer();
  117.   }
  118.   CloseLibrary(IntuitionBase);
  119. }
  120.   return 1;
  121. }
  122.  
  123.  
  124. void errorMessage(STRPTR error, int Err)
  125. {
  126.   if (error)
  127.   {
  128.     EasyRequest(NULL, &myES, NULL, error, Err);
  129.   }
  130. }
  131.  
  132. VOID handleGadgetEvent(struct Window *win, struct Gadget *gad, UWORD code,
  133.     struct Gadget *my_gads[])
  134. {
  135.   switch (gad->GadgetID)
  136.   {
  137.     case MYGAD_FILENAME:
  138.         strcpy(FileName,  ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  139.         break;
  140.  
  141.     case MYGAD_BUTTON: if (FileRequest())
  142.                        {
  143.                         GT_SetGadgetAttrs(my_gads[MYGAD_FILENAME], win, NULL,
  144.                             GTST_String,   FileName,
  145.                             TAG_END);
  146.                        }
  147.                       break;
  148.  
  149.     case MYGAD_PROGRAM:
  150.          StartProgram(1);
  151.          break;
  152.  
  153.     case MYGAD_READF:
  154.           StartRead();
  155.           break;
  156.  
  157.     case MYGAD_EEPROM:
  158.          StartProgram(2);
  159.          break;
  160.  
  161.     case MYGAD_LOCK:
  162.          {
  163.            Lock^=1;
  164.          }
  165.          break;
  166.   }
  167. }
  168.  
  169.  
  170. VOID handleVanillaKey(struct Window *win, UWORD code, struct Gadget *my_gads[])
  171. {
  172.  
  173.   switch (code)
  174.   {
  175.     case 's':
  176.     case 'S':
  177.         ActivateGadget(my_gads[MYGAD_FILENAME], win, NULL);
  178.         break;
  179.  
  180.     case 'w':
  181.     case 'W':
  182.          StartProgram(2);
  183.          break;
  184.  
  185.     case 'c':
  186.     case 'C':
  187.          if (FileRequest())
  188.          {
  189.            GT_SetGadgetAttrs(my_gads[MYGAD_FILENAME], win, NULL,
  190.                              GTST_String,   FileName,
  191.                             TAG_END);
  192.          }
  193.         break;
  194.     case 'l':
  195.     case 'L':
  196.         Lock^=1;
  197.         if(Lock)
  198.         {
  199.           GT_SetGadgetAttrs(my_gads[MYGAD_LOCK],win,NULL,GTCB_Checked,TRUE,TAG_END);
  200.         }
  201.         else
  202.         {
  203.           GT_SetGadgetAttrs(my_gads[MYGAD_LOCK],win,NULL,GTCB_Checked,FALSE,TAG_END);
  204.         }
  205.         break;
  206.     case 'p':
  207.     case 'P':
  208.         StartProgram(1);
  209.         break;
  210.   
  211.     case 'r':
  212.     case 'R':
  213.         StartRead();
  214.         break;
  215.   }
  216. }
  217.  
  218.  
  219. int createAllGadgets(struct Gadget **glistptr, void *vi,
  220.     UWORD topborder, struct Gadget *my_gads[])
  221. {
  222. struct NewGadget ng;
  223. struct Gadget *gad;
  224.  
  225. gad = CreateContext(glistptr);
  226.  
  227. ng.ng_LeftEdge   = 100;
  228. ng.ng_TopEdge    = 10+topborder;
  229. ng.ng_Width      = 200;
  230. ng.ng_Height     = 12;
  231. ng.ng_TextAttr   = &Topaz80;
  232. /* ng.ng_TextAttr   = mysc->Font; */
  233. ng.ng_VisualInfo = vi;
  234. ng.ng_GadgetText = "";
  235. ng.ng_Flags      = NG_HIGHLABEL;
  236. ng.ng_GadgetID   = MYGAD_STRING1;
  237. my_gads[MYGAD_STRING1] = gad = CreateGadget(TEXT_KIND, gad, &ng,
  238.                     GTTX_Text,   "ATMEL In Circuit Programmer.",
  239.                     GTTX_Border, FALSE,
  240.                     TAG_END);
  241. if(gad == NULL) return 1;
  242.  
  243. ng.ng_TopEdge   += 15;
  244. ng.ng_Width      = 200;
  245. ng.ng_GadgetText = "_S Record:";
  246. ng.ng_GadgetID   = MYGAD_FILENAME;
  247. my_gads[MYGAD_FILENAME] = gad = CreateGadget(STRING_KIND, gad, &ng,
  248.                     GTST_String,   FileName,
  249.                     GTST_MaxChars, 256,
  250.                     GT_Underscore, '_',
  251.                     TAG_END);
  252. if(gad == NULL) return 2;
  253.  
  254. ng.ng_TopEdge   += 20;
  255. ng.ng_LeftEdge   = 20;
  256. ng.ng_Width      = 100;
  257. ng.ng_Height     = 12;
  258. ng.ng_GadgetText = "Select _code";
  259. ng.ng_GadgetID   = MYGAD_BUTTON;
  260. ng.ng_Flags      = 0;
  261. my_gads[MYGAD_BUTTON] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
  262.                     GT_Underscore, '_',
  263.                     TAG_END);
  264. if(gad == NULL) return 3;
  265.  
  266. ng.ng_LeftEdge  += 120;
  267. ng.ng_Width      = 100;
  268. ng.ng_Height     = 12;
  269. ng.ng_GadgetText = "_Program";
  270. ng.ng_GadgetID   = MYGAD_PROGRAM;
  271. my_gads[MYGAD_PROGRAM] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
  272.                     GT_Underscore, '_',
  273.                     TAG_END);
  274. if(gad == NULL) return 4;
  275.  
  276. ng.ng_LeftEdge  += 170;
  277. ng.ng_Height     = 12;
  278. ng.ng_GadgetText = "_Lock";
  279. ng.ng_GadgetID   = MYGAD_LOCK;
  280. my_gads[MYGAD_LOCK] = gad = CreateGadget(CHECKBOX_KIND, gad, &ng,GT_Underscore, '_',
  281.                     TAG_END);
  282. if(gad == NULL) return 5;
  283.  
  284. ng.ng_LeftEdge  -= 170;
  285. ng.ng_TopEdge   += 20;
  286. ng.ng_GadgetText = "_Read Flash";
  287. ng.ng_GadgetID   = MYGAD_READF;
  288. my_gads[MYGAD_READF] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
  289.                     GT_Underscore, '_',
  290.                     TAG_END);
  291. if(gad == NULL) return 6;
  292.  
  293. ng.ng_LeftEdge  -= 120;
  294. ng.ng_GadgetText = "_Write EEPROM";
  295. ng.ng_GadgetID   = MYGAD_EEPROM;
  296. my_gads[MYGAD_EEPROM] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
  297.                     GT_Underscore, '_',
  298.                     TAG_END);
  299. if(gad == NULL) return 7;
  300.  
  301. return 0;
  302. }
  303.  
  304. VOID process_window_events(struct Window *mywin, struct Gadget *my_gads[])
  305. {
  306.   struct IntuiMessage *imsg;
  307.   ULONG imsgClass;
  308.   UWORD imsgCode;
  309.   struct Gadget *gad;
  310.   BOOL terminated = FALSE;
  311.  
  312.   while (!terminated)
  313.   {
  314.     Wait (1 << mywin->UserPort->mp_SigBit);
  315.  
  316.     while ( (!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)) )
  317.     {
  318.       gad = (struct Gadget *)imsg->IAddress;
  319.  
  320.       imsgClass = imsg->Class;
  321.       imsgCode = imsg->Code;
  322.  
  323.       GT_ReplyIMsg(imsg);
  324.  
  325.       switch (imsgClass)
  326.       {
  327.         case IDCMP_GADGETDOWN:
  328.        /* case IDCMP_MOUSEMOVE:*/
  329.         case IDCMP_GADGETUP:
  330.                  handleGadgetEvent(mywin, gad, imsgCode, my_gads);
  331.                         break;
  332.         case IDCMP_VANILLAKEY:
  333.                 handleVanillaKey(mywin, imsgCode, my_gads);
  334.                 break;
  335.         case IDCMP_CLOSEWINDOW:
  336.                 terminated = TRUE;
  337.                 break;
  338.         case IDCMP_REFRESHWINDOW:
  339.                 GT_BeginRefresh(mywin);
  340.                 GT_EndRefresh(mywin, TRUE);
  341.                 break;
  342.       }
  343.     }
  344.   }
  345. }
  346.  
  347. VOID gadtoolsWindow(VOID)
  348. {
  349. struct Window   *mywin;
  350. struct Gadget   *glist, *my_gads[MAXGADS];
  351. void            *vi;
  352. UWORD           topborder;
  353. int Err;
  354.  
  355.   if (NULL == (mysc = LockPubScreen(NULL)))
  356.   {
  357.     errorMessage( "Couldn't lock default public screen",0);
  358.   }
  359.   else
  360.   {
  361.     if (NULL == (vi = GetVisualInfo(mysc, TAG_END)))
  362.     {
  363.       errorMessage( "GetVisualInfo() failed",0);
  364.     }
  365.     else
  366.     {
  367.       topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  368.  
  369.       if (Err = createAllGadgets(&glist, vi, topborder, my_gads))
  370.       {
  371.         errorMessage( "createAllGadgets() failed", Err);
  372.       }
  373.       else
  374.       {
  375.         mywin = OpenWindowTags(NULL,
  376.                      WA_Title,     "INCCT "VERSION" ©2000 LJS",
  377.                      WA_Gadgets,   glist,      WA_AutoAdjust,    TRUE,
  378.                      WA_Width,       400,      WA_MinWidth,        50,
  379.                      WA_InnerHeight, 80,      WA_MinHeight,       50,
  380.                      WA_DragBar,    TRUE,      WA_DepthGadget,   TRUE,
  381.                      WA_Activate,   TRUE,      WA_CloseGadget,   TRUE,
  382.                      WA_SizeGadget, FALSE,      WA_SimpleRefresh, TRUE,
  383.                      WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  384.                          IDCMP_VANILLAKEY | SLIDERIDCMP | STRINGIDCMP |
  385.                          BUTTONIDCMP,
  386.                      WA_PubScreen, mysc,
  387.                      TAG_END);
  388.         if(mywin == NULL)
  389.         { 
  390.           errorMessage( "OpenWindow() failed",0);
  391.         }
  392.         else
  393.         {
  394.           GT_RefreshWindow(mywin, NULL);
  395.  
  396.           process_window_events(mywin, my_gads);
  397.           CloseWindow(mywin);
  398.         }
  399.       }
  400.       FreeGadgets(glist);
  401.       FreeVisualInfo(vi);
  402.     }
  403.     UnlockPubScreen(NULL, mysc);
  404.   }
  405. }
  406.  
  407. void StartProgram(BYTE X)
  408. {
  409.   char *Args[3];
  410.   BYTE Aargc=2;
  411.   switch(X)
  412.   {
  413.     case 1:
  414.       if(Lock)
  415.       {
  416.         Args[2]="-l";
  417.         Aargc=3;
  418.       }
  419.       break;
  420.  
  421.     case 2:
  422.       Args[2]="-ee";
  423.       Aargc=3;
  424.       break;
  425.  
  426.     default:
  427.       break;
  428.   }
  429.  
  430.   Args[0]="";
  431.   Args[1]=FileName;
  432.   Program_It(Aargc,Args);
  433. }
  434.  
  435. void StartRead(void)
  436. {
  437.   char *Args[3];
  438.   Args[0]="";
  439.   Args[1]=FileName;
  440.   Args[2]="-r";
  441.   Program_It(3,Args);
  442. }
  443.  
  444. int SaveParallel(void)
  445. {
  446.   if (ParallelMP=CreatePort(0,0) )
  447.   {
  448.     if (ParallelIO=(struct IOExtPar *)
  449.         CreateExtIO(ParallelMP,sizeof(struct IOExtPar)) )
  450.     {
  451.       if (OpenDevice(PARALLELNAME,0L,(struct IORequest *)ParallelIO,0) )
  452.       {
  453.         return 0;
  454.       }
  455.       else
  456.       {
  457.         return 1;
  458.       }
  459.     }
  460.   }
  461. }
  462.  
  463. void FreeParallel(void)
  464. {
  465.   if(ParallelIO) DeleteExtIO((struct IORequest*)ParallelIO);
  466.   if(ParallelMP) DeletePort(ParallelMP);
  467. }
  468.