home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / x11pmhk.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  8KB  |  321 lines

  1.  
  2. #include "Xlib.h"
  3. #define INCL_BASE
  4. #define INCL_WIN
  5. #define INCL_GPI
  6. #define INCL_WINATOM
  7. #include <os2.h>
  8. #include "X11pmhk.h"
  9. #include <process.h>
  10.  
  11. #include "x11pmvk.h"
  12.  
  13. #define CLIENTHWND(wnd) WinWindowFromID(FID_CLIENT, wnd)
  14.  
  15. static Xlib_Grab *Grab = NULL;
  16. static Xlib_Grab GrabList[1024], *FreeList[1024];
  17. static int LastGrab = 0, GrabAvail = 0;
  18. static HMTX hkmutex = NULLHANDLE;
  19.  
  20. static char *AtomTbl[1024] = {"", NULL};
  21. static char AtomBuf[65536], *AtomTail = AtomBuf;
  22. static int LastAtom = 1;
  23.  
  24. #ifndef NO_RTE
  25. int _CRT_init(void);
  26. void _CRT_term(void);
  27. void __ctordtorInit(void);
  28. void __ctordtorTerm(void);
  29. #endif
  30.  
  31. unsigned long _DLL_InitTerm(unsigned long mod_handle, unsigned long flag)
  32. {
  33.     APIRET rc;
  34.     switch (flag) {
  35.     case 0:
  36. #ifndef NO_RTE
  37.         if (_CRT_init() != 0) return 0;
  38.         __ctordtorInit();
  39. #endif
  40.         if (hkmutex == NULLHANDLE)
  41.             rc = DosCreateMutexSem(NULL, &hkmutex, DC_SEM_SHARED, FALSE);
  42.         else
  43.             rc = DosOpenMutexSem(NULL, &hkmutex);
  44.         if (rc != NO_ERROR) return 0;
  45.         return 1;
  46.     case 1:
  47.         rc = DosCloseMutexSem(hkmutex);
  48. #ifndef NO_RTE
  49.         __ctordtorTerm();
  50.         _CRT_term();
  51. #endif
  52.         return 1;
  53.     default:
  54.         return 0;
  55.     }
  56.     return 1;
  57. }
  58.  
  59. #ifdef NO_RTE
  60. int getpid(void) {
  61.     PPIB pib;
  62.     DosGetInfoBlocks(NULL, &pib);
  63.     return pib->pib_ulpid;
  64. }
  65. #endif
  66.  
  67. Xlib_Grab *Xlib_NewGrab(void)
  68. {
  69.     Xlib_Grab *newg;
  70.  
  71.     /* Add event to the bottom of the list */
  72.     DosRequestMutexSem(hkmutex, SEM_INDEFINITE_WAIT);
  73.  
  74.     if (GrabAvail)
  75.         newg = FreeList[--GrabAvail];
  76.     else
  77.         newg = &GrabList[LastGrab++];
  78.  
  79.         newg->special = 0;
  80.     newg->pid = getpid();
  81.     newg->next = NULL;
  82.  
  83.     if(Grab == NULL)
  84.         Grab = newg;
  85.     else
  86.     {
  87.         Xlib_Grab *tmp;
  88.  
  89.         tmp = Grab;
  90.         while(tmp->next != NULL)
  91.             tmp = tmp->next;
  92.         tmp->next = newg;
  93.     }
  94.     DosReleaseMutexSem(hkmutex);
  95.     return newg;
  96. }
  97.  
  98. void Xlib_RemoveGrab(int type)
  99. {
  100.     Xlib_Grab *prev = NULL, *tmp;
  101.     int pid = getpid();
  102.  
  103.     DosRequestMutexSem(hkmutex, SEM_INDEFINITE_WAIT);
  104.  
  105.     tmp = Grab;
  106.     while(tmp != NULL)
  107.     {
  108.         if(tmp->pid == pid && (tmp->type == type || type == GrabAny))
  109.         {
  110.             if(prev) 
  111.                 prev->next = tmp->next;
  112.             else
  113.                 Grab = tmp->next;
  114.             tmp->pid = tmp->type = 0;
  115.             FreeList[GrabAvail++] = tmp;
  116.             DosReleaseMutexSem(hkmutex);
  117.             return;
  118.         }
  119.         prev = tmp;
  120.         tmp=tmp->next;
  121.     }
  122.     DosReleaseMutexSem(hkmutex);
  123. }
  124.  
  125. void Xlib_RemoveGrabAny(int type)
  126. {
  127.     Xlib_Grab *prev = NULL, *tmp;
  128.         int pid = getpid();
  129.         HFILE hfile = 0L;
  130.         ULONG action = 0L, wrote;
  131.  
  132.         DosRequestMutexSem(hkmutex, SEM_INDEFINITE_WAIT);
  133.         tmp = Grab;
  134.     while(tmp != NULL)
  135.     {
  136.         if(tmp->type == type || type == GrabAny)
  137.                 {
  138.             if(prev) 
  139.                 prev->next = tmp->next;
  140.             else
  141.                 Grab = tmp->next;
  142.             tmp->pid = tmp->type = 0;
  143.                         FreeList[GrabAvail++] = tmp;
  144.                         if(prev)
  145.                             tmp = prev;
  146.                         else
  147.                             tmp = Grab;
  148.         }
  149.                 prev = tmp;
  150.                 if(tmp)
  151.                     tmp=tmp->next;
  152.         }
  153.     DosReleaseMutexSem(hkmutex);
  154. }
  155.  
  156. Xlib_Grab *__FindGrab(int pid, int type)
  157. {
  158.     Xlib_Grab *tmp;
  159.  
  160.     DosRequestMutexSem(hkmutex, SEM_INDEFINITE_WAIT);
  161.     tmp = Grab;
  162.     while(tmp != NULL)
  163.     {
  164.         if((!pid || tmp->pid == pid) && (tmp->type == type || type == GrabAny))
  165.         {
  166.         DosReleaseMutexSem(hkmutex);
  167.             return tmp;
  168.         }
  169.         
  170.         tmp = tmp->next;
  171.     }
  172.     DosReleaseMutexSem(hkmutex);
  173.     return NULL;
  174. }
  175.  
  176. Xlib_Grab *Xlib_FindGrab(int type)
  177. {
  178.     return __FindGrab(getpid(), type);
  179. }
  180.  
  181. BOOL EXPENTRY Xlib_InputQueueHook (HAB hab, PQMSG pQmsg, USHORT fs)
  182.  
  183. {
  184.     Xlib_Grab *current;
  185.     HWND client;
  186.  
  187.     if(!Grab)
  188.         return FALSE;
  189.  
  190.     switch(pQmsg->msg)
  191.     {
  192.     case WM_CHAR:
  193.         if ((current = __FindGrab(0,GrabKeyboard)) && 
  194.         CLIENTHWND(pQmsg->hwnd) != (client = CLIENTHWND(current->window)))
  195.         {
  196.             if(current->special == SyncKeyboard)
  197.             {
  198.                 current->special = 0;
  199.                 return FALSE;
  200.             }
  201.             WinPostMsg(client, pQmsg->msg, pQmsg->mp1, pQmsg->mp2);
  202.             if(current->keyboard_mode == GrabModeAsync)
  203.                 return FALSE;
  204.             else
  205.                 return TRUE;
  206.         }
  207.         if ((current = __FindGrab(0,GrabKey)) && 
  208.         CLIENTHWND(pQmsg->hwnd) != (client = CLIENTHWND(current->window)))
  209.         {
  210.         int keycode;
  211.             /* TODO: Antony, there probably needs to be keycode
  212.              translation done here, want to do it or should I
  213.              try and figure it out? :) Brian */
  214.         /* RPLY: Brian, how's this code? %^) */
  215.         if (SHORT1FROMMP(pQmsg->mp1) & KC_CHAR)
  216.         keycode = vk(SHORT1FROMMP(pQmsg->mp2), \
  217.             SHORT1FROMMP(pQmsg->mp1)&~(KC_SCANCODE|KC_VIRTUALKEY)); else
  218.         if (SHORT1FROMMP(pQmsg->mp1) & KC_VIRTUALKEY)
  219.         keycode = vk(SHORT2FROMMP(pQmsg->mp2), \
  220.             SHORT1FROMMP(pQmsg->mp1)&~(KC_CHAR|KC_SCANCODE)); else
  221.         if (SHORT1FROMMP(pQmsg->mp1) & KC_SCANCODE)
  222.         keycode = vk(CHAR4FROMMP(pQmsg->mp1), \
  223.             SHORT1FROMMP(pQmsg->mp1)&~(KC_CHAR|KC_VIRTUALKEY)); 
  224.         else return TRUE;
  225.             if (vkmatch(current->keycode, keycode))
  226.             {
  227.                 WinPostMsg(client, pQmsg->msg, pQmsg->mp1, pQmsg->mp2);
  228.                 if(current->keyboard_mode == GrabModeAsync)
  229.                     return FALSE;
  230.                 else
  231.                     return TRUE;
  232.             }
  233.         }
  234.         if((current = __FindGrab(0,GrabServer)) && 
  235.         CLIENTHWND(pQmsg->hwnd) != (client = CLIENTHWND(current->window)))
  236.         {
  237.             WinPostMsg(client, pQmsg->msg, pQmsg->mp1, pQmsg->mp2);
  238.             return TRUE;
  239.         }
  240.         break;
  241.     case WM_BUTTON1UP:
  242.     case WM_BUTTON2UP:
  243.     case WM_BUTTON3UP:
  244.     case WM_BUTTON1DOWN:
  245.     case WM_BUTTON2DOWN:
  246.     case WM_BUTTON3DOWN:
  247.     case WM_MOUSEMOVE:
  248.         if((current = __FindGrab(0,GrabPointer)) && 
  249.         CLIENTHWND(pQmsg->hwnd) != (client = CLIENTHWND(current->window)))
  250.         {
  251.             if(current->special == SyncPointer)
  252.             {
  253.                 current->special = 0;
  254.                 return FALSE;
  255.             }
  256.             WinPostMsg(client, pQmsg->msg, pQmsg->mp1, pQmsg->mp2);
  257.             if(current->pointer_mode == GrabModeAsync)
  258.                 return FALSE;
  259.             else
  260.                 return TRUE;
  261.         }
  262.         if((current = __FindGrab(0,GrabButton)) && 
  263.         CLIENTHWND(pQmsg->hwnd) != (client = CLIENTHWND(current->window)))
  264.         {
  265.             if((current->button == 1 && (pQmsg->msg == WM_BUTTON1UP || pQmsg->msg == WM_BUTTON1DOWN)) ||
  266.                (current->button == 2 && (pQmsg->msg == WM_BUTTON3UP || pQmsg->msg == WM_BUTTON3DOWN)) ||
  267.                (current->button == 3 && (pQmsg->msg == WM_BUTTON2UP || pQmsg->msg == WM_BUTTON2DOWN)))
  268.             {
  269.                 WinPostMsg(client, pQmsg->msg, pQmsg->mp1, pQmsg->mp2);
  270.                 if(current->pointer_mode == GrabModeAsync)
  271.                     return FALSE;
  272.                 else
  273.                     return TRUE;
  274.             }
  275.         }
  276.         if((current = __FindGrab(0,GrabServer)) && 
  277.         CLIENTHWND(pQmsg->hwnd) != (client = CLIENTHWND(current->window)))
  278.         {
  279.             WinPostMsg(client, pQmsg->msg, pQmsg->mp1, pQmsg->mp2);
  280.             return TRUE;
  281.         }
  282.         break;
  283.     }
  284.         
  285.     return FALSE;
  286. }
  287.  
  288. Atom Xlib_XInternAtom(char* atom_name, Bool only_if_exists)
  289. {
  290.     char **tbl = AtomTbl;
  291.     int i = 0;
  292.     if (atom_name) {
  293.         DosRequestMutexSem(hkmutex, SEM_INDEFINITE_WAIT);
  294.  
  295.         while (i<LastAtom) {
  296.             if (!strcmp(*tbl, atom_name)) {
  297.                 DosReleaseMutexSem(hkmutex);
  298.                 return (Atom) i;
  299.             }
  300.             i++; tbl++;
  301.         }
  302.         if (!only_if_exists) {
  303.             strcpy(AtomTbl[i = LastAtom++] = AtomTail, atom_name);
  304.             AtomTail += strlen(atom_name)+1;
  305.             DosReleaseMutexSem(hkmutex);
  306.             return (Atom) i;
  307.         }
  308.         DosReleaseMutexSem(hkmutex);
  309.     }
  310.     return (Atom) 0;
  311. }
  312.  
  313. char *Xlib_GetAtomName(Atom atom)
  314. {
  315.     if (atom > 0 && atom < LastAtom) {
  316.         return AtomTbl[atom];
  317.     } else 
  318.         return NULL;
  319. }
  320.  
  321.