accupoint.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <event.h>
  5. /*
  6. * Convert AccuPoint buttons 4 and 5 to a simulation of button 2.
  7. * The buttons generate down events, repeat, and have no up events,
  8. * so it's a struggle. This program turns the left button into a near-as-
  9. * possible simulation of a regular button 2, but it can only sense up
  10. * events by timeout, so it's sluggish. Thus it also turns the right button
  11. * into a click on button 2, useful for acme and chords.
  12. */
  13. typedef struct M M;
  14. struct M
  15. {
  16. Mouse;
  17. int byte;
  18. };
  19. int button2;
  20. int interrupted;
  21. int
  22. readmouse(M *m)
  23. {
  24. char buf[1+4*12];
  25. int n;
  26. n = read(0, buf, sizeof buf);
  27. if(n < 0)
  28. return n;
  29. if(n != sizeof buf)
  30. return 0;
  31. m->byte = buf[0];
  32. m->xy.x = atoi(buf+1+0*12);
  33. m->xy.y = atoi(buf+1+1*12);
  34. m->buttons = atoi(buf+1+2*12);
  35. m->msec = atoi(buf+1+3*12);
  36. return 1;
  37. }
  38. void
  39. writemouse(M *m)
  40. {
  41. print("%c%11d %11d %11d %11ld ",
  42. m->byte,
  43. m->xy.x,
  44. m->xy.y,
  45. m->buttons&7,
  46. m->msec);
  47. }
  48. void
  49. notifyf(void*, char *s)
  50. {
  51. if(strcmp(s, "alarm") == 0)
  52. interrupted = 1;
  53. noted(NCONT);
  54. }
  55. void
  56. main(void)
  57. {
  58. M m, om;
  59. int n;
  60. notify(notifyf);
  61. memset(&m, 0, sizeof m);
  62. om = m;
  63. for(;;){
  64. interrupted = 0;
  65. /* first click waits 500ms before repeating; after that they're 150, but that's ok */
  66. if(button2)
  67. alarm(550);
  68. n = readmouse(&m);
  69. if(button2)
  70. alarm(0);
  71. if(interrupted){
  72. /* timed out; clear button 2 */
  73. om.buttons &= ~2;
  74. button2 = 0;
  75. writemouse(&om);
  76. continue;
  77. }
  78. if(n <= 0)
  79. break;
  80. /* avoid bounce caused by button 5 click */
  81. if((om.buttons&16) && (m.buttons&16)){
  82. om.buttons &= ~16;
  83. continue;
  84. }
  85. if(m.buttons & 2)
  86. button2 = 0;
  87. else{
  88. /* only check 4 and 5 if 2 isn't down of its own accord */
  89. if(m.buttons & 16){
  90. /* generate quick button 2 click */
  91. button2 = 0;
  92. m.buttons |= 2;
  93. writemouse(&m);
  94. m.buttons &= ~2;
  95. /* fall through to generate up event */
  96. }else if(m.buttons & 8){
  97. /* press and hold button 2 */
  98. button2 = 1;
  99. }
  100. }
  101. if(button2)
  102. m.buttons |= 2;
  103. if(m.byte!=om.byte || m.buttons!=om.buttons || !eqpt(m.xy, om.xy))
  104. writemouse(&m);
  105. om = m;
  106. }
  107. }