devnmouse.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #define Image IMAGE
  8. #include <draw.h>
  9. #include <memdraw.h>
  10. #include <cursor.h>
  11. #include "screen.h"
  12. typedef struct Mouseinfo Mouseinfo;
  13. struct Mouseinfo
  14. {
  15. Point xy;
  16. int redraw; /* update cursor on screen */
  17. };
  18. Mouseinfo mouse;
  19. Cursorinfo cursor;
  20. int mouseshifted;
  21. Cursor curs;
  22. void Cursortocursor(Cursor*);
  23. int mousechanged(void*);
  24. static void mouseclock(void);
  25. enum{
  26. Qdir,
  27. Qcursor,
  28. Qmousepoint,
  29. };
  30. static Dirtab mousedir[]={
  31. ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
  32. "cursor", {Qcursor}, 0, 0660,
  33. "mousepoint", {Qmousepoint}, 0, 0220,
  34. };
  35. extern Memimage* gscreen;
  36. static void
  37. mousereset(void)
  38. {
  39. if(!conf.monitor)
  40. return;
  41. curs = arrow;
  42. Cursortocursor(&arrow);
  43. /* redraw cursor about 30 times per second */
  44. addclock0link(mouseclock, 33);
  45. }
  46. static void
  47. mouseinit(void)
  48. {
  49. if(!conf.monitor)
  50. return;
  51. cursoron(1);
  52. }
  53. static Chan*
  54. mouseattach(char *spec)
  55. {
  56. if(!conf.monitor)
  57. error(Egreg);
  58. return devattach('m', spec);
  59. }
  60. static Walkqid*
  61. mousewalk(Chan *c, Chan *nc, char **name, int nname)
  62. {
  63. return devwalk(c, nc, name, nname, mousedir, nelem(mousedir), devgen);
  64. }
  65. static int
  66. mousestat(Chan *c, uchar *db, int n)
  67. {
  68. return devstat(c, db, n, mousedir, nelem(mousedir), devgen);
  69. }
  70. static Chan*
  71. mouseopen(Chan *c, int omode)
  72. {
  73. switch((ulong)c->qid.path){
  74. case Qdir:
  75. if(omode != OREAD)
  76. error(Eperm);
  77. break;
  78. case Qmousepoint:
  79. if(omode != OWRITE)
  80. error(Eperm);
  81. break;
  82. default:
  83. break;
  84. }
  85. c->mode = openmode(omode);
  86. c->flag |= COPEN;
  87. c->offset = 0;
  88. return c;
  89. }
  90. static void
  91. mouseclose(Chan*)
  92. {
  93. }
  94. static long
  95. mouseread(Chan *c, void *va, long n, vlong off)
  96. {
  97. uchar *p;
  98. p = va;
  99. switch((ulong)c->qid.path){
  100. case Qdir:
  101. return devdirread(c, va, n, mousedir, nelem(mousedir), devgen);
  102. case Qcursor:
  103. if(off != 0)
  104. return 0;
  105. if(n < 2*4+2*2*16)
  106. error(Eshort);
  107. n = 2*4+2*2*16;
  108. lock(&cursor);
  109. BPLONG(p+0, curs.offset.x);
  110. BPLONG(p+4, curs.offset.y);
  111. memmove(p+8, curs.clr, 2*16);
  112. memmove(p+40, curs.set, 2*16);
  113. unlock(&cursor);
  114. return n;
  115. default:
  116. error(Egreg);
  117. }
  118. return 0;
  119. }
  120. static long
  121. mousewrite(Chan *c, void *va, long n, vlong)
  122. {
  123. char *p;
  124. Point pt;
  125. char buf[64];
  126. p = va;
  127. switch((ulong)c->qid.path){
  128. case Qdir:
  129. error(Eisdir);
  130. case Qcursor:
  131. cursoroff(1);
  132. if(n < 2*4+2*2*16){
  133. curs = arrow;
  134. Cursortocursor(&arrow);
  135. }else{
  136. n = 2*4+2*2*16;
  137. curs.offset.x = BGLONG(p+0);
  138. curs.offset.y = BGLONG(p+4);
  139. memmove(curs.clr, p+8, 2*16);
  140. memmove(curs.set, p+40, 2*16);
  141. Cursortocursor(&curs);
  142. }
  143. mouse.redraw = 1;
  144. mouseclock();
  145. cursoron(1);
  146. return n;
  147. case Qmousepoint:
  148. if(n > sizeof buf-1)
  149. n = sizeof buf -1;
  150. memmove(buf, va, n);
  151. buf[n] = 0;
  152. p = 0;
  153. pt.x = strtoul(buf, &p, 0);
  154. if(p == 0)
  155. error(Eshort);
  156. pt.y = strtoul(p, 0, 0);
  157. /*
  158. * this used to be qlocked here but not locked below.
  159. */
  160. if(gscreen && ptinrect(pt, gscreen->r)){
  161. mouse.xy = pt;
  162. mouse.redraw = 1;
  163. mouseclock();
  164. }
  165. return n;
  166. }
  167. error(Egreg);
  168. return -1;
  169. }
  170. Dev nmousedevtab = {
  171. 'm',
  172. "mouse",
  173. mousereset,
  174. mouseinit,
  175. devshutdown,
  176. mouseattach,
  177. mousewalk,
  178. mousestat,
  179. mouseopen,
  180. devcreate,
  181. mouseclose,
  182. mouseread,
  183. devbread,
  184. mousewrite,
  185. devbwrite,
  186. devremove,
  187. devwstat,
  188. };
  189. void
  190. Cursortocursor(Cursor *c)
  191. {
  192. lock(&cursor);
  193. memmove(&cursor.Cursor, c, sizeof(Cursor));
  194. setcursor(c);
  195. unlock(&cursor);
  196. }
  197. /*
  198. * called by the clock routine to redraw the cursor
  199. */
  200. static void
  201. mouseclock(void)
  202. {
  203. if(mouse.redraw && canlock(&cursor)){
  204. mouse.redraw = 0;
  205. cursoroff(0);
  206. mouse.redraw = cursoron(0);
  207. unlock(&cursor);
  208. }
  209. drawactive(0);
  210. }
  211. Point
  212. mousexy(void)
  213. {
  214. return mouse.xy;
  215. }