home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / haswinlib / c / poll < prev    next >
Encoding:
Text File  |  1991-02-04  |  9.9 KB  |  261 lines

  1. /* > $.CLIB.C.poll
  2.  *
  3.  *      HASWIN Graphics Library
  4.  *     =========================
  5.  *
  6.  *      Copyright (C) H.A.Shaw 1990.
  7.  *              Howard A. Shaw.
  8.  *              The Unit for Space Sciences,
  9.  *              Room 165,
  10.  *              Physics Building,
  11.  *              University of Kent at Canterbury.
  12.  *              Canterbury.
  13.  *              Kent.  CT2 7NJ
  14.  *      You may use and distribute this code freely, however please leave
  15.  *      it alone.  If you find bugs (and there will be many) please contact
  16.  *      me and the master source can be modified.  If you keep me informed
  17.  *      of who you give copies of this to then I can get release upgrades
  18.  *      to them.
  19.  *
  20.  *      routines to perform SWI(Wimp_Poll)
  21.  */
  22. #include "c.poll_h"
  23. extern pdriver *haswin_pjob;
  24. static int (*haswin_user)(int, buffer *);
  25. static int haswin_pollnull = 0;
  26.  
  27. int haswin_setuserpollroutine(int (*user)(int, buffer *)) {
  28.  
  29.         haswin_user = user;
  30.         return(HASWIN_TRUE);
  31. }
  32.  
  33. /*
  34.  *      decode the wimp event and perform the code for it
  35.  */
  36. static int dopollcode(int code, buffer *buff) {
  37.  
  38.         int     res = HASWIN_FALSE;
  39.  
  40.         switch (code) {
  41.         case HASWIN_POLL_Nothing:
  42.                 haswin_pollnull = 0;
  43.                 if (haswin_user)
  44.                         res = (*haswin_user)(code, buff);
  45.                 break;
  46.         case HASWIN_POLL_Redraw:
  47.                 res = haswin_poll_redraw(haswin_user, buff);
  48.                 break;
  49.         case HASWIN_POLL_Open:
  50.                 res = haswin_poll_open(haswin_user, buff);
  51.                 break;
  52.         case HASWIN_POLL_Close:
  53.                 if (!haswin_inactivelist(buff->i[0])) {
  54.                         if (haswin_user)
  55.                                 res = (*haswin_user)(code, buff);
  56.                         break;
  57.                 }
  58.                 res = haswin_poll_close(haswin_user, buff);
  59.                 break;
  60.         case HASWIN_POLL_Leaving:
  61.                 if (!haswin_inactivelist(buff->i[0])) {
  62.                         if (haswin_user)
  63.                                 res = (*haswin_user)(code, buff);
  64.                         break;
  65.                 }
  66.                 res = haswin_poll_leaving(haswin_user, buff);
  67.                 break;
  68.         case HASWIN_POLL_Entering:
  69.                 if (!haswin_inactivelist(buff->i[0])) {
  70.                         if (haswin_user)
  71.                                 res = (*haswin_user)(code, buff);
  72.                         break;
  73.                 }
  74.                 res = haswin_poll_entering(haswin_user, buff);
  75.                 break;
  76.         case HASWIN_POLL_Drag:
  77.                 if (!haswin_dragwindow) {
  78.                         res = haswin_poll_drag(haswin_user, buff);
  79.                         break;
  80.                 }
  81.                 if (!haswin_inactivelist(haswin_dragwindow->handle)) {
  82.                         if (haswin_user)
  83.                                 res = (*haswin_user)(code, buff);
  84.                         break;
  85.                 }
  86.                 res = haswin_poll_drag(haswin_user, buff);
  87.                 break;
  88.         case HASWIN_POLL_Mouse:
  89.                 if (!haswin_inactivelist(buff->i[3])) {
  90.                         if (haswin_user)
  91.                                 res = (*haswin_user)(code, buff);
  92.                         break;
  93.                 }
  94.                 res = haswin_poll_mouse(haswin_user, buff);
  95.                 break;
  96.         case HASWIN_POLL_Key:
  97.                 if (!haswin_inactivelist(buff->i[0])) {
  98.                         if (haswin_user)
  99.                                 res = (*haswin_user)(code, buff);
  100.                         break;
  101.                 }
  102.                 res = haswin_poll_key(haswin_user, buff);
  103.                 break;
  104.         case HASWIN_POLL_Menu:
  105.                 if (!haswin_menu.actual)
  106.                         break;
  107.                 if (!haswin_inactivelist(haswin_menu.wfrom)) {
  108.                         if (haswin_user)
  109.                                 res = (*haswin_user)(code, buff);
  110.                         break;
  111.                 }
  112.                 res = haswin_poll_menu(haswin_user, buff);
  113.                 break;
  114.         case HASWIN_POLL_Scroll:
  115.                 if (!haswin_inactivelist(buff->i[0])) {
  116.                         if (haswin_user)
  117.                                 res = (*haswin_user)(code, buff);
  118.                         break;
  119.                 }
  120.                 res = haswin_poll_scroll(haswin_user, buff);
  121.                 break;
  122.         case HASWIN_POLL_Lose_Caret:
  123.                 if (!haswin_inactivelist(buff->i[0])) {
  124.                         if (haswin_user)
  125.                                 res = (*haswin_user)(code, buff);
  126.                         break;
  127.                 }
  128.                 res = haswin_poll_lcaret(haswin_user, buff);
  129.                 break;
  130.         case HASWIN_POLL_Gane_Caret:
  131.                 if (!haswin_inactivelist(buff->i[0])) {
  132.                         if (haswin_user)
  133.                                 res = (*haswin_user)(code, buff);
  134.                         break;
  135.                 }
  136.                 res = haswin_poll_gcaret(haswin_user, buff);
  137.                 break;
  138.         case HASWIN_POLL_Message:
  139.         case HASWIN_POLL_Message_Rec:
  140.         case HASWIN_POLL_Message_Ack:
  141.                 res = haswin_poll_message(haswin_user, buff);
  142.                 break;
  143.         default:        /* anything else */
  144.                 if (haswin_user) 
  145.                         res = (*haswin_user)(code, buff);
  146.                 break;
  147.         }
  148.         return(res);
  149. }
  150.  
  151. /*
  152.  *      poll the real WIMP and perform any automatic functions.
  153.  *      this returns HASWIN_TRUE if the routine performed ok, and
  154.  *      HASWIN_FALSE on null codes, or if something was amiss.
  155.  *
  156.  *      We must try to perform some processing.  The main problem is
  157.  *      double clicking.  If we get a mouse pressed event then don't do it.
  158.  *      wait for a while, if we get another click then use it, if not then
  159.  *      just use the first one followed by whatever we got the second time.
  160.  */
  161. #define HASWIN_POLL_TIMESTEP   15        /* centi-seconds delay for double */
  162.                                          /* keypress actions (drag etc.)   */
  163.  
  164. int haswin_poll() {
  165.  
  166.         int                     timer, code, button, prtjob;
  167.         buffer                  buff, sbuff;
  168.         window                  *wptr;
  169.         icon                    *iptr;
  170.         _kernel_swi_regs        regs;
  171.  
  172.         if (!haswin_swi(OS_ReadMonotonicTime, ®s))
  173.                 return(HASWIN_FALSE);
  174.         timer = regs.r[0] + HASWIN_POLL_TIMESTEP;
  175.         regs.r[0] = 0;
  176.         regs.r[1] = (int)&buff;
  177.         prtjob = 0;
  178.         if (haswin_flags & HASWIN_FLAGS_PRINTER)
  179.                 prtjob = haswin_pdriverselect(0);
  180.         if (haswin_flags & HASWIN_FLAGS_FASTPOLL) {
  181.                 /* use SWI(WIMP_poll) to return immediatly */
  182.                 if (!haswin_swi(HASWIN_Poll, ®s)) {
  183.                         if (prtjob)
  184.                                 haswin_pdriverselect(prtjob);
  185.                         return(HASWIN_FALSE);
  186.                 }
  187.         } else {
  188.                 /* use SWI(WIMP_pollidle) to increase efficiency */
  189.                 regs.r[2] = timer;
  190.                 if (!haswin_swi(HASWIN_Poll_idle, ®s)) {
  191.                         if (prtjob)
  192.                                 haswin_pdriverselect(prtjob);
  193.                         return(HASWIN_FALSE);
  194.                 }
  195.         }
  196.         if (prtjob)
  197.                 /* we had a print job going so restart it */
  198.                 haswin_pdriverselect(prtjob);
  199.         code = regs.r[0];
  200.         if (code == HASWIN_POLL_Mouse) {
  201.                 /*
  202.                  * we have a mouse click.  If it is a SELECT or ADJUST it
  203.                  * could be the first of a double-click or drag so check for
  204.                  * these and if the button type allows double actions then
  205.                  * call SWI(WIMP_Poll) again.
  206.                  */
  207.                 wptr=haswin_findwindowhandle(buff.i[3]);
  208.                 iptr=haswin_findiconhandle(wptr, buff.i[4]);
  209.                 button = haswin_buttonWIMPtoHASWIN(wptr, iptr, buff.i[2]);
  210.                 if (((button==HASWIN_MOUSE_L) || (button==HASWIN_MOUSE_R)) &&
  211.                     haswin_canbuttondouble(button)) {
  212.                         /*
  213.                          * mouse action could be double-click or drag
  214.                          * so recall SWI(WIMP_Poll) with a time delay
  215.                          * and then try again.
  216.                          */
  217.                         regs.r[0] = 0;
  218.                         regs.r[1] = (int)&sbuff;
  219.                         timer += HASWIN_POLL_TIMESTEP;
  220.                         regs.r[2] = timer;
  221.                         if (haswin_flags & HASWIN_FLAGS_PRINTER)
  222.                                 prtjob = haswin_pdriverselect(0);
  223.                         if (!haswin_swi(HASWIN_Poll_idle, ®s)) {
  224.                                 if (prtjob)
  225.                                         haswin_pdriverselect(prtjob);
  226.                                 return(HASWIN_FALSE);
  227.                         }
  228.                         if (prtjob)
  229.                                 haswin_pdriverselect(prtjob);
  230.                         code = regs.r[0];
  231.                         if (code == HASWIN_POLL_Mouse)
  232.                                 return(dopollcode(code, &sbuff));
  233.                         /*
  234.                          * got something not a keypress, try the
  235.                          * original keypress first, and then the
  236.                          * new action
  237.                          */
  238.                         dopollcode(HASWIN_POLL_Mouse, &buff);
  239.                         return(dopollcode(HASWIN_POLL_Mouse, &sbuff));
  240.                 }
  241.         }
  242.         return(dopollcode(code, &buff));
  243. }
  244.  
  245. /*
  246.  *      continue to poll until a NULL event is processed.  Poll fast.
  247.  */
  248. int haswin_pollcomplete(void) {
  249.         int     flgs, ans;
  250.  
  251.         flgs = haswin_flags;
  252.         haswin_flags &= ~HASWIN_FLAGS_FASTPOLL;
  253.         haswin_pollnull++;
  254.         while (haswin_pollnull)
  255.                 ans=haswin_poll();
  256.         haswin_flags |= flgs & HASWIN_FLAGS_FASTPOLL;
  257.         return(ans);
  258. }
  259.  
  260.  
  261.