mouse.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <thread.h>
  13. #include <cursor.h>
  14. #include <mouse.h>
  15. void
  16. moveto(Mousectl *m, Point pt)
  17. {
  18. fprint(m->mfd, "m%d %d", pt.x, pt.y);
  19. m->xy = pt;
  20. }
  21. void
  22. closemouse(Mousectl *mc)
  23. {
  24. if(mc == nil)
  25. return;
  26. postnote(PNPROC, mc->pid, "kill");
  27. do; while(nbrecv(mc->c, (Mouse *)mc) > 0);
  28. close(mc->mfd);
  29. close(mc->cfd);
  30. free(mc->file);
  31. free(mc->c);
  32. free(mc->resizec);
  33. free(mc);
  34. }
  35. int
  36. readmouse(Mousectl *mc)
  37. {
  38. if(mc->image)
  39. flushimage(mc->image->display, 1);
  40. if(recv(mc->c, (Mouse *)mc) < 0){
  41. fprint(2, "readmouse: %r\n");
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. static
  47. void
  48. _ioproc(void *arg)
  49. {
  50. int n, nerr, one;
  51. char buf[1+5*12];
  52. Mouse m;
  53. Mousectl *mc;
  54. mc = arg;
  55. threadsetname("mouseproc");
  56. one = 1;
  57. memset(&m, 0, sizeof m);
  58. mc->pid = getpid();
  59. nerr = 0;
  60. for(;;){
  61. n = read(mc->mfd, buf, sizeof buf);
  62. if(n != 1+4*12){
  63. yield(); /* if error is due to exiting, we'll exit here */
  64. fprint(2, "mouse: bad count %d not 49: %r\n", n);
  65. if(n<0 || ++nerr>10)
  66. threadexits("read error");
  67. continue;
  68. }
  69. nerr = 0;
  70. switch(buf[0]){
  71. case 'r':
  72. send(mc->resizec, &one);
  73. /* fall through */
  74. case 'm':
  75. m.xy.x = atoi(buf+1+0*12);
  76. m.xy.y = atoi(buf+1+1*12);
  77. m.buttons = atoi(buf+1+2*12);
  78. m.msec = atoi(buf+1+3*12);
  79. send(mc->c, &m);
  80. /*
  81. * mc->Mouse is updated after send so it doesn't have wrong value if we block during send.
  82. * This means that programs should receive into mc->Mouse (see readmouse() above) if
  83. * they want full synchrony.
  84. mc->Mouse = m;
  85. */
  86. *(Mouse *)mc = m;
  87. break;
  88. }
  89. }
  90. }
  91. Mousectl*
  92. initmouse(char *file, Image *i)
  93. {
  94. Mousectl *mc;
  95. char *t, *sl;
  96. mc = mallocz(sizeof(Mousectl), 1);
  97. if(file == nil)
  98. file = "/dev/mouse";
  99. mc->file = strdup(file);
  100. mc->mfd = open(file, ORDWR|OCEXEC);
  101. if(mc->mfd<0 && strcmp(file, "/dev/mouse")==0){
  102. bind("#m", "/dev", MAFTER);
  103. mc->mfd = open(file, ORDWR|OCEXEC);
  104. }
  105. if(mc->mfd < 0){
  106. free(mc);
  107. return nil;
  108. }
  109. t = malloc(strlen(file)+16);
  110. if (t == nil) {
  111. close(mc->mfd);
  112. free(mc);
  113. return nil;
  114. }
  115. strcpy(t, file);
  116. sl = utfrrune(t, '/');
  117. if(sl)
  118. strcpy(sl, "/cursor");
  119. else
  120. strcpy(t, "/dev/cursor");
  121. mc->cfd = open(t, ORDWR|OCEXEC);
  122. free(t);
  123. mc->image = i;
  124. mc->c = chancreate(sizeof(Mouse), 0);
  125. mc->resizec = chancreate(sizeof(int), 2);
  126. proccreate(_ioproc, mc, 4096);
  127. return mc;
  128. }
  129. void
  130. setcursor(Mousectl *mc, Cursor *c)
  131. {
  132. char curs[2*4+2*2*16];
  133. if(c == nil)
  134. write(mc->cfd, curs, 0);
  135. else{
  136. BPLONG(curs+0*4, c->offset.x);
  137. BPLONG(curs+1*4, c->offset.y);
  138. memmove(curs+2*4, c->clr, 2*2*16);
  139. write(mc->cfd, curs, sizeof curs);
  140. }
  141. }