home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 215 / DDJ9206.ZIP / DFLT12.ZIP / MESSAGE.C < prev    next >
Text File  |  1992-03-28  |  18KB  |  598 lines

  1. /* --------- message.c ---------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. static int px = -1, py = -1;
  6. static int pmx = -1, pmy = -1;
  7. static int mx, my;
  8. static int handshaking = 0;
  9. static BOOL CriticalError;
  10. BOOL AllocTesting = FALSE;
  11. jmp_buf AllocError;
  12.  
  13. /* ---------- event queue ---------- */
  14. static struct events    {
  15.     MESSAGE event;
  16.     int mx;
  17.     int my;
  18. } EventQueue[MAXMESSAGES];
  19.  
  20. /* ---------- message queue --------- */
  21. static struct msgs {
  22.     WINDOW wnd;
  23.     MESSAGE msg;
  24.     PARAM p1;
  25.     PARAM p2;
  26. } MsgQueue[MAXMESSAGES];
  27.  
  28. static int EventQueueOnCtr;
  29. static int EventQueueOffCtr;
  30. static int EventQueueCtr;
  31.  
  32. static int MsgQueueOnCtr;
  33. static int MsgQueueOffCtr;
  34. static int MsgQueueCtr;
  35.  
  36. static int lagdelay = FIRSTDELAY;
  37.  
  38. static void (interrupt far *oldtimer)(void);
  39. static void (interrupt far *oldkeyboard)(void);
  40.  
  41. static int keyportvalue;    /* for watching for key release */
  42.  
  43. WINDOW CaptureMouse;
  44. WINDOW CaptureKeyboard;
  45. static BOOL NoChildCaptureMouse;
  46. static BOOL NoChildCaptureKeyboard;
  47.  
  48. static int doubletimer = -1;
  49. static int delaytimer  = -1;
  50. static int clocktimer  = -1;
  51.  
  52. static WINDOW Cwnd;
  53.  
  54. static void interrupt far newkeyboard(void)
  55. {
  56.     keyportvalue = inp(KEYBOARDPORT);
  57.     oldkeyboard();
  58. }
  59.  
  60. /* ------- timer interrupt service routine ------- */
  61. static void interrupt far newtimer(void)
  62. {
  63.     if (timer_running(doubletimer))
  64.         countdown(doubletimer);
  65.     if (timer_running(delaytimer))
  66.         countdown(delaytimer);
  67.     if (timer_running(clocktimer))
  68.         countdown(clocktimer);
  69.     oldtimer();
  70. }
  71.  
  72. static char ermsg[] = "Error accessing drive x";
  73.  
  74. /* -------- test for critical errors --------- */
  75. int TestCriticalError(void)
  76. {
  77.     int rtn = 0;
  78.     if (CriticalError)    {
  79.         rtn = 1;
  80.         CriticalError = FALSE;
  81.         if (TestErrorMessage(ermsg) == FALSE)
  82.             rtn = 2;
  83.     }
  84.     return rtn;
  85. }
  86.  
  87. /* ------ critical error interrupt service routine ------ */
  88. static void interrupt far newcrit(IREGS ir)
  89. {
  90.     if (!(ir.ax & 0x8000))     {
  91.         ermsg[sizeof(ermsg) - 2] = (ir.ax & 0xff) + 'A';
  92.         CriticalError = TRUE;
  93.     }
  94.     ir.ax = 0;
  95. }
  96.  
  97. static void StopMsg(void)
  98. {
  99.     if (oldtimer != NULL)    {
  100.         setvect(TIMER, oldtimer);
  101.         oldtimer = NULL;
  102.     }
  103.     if (oldkeyboard != NULL)    {
  104.         setvect(KEYBOARDVECT, oldkeyboard);
  105.         oldkeyboard = NULL;
  106.     }
  107.     restorevideo();
  108.     ClearClipboard();
  109.     ClearDialogBoxes();
  110.     restorecursor();    
  111.     unhidecursor();
  112.     hide_mousecursor();
  113. }
  114.  
  115. /* ------------ initialize the message system --------- */
  116. BOOL init_messages(void)
  117. {
  118.     AllocTesting = TRUE;
  119.     if (setjmp(AllocError) != 0)    {
  120.         StopMsg();
  121.         return FALSE;
  122.     }
  123.     initvideo();
  124.     resetmouse();
  125.     set_mousetravel(0, SCREENWIDTH-1, 0, SCREENHEIGHT-1);
  126.     savecursor();
  127.     hidecursor();
  128.     px = py = -1;
  129.     pmx = pmy = -1;
  130.     mx = my = 0;
  131.     CaptureMouse = CaptureKeyboard = NULL;
  132.     NoChildCaptureMouse = FALSE;
  133.     NoChildCaptureKeyboard = FALSE;
  134.     MsgQueueOnCtr = MsgQueueOffCtr = MsgQueueCtr = 0;
  135.     EventQueueOnCtr = EventQueueOffCtr = EventQueueCtr = 0;
  136.     if (oldtimer == NULL)    {
  137.         oldtimer = getvect(TIMER);
  138.         setvect(TIMER, newtimer);
  139.     }
  140.     if (oldkeyboard == NULL)    {
  141.         oldkeyboard = getvect(KEYBOARDVECT);
  142.         setvect(KEYBOARDVECT, newkeyboard);
  143.     }
  144.     setvect(CRIT, newcrit);
  145.     PostMessage(NULL,START,0,0);
  146.     lagdelay = FIRSTDELAY;
  147.     return TRUE;
  148. }
  149.  
  150. /* ----- post an event and parameters to event queue ---- */
  151. static void PostEvent(MESSAGE event, int p1, int p2)
  152. {
  153.     if (EventQueueCtr != MAXMESSAGES)    {
  154.         EventQueue[EventQueueOnCtr].event = event;
  155.         EventQueue[EventQueueOnCtr].mx = p1;
  156.         EventQueue[EventQueueOnCtr].my = p2;
  157.         if (++EventQueueOnCtr == MAXMESSAGES)
  158.             EventQueueOnCtr = 0;
  159.         EventQueueCtr++;
  160.     }
  161. }
  162.  
  163. /* ------ collect mouse, clock, and keyboard events ----- */
  164. static void near collect_events(void)
  165. {
  166.     static int ShiftKeys = 0;
  167.     int sk;
  168.     struct tm *now;
  169.     static BOOL flipflop = FALSE;
  170.     static char timestr[9];
  171.     int hr;
  172.  
  173.     /* -------- test for a clock event (one/second) ------- */
  174.     if (timed_out(clocktimer))    {
  175.         /* ----- get the current time ----- */
  176.         time_t t = time(NULL);
  177.         now = localtime(&t);
  178.         hr = now->tm_hour > 12 ?
  179.              now->tm_hour - 12 :
  180.              now->tm_hour;
  181.         if (hr == 0)
  182.             hr = 12;
  183.         sprintf(timestr, "%2d:%02d", hr, now->tm_min);
  184.         strcpy(timestr+5, now->tm_hour > 11 ? "pm " : "am ");
  185.         /* ------- blink the : at one-second intervals ----- */
  186.         if (flipflop)
  187.             *(timestr+2) = ' ';
  188.         flipflop ^= TRUE;
  189.         /* -------- reset the timer -------- */
  190.         set_timer(clocktimer, 1);
  191.         /* -------- post the clock event -------- */
  192.         PostEvent(CLOCKTICK, FP_SEG(timestr), FP_OFF(timestr));
  193.     }
  194.  
  195.     /* --------- keyboard events ---------- */
  196.     if ((sk = getshift()) != ShiftKeys)    {
  197.         ShiftKeys = sk;
  198.         /* ---- the shift status changed ---- */
  199.         PostEvent(SHIFT_CHANGED, sk, 0);
  200.     }
  201.  
  202.     /* ---- build keyboard events for key combinations that
  203.         BIOS doesn't report --------- */
  204.     if (sk & ALTKEY)    {
  205.         if (keyportvalue == 14)    {
  206.             waitforkeyboard();
  207.             PostEvent(KEYBOARD, ALT_BS, sk);
  208.         }
  209.         if (keyportvalue == 83)    {
  210.             waitforkeyboard();
  211.             PostEvent(KEYBOARD, ALT_DEL, sk);
  212.         }
  213.     }
  214.     if (sk & CTRLKEY)    {
  215.         if (keyportvalue == 82)    {
  216.             while (!(inp(0x60) & 0x80))
  217.             waitforkeyboard();
  218.             PostEvent(KEYBOARD, CTRL_INS, sk);
  219.         }
  220.     }
  221.     /* ----------- test for keystroke ------- */
  222.     if (keyhit())    {
  223.         static int cvt[] = {SHIFT_INS,END,DN,PGDN,BS,'5',
  224.                         FWD,HOME,UP,PGUP};
  225.         int c = getkey();
  226.  
  227.         /* -------- convert numeric pad keys ------- */
  228.         if (sk & (LEFTSHIFT | RIGHTSHIFT))    {
  229.             if (c >= '0' && c <= '9')
  230.                 c = cvt[c-'0'];
  231.             else if (c == '.' || c == DEL)
  232.                 c = SHIFT_DEL;
  233.             else if (c == INS)
  234.                 c = SHIFT_INS;
  235.         }
  236.         if (c != '\r' && (c < ' ' || c > 127))
  237.             clearBIOSbuffer();
  238.         /* ------ post the keyboard event ------ */
  239.         PostEvent(KEYBOARD, c, sk);
  240.     }
  241.  
  242.     /* ------------ test for mouse events --------- */
  243.     if (button_releases())    {
  244.         /* ------- the button was released -------- */
  245.         doubletimer = DOUBLETICKS;
  246.         PostEvent(BUTTON_RELEASED, mx, my);
  247.         disable_timer(delaytimer);
  248.     }
  249.     get_mouseposition(&mx, &my);
  250.     if (mx != px || my != py)  {
  251.         px = mx;
  252.         py = my;
  253.         PostEvent(MOUSE_MOVED, mx, my);
  254.     }
  255.     if (rightbutton())
  256.         PostEvent(RIGHT_BUTTON, mx, my);
  257.     if (leftbutton())    {
  258.         if (mx == pmx && my == pmy)    {
  259.             /* ---- same position as last left button ---- */
  260.             if (timer_running(doubletimer))    {
  261.                 /* -- second click before double timeout -- */
  262.                 disable_timer(doubletimer);
  263.                 PostEvent(DOUBLE_CLICK, mx, my);
  264.             }
  265.             else if (!timer_running(delaytimer))    {
  266.                 /* ---- button held down a while ---- */
  267.                 delaytimer = lagdelay;
  268.                 lagdelay = DELAYTICKS;
  269.                 /* ---- post a typematic-like button ---- */
  270.                 PostEvent(LEFT_BUTTON, mx, my);
  271.             }
  272.         }
  273.         else    {
  274.             /* --------- new button press ------- */
  275.             disable_timer(doubletimer);
  276.             delaytimer = FIRSTDELAY;
  277.             lagdelay = DELAYTICKS;
  278.             PostEvent(LEFT_BUTTON, mx, my);
  279.             pmx = mx;
  280.             pmy = my;
  281.         }
  282.     }
  283.     else
  284.         lagdelay = FIRSTDELAY;
  285. }
  286.  
  287. /* ----- post a message and parameters to msg queue ---- */
  288. void PostMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  289. {
  290.     if (msg == ENDDIALOG)    {
  291.         msg++;
  292.         --msg;
  293.     }
  294.     if (MsgQueueCtr != MAXMESSAGES)    {
  295.         MsgQueue[MsgQueueOnCtr].wnd = wnd;
  296.         MsgQueue[MsgQueueOnCtr].msg = msg;
  297.         MsgQueue[MsgQueueOnCtr].p1 = p1;
  298.         MsgQueue[MsgQueueOnCtr].p2 = p2;
  299.         if (++MsgQueueOnCtr == MAXMESSAGES)
  300.             MsgQueueOnCtr = 0;
  301.         MsgQueueCtr++;
  302.     }
  303. }
  304.  
  305. /* --------- send a message to a window ----------- */
  306. int SendMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  307. {
  308.     int rtn = TRUE, x, y;
  309.  
  310. #ifdef INCLUDE_LOGGING
  311.     LogMessages(wnd, msg, p1, p2);
  312. #endif
  313.     if (wnd != NULL)
  314.         switch (msg)    {
  315.             case PAINT:
  316.             case BORDER:
  317.                 /* ------- don't send these messages unless the
  318.                     window is visible -------- */
  319.                 if (isVisible(wnd))
  320.                     rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
  321.                 break;
  322.             case RIGHT_BUTTON:
  323.             case LEFT_BUTTON:
  324.             case DOUBLE_CLICK:
  325.             case BUTTON_RELEASED:
  326.                 /* --- don't send these messages unless the
  327.                     window is visible or has captured the mouse -- */
  328.                 if (isVisible(wnd) || wnd == CaptureMouse)
  329.                     rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
  330.                 break;
  331.             case KEYBOARD:
  332.             case SHIFT_CHANGED:
  333.                 /* ------- don't send these messages unless the
  334.                     window is visible or has captured the keyboard -- */
  335.                 if (!(isVisible(wnd) || wnd == CaptureKeyboard))
  336.                     break;
  337.             default:
  338.                 rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
  339.                 break;
  340.         }
  341.     /* ----- window processor returned true or the message was sent
  342.         to no window at all (NULL) ----- */
  343.     if (rtn != FALSE)    {
  344.         /* --------- process messages that a window sends to the
  345.             system itself ---------- */
  346.         switch (msg)    {
  347.             case STOP:
  348.                 StopMsg();
  349.                 break;
  350.             /* ------- clock messages --------- */
  351.             case CAPTURE_CLOCK:
  352.                 Cwnd = wnd;
  353.                 set_timer(clocktimer, 0);
  354.                 break;
  355.             case RELEASE_CLOCK:
  356.                 Cwnd = NULL;
  357.                 disable_timer(clocktimer);
  358.                 break;
  359.             /* -------- keyboard messages ------- */
  360.             case KEYBOARD_CURSOR:
  361.                 if (wnd == NULL)
  362.                     cursor((int)p1, (int)p2);
  363.                 else if (wnd == inFocus)
  364.                     cursor(GetClientLeft(wnd)+(int)p1,
  365.                                 GetClientTop(wnd)+(int)p2);
  366.                 break;
  367.             case CAPTURE_KEYBOARD:
  368.                 if (p2)
  369.                     ((WINDOW)p2)->PrevKeyboard=CaptureKeyboard;
  370.                 else
  371.                     wnd->PrevKeyboard = CaptureKeyboard;
  372.                 CaptureKeyboard = wnd;
  373.                 NoChildCaptureKeyboard = (int)p1;
  374.                 break;
  375.             case RELEASE_KEYBOARD:
  376.                 if (wnd != NULL)    {
  377.                     if (CaptureKeyboard == wnd || (int)p1)
  378.                         CaptureKeyboard = wnd->PrevKeyboard;
  379.                     else    {
  380.                         WINDOW twnd = CaptureKeyboard;
  381.                         while (twnd != NULL)    {
  382.                             if (twnd->PrevKeyboard == wnd)    {
  383.                                 twnd->PrevKeyboard = wnd->PrevKeyboard;
  384.                                 break;
  385.                             }
  386.                             twnd = twnd->PrevKeyboard;
  387.                         }
  388.                         if (twnd == NULL)
  389.                             CaptureKeyboard = NULL;
  390.                     }
  391.                     wnd->PrevKeyboard = NULL;
  392.                 }
  393.                 else
  394.                     CaptureKeyboard = NULL;
  395.                 NoChildCaptureKeyboard = FALSE;
  396.                 break;
  397.             case CURRENT_KEYBOARD_CURSOR:
  398.                 curr_cursor(&x, &y);
  399.                 *(int*)p1 = x;
  400.                 *(int*)p2 = y;
  401.                 break;
  402.             case SAVE_CURSOR:
  403.                 savecursor();
  404.                 break;
  405.             case RESTORE_CURSOR:
  406.                 restorecursor();
  407.                 break;
  408.             case HIDE_CURSOR:
  409.                 normalcursor();
  410.                 hidecursor();
  411.                 break;
  412.             case SHOW_CURSOR:
  413.                 if (p1)
  414.                     set_cursor_type(0x0106);
  415.                 else
  416.                     set_cursor_type(0x0607);
  417.                 unhidecursor();
  418.                 break;
  419.             case WAITKEYBOARD:
  420.                 waitforkeyboard();
  421.                 break;
  422.             /* -------- mouse messages -------- */
  423.             case RESET_MOUSE:
  424.                 resetmouse();
  425.                 set_mousetravel(0, SCREENWIDTH-1, 0, SCREENHEIGHT-1);
  426.                 break;
  427.             case MOUSE_INSTALLED:
  428.                 rtn = mouse_installed();
  429.                 break;
  430.             case MOUSE_TRAVEL:    {
  431.                 RECT rc;
  432.                 if (!p1)    {
  433.                     rc.lf = rc.tp = 0;
  434.                     rc.rt = SCREENWIDTH-1;
  435.                     rc.bt = SCREENHEIGHT-1;
  436.                 }
  437.                 else 
  438.                     rc = *(RECT *)p1;
  439.                 set_mousetravel(rc.lf, rc.rt, rc.tp, rc.bt);
  440.                 break;
  441.             }
  442.             case SHOW_MOUSE:
  443.                 show_mousecursor();
  444.                 break;
  445.             case HIDE_MOUSE:
  446.                 hide_mousecursor();
  447.                 break;
  448.             case MOUSE_CURSOR:
  449.                 set_mouseposition((int)p1, (int)p2);
  450.                 break;
  451.             case CURRENT_MOUSE_CURSOR:
  452.                 get_mouseposition((int*)p1,(int*)p2);
  453.                 break;
  454.             case WAITMOUSE:
  455.                 waitformouse();
  456.                 break;
  457.             case TESTMOUSE:
  458.                 rtn = mousebuttons();
  459.                 break;
  460.             case CAPTURE_MOUSE:
  461.                 if (p2)
  462.                     ((WINDOW)p2)->PrevMouse = CaptureMouse;
  463.                 else
  464.                     wnd->PrevMouse = CaptureMouse;
  465.                 CaptureMouse = wnd;
  466.                 NoChildCaptureMouse = (int)p1;
  467.                 break;
  468.             case RELEASE_MOUSE:
  469.                 if (wnd != NULL)    {
  470.                     if (CaptureMouse == wnd || (int)p1)
  471.                         CaptureMouse = wnd->PrevMouse;
  472.                     else    {
  473.                         WINDOW twnd = CaptureMouse;
  474.                         while (twnd != NULL)    {
  475.                             if (twnd->PrevMouse == wnd)    {
  476.                                 twnd->PrevMouse = wnd->PrevMouse;
  477.                                 break;
  478.                             }
  479.                             twnd = twnd->PrevMouse;
  480.                         }
  481.                         if (twnd == NULL)
  482.                             CaptureMouse = NULL;
  483.                     }
  484.                     wnd->PrevMouse = NULL;
  485.                 }
  486.                 else
  487.                     CaptureMouse = NULL;
  488.                 NoChildCaptureMouse = FALSE;
  489.                 break;
  490.             default:
  491.                 break;
  492.         }
  493.     }
  494.     return rtn;
  495. }
  496.  
  497. static WINDOW MouseWindow(int x, int y)
  498. {
  499.     /* ------ get the window in which a
  500.                     mouse event occurred ------ */
  501.     WINDOW Mwnd = inWindow(NULL, x, y);
  502.  
  503.     /* ---- process mouse captures ----- */
  504.     if (CaptureMouse != NULL)
  505.         if (Mwnd == NULL ||
  506.                 NoChildCaptureMouse ||
  507.                     GetParent(Mwnd) != CaptureMouse)
  508.             Mwnd = CaptureMouse;
  509.     return Mwnd;
  510. }
  511.  
  512. void handshake(void)
  513. {
  514.     handshaking++;
  515.     dispatch_message();
  516.     --handshaking;
  517. }
  518.  
  519. /* ---- dispatch messages to the message proc function ---- */
  520. BOOL dispatch_message(void)
  521. {
  522.     WINDOW Mwnd, Kwnd;
  523.     /* -------- collect mouse and keyboard events ------- */
  524.     collect_events();
  525.     /* --------- dequeue and process events -------- */
  526.     while (EventQueueCtr > 0)  {
  527.         struct events ev;
  528.             
  529.         ev = EventQueue[EventQueueOffCtr];
  530.         if (++EventQueueOffCtr == MAXMESSAGES)
  531.             EventQueueOffCtr = 0;
  532.         --EventQueueCtr;
  533.  
  534.         /* ------ get the window in which a
  535.                         keyboard event occurred ------ */
  536.         Kwnd = inFocus;
  537.  
  538.         /* ---- process keyboard captures ----- */
  539.         if (CaptureKeyboard != NULL)
  540.             if (Kwnd == NULL ||
  541.                     NoChildCaptureKeyboard ||
  542.                         GetParent(Kwnd) != CaptureKeyboard)
  543.                 Kwnd = CaptureKeyboard;
  544.  
  545.         /* -------- send mouse and keyboard messages to the
  546.             window that should get them -------- */
  547.         switch (ev.event)    {
  548.             case SHIFT_CHANGED:
  549.             case KEYBOARD:
  550.                 if (!handshaking)
  551.                     SendMessage(Kwnd, ev.event, ev.mx, ev.my);
  552.                 break;
  553.             case LEFT_BUTTON:
  554.                 if (!handshaking)    {
  555.                     Mwnd = MouseWindow(ev.mx, ev.my);
  556.                     if (!CaptureMouse ||
  557.                             (!NoChildCaptureMouse &&
  558.                                 GetParent(Mwnd) == CaptureMouse))
  559.                         if (Mwnd != inFocus)
  560.                             SendMessage(Mwnd, SETFOCUS, TRUE, 0);
  561.                     SendMessage(Mwnd, LEFT_BUTTON, ev.mx, ev.my);
  562.                 }
  563.                 break;
  564.             case BUTTON_RELEASED:
  565.             case DOUBLE_CLICK:
  566.             case RIGHT_BUTTON:
  567.                 if (handshaking)
  568.                     break;
  569.             case MOUSE_MOVED:
  570.                 Mwnd = MouseWindow(ev.mx, ev.my);
  571.                 SendMessage(Mwnd, ev.event, ev.mx, ev.my);
  572.                 break;
  573.             case CLOCKTICK:
  574.                 SendMessage(Cwnd, ev.event,
  575.                     (PARAM) MK_FP(ev.mx, ev.my), 0);
  576.                 break;
  577.             default:
  578.                 break;
  579.         }
  580.     }
  581.     /* ------ dequeue and process messages ----- */
  582.     while (MsgQueueCtr > 0)  {
  583.         struct msgs mq;
  584.  
  585.         mq = MsgQueue[MsgQueueOffCtr];
  586.         if (++MsgQueueOffCtr == MAXMESSAGES)
  587.             MsgQueueOffCtr = 0;
  588.         --MsgQueueCtr;
  589.         SendMessage(mq.wnd, mq.msg, mq.p1, mq.p2);
  590.         if (mq.msg == ENDDIALOG)
  591.             return FALSE;
  592.         if (mq.msg == STOP)
  593.             return FALSE;
  594.     }
  595.     return TRUE;
  596. }
  597.  
  598.