File: fookb.c

package info (click to toggle)
fookb 2.21-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 360 kB
  • ctags: 49
  • sloc: ansic: 567; makefile: 85; sh: 44
file content (286 lines) | stat: -rw-r--r-- 6,323 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * fookb.c
 *
 * (c) 1998, 1999, 2000, 2001 Alexey Vyskubov <alexey@pepper.spb.ru>
 */

#include <stdlib.h>		/* malloc() */
#include <stdio.h>		/* puts() */

/* X Window headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>

/* Command line parsing and X resource manager */
#include <X11/Xresource.h>

/* XKB fun */
#include <X11/XKBlib.h>

/* My own fun */
#include "fookb.h"
#include "images.h"
#include "sound.h"
#include "opts.h"

#define sterror(x) (void)printf("Strange error, please report! %s:%d, %s\n", __FILE__, __LINE__, x)

static void getGC(win, gc)
Window win;
GC *gc;
{
	unsigned long valuemask = 0;	/* No data in ``values'' */
	XGCValues values;
	*gc = XCreateGC(dpy, win, valuemask, &values);
/*	XSetForeground(dpy, *gc, BlackPixel(dpy, scr)); */
}


int main(argc, argv)
int argc;
register char *argv[];
{
	int err;

	int *event_rtrn;	/* stupid things for XkbOpenDisplay */
	int *error_rtrn;
	int *reason_rtrn;

	XkbEvent labuda;	/* Xkb event. X event will be labuda.core */

	Window billy;		/*   _The_ Window  */

#ifdef WMAKER
	Window dilly;		/* ...and his icon */
#endif

	int border = 0;		/* _The_ Window parameters */
	unsigned int wth = 48;	/* Well, you understood */
	unsigned int hth = 48;

	XWMHints *wm_hints;
	XClassHint class_hints;

#ifdef WMAKER			/* If we use Windowmaker, _The_ Window will */
	XSizeHints *BigBunny;	/* be withdrawn. And we do not want to
				   place empty border by hand. */
#endif

	GC gc;			/* Graphic context */

	char resn[] = "fookb";
	char resc[] = "Fookb";


	int state = 0;		/* We suppose that latin keyboard is the
				   primal state FIXME */

	event_rtrn = malloc(sizeof(int));
	error_rtrn = malloc(sizeof(int));
	reason_rtrn = malloc(sizeof(int));

	XrmInitialize();	/* We should initialize X resource
				   manager before doing something else 
				 */

	ParseOptions(&argc, argv);	/* We should parse command line
					   options and try to find '-display'
					   before opening X display */

/* Go, fighters, go! */
	dpy = XkbOpenDisplay(mydispname,
			     event_rtrn,
			     error_rtrn, NULL, NULL, reason_rtrn);

/* Oops. */
	if (dpy == NULL) {
		(void)puts("Cannot open display.");
		exit(EXIT_FAILURE);
	}

	scr = DefaultScreen(dpy);
	root = RootWindow(dpy, scr);

	MoreOptions();		/* Now we can parse X server resource
				   database. It is not available
				   before display is opened */

/* We would like receive the only Xkb event: XkbStateNotify. And only
 * when XkbLockGroup happens. */
	if (False == XkbSelectEventDetails(dpy, XkbUseCoreKbd, XkbStateNotify, 0xFFF,
			      XkbGroupLockMask)) {
		sterror("Cannot XkbSelectEventDetails. It's your problems -- not mine.");
		exit(EXIT_FAILURE);
	}

/* Run out! */
	billy = XCreateSimpleWindow(dpy,
				    root,
				    0, 0,
				    wth, hth,
				    border,
				    BlackPixel(dpy, scr),
				    WhitePixel(dpy, scr));

#ifdef WMAKER
	dilly = XCreateSimpleWindow(dpy,
				    root,
				    0, 0,
				    wth, hth,
				    border,
				    BlackPixel(dpy, scr),
				    WhitePixel(dpy, scr));
#endif

	class_hints.res_name = resn;
	class_hints.res_class = resc;

	err = XSetClassHint(dpy, billy, &class_hints);
	switch(err) {
		case BadAlloc:
			sterror("BadAlloc");
			exit(EXIT_FAILURE);
		case BadWindow:
			sterror("BadWindow");
			exit(EXIT_FAILURE);
	}

	wm_hints = XAllocWMHints();
	wm_hints->window_group = billy;

#ifdef WMAKER
	wm_hints->icon_window = dilly;
#endif

	wm_hints->input = False;
	wm_hints->flags = InputHint | WindowGroupHint;

#ifdef WMAKER
	wm_hints->flags = wm_hints->flags | IconWindowHint;
	err = XSetWMHints(dpy, dilly, wm_hints);
	switch(err) {
		case BadAlloc:
			sterror("BadAlloc");
			exit(EXIT_FAILURE);
		case BadWindow:
			sterror("BadWindow");
			exit(EXIT_FAILURE);
	}
	wm_hints->initial_state = WithdrawnState;
	wm_hints->flags = wm_hints->flags | StateHint;
#endif

	err = XSetWMHints(dpy, billy, wm_hints);
	switch(err) {
		case BadAlloc:
			sterror("BadAlloc");
			exit(EXIT_FAILURE);
		case BadWindow:
			sterror("BadWindow");
			exit(EXIT_FAILURE);
	}

#ifdef WMAKER			/* Look at the comment for XSizeHints * BigBunny */
	BigBunny = XAllocSizeHints();
	if (NULL == BigBunny) {
		(void)printf("Not enough memory, %s:%d.\n", __FILE__, __LINE__);
		exit(EXIT_FAILURE);
	}
/* Nasty Hack. XSizeHints.x & XSizeHints.y are obsolete. */
	BigBunny->x = 0;
	BigBunny->y = 0;
	BigBunny->flags = PPosition;
	XSetWMNormalHints(dpy, billy, BigBunny);
	XFree(BigBunny);
#endif

/* The only thing we would like to do - update our billy */
#ifndef WMAKER
	XSelectInput(dpy, billy, ExposureMask | ButtonPressMask);
#else				/* ...or dilly?! */
	XSelectInput(dpy, dilly, ExposureMask | ButtonPressMask);
#endif

	XSetCommand(dpy, billy, argv, argc);

/* Programmer supplied functions */
#ifndef WMAKER
	getGC(billy, &gc);
#else
	getGC(dilly, &gc);
#endif

	read_images();		/* Let's read icon images */

/* Let's look */
	XMapWindow(dpy, billy);	/* We would like to see the window. */

/* HELLO! HELLO! HELLO! Is that our GOOD FRIEND main loop here? */
	while (1) {
		XNextEvent(dpy, &labuda.core);
		switch (labuda.core.type) {

		case Expose:	/* We should update our window. */
			if (labuda.core.xexpose.count != 0)
				/* Well, I knew what does it mean,
				   but I forgot :) */
				break;
#ifndef WMAKER
			update_window(billy, gc, state);
#else
			update_window(dilly, gc, state);
#endif
			break;
		case ButtonPress:

			switch (labuda.core.xbutton.button) {
			case Button1:
#ifdef DEBUG
				puts("Button1 pressed.");
#endif
				XkbLockGroup(dpy,
					     XkbUseCoreKbd,
					     (state + 1) % 4);
				break;
			case Button2:
#ifdef DEBUG
				puts("Button2 pressed.");
#endif
				XkbLockGroup(dpy,
					     XkbUseCoreKbd,
					     (state + 3) % 4);
				break;
			case Button3:
#ifdef DEBUG
				puts("Button3 pressed, bye.");
#endif
				XFreeGC(dpy, gc);
				XDestroyWindow(dpy, billy);
#ifdef WMAKER
				XDestroyWindow(dpy, dilly);
#endif
				XCloseDisplay(dpy);
				exit(0);
			}
			break;
		default:	/* XkbLockGroup happens : FIXME */
			drip();
			state = labuda.state.group;
#ifdef DEBUG
			printf("%u\n", state);
#endif
			if ((state < 0) || (state > 4))
				state = 4;
#ifndef WMAKER
			update_window(billy, gc, state);
#else
			update_window(dilly, gc, state);
#endif

#ifdef DEBUG
			puts(".");	/* XkbLockGroup happens */
#endif
		}
	}

}