|
|
The following sample program opens an event queue with a mouse (relative device) and the keyboard (string device). It prints out information about events as they enter the queue. Note that depending on your system, you may have to configure system event parameters.
This program illustrates basic event manager functionality. It does not use the routines for querying or modifying the devices or event masks for the queue, nor the routines for suspending the event queue.
1 #include <stdio.h> 2 #include <signal.h> 3 #include <sys/machdep.h> 4 #include <sys/types.h> 5 #include <sys/param.h> 6 #include <sys/sysmacros.h> 7 #include <sys/page.h> 8 #include <sys/event.h> 9 #include <mouse.h>
10 #define DEL 0x7f
11 char progname[80]; 12 extern int errno;
13 main(argc,argv) 14 int argc; 15 char *argv[]; 16 { 17 int qfd,finish(),report(); 18 EVENT *evp; 19 int ret; 20 dmask_t dmask = D_STRING | D_REL | D_BUTTON; 21 extern int ev_errlev;
#include
d
in this order in any program that uses the event manager.
32 ev_errlev = 1; 33 strcpy(progname,argv[0]); 34 signal(SIGINT,finish); 35 signal(SIGSEGV, report); 36 signal(SIGSYS, report);
37 printf("\n"); 38 ret = ev_init(); 39 printf("init == %d\n", ret); 40 if ( ret < 0 ) 41 finish(); 42 qfd = ev_open(&dmask); 43 printf("open == %d\n", qfd); 44 if ( qfd < 0 ) 45 fail("could not open event queue"); 46 if ( dmask != (D_STRING | D_REL | D_BUTTON)) 47 fail("could not attach mouse and keyboard"); 48 while ( !(ev_block()) ) 49 if ( (evp = ev_read()) != (EVENT *) NULL ) { 50 evprint(evp); 51 ev_pop(); 52 } 53 return 0; 54 } 55
56 fail(s) 57 char *s; 58 { 59 extern int errno;
60 printf("s(%d): %s\n",progname,errno,s); 61 finish(); 62 return 0; 63 }
The argument to the ev_open routine points to the device mask that was defined in line 20. The function tries to attach devices of the indicated type(s) to the vent queue. If ev_open cannot find any devices to attach to the queue, it closes the queue and returns a negative number indicating an error. If it is able to attach devices, it sets the mask to indicate what kinds of devices it has found. So line 36 verifies that the mask is still set as it was in line 20; if it is not, the program exits and returns an error message using the fail( ) function defined lin lines 47-54.
If ev_open is successful, it returns a file descriptor that is used with the select(S) system call. It should not be used for reading or writing.
ev_read takes no arguments.
It reads the next element in the event queue
and returns a pointer to the next element in the vent queue,
or to NULL if the queue is empty.
Multiple calls to this routine
return the same pointer
until a call to ev_pop is made.
ev_pop pops the next element off the queue.
As soon as this routine is called,
the pointer returned by the most recent call
to ev_read must be considered invalid,
because the event driver can overwrite
the pointed area with new information.
ev_pop fails if the queue is empty. Otherwise, it returns a number that indicates how many events have been lost to queue overrun since the last ev_pop. The queue is of a fixed size (determined by the EVDEVSPERQ tunable parameter). If the application does not read the queue fast enough, events may be lost; this is called ``queue overrun'' because the event driver would overrun the tail of the queue if it wrote new events. The event driver provides a counter that indicates the number of events it has lost. That counter is cleared when it succeeds in writing an event. An ev_pop call always causes a new slot to be available, which clears the overrun counter. ev_pop returns the old overrun counter so that an application is aware if events are being lost.
54 evprint(evp) 55 EVENT *evp; 56 { 57 static long time=-1;
58 if (time == -1) 59 time = evp->timestamp; 60 if (EV_TAG(*evp) & T_STRING) { 61 printf("time(%d) tag(%d) bufsize(%d) buf(%x)\n", 62 EV_TIME(*evp)-time, 63 EV_TAG(*evp), 64 EV_BUFCNT(*evp), 65 EV_BUF(*evp)[0]); 66 if (EV_BUF(*evp[0]) == DEL) 67 finish(); 68 } 69 else { 70 printf("time(%ld) tag(%d) butts(%d) x(%ld) y(%ld)\n", 71 EV_TIME(*evp) - time, 72 EV_TAG(*evp), 73 EV_BUTTONS(*evp), 74 EV_DX(*evp), 75 EV_DY(*evp)); 76 if (EV_BUTTONS(*evp) == 7) 77 finish(); 78 } 79 return 0; 80 }
81 finish() 82 { 83 ev_close(); 84 printf("done\n"); 85 exit(0); 86 }
87 report(s) 88 int s; 89 { 90 printf("got signal %d\n", s); 91 finish(); 92 }