home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / rainbow / msdos / decus / RB140 / grlib03a.arj / MSMOUSE.C < prev    next >
Text File  |  1988-12-19  |  11KB  |  320 lines

  1. /***************************************************/
  2. /*        MOUSE SUPPORT      mouse.c                 */
  3. /***************************************************/
  4.  
  5. #include "stdio.h"
  6. #include "mouse.h"
  7.  
  8. #define MODEM_OFF 0                /*drop dtr/rts*/
  9. #define MODEM_ON  0x0C                /*raise dtr/rts*/
  10.  
  11. extern int _carry;                /*dos flag*/
  12.  
  13. struct  Rci_block {
  14.     unsigned char rb_function;        /*PKT function */
  15.     unsigned char rb_func_retc;        /*PKT return code*/
  16.     unsigned char rb_character;        /*PKT character in/out*/
  17.     unsigned char rb_char_stat;        /*PKT character status*/
  18.     unsigned char  rb_device;        /*RCI device number*/
  19.     unsigned int rb_offset,            /*RCI offset*/
  20.              rb_segment;        /*RCI segment*/
  21.     };
  22.  
  23. static char *Mouse_port[] = {
  24.         "NUL",
  25.         "AUX",                /* port = MDM */
  26.         "PRN",                /* port = PRT */
  27.         "AUX2"                /* port = AUX */
  28.         };
  29.  
  30. static struct COMM_BLOCK save_config_;        /*save current values*/
  31.  
  32. static CONDITION_PACKET             /* action_ conditions*/
  33.     action_   = {FALSE,0,0,0,0,0,0},
  34.     noaction_ = {FALSE,0,0,0,0,0,0};
  35.  
  36. /*************************************************/
  37. /* MSOPEN                                        */
  38. /* msopen(device,control_packet,buffer,          */
  39. /*        buffer_byte_size,mouse_type);          */
  40. /* returns TRUE if mouse avail, else FALSE       */
  41. /*************************************************/
  42. msopen(device,mptr,bptr,bcnt,mtype)
  43. int             device;            /*= port number*/
  44. MOUSE_PACKET   *mptr;            /*= Mouse control block*/
  45. unsigned char  *bptr;            /*= new circ. buffer for comms*/
  46. int             bcnt;            /*= size of buffer in bytes*/
  47. int             mtype;            /*= mouse type */
  48.  
  49. {   struct COMM_BLOCK config;
  50.     int i, temp;
  51.     unsigned int getds_(), getcs_();
  52.     unsigned char c,  *testbyte_();
  53.  
  54.   if (mptr->Mouse_enabled == TRUE)
  55.     msclose(mptr);
  56.   mptr->Mouse_programmed = mptr->Mouse_enabled = FALSE;
  57.   mptr->Mouse_type = mtype;
  58.   mptr->Mouse_bstate = mptr->Deltax = mptr->Deltay = 0;
  59.   mptr->X_ratio = Mouse_x_speed;
  60.   mptr->X_threshold = Mouse_x_threshold;
  61.   mptr->Y_ratio = Mouse_y_speed;
  62.   mptr->Y_threshold = Mouse_y_threshold;
  63.  
  64.   mptr->Mouse_device = device;            /*see if port is there & open I/O*/
  65.   strcpy(mptr->Mouse_port_name,Mouse_port[device]);
  66.   mptr->Mouse_handle = msdos((0x3D02), 0, 0, mptr->Mouse_port_name);
  67.   if (_carry)
  68.     return FALSE;
  69.  
  70.   Msioctl_(mptr, 3,  &save_config_);        /*save what went on before*/
  71.   config.cb_character = MODEM_OFF;        /*turn off DTR-RTS*/
  72.   Msioctl_(mptr, 14,  &config);          /*drop modem lines & wait*/
  73.   for (i=0; i<10000; i++);
  74.  
  75.   Msioctl_(mptr, 5,  &config);            /*disable rec interrupts*/
  76.   config.cb_control = 1;                /*mode (data leads)*/
  77.   config.cb_stop_bits = 1;                /*stop bits*/
  78.   config.cb_data_bits = 3;                /*7 data bits*/
  79.   config.cb_parity_out = 3;                /*no parity*/
  80.   config.cb_rcv_baud = 9;                /*rx 1200 baud*/
  81.   config.cb_xmit_baud = 9;                /*tx 1200 baud*/
  82.   config.cb_xon_char = config.cb_xoff_char = 0;        /*XON/XOFF chars*/
  83.   config.cb_in_xon = config.cb_out_xon = 2;        /*disable XON/XOFF*/
  84.   config.cb_buffer_size = bcnt;                /*user buffer*/
  85.   config.cb_buffer_offset = bptr;
  86.   config.cb_buffer_segment = getds_();
  87.  
  88.   if(Msioctl_(mptr, 0, &config) != 0)               /*reprogram Mouse port*/
  89.     mptr->Mouse_programmed = TRUE;
  90.   Msioctl_(mptr, 4,  &config);            /*enable rec interrupts*/
  91.  
  92.   config.cb_character = MODEM_ON;        /*turn on DTR-RTS*/
  93.   Msioctl_(mptr, 14,  &config);          /*raise modem lines & wait*/
  94.   for (i=0; i<64; i++);
  95.  
  96.   if( (temp = msack(mptr)) == TRUE);
  97.     rci_attach(mptr,&action_,bptr);
  98.   return temp;
  99. }
  100.  
  101. /***********************************************/
  102. /*   Mouse close                               */
  103. /***********************************************/
  104. msclose(mptr)
  105. MOUSE_PACKET      *mptr;
  106.  
  107. { struct COMM_BLOCK config;
  108.   int ret=TRUE;
  109.   Msioctl_(mptr, 5,  &config);            /*disable rec interrupts*/
  110.   if (mptr->Mouse_new_vectors == TRUE)        /*reset system vectors*/
  111.     rci_detach(mptr);
  112.   Msioctl_(mptr, 2,  &config);            /*reset to defaults Buffer*/
  113.   Msioctl_(mptr, 1,  &config);            /*reset to 'NVM'
  114.                            & enable rec interrupts*/
  115.   Msioctl_(mptr, 5,  &config);            /*disable rec interrupts*/
  116.   if(!Msioctl_(mptr, 0, &save_config_))        /*reprogram Mouse port*/        ret=FALSE;
  117.   Msioctl_(mptr, 4,  &config);            /*enable rec interrupts*/
  118.   msdos((0x3E<<8), mptr->Mouse_handle, 0, 0);    /*close mouse port*/
  119.   mptr->Mouse_programmed = mptr->Mouse_enabled = FALSE;
  120.   return ret;
  121. }
  122.  
  123. /***********************************************/
  124. /*   see if a Mouse byte wants to come out     */
  125. /*   of its hole, return if yes, FF if not     */
  126. /***********************************************/
  127. unsigned char *testbyte_(mptr)
  128. MOUSE_PACKET      *mptr;
  129. { struct COMM_BLOCK config;
  130.  
  131.   Msioctl_(mptr, 7, &config);            /*anyone home?*/
  132.   if (config.cb_func_retc == 0xFF)
  133.     return config.cb_character & 0x7F;
  134.   return 0xFF;
  135. }
  136. /*************************************************/
  137. /* Attach RCI vector to port                     */
  138. /*************************************************/
  139. rci_attach(mptr,cptr,buff)
  140. MOUSE_PACKET      *mptr;
  141. CONDITION_PACKET  *cptr;
  142. char              *buff;
  143. { struct Rci_block rci;
  144.   unsigned int getcs_(),  _chaddr();
  145.  
  146.   rci.rb_offset = _chaddr(mptr,cptr,buff);    /*received char int vectors*/
  147.   rci.rb_segment= getcs_();
  148.   if(Msioctl_(mptr, 17, &rci) != 0)        /*set Mouse port int vector*/
  149.     mptr->Mouse_new_vectors = TRUE;
  150. }
  151.  
  152. /*************************************************/
  153. /* Detach RCI vector from port                   */
  154. /*************************************************/
  155. rci_detach(mptr)
  156. MOUSE_PACKET      *mptr;
  157. { struct Rci_block rci;
  158.  
  159.   Msioctl_(mptr, 18, &rci);            /*reset to defaults RX int*/
  160.   mptr->Mouse_new_vectors = FALSE;
  161.  
  162. }
  163.  
  164. /*************************************************/
  165. /* MSIOCTL -- communicate with O.S.                */
  166. /*    pass device#, function, and &command buff. */
  167. /*************************************************/
  168.  
  169. int Msioctl_(mptr, function, ccb)
  170. MOUSE_PACKET      *mptr;
  171. unsigned int function;
  172. struct COMM_BLOCK *ccb;
  173. {
  174.  
  175.   ccb->cb_device = mptr->Mouse_device;
  176.   ccb->cb_function = function;
  177.   msdos((0x44<<8)+2, mptr->Mouse_handle, 0, ccb);
  178.   return ccb->cb_func_retc;
  179. }
  180.  
  181. /**************************************************/
  182. /*     return current Data Segment register       */
  183. /**************************************************/
  184. unsigned int getds_()
  185. {   struct {unsigned int rcs,rss,rds,res;} rregs;
  186.  
  187.   segread(&rregs);
  188.   return rregs.rds;
  189. }
  190.  
  191. /**************************************************/
  192. /*     return current Code Segment register       */
  193. /**************************************************/
  194. unsigned int getcs_()
  195. {   struct {unsigned int rcs,rss,rds,res;} rregs;
  196.  
  197.   segread(&rregs);
  198.   return rregs.rcs;
  199. }
  200.  
  201.  
  202. /*****************************************************/
  203. /* Serial Microsoft Mouse hardware dependent code    */
  204. /*****************************************************/
  205.  
  206. /*-------------------------- Microsoft Mouse 3 byte packet
  207. ;
  208. ;
  209. ;    First byte
  210. ;
  211. ;    +----+----+----+----+----+----+----+----+
  212. ;    | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  213. ;    +----+----+----+----+----+----+----+----+
  214. ;      |    |    |    |    |    |    |    |
  215. ;      |    |    |    |    |    |    |    +---- X6  Delta X
  216. ;      |    |    |    |    |    |    +--------- X7  Delta X
  217. ;      |    |    |    |    |    +-------------- Y6  Delta Y
  218. ;      |    |    |    |    +------------------- Y7  Delta Y
  219. ;      |    |    |    +------------------------ Right Mouse Button
  220. ;      |    |    +----------------------------- Left Mouse Button
  221. ;      |    +---------------------------------- 1 = first byte (synchronize)
  222. ;      +--------------------------------------- 7 bit ascii (MUST MASK)
  223.  
  224. ;    Second byte
  225. ;
  226. ;    +----+----+----+----+----+----+----+----+
  227. ;    | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  228. ;    +----+----+----+----+----+----+----+----+
  229. ;      |    |    |    |    |    |    |    |
  230. ;      |    |    |    |    |    |    |    +---- X0  Delta X
  231. ;      |    |    |    |    |    |    +--------- X1  Delta X
  232. ;      |    |    |    |    |    +-------------- X2  Delta X
  233. ;      |    |    |    |    +------------------- X3  Delta X
  234. ;      |    |    |    +------------------------ X4  Delta X
  235. ;      |    |    +----------------------------- X5  Delta X
  236. ;      |    +---------------------------------- 0 = data byte (not first)
  237. ;      +--------------------------------------- 7 bit ascii   (MUST MASK)
  238.  
  239. ;    Third byte
  240. ;
  241. ;    +----+----+----+----+----+----+----+----+
  242. ;    | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  243. ;    +----+----+----+----+----+----+----+----+
  244. ;      |    |    |    |    |    |    |    |
  245. ;      |    |    |    |    |    |    |    +---- Y0  Delta Y
  246. ;      |    |    |    |    |    |    +--------- Y1  Delta Y
  247. ;      |    |    |    |    |    +-------------- Y2  Delta Y
  248. ;      |    |    |    |    +------------------- Y3  Delta Y
  249. ;      |    |    |    +------------------------ Y4  Delta Y
  250. ;      |    |    +----------------------------- Y5  Delta Y
  251.  ;      |    +---------------------------------- 0 = data byte (not first)
  252.  ;      +--------------------------------------- 7 bit ascii   (MUST MASK)
  253. ;
  254.  */
  255.  
  256. /***********************************************/
  257. /*   Microsoft Mouse acknowledge check         */
  258. /***********************************************/
  259. msack(mptr)
  260. MOUSE_PACKET      *mptr;
  261.  
  262. {
  263.   int i;
  264.   unsigned char c;
  265.   for (i=0; i<64; i++)             /*wait for ACK*/
  266.     if ( (c = testbyte_(mptr)) == 'M')
  267.         return (mptr->Mouse_enabled = TRUE);
  268.   return FALSE;
  269. }
  270.  
  271. /***********************************************/
  272. /*   Microsoft Mouse packet decode             */
  273. /***********************************************/
  274. CONDITION_PACKET *msdecode(mptr)
  275. MOUSE_PACKET *mptr;
  276.  
  277. {
  278.   action_.Delta_x = action_.Delta_y = 0;    /*reset delta return*/
  279.   action_.Movement = FALSE;        /*reset interrupt conditions*/
  280.   if (mptr->Mouse_enabled != TRUE)
  281.     return &action_;
  282.  
  283.   switch (mptr->P_button) {
  284.     case TRUE:  if (action_.P_button_rel) {
  285.             action_.P_button_rel--;
  286.             mptr->P_button = FALSE;
  287.             }
  288.         break;
  289.     case FALSE: if (action_.P_button_dep) {
  290.             action_.P_button_dep--;
  291.             mptr->P_button = TRUE;
  292.             }
  293.         break;
  294.     }
  295.   switch (mptr->S_button) {
  296.     case TRUE:  if (action_.S_button_rel) {
  297.             action_.S_button_rel--;
  298.             mptr->S_button = FALSE;
  299.             }
  300.         break;
  301.     case FALSE: if (action_.S_button_dep) {
  302.             action_.S_button_dep--;
  303.             mptr->S_button = TRUE;
  304.             }
  305.         break;
  306.     }
  307.   if (mptr->Deltax | mptr->Deltay) {         /*if movement ... */
  308.     action_.Movement = TRUE;             /*flag "mouse moved"*/
  309.     action_.Delta_x = mptr->Deltax * mptr->X_ratio;  /*add in delta_x*/
  310.     action_.Delta_y = mptr->Deltay * mptr->Y_ratio;  /*add in delta_y*/
  311.     mptr->Deltax = mptr->Deltay = 0;
  312.     }
  313.   return &action_;
  314. }
  315. testbit_(b,mask)            /*test bit, ret != 0 if yes*/
  316. unsigned b,mask;
  317. { return (b &= mask); }
  318.  
  319. 
  320.