Blob


1 #include <u.h>
2 #include <sys/select.h>
3 #include <errno.h>
4 #include "x11-inc.h"
5 #include <libc.h>
6 #include <draw.h>
7 #include <memdraw.h>
8 #include <memlayer.h>
9 #include <keyboard.h>
10 #include <mouse.h>
11 #include <cursor.h>
12 #include <drawfcall.h>
13 #include "x11-memdraw.h"
14 #include "devdraw.h"
16 #undef time
18 #define MouseMask (\
19 ButtonPressMask|\
20 ButtonReleaseMask|\
21 PointerMotionMask|\
22 Button1MotionMask|\
23 Button2MotionMask|\
24 Button3MotionMask)
26 #define Mask MouseMask|ExposureMask|StructureNotifyMask|KeyPressMask|EnterWindowMask|LeaveWindowMask
28 void runxevent(XEvent*);
30 void
31 usage(void)
32 {
33 fprint(2, "usage: snarf [-a] [-o | text]\n");
34 exits("usage");
35 }
37 void
38 main(int argc, char **argv)
39 {
40 int apple;
41 int out;
43 apple = 0;
44 out = 0;
46 ARGBEGIN{
47 case 'a':
48 apple = 1;
49 break;
50 case 'o':
51 out = 1;
52 break;
53 default:
54 usage();
55 }ARGEND
57 if(out && argc != 0)
58 usage();
59 if(!out && argc != 1)
60 usage();
62 _x.fd = -1;
64 memimageinit();
65 _xattach("snarf", "20x20");
67 XSelectInput(_x.display, _x.drawable, Mask);
68 XFlush(_x.display);
70 if(out){
71 char *s;
72 if(apple)
73 s = _applegetsnarf();
74 else
75 s = _xgetsnarf();
76 write(1, s, strlen(s));
77 write(1, "\n", 1);
78 exits(0);
79 }else{
80 _xputsnarf(argv[0]);
81 for(;;){
82 XEvent event;
83 XNextEvent(_x.display, &event);
84 runxevent(&event);
85 }
86 }
87 }
89 /*
90 * Handle an incoming X event.
91 */
92 void
93 runxevent(XEvent *xev)
94 {
95 switch(xev->type){
96 case Expose:
97 _xexpose(xev);
98 break;
100 case DestroyNotify:
101 if(_xdestroy(xev))
102 exits(0);
103 break;
105 case SelectionRequest:
106 _xselect(xev);
107 break;