home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / him / kmdemo1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-01  |  7.1 KB  |  262 lines

  1. /***************** Program Source Module ***********************/
  2. #ifdef AUTOPDOC
  3.  
  4. MODULE:     kmdemo1.c
  5.  
  6. DESCRIPTION:
  7.  
  8. This program will demonstrate how a mouse can be used to simulate arrow
  9. keycodes with the Keyboard Manager package.  It will be registered as
  10. a User Input Routine (UIR) and whenever movement of the mouse is noted the
  11. correct arrow keycode will be sent to the Keyboard Manager.  Added
  12. capabilities such as button command support is not present in this
  13. example but could easily be added.
  14.  
  15. This program will create a window and place the cursor in the center.  As
  16. the user enters arrow keycodes the cursor will be moved.  Hit the ESCAPE
  17. key or press any mouse button to exit the program.
  18.  
  19. This program must be linked with the mouse driver library in addition to
  20. the Forms Processing Lib.
  21.  
  22. NOTE:  The mouse driver must be loaded.
  23.  
  24. #endif
  25. /*
  26.  *
  27.  *   Copyright 1988  Allsoft (tm)
  28.  *   100 Calle Playa Del Sol NE
  29.  *   Albuquerque, NM  87109
  30.  *
  31.  *   ALL RIGHTS RESERVED.
  32.  *
  33.  *   Unauthorized distribution, adaptation or use may be 
  34.  *   subject to civil and criminal penalties.
  35.  *
  36.  */
  37. /********** Filled by Polytron Version Control System **********
  38.  
  39. $Author:   james borders  $
  40.  
  41. $Date:   01 Dec 1988 12:15:48  $
  42.  
  43. $Revision:   1.0  $
  44.  
  45. $Log:   D:/C/FMLIB/KM/VCS/KMDEMO1.C  $
  46.  * 
  47.  *    Rev 1.0   01 Dec 1988 12:15:48   james borders
  48.  * Initial revision.
  49.  
  50. ****************************************************************/
  51.  
  52. /*** Include Files ***/
  53.  
  54. #include  "stdio.h"
  55. #ifdef MSC
  56. #include  "malloc.h"
  57. #endif
  58. #ifdef TURBOC
  59. #include  "alloc.h"
  60. #endif
  61. #include  "lm.h"
  62. #include  "km.h"
  63. #include  "wn.h"
  64.  
  65. /*** Typedefs ***/
  66.  
  67. /*** Constants ***/
  68.  
  69. /*** Global Vars ***/
  70.  
  71. /*** Macros ***/
  72.  
  73. /*
  74.     case is important for MOUSECML/MOUSECMS, the turbo shell tcc passes
  75.     a /c (case sensitive switch) to tlink.
  76. */
  77. #define   MOUSE MOUSECML(&m1,&m2,&m3,&m4) /* MOUSECMS=small, MOUSECML=large */
  78.  
  79. int  m1,m2,m3,m4;
  80.  
  81. main()
  82. /***********/
  83. {
  84. int  kbhit(),getch();           /* for kminit call */
  85. int  crow,ccol,lrow,lcol;       /* current and last cursor row,col */
  86. int  keycode;
  87. int  wnum;                      /* window number */
  88. int  uir;                       /* uir number from kmsuir() */
  89. int  mouseuir();                /* our mouse User Input Routine */
  90.  
  91. lminit((char *(*)())malloc,free,0);          /* init list manager */
  92. kminit(kbhit,getch);            /* init keyboard manager */
  93. wninit(0,0,NULL,0,(char *(*)())malloc,free); /* init window manager */
  94.  
  95. wnum = wncreate(0,0,80,25,WNBLUE,WNCYAN,WNBLUE,WNCYAN); /* create window */
  96. wnttitle(wnum,"[ KMDEMO1 V1.0 Mouse Tester ]");         /* put up top title */
  97. wnbtitle(wnum,"[ ESC or mouse button to exit ]");       /* bottom title */
  98.  
  99. m1 = 0;
  100. MOUSE;         /* see if driver is installed (m1==0) */
  101. if (m1 != -1){
  102.     wndestroy(wnum);            /* take down window */
  103.     printf("\nSorry.  Mouse driver not found.");
  104.     exit(1);
  105.     }
  106. else{
  107.     uir = kmsuir(mouseuir);     /* register User Input Routine */
  108.     kmmsabort(KESC);            /* tell keyboard package what an abort is */
  109.     crow = 10;
  110.     lrow = -1;
  111.     ccol = 40;
  112.     lcol = -1;
  113.     for (;;){
  114.         if ((lrow != crow) || (lcol != ccol)){ /* don't move unless changed */
  115.             wnlocate(wnum,crow,ccol);   /* position cursor */
  116.             lrow = crow;
  117.             lcol = ccol;        /* update last coords */
  118.             }
  119.         keycode = kmgetch();    /* get a key from keyboard manager */
  120.         switch (keycode){
  121.             case    KUP:        /* up arrow key */
  122.                 crow = (crow == 1)?crow:--crow;
  123.                 break;
  124.             case    KDOWN:      /* down arrow key */
  125.                 crow = (crow == 23)?crow:++crow;
  126.                 break;
  127.             case    KRIGHT:     /* right arrow key */
  128.                 ccol = (ccol == 78)?ccol:++ccol;
  129.                 break;
  130.             case    KLEFT:      /* left arrow key */
  131.                 ccol = (ccol == 1)?ccol:--ccol;
  132.                 break;
  133.             case    KESC:       /* escape keycode */
  134.                 wndestroy(wnum);/* take down window */
  135.                 kmduir(uir);    /* remove from list just to be clean */
  136.                 exit(0);
  137.                 break;
  138.             }
  139.         }
  140.     }
  141. } /* end of main */
  142.  
  143.  
  144. /************** User Input Routine section **************/
  145.  
  146.  
  147. /* these global static vars are for the mouse input routines */
  148.  
  149. #define KQ 20
  150. int  current_abort_code = KESC;
  151. int  waiting_keycode[KQ];
  152. int  nwaiting,in,out;
  153.  
  154.  
  155. mouseuir(func,data)
  156. /******************/
  157. /* This is the actual user input routine which was registered via kmsuir()
  158.    above.
  159. */
  160. int func,data;
  161. {
  162. int tkey;
  163.  
  164. switch(func){   /* a UIR must be capable of performing 4 functions: */
  165.     case    fuirsabort: /* set abort keycode */
  166.         current_abort_code = data;
  167.         break;
  168.  
  169.     case    fuirqabort:             /* is abort code pending */
  170.         m1 = 3;                     /* get mouse position and button status */
  171.         MOUSE;     /* call the mouse driver */
  172.         if (m2 & 7){
  173.             push_keycode(current_abort_code);
  174.             return(1);              /* button down, return abort true */
  175.             }
  176.         else
  177.             return(0);              /* no button down */
  178.  
  179.     case    fuirchavail:            /* keycode ready yet? */
  180.         return(mouseuirchavail());
  181.  
  182.     case    fuirgetch:              /* don't return until char ready */
  183.         while (!mouseuirchavail())
  184.             ;                       /* wait for keycode */
  185.         return(pop_keycode());
  186.     }
  187. } /* end of mouseuir */
  188.  
  189. mouseuirchavail()
  190. /****************/
  191. {
  192. if (any_keycodes())
  193.     return(1);
  194. else{
  195.     if (mouseuirbuttondown()){
  196.         push_keycode(current_abort_code);
  197.         return(1);                  /* yes, keycode is ready */
  198.         }
  199.     m1 = 11;                        /* get relative mouse movement */
  200.     MOUSE;         /* call driver */
  201.     if (m3 > 0)                     /* did mouse move to the right? */
  202.         push_keycode(KRIGHT);
  203.     else if (m3 < 0)                /* did mouse move to the left? */
  204.         push_keycode(KLEFT);
  205.     if (m4 > 0)                     /* did mouse move down? */
  206.         push_keycode(KDOWN);
  207.     else if (m4 < 0)                /* did mouse move up? */
  208.         push_keycode(KUP);
  209.  
  210.     return(any_keycodes());         /* return updated status */
  211.     }
  212. } /* end of mouseuirchavail */
  213.  
  214. mouseuirbuttondown()
  215. /*******************/
  216. {
  217. m1 = 3;                             /* get mouse position and button status */
  218. MOUSE;             /* call driver */
  219. if (m2 & 7)
  220.     return(1);                      /* yes, button is down */
  221. else
  222.     return(0);
  223. }
  224.  
  225. clear_keycodes()
  226. /****************/
  227. {
  228. nwaiting = 0;
  229. in = out = 0;
  230. }
  231.  
  232. any_keycodes()
  233. /*************/
  234. {
  235. return(nwaiting);
  236. }
  237.  
  238. push_keycode(keycode)
  239. /********************/
  240. int keycode;
  241. {
  242. if (nwaiting < KQ){
  243.     waiting_keycode[in++] = keycode;
  244.     in = (in == KQ)?0:in;
  245.     nwaiting++;
  246.     }
  247. }
  248.  
  249. pop_keycode()
  250. /*************/
  251. {
  252. int i;
  253.  
  254. i = waiting_keycode[out++];
  255. out = (out == KQ)?0:out;
  256. nwaiting--;
  257. return(i);
  258. }
  259.  
  260.  
  261.         
  262.