home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / dflat.zip / MESSAGE.C < prev    next >
Text File  |  1991-02-18  |  10KB  |  423 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. static struct events    {
  17.     MESSAGE event;
  18.     int mx;
  19.     int my;
  20. } EventQueue[MAXMESSAGES];
  21.  
  22. static struct msgs {
  23.     WINDOW wnd;
  24.     MESSAGE msg;
  25.     PARAM p1;
  26.     PARAM p2;
  27. } MsgQueue[MAXMESSAGES];
  28.  
  29. static int EventQueueOnCtr;
  30. static int EventQueueOffCtr;
  31. static int EventQueueCtr;
  32.  
  33. static int MsgQueueOnCtr;
  34. static int MsgQueueOffCtr;
  35. static int MsgQueueCtr;
  36.  
  37. static int lagdelay = FIRSTDELAY;
  38.  
  39. static void (interrupt far *oldtimer)(void) = NULL;
  40. WINDOW CaptureMouse = NULLWND;
  41. WINDOW CaptureKeyboard = NULLWND;
  42. static int NoChildCaptureMouse = FALSE;
  43. static int NoChildCaptureKeyboard = FALSE;
  44.  
  45. static int doubletimer = -1;
  46. static int delaytimer  = -1;
  47. static int clocktimer  = -1;
  48.  
  49. WINDOW Cwnd = NULLWND;
  50.  
  51. static void interrupt far newtimer(void)
  52. {
  53.     if (timer_running(doubletimer))
  54.         countdown(doubletimer);
  55.     if (timer_running(delaytimer))
  56.         countdown(delaytimer);
  57.     if (timer_running(clocktimer))
  58.         countdown(clocktimer);
  59.     oldtimer();
  60. }
  61.  
  62. static char ermsg[] = "Error accessing drive x";
  63.  
  64. int TestCriticalError(void)
  65. {
  66.     int rtn = 0;
  67.     if (CriticalError)    {
  68.         rtn = 1;
  69.         CriticalError = FALSE;
  70.         if (TestErrorMessage(ermsg) == FALSE)
  71.             rtn = 2;
  72.     }
  73.     return rtn;
  74. }
  75.  
  76. static void interrupt far newcrit(IREGS ir)
  77. {
  78.     if (!(ir.ax & 0x8000))     {
  79.         ermsg[sizeof(ermsg) - 2] = (ir.ax & 0xff) + 'A';
  80.         CriticalError = TRUE;
  81.     }
  82.     ir.ax = 0;
  83. }
  84.  
  85. void init_messages(void)
  86. {
  87.     resetmouse();
  88.     show_mousecursor();
  89.     px = py = -1;
  90.     pmx = pmy = -1;
  91.     mx = my = 0;
  92.     CaptureMouse = CaptureKeyboard = NULLWND;
  93.     NoChildCaptureMouse = FALSE;
  94.     NoChildCaptureKeyboard = FALSE;
  95.     MsgQueueOnCtr = MsgQueueOffCtr = MsgQueueCtr = 0;
  96.     EventQueueOnCtr = EventQueueOffCtr = EventQueueCtr = 0;
  97.     if (oldtimer == NULL)    {
  98.         oldtimer = getvect(TIMER);
  99.         setvect(TIMER, newtimer);
  100.     }
  101.     setvect(CRIT, newcrit);    
  102.     PostMessage(NULLWND,START,0,0);
  103.     lagdelay = FIRSTDELAY;
  104. }
  105.  
  106. /* ----- post an event and parameters to event queue ---- */
  107. static void PostEvent(MESSAGE event, int p1, int p2)
  108. {
  109.     if (EventQueueCtr != MAXMESSAGES)    {
  110.         EventQueue[EventQueueOnCtr].event = event;
  111.         EventQueue[EventQueueOnCtr].mx = p1;
  112.         EventQueue[EventQueueOnCtr].my = p2;
  113.         if (++EventQueueOnCtr == MAXMESSAGES)
  114.             EventQueueOnCtr = 0;
  115.         EventQueueCtr++;
  116.     }
  117. }
  118.  
  119. static void near collect_events(void)
  120. {
  121.     struct tm *now;
  122.     static int flipflop = FALSE;
  123.     static char timestr[8];
  124.     int hr, sk;
  125.     static int ShiftKeys = 0;
  126.  
  127.     if (timed_out(clocktimer))    {
  128.         time_t t = time(NULL);
  129.         now = localtime(&t);
  130.         hr = now->tm_hour > 12 ? now->tm_hour - 12 : now->tm_hour;
  131.         if (hr == 0)
  132.             hr = 12;
  133.         sprintf(timestr, "%2.2d:%02d", hr, now->tm_min);
  134.         strcpy(timestr+5, now->tm_hour > 11 ? "pm" : "am");
  135.         if (flipflop)
  136.             *(timestr+2) = ' ';
  137.         flipflop ^= TRUE;
  138.         set_timer(clocktimer, 1);
  139.         PostEvent(CLOCKTICK, FP_SEG(timestr), FP_OFF(timestr));
  140.     }
  141.  
  142.     if ((sk = getshift()) != ShiftKeys)    {
  143.         ShiftKeys = sk;
  144.         PostEvent(SHIFT_CHANGED, sk, 0);
  145.     }
  146.  
  147.     if (sk & ALTKEY)
  148.         if (inp(0x60) == 14)    {
  149.             while (!(inp(0x60) & 0x80))
  150.                 ;
  151.             PostEvent(KEYBOARD, ALT_BS, sk);
  152.         }
  153.     if (sk & CTRLKEY)
  154.         if (inp(0x60) == 82)    {
  155.             while (!(inp(0x60) & 0x80))
  156.                 ;
  157.             PostEvent(KEYBOARD, CTRL_INS, sk);
  158.         }
  159.  
  160.     if (keyhit())    {
  161.         static int cvt[] = {SHIFT_INS,END,DN,PGDN,BS,'5',
  162.                         FWD,HOME,UP,PGUP};
  163.         int c = getkey();
  164.         if (sk & (LEFTSHIFT | RIGHTSHIFT))    {
  165.             if (c >= '0' && c <= '9')
  166.                 c = cvt[c-'0'];
  167.             else if (c == '.' || c == DEL)
  168.                 c = SHIFT_DEL;
  169.             else if (c == INS)
  170.                 c = SHIFT_INS;
  171.         }
  172.         *(int far *)(MK_FP(0x40,0x1a)) =
  173.             *(int far *)(MK_FP(0x40,0x1c));
  174.         if (c == F1)
  175.             HelpFunction();
  176.         else
  177.             PostEvent(KEYBOARD, c, sk);
  178.     }
  179.  
  180.     get_mouseposition(&mx, &my);
  181.     if (mx != px || my != py)  {
  182.         px = mx;
  183.         py = my;
  184.         PostEvent(MOUSE_MOVED, mx, my);
  185.     }
  186.     if (rightbutton())
  187.         PostEvent(RIGHT_BUTTON, mx, my);
  188.     if (leftbutton())    {
  189.         if (mx == pmx && my == pmy)    {
  190.             if (timer_running(doubletimer))    {
  191.                 disable_timer(doubletimer);
  192.                 PostEvent(DOUBLE_CLICK, mx, my);
  193.             }
  194.             else if (!timer_running(delaytimer))    {
  195.                 delaytimer = lagdelay;
  196.                 lagdelay = DELAYTICKS;
  197.                 PostEvent(LEFT_BUTTON, mx, my);
  198.             }
  199.         }
  200.         else    {
  201.             disable_timer(doubletimer);
  202.             delaytimer = FIRSTDELAY;
  203.             lagdelay = DELAYTICKS;
  204.             PostEvent(LEFT_BUTTON, mx, my);
  205.             pmx = mx;
  206.             pmy = my;
  207.         }
  208.     }
  209.     else
  210.         lagdelay = FIRSTDELAY;
  211.     if (button_releases())    {
  212.         doubletimer = DOUBLETICKS;
  213.         PostEvent(BUTTON_RELEASED, mx, my);
  214.         disable_timer(delaytimer);
  215.     }
  216. }
  217.  
  218. /* ----- post a message and parameters to msg queue ---- */
  219. void PostMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  220. {
  221.     if (MsgQueueCtr != MAXMESSAGES)    {
  222.         MsgQueue[MsgQueueOnCtr].wnd = wnd;
  223.         MsgQueue[MsgQueueOnCtr].msg = msg;
  224.         MsgQueue[MsgQueueOnCtr].p1 = p1;
  225.         MsgQueue[MsgQueueOnCtr].p2 = p2;
  226.         if (++MsgQueueOnCtr == MAXMESSAGES)
  227.             MsgQueueOnCtr = 0;
  228.         MsgQueueCtr++;
  229.     }
  230. }
  231.  
  232. int SendMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  233. {
  234.     int rtn = TRUE, x, y;
  235.     if (wnd != NULLWND)
  236.         switch (msg)    {
  237.             case PAINT:
  238.             case BORDER:
  239.             case RIGHT_BUTTON:
  240.             case LEFT_BUTTON:
  241.             case DOUBLE_CLICK:
  242.             case BUTTON_RELEASED:
  243.             case KEYBOARD:
  244.             case SHIFT_CHANGED:
  245.                 if (!isVisible(wnd))
  246.                     break;
  247.             default:
  248.                 rtn = wnd->wndproc(wnd, msg, p1, p2);
  249.                 break;
  250.         }
  251.     if (rtn != FALSE)    {
  252.         switch (msg)    {
  253.             case STOP:
  254.                 hide_mousecursor();
  255.                 if (oldtimer != NULL)    {
  256.                     setvect(TIMER, oldtimer);
  257.                     oldtimer = NULL;
  258.                 }
  259.                 break;
  260.             case CAPTURE_CLOCK:
  261.                 Cwnd = wnd;
  262.                 set_timer(clocktimer, 0);
  263.                 break;
  264.             case RELEASE_CLOCK:
  265.                 Cwnd = NULLWND;
  266.                 disable_timer(clocktimer);
  267.                 break;
  268.             /* -------- keyboard messages ------- */
  269.             case KEYBOARD_CURSOR:
  270.                 if (wnd == NULLWND)
  271.                     cursor((int)p1, (int)p2);
  272.                 else
  273.                     cursor(GetClientLeft(wnd)+(int)p1,
  274.                                 GetClientTop(wnd)+(int)p2);
  275.                 break;
  276.             case CURRENT_KEYBOARD_CURSOR:
  277.                 curr_cursor(&x, &y);
  278.                 *(int*)p1 = x;
  279.                 *(int*)p2 = y;
  280.                 break;
  281.             case SAVE_CURSOR:
  282.                 savecursor();
  283.                 break;
  284.             case RESTORE_CURSOR:
  285.                 restorecursor();
  286.                 break;
  287.             case HIDE_CURSOR:
  288.                 normalcursor();
  289.                 hidecursor();
  290.                 break;
  291.             case SHOW_CURSOR:
  292.                 if (p1)
  293.                     set_cursor_type(0x0106);
  294.                 else
  295.                     set_cursor_type(0x0607);
  296.                 unhidecursor();
  297.                 break;
  298.             /* -------- mouse messages -------- */
  299.             case MOUSE_INSTALLED:
  300.                 rtn = mouse_installed();
  301.                 break;
  302.             case SHOW_MOUSE:
  303.                 show_mousecursor();
  304.                 break;
  305.             case HIDE_MOUSE:
  306.                 hide_mousecursor();
  307.                 break;
  308.             case MOUSE_CURSOR:
  309.                 set_mouseposition((int)p1, (int)p2);
  310.                 break;
  311.             case CURRENT_MOUSE_CURSOR:
  312.                 get_mouseposition((int*)p1,(int*)p2);
  313.                 break;
  314.             case WAITMOUSE:
  315.                 waitformouse();
  316.                 break;
  317.             case TESTMOUSE:
  318.                 rtn = mousebuttons();
  319.                 break;
  320.             case CAPTURE_MOUSE:
  321.                 if (p2)
  322.                     ((WINDOW)p2)->PrevMouse = CaptureMouse;
  323.                 else
  324.                     wnd->PrevMouse = CaptureMouse;
  325.                 CaptureMouse = wnd;
  326.                 NoChildCaptureMouse = (int)p1;
  327.                 break;
  328.             case RELEASE_MOUSE:
  329.                 CaptureMouse = wnd->PrevMouse;
  330.                 NoChildCaptureMouse = FALSE;
  331.                 break;
  332.             case CAPTURE_KEYBOARD:
  333.                 if (p2)
  334.                     ((WINDOW)p2)->PrevKeyboard = CaptureKeyboard;
  335.                 else
  336.                     wnd->PrevKeyboard = CaptureKeyboard;
  337.                 CaptureKeyboard = wnd;
  338.                 NoChildCaptureKeyboard = (int)p1;
  339.                 break;
  340.             case RELEASE_KEYBOARD:
  341.                 CaptureKeyboard = wnd->PrevKeyboard;
  342.                 NoChildCaptureKeyboard = FALSE;
  343.                 break;
  344.             default:
  345.                 break;
  346.         }
  347.     }
  348.     return rtn;
  349. }
  350.  
  351. /* ---- dispatch messages to the message proc function ---- */
  352. int dispatch_message(void)
  353. {
  354.     WINDOW Mwnd, Kwnd;
  355.  
  356.     /* -------- collect mouse and keyboard events ------- */
  357.     collect_events();
  358.  
  359.     while (EventQueueCtr > 0)  {
  360.         struct events ev = EventQueue[EventQueueOffCtr];
  361.  
  362.         if (++EventQueueOffCtr == MAXMESSAGES)
  363.             EventQueueOffCtr = 0;
  364.         --EventQueueCtr;
  365.  
  366.         Mwnd = inWindow(ev.mx, ev.my);
  367.         if (CaptureMouse != NULLWND)
  368.             if (Mwnd == NULLWND ||
  369.                     NoChildCaptureMouse ||
  370.                         GetParent(Mwnd) != CaptureMouse)
  371.                 Mwnd = CaptureMouse;
  372.  
  373.         Kwnd = inFocus;
  374.         if (CaptureKeyboard != NULLWND)
  375.             if (Kwnd == NULLWND ||
  376.                     NoChildCaptureKeyboard ||
  377.                         GetParent(Kwnd) != CaptureKeyboard)
  378.                 Kwnd = CaptureKeyboard;
  379.  
  380.         switch (ev.event)    {
  381.             case SHIFT_CHANGED:
  382.             case KEYBOARD:
  383.                 SendMessage(Kwnd, ev.event, ev.mx, ev.my);
  384.                 break;
  385.             case LEFT_BUTTON:
  386.                 if (!CaptureMouse ||
  387.                         (!NoChildCaptureMouse &&
  388.                             GetParent(Mwnd) == CaptureMouse))
  389.                     if (Mwnd != inFocus)
  390.                         SendMessage(Mwnd, SETFOCUS, TRUE, 0);
  391.             case BUTTON_RELEASED:
  392.             case DOUBLE_CLICK:
  393.             case RIGHT_BUTTON:
  394.             case MOUSE_MOVED:
  395.                  SendMessage(Mwnd, ev.event, ev.mx, ev.my);
  396.                 break;
  397.             case CLOCKTICK:
  398.                 SendMessage(Cwnd, ev.event, (PARAM) MK_FP(ev.mx, ev.my), 0);
  399.             default:
  400.                 break;
  401.         }
  402.     }
  403.  
  404.     /* ------ dequeue all messages ----- */
  405.     while (MsgQueueCtr > 0)  {
  406.         struct msgs mq = MsgQueue[MsgQueueOffCtr];
  407.         if (++MsgQueueOffCtr == MAXMESSAGES)
  408.             MsgQueueOffCtr = 0;
  409.         --MsgQueueCtr;
  410.         SendMessage(mq.wnd, mq.msg, mq.p1, mq.p2);
  411.         if (mq.msg == STOP || mq.msg == ENDDIALOG)
  412.                return FALSE;
  413.     }
  414.  
  415.     return TRUE;
  416. }
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.