ms.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Count bytes within runes, if it fits in a uint64_t, and other things.
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. static int packetsize = 3;
  15. void
  16. main(int argc, char *argv[])
  17. {
  18. int intellimouse = 0;
  19. int shift = 0;
  20. int ps2fd;
  21. int mousefd;
  22. int c;
  23. static short msg[4];
  24. static int nb;
  25. static uint8_t b[] = {0, 1, 4, 5, 2, 3, 6, 7, 0, 1, 2, 3, 2, 3, 6, 7 };
  26. int buttons, dx, dy;
  27. ps2fd = open("#P/ps2mouse", OREAD);
  28. if (ps2fd < 0) {
  29. print("#P/ps2mouse: %r");
  30. exits("can't open #P/ps2mouse");
  31. }
  32. mousefd = open("/dev/mousein", OWRITE);
  33. if (mousefd < 0) {
  34. print("/dev/mousein: %r");
  35. exits("can't open /dev/mouse");
  36. }
  37. while (read(ps2fd, &c, 1) > 0) {
  38. /*
  39. * non-ps2 keyboards might not set shift
  40. * but still set mouseshifted.
  41. */
  42. //shift |= mouseshifted;
  43. /*
  44. * check byte 0 for consistency
  45. */
  46. if(nb==0 && (c&0xc8)!=0x08)
  47. if(intellimouse && (c==0x00 || c==0x01 || c==0xFF)){
  48. /* last byte of 4-byte packet */
  49. packetsize = 4;
  50. continue;
  51. }
  52. msg[nb] = c;
  53. if(++nb == packetsize){
  54. nb = 0;
  55. if(msg[0] & 0x10)
  56. msg[1] |= 0xFF00;
  57. if(msg[0] & 0x20)
  58. msg[2] |= 0xFF00;
  59. buttons = b[(msg[0]&7) | (shift ? 8 : 0)];
  60. if(intellimouse && packetsize==4){
  61. if((msg[3]&0xc8) == 0x08){
  62. /* first byte of 3-byte packet */
  63. packetsize = 3;
  64. msg[0] = msg[3];
  65. nb = 1;
  66. /* fall through to emit previous packet */
  67. }else{
  68. /* The AccuPoint on the Toshiba 34[48]0CT
  69. * encodes extra buttons as 4 and 5. They repeat
  70. * and don't release, however, so user-level
  71. * timing code is required. Furthermore,
  72. * intellimice with 3buttons + scroll give a
  73. * two's complement number in the lower 4 bits
  74. * (bit 4 is sign extension) that describes
  75. * the amount the scroll wheel has moved during
  76. * the last sample. Here we use only the sign to
  77. * decide whether the wheel is moving up or down
  78. * and generate a single button 4 or 5 click
  79. * accordingly.
  80. */
  81. if((msg[3] >> 3) & 1)
  82. buttons |= 1<<3;
  83. else if(msg[3] & 0x7)
  84. buttons |= 1<<4;
  85. }
  86. }
  87. dx = msg[1];
  88. dy = -msg[2];
  89. //mousetrack(dx, dy, buttons, TK2MS(MACHP(0)->ticks));
  90. fprint(mousefd,"m %d %d %#x\n", dx, dy, buttons);
  91. }
  92. }
  93. }