plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

mouse.c (1750B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <draw.h>
      4 #include <thread.h>
      5 #include <cursor.h>
      6 #include <mouse.h>
      7 
      8 void
      9 moveto(Mousectl *mc, Point pt)
     10 {
     11 	_displaymoveto(mc->display, pt);
     12 	mc->m.xy = pt;
     13 }
     14 
     15 void
     16 closemouse(Mousectl *mc)
     17 {
     18 	if(mc == nil)
     19 		return;
     20 
     21 /*	postnote(PNPROC, mc->pid, "kill"); */
     22 
     23 	do; while(nbrecv(mc->c, &mc->m) > 0);
     24 	chanfree(mc->c);
     25 	chanfree(mc->resizec);
     26 	free(mc);
     27 }
     28 
     29 int
     30 readmouse(Mousectl *mc)
     31 {
     32 	if(mc->display)
     33 		flushimage(mc->display, 1);
     34 	if(recv(mc->c, &mc->m) < 0){
     35 		fprint(2, "readmouse: %r\n");
     36 		return -1;
     37 	}
     38 	return 0;
     39 }
     40 
     41 static
     42 void
     43 _ioproc(void *arg)
     44 {
     45 	int one, resized;
     46 	Mouse m;
     47 	Mousectl *mc;
     48 
     49 	mc = arg;
     50 	threadsetname("mouseproc");
     51 	memset(&m, 0, sizeof m);
     52 	one = 1;
     53 	resized = 0;
     54 	for(;;){
     55 		if(_displayrdmouse(mc->display, &m, &resized) < 0) {
     56 			if(postnote(PNPROC, getpid(), "hangup") < 0)
     57 				fprint(2, "postnote: %r\n");
     58 			sleep(10*1000);
     59 			threadexitsall("mouse read error");
     60 		}
     61 		if(resized)
     62 			send(mc->resizec, &one);
     63 		send(mc->c, &m);
     64 		/*
     65 		 * mc->m is updated after send so it doesn't have wrong value if we block during send.
     66 		 * This means that programs should receive into mc->Mouse (see readmouse() above) if
     67 		 * they want full synchrony.
     68 		 */
     69 		mc->m = m;
     70 	}
     71 }
     72 
     73 Mousectl*
     74 initmouse(char *file, Image *i)
     75 {
     76 	Mousectl *mc;
     77 
     78 	mc = mallocz(sizeof(Mousectl), 1);
     79 	if(i)
     80 		mc->display = i->display;
     81 	mc->c = chancreate(sizeof(Mouse), 0);
     82 	chansetname(mc->c, "mousec");
     83 	mc->resizec = chancreate(sizeof(int), 2);
     84 	chansetname(mc->resizec, "resizec");
     85 	proccreate(_ioproc, mc, 32*1024);
     86 	return mc;
     87 }
     88 
     89 void
     90 setcursor(Mousectl *mc, Cursor *c)
     91 {
     92 	_displaycursor(mc->display, c, nil);
     93 }
     94 
     95 void
     96 setcursor2(Mousectl *mc, Cursor *c, Cursor2 *c2)
     97 {
     98 	_displaycursor(mc->display, c, c2);
     99 }