aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/devdraw/snarf.c
blob: 1e7a93a1cb862387fda12eca24c572c3125ba272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <u.h>
#include <sys/select.h>
#include <errno.h>
#include "x11-inc.h"
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include <memlayer.h>
#include <keyboard.h>
#include <mouse.h>
#include <cursor.h>
#include <drawfcall.h>
#include "x11-memdraw.h"
#include "devdraw.h"

#undef time

#define MouseMask (\
	ButtonPressMask|\
	ButtonReleaseMask|\
	PointerMotionMask|\
	Button1MotionMask|\
	Button2MotionMask|\
	Button3MotionMask)

#define Mask MouseMask|ExposureMask|StructureNotifyMask|KeyPressMask|EnterWindowMask|LeaveWindowMask

void runxevent(XEvent*);

void
usage(void)
{
	fprint(2, "usage: snarf [-a] [-o | text]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	int apple;
	int out;

	apple = 0;
	out = 0;

	ARGBEGIN{
	case 'a':
		apple = 1;
		break;
	case 'o':
		out = 1;
		break;
	default:
		usage();
	}ARGEND

	if(out && argc != 0)
		usage();
	if(!out && argc != 1)
		usage();

	_x.fd = -1;

	memimageinit();
	_xattach("snarf", "20x20");

	XSelectInput(_x.display, _x.drawable, Mask);
	XFlush(_x.display);

	if(out){
		char *s;
		if(apple)
			s = _applegetsnarf();
		else
			s = _xgetsnarf();
		write(1, s, strlen(s));
		write(1, "\n", 1);
		exits(0);
	}else{
		_xputsnarf(argv[0]);
		for(;;){
			XEvent event;
			XNextEvent(_x.display, &event);
			runxevent(&event);
		}
	}
}

/*
 * Handle an incoming X event.
 */
void
runxevent(XEvent *xev)
{
	switch(xev->type){
	case Expose:
		_xexpose(xev);
		break;
	
	case DestroyNotify:
		if(_xdestroy(xev))
			exits(0);
		break;

	case SelectionRequest:
		_xselect(xev);
		break;
	}
}