home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 08 / dflat5 / message.c < prev    next >
Text File  |  1991-06-27  |  17KB  |  539 lines

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