home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 325.lha / OSK / main.c < prev    next >
C/C++ Source or Header  |  1989-12-28  |  10KB  |  375 lines

  1. /****************************************************************************
  2.  
  3.                          OSK - The OnScreen Keyboard
  4.  
  5.                      © Copyright 1989 by Jonathan Potter
  6.  
  7.        Ever been caught without your keyboard? This program is for you!
  8.  
  9.           Not public domain, but as always, freely redistributable.
  10.  
  11. ****************************************************************************/
  12.  
  13. /* To make from Manx
  14.  
  15.    cc +L main
  16.    cc +L data
  17.    cc +L image
  18.    ln +Cd -O OSK main data image -Lc32 */
  19.  
  20. /* Not using pre-compiled includes? Work them out yourself, then */
  21.  
  22. #include "OSK.h"
  23.  
  24. main(argc,argv)
  25. int argc;
  26. char *argv[];
  27. {
  28.     ULONG class;
  29.     USHORT code,gadgetid,qual=0;
  30.     int a,x,y;
  31.     struct Gadget *gad;
  32.  
  33.     port=CreatePort(0,0);
  34.     request=CreateStdIO(port);
  35.     OpenDevice("input.device",0,request,0);
  36.  
  37.     IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",0);
  38.     GfxBase=(struct GfxBase *) OpenLibrary("graphics.library",0);
  39.  
  40.     Window=NULL;
  41.     fakewindow=IntuitionBase->ActiveWindow;
  42.     if (argc>1 && strncmp(argv[1],"-i",2)==0) iconify();
  43.     else setupall();
  44.  
  45.     FOREVER {
  46.         Wait(1<<Window->UserPort->mp_SigBit);
  47.         while (Msg=(struct IntuiMessage *) GetMsg(Window->UserPort)) {
  48.             class=Msg->Class; code=Msg->Code;
  49.             if (class==GADGETUP || class==GADGETDOWN) {
  50.                 gad=(struct Gadget *) Msg->IAddress;
  51.                 gadgetid=gad->GadgetID;
  52.             }
  53.             ReplyMsg((struct Message *) Msg);
  54.             switch (class) {
  55.                 case CLOSEWINDOW:
  56.                     quit();
  57.                 case GADGETUP:
  58.                 case GADGETDOWN:
  59.                     if (gadgetid==999) {
  60.                         /* Choose a new window */
  61.                         hilitegad(gad);
  62.                         while (mousebutton());
  63.                         while (!mousebutton());
  64.                         Delay(5); /* Give Intuition a chance to catch up */
  65.                         if (IntuitionBase->ActiveWindow) /* Make sure it was a window,
  66.                                                 not just an empty screen */
  67.                             fakewindow=IntuitionBase->ActiveWindow;
  68.                         hilitegad(gad);
  69.                         break;
  70.                     }
  71.           /* Work out qualifiers */
  72.                     if (gadgetid==0x63) {
  73.                         if (qual&IEQUALIFIER_CONTROL) qual-=IEQUALIFIER_CONTROL;
  74.                         else qual+=IEQUALIFIER_CONTROL;
  75.                     }
  76.                     else if (gadgetid==0x62) {
  77.                         if (qual&IEQUALIFIER_CAPSLOCK) qual-=IEQUALIFIER_CAPSLOCK;
  78.                         else qual+=IEQUALIFIER_CAPSLOCK;
  79.                     }
  80.                     else if (gadgetid==0x60) {
  81.                         if (qual&IEQUALIFIER_LSHIFT) qual-=IEQUALIFIER_LSHIFT;
  82.                         else qual+=IEQUALIFIER_LSHIFT;
  83.                     }
  84.                     else if (gadgetid==0x61) {
  85.                         if (qual&IEQUALIFIER_RSHIFT) qual-=IEQUALIFIER_RSHIFT;
  86.                         else qual+=IEQUALIFIER_RSHIFT;
  87.                     }
  88.                     else if (gadgetid==0x64) {
  89.                         if (qual&IEQUALIFIER_LALT) qual-=IEQUALIFIER_LALT;
  90.                         else qual+=IEQUALIFIER_LALT;
  91.                     }
  92.                     else if (gadgetid==0x65) {
  93.                         if (qual&IEQUALIFIER_RALT) qual-=IEQUALIFIER_RALT;
  94.                         else qual+=IEQUALIFIER_RALT;
  95.                     }
  96.                     else if (gadgetid==0x66) {
  97.                         if (qual&IEQUALIFIER_LCOMMAND) qual-=IEQUALIFIER_LCOMMAND;
  98.                         else qual+=IEQUALIFIER_LCOMMAND;
  99.                     }
  100.                     else if (gadgetid==0x67) {
  101.                         if (qual&IEQUALIFIER_RCOMMAND) qual-=IEQUALIFIER_RCOMMAND;
  102.                         else qual+=IEQUALIFIER_RCOMMAND;
  103.                     }
  104.                     else if (gadgetid==1000) iconify();
  105.                     else {
  106.             /* Activate window so key presses will be sent to it */
  107.                         ActivateWindow(fakewindow);
  108.                         hilitegad(gad);
  109.                         FakeKey(gadgetid,qual);
  110.                         if (!mousebutton()) {
  111.                             hilitegad(gad);
  112.                             break;
  113.                         }
  114.                         Delay(5);
  115.                         if (!mousebutton()) {
  116.                             hilitegad(gad);
  117.                             break;
  118.                         }
  119.                         FOREVER {
  120.                             FakeKey(gadgetid,qual);
  121.                             if (!mousebutton()) {
  122.                                 hilitegad(gad);
  123.                                 break;
  124.                             }
  125.                             Delay(1);
  126.                         }
  127.                     }
  128.                     break;
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134. quit()
  135. {
  136.     freekeys(); /* De-allocate key gadgets */
  137.     if (Window) CloseWindow(Window);
  138.     if (GfxBase) CloseLibrary((struct Library *) GfxBase);
  139.     if (IntuitionBase) CloseLibrary((struct IntuitionBase *) IntuitionBase);
  140.     CloseDevice(request);
  141.     DeleteStdIO(request);
  142.     DeletePort(port);
  143.     exit(0);
  144. }
  145.  
  146. setupall()
  147. {
  148.     int a;
  149.     char buf[80];
  150.  
  151.     if (!(Window=OpenWindow(&win))) quit();
  152.     rp=Window->RPort;
  153.     SetAPen(rp,1);
  154.     RectFill(rp,4,11,499,103);
  155.     SetAPen(rp,2);
  156.  
  157.     first=NULL; gadget=NULL;
  158.  
  159.     AddGList(Window,&iconGadget,1,1,NULL);
  160.     RefreshGList(&iconGadget,Window,NULL,1);
  161.  
  162.     /* Setup keyboard */
  163.  
  164.     addkey("ESC",10,15,26,0,0x45); /* <-- last value is GadgetID.
  165.                                         We use the rawkey code for that key
  166.                                         to make things much easier */
  167.     for (a=0;a<10;a++) {
  168.         sprintf(buf,"F%d",a+1);
  169.         addkey(buf,50+(a*32),15,26,0,0x50+a);
  170.     }
  171.     addkey("DEL",378,15,26,0,0x46);
  172.     addkey("`",10,30,27,0,0);
  173.     for (a=0;a<10;a++) {
  174.         if (a<9) sprintf(buf,"%d",a+1);
  175.         else strcpy(buf,"0");
  176.         addkey(buf,45+(a*22),30,15,0,a+1);
  177.     }
  178.     addkey("-",265,30,15,0,0xb);
  179.     addkey("=",287,30,15,0,0xc);
  180.     addkey("\\",309,30,15,0,0xd);
  181.     addkey("BACKSPACE",331,30,73,0,0x41);
  182.  
  183.     addkey("TAB",10,45,37,0,0x42);
  184.     addkey("q",55,45,15,0,0x10);
  185.     addkey("w",77,45,15,0,0x11);
  186.     addkey("e",99,45,15,0,0x12);
  187.     addkey("r",121,45,15,0,0x13);
  188.     addkey("t",143,45,15,0,0x14);
  189.     addkey("y",165,45,15,0,0x15);
  190.     addkey("u",187,45,15,0,0x16);
  191.     addkey("i",209,45,15,0,0x17);
  192.     addkey("o",231,45,15,0,0x18);
  193.     addkey("p",253,45,15,0,0x19);
  194.     addkey("[",275,45,15,0,0x1a);
  195.     addkey("]",297,45,15,0,0x1b);
  196.     addkey("CR",319,45,43,0,0x44);
  197.     addkey("HELP",369,45,35,0,0x5f);
  198.  
  199.     addkey("CT",10,60,18,1,0x63);
  200.     addkey("CP",35,60,18,1,0x62);
  201.     addkey("a",60,60,15,0,0x20);
  202.     addkey("s",82,60,15,0,0x21);
  203.     addkey("d",104,60,15,0,0x22);
  204.     addkey("f",126,60,15,0,0x23);
  205.     addkey("g",148,60,15,0,0x24);
  206.     addkey("h",170,60,15,0,0x25);
  207.     addkey("j",192,60,15,0,0x26);
  208.     addkey("k",214,60,15,0,0x27);
  209.     addkey("l",236,60,15,0,0x28);
  210.     addkey(";",258,60,15,0,0x29);
  211.     addkey("'",280,60,15,0,0x2a);
  212.     addkey("CR",302,60,60,0,0x44);
  213.     addkey("/\\",369,60,35,0,0x4c);
  214.  
  215.     addkey("SHIFT",10,75,50,1,0x60);
  216.     addkey("z",67,75,15,0,0x31);
  217.     addkey("x",89,75,15,0,0x32);
  218.     addkey("c",111,75,15,0,0x33);
  219.     addkey("v",133,75,15,0,0x34);
  220.     addkey("b",155,75,15,0,0x35);
  221.     addkey("n",177,75,15,0,0x36);
  222.     addkey("m",199,75,15,0,0x37);
  223.     addkey(",",221,75,15,0,0x38);
  224.     addkey(".",243,75,15,0,0x39);
  225.     addkey("/",265,75,15,0,0x3a);
  226.     addkey("SHIFT",287,75,50,1,0x61);
  227.     addkey("<-",344,75,35,0,0x4f);
  228.     addkey("->",386,75,35,0,0x4e);
  229.  
  230.     addkey("ALT",30,90,30,1,0x64);
  231.     addkey("A",67,90,30,1,0x66);
  232.     addkey("SPACE",104,90,146,0,0x40);
  233.     addkey("A",257,90,30,1,0x67);
  234.     addkey("ALT",294,90,30,1,0x65);
  235.     addkey("\\/",369,90,35,0,0x4d);
  236.  
  237.     addkey("7",435,30,15,0,0x3d);
  238.     addkey("8",457,30,15,0,0x3e);
  239.     addkey("9",479,30,15,0,0x3f);
  240.     addkey("4",435,45,15,0,0x2d);
  241.     addkey("5",457,45,15,0,0x2e);
  242.     addkey("6",479,45,15,0,0x2f);
  243.     addkey("1",435,60,15,0,0x1d);
  244.     addkey("2",457,60,15,0,0x1e);
  245.     addkey("3",479,60,15,0,0x1f);
  246.     addkey("0",435,75,37,0,0xf);
  247.     addkey(".",479,75,15,0,0x3c);
  248.     addkey("-",435,90,15,0,0x4a);
  249.     addkey("CR",457,90,37,0,0x43);
  250.  
  251.     addkey("Window",435,15,59,0,999); /* <-- This is the only non-key gadget
  252.                                            and lets us choose a new window */
  253. }
  254.  
  255. iconify()
  256. {
  257.     ULONG prevsec=0,prevmic=0,sec,mic;
  258.     freekeys();
  259.     if (Window) {
  260.         win.LeftEdge=Window->LeftEdge; win.TopEdge=Window->TopEdge;
  261.         CloseWindow(Window);
  262.     }
  263.     Window=(struct Window *) OpenWindow(&iconwin);
  264.     FOREVER {
  265.         Wait(1<<Window->UserPort->mp_SigBit);
  266.         while (Msg=(struct IntuiMessage *) GetMsg(Window->UserPort)) {
  267.             sec=Msg->Seconds; mic=Msg->Micros;
  268.             ReplyMsg((struct Message *) Msg);
  269.             if (DoubleClick(prevsec,prevmic,sec,mic)) {
  270.                 iconwin.LeftEdge=Window->LeftEdge; iconwin.TopEdge=Window->TopEdge;
  271.                 CloseWindow(Window);
  272.                 setupall();
  273.                 return(0);
  274.             }
  275.             prevsec=sec; prevmic=mic;
  276.         }
  277.     }
  278. }
  279.  
  280. /* A routine to generate gadgets.. shorter than having data
  281.    for hundreds of gadgets */
  282.  
  283. addkey(str,x,y,w,t,id)
  284. char *str;
  285. SHORT x,y,w,t;
  286. USHORT id;
  287. {
  288.     temp=(struct GList *) AllocMem(sizeof(struct GList),MEMF_CLEAR);
  289.     if (first==NULL) first=temp;
  290.     else gadget->next=temp;
  291.     gadget=temp;
  292.     gadget->next=NULL;
  293.  
  294.     gadget->text.FrontPen=0;
  295.     gadget->text.BackPen=0;
  296.     gadget->text.DrawMode=JAM1;
  297.     gadget->text.LeftEdge=(w-(strlen(str)*8))/2;
  298.     gadget->text.TopEdge=1;
  299.     gadget->text.ITextFont=NULL;
  300.     gadget->text.IText=(UBYTE *) str;
  301.     gadget->text.NextText=NULL;
  302.  
  303.     gadget->gadget.NextGadget=NULL;
  304.     gadget->gadget.LeftEdge=x;
  305.     gadget->gadget.TopEdge=y;
  306.     gadget->gadget.Width=w;
  307.     gadget->gadget.Height=10;
  308.     gadget->gadget.Flags=GADGHCOMP;
  309.     if (t==0) gadget->gadget.Activation=GADGIMMEDIATE;
  310.     else gadget->gadget.Activation=GADGIMMEDIATE|TOGGLESELECT;
  311.     gadget->gadget.GadgetType=BOOLGADGET;
  312.     gadget->gadget.GadgetRender=NULL;
  313.     gadget->gadget.SelectRender=NULL;
  314.     gadget->gadget.GadgetText=&gadget->text;
  315.     gadget->gadget.MutualExclude=NULL;
  316.     gadget->gadget.SpecialInfo=NULL;
  317.     gadget->gadget.GadgetID=id;
  318.     gadget->gadget.UserData=NULL;
  319.  
  320.     AddGList(Window,&gadget->gadget,-1,1,NULL);
  321.     RefreshGList(&gadget->gadget,Window,NULL,1);
  322.  
  323.     /* Draw the border ourselves */
  324.     Move(rp,x-1,y-1);
  325.     Draw(rp,x+w,y-1);
  326.     Draw(rp,x+w,y+10);
  327.     Draw(rp,x-1,y+10);
  328.     Draw(rp,x-1,y-1);
  329. }
  330.  
  331. /* Deallocate gadget structures */
  332. freekeys()
  333. {
  334.     gadget=first;
  335.     while (gadget) {
  336.         temp=gadget->next;
  337.         if (gadget) FreeMem(gadget,sizeof(struct GList));
  338.         gadget=temp;
  339.     }
  340. }
  341.  
  342. /* Generate a fake key press */
  343. FakeKey(key,qual)
  344. USHORT key,qual;
  345. {
  346.     request->io_Command=IND_WRITEEVENT;
  347.     request->io_Flags=0;
  348.     request->io_Length=sizeof(struct InputEvent);
  349.     request->io_Data=(APTR)&Event;
  350.     Event.ie_NextEvent=NULL;
  351.     Event.ie_Class=IECLASS_RAWKEY;
  352.     Event.ie_TimeStamp.tv_secs=0;
  353.     Event.ie_TimeStamp.tv_micro=0;
  354.     Event.ie_Code=key;
  355.     Event.ie_Qualifier=qual;
  356.     Event.ie_X=0;
  357.     Event.ie_Y=0;
  358.     DoIO(request);
  359. }
  360.  
  361. /* Return state of left mousebutton */
  362. mousebutton()
  363. {
  364.     return(!((USHORT)ciaa.ciapra>>6&1));
  365. }
  366.  
  367. hilitegad(gad)
  368. struct Gadget *gad;
  369. {
  370.     SetDrMd(rp,COMPLEMENT);
  371.     RectFill(rp,gad->LeftEdge,gad->TopEdge,
  372.         gad->LeftEdge+gad->Width-1,gad->TopEdge+gad->Height-1);
  373.     SetDrMd(rp,JAM1);
  374. }
  375.