summary refs log tree commit diff
path: root/calmwm.c
blob: 336c16c6c6b433efc4c2cf0a0d726fa545468f19 (plain) (blame)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * calmwm - the calm window manager
 *
 * Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
 * All rights reserved.
 *
 * $Id$
 */

#include "headers.h"
#include "calmwm.h"

Display				*X_Dpy;
XFontStruct			*X_Font;

Cursor				 Cursor_move;
Cursor				 Cursor_resize;
Cursor				 Cursor_select;
Cursor				 Cursor_default;
Cursor				 Cursor_question;

struct screen_ctx_q		 Screenq;
struct screen_ctx		*Curscreen;
u_int				 Nscreens;

struct client_ctx_q		 Clientq;

int				 Doshape, Shape_ev;
int				 Starting;
struct conf			 Conf;
struct fontdesc                 *DefaultFont;
char                            *DefaultFontName;

/* From TWM */
#define gray_width 2
#define gray_height 2
static char gray_bits[] = {0x02, 0x01};

/* List borrowed from 9wm/rio */
char *tryfonts[] = {
	"9x15bold",
	"blit",
	"*-lucidatypewriter-bold-*-14-*-75-*",
	"*-lucidatypewriter-medium-*-12-*-75-*",
	"fixed",
	"*",
	NULL,
};

static void _sigchld_cb(int);

int
main(int argc, char **argv)
{
	int ch;
	int conf_flags = 0;

	char *display_name = NULL;

	DefaultFontName = "sans-serif:pixelsize=14:bold";

	while ((ch = getopt(argc, argv, "d:sf:")) != -1) {
		switch (ch) {
		case 'd':
			display_name = optarg;
			break;
		case 's':
			conf_flags |= CONF_STICKY_GROUPS;
			break;
		case 'f':
			DefaultFontName = xstrdup(optarg);
			break;
		default:
			errx(1, "Unknown option '%c'", ch);
		}
	}
	argc -= optind;
	argv += optind;

	/* Ignore a few signals. */
        if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
                err(1, "signal");

        if (signal(SIGCHLD, _sigchld_cb) == SIG_ERR)
                err(1, "signal");

	group_init();

	Starting = 1;
	conf_setup(&Conf);
	Conf.flags |= conf_flags;
	client_setup();
	x_setup(display_name);
	Starting = 0;

	xev_init();
	XEV_QUICK(NULL, NULL, MapRequest, xev_handle_maprequest, NULL);
	XEV_QUICK(NULL, NULL, UnmapNotify, xev_handle_unmapnotify, NULL);
	XEV_QUICK(NULL, NULL, ConfigureRequest,
	    xev_handle_configurerequest, NULL);
	XEV_QUICK(NULL, NULL, PropertyNotify, xev_handle_propertynotify, NULL);
	XEV_QUICK(NULL, NULL, EnterNotify, xev_handle_enternotify, NULL);
	XEV_QUICK(NULL, NULL, LeaveNotify, xev_handle_leavenotify, NULL);
	XEV_QUICK(NULL, NULL, ButtonPress, xev_handle_buttonpress, NULL);
	XEV_QUICK(NULL, NULL, ButtonRelease, xev_handle_buttonrelease, NULL);
	XEV_QUICK(NULL, NULL, KeyPress, xev_handle_keypress, NULL);
	XEV_QUICK(NULL, NULL, KeyRelease, xev_handle_keyrelease, NULL);
	XEV_QUICK(NULL, NULL, Expose, xev_handle_expose, NULL);
	XEV_QUICK(NULL, NULL, DestroyNotify, xev_handle_destroynotify, NULL);
	XEV_QUICK(NULL, NULL, ClientMessage, xev_handle_clientmessage, NULL);

	xev_loop();

	return (0);
}

void
x_setup(char *display_name)
{
	int i;
	struct screen_ctx *sc;
	char *fontname;

	TAILQ_INIT(&Screenq);

	if ((X_Dpy = XOpenDisplay(display_name)) == NULL)
		errx(1, "%s:%d XOpenDisplay()", __FILE__, __LINE__);

	XSetErrorHandler(x_errorhandler);

	Doshape = XShapeQueryExtension(X_Dpy, &Shape_ev, &i);

	i = 0;
	while ((fontname = tryfonts[i++]) != NULL) {
		if ((X_Font = XLoadQueryFont(X_Dpy, fontname)) != NULL)
			break;
	}

	if (fontname == NULL)
		errx(1, "Couldn't load any fonts.");

	Nscreens = ScreenCount(X_Dpy);
	for (i = 0; i < (int)Nscreens; i++) {
		XMALLOC(sc, struct screen_ctx);
		x_setupscreen(sc, i);
		TAILQ_INSERT_TAIL(&Screenq, sc, entry);
	}

	Cursor_move = XCreateFontCursor(X_Dpy, XC_fleur);
	Cursor_resize = XCreateFontCursor(X_Dpy, XC_bottom_right_corner);
	/* (used to be) XCreateFontCursor(X_Dpy, XC_hand1); */
	Cursor_select = XCreateFontCursor(X_Dpy, XC_hand1);
/* 	Cursor_select = cursor_bigarrow(Curscreen); */
	Cursor_default = XCreateFontCursor(X_Dpy, XC_X_cursor);
/* 	Cursor_default = cursor_bigarrow(Curscreen); */
	Cursor_question = XCreateFontCursor(X_Dpy, XC_question_arrow);
}

int
x_setupscreen(struct screen_ctx *sc, u_int which)
{
	XColor tmp;
	XGCValues gv, gv1/* , gv2 */;
	Window *wins, w0, w1;
	u_int nwins, i = 0;
	XWindowAttributes winattr;
	XSetWindowAttributes rootattr;
	struct keybinding *kb;

	sc->display = x_screenname(which);
	sc->which = which;
	sc->rootwin = RootWindow(X_Dpy, which);
	XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
	    "black", &sc->fgcolor, &tmp);
	XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
	    "#00cc00", &sc->bgcolor, &tmp);
	XAllocNamedColor(X_Dpy,DefaultColormap(X_Dpy, which),
	    "blue", &sc->fccolor, &tmp);
	XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
	    "red", &sc->redcolor, &tmp);
	XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
	    "#00ccc8", &sc->cyancolor, &tmp);
	XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
	    "white", &sc->whitecolor, &tmp);
	XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
	    "black", &sc->blackcolor, &tmp);

	TAILQ_FOREACH(kb, &Conf.keybindingq, entry)
		xu_key_grab(sc->rootwin, kb->modmask, kb->keysym);

	/* Special -- for alt state. */
/* 	xu_key_grab(sc->rootwin, 0, XK_Alt_L); */
/* 	xu_key_grab(sc->rootwin, 0, XK_Alt_R); */

	sc->blackpixl = BlackPixel(X_Dpy, sc->which);
	sc->whitepixl = WhitePixel(X_Dpy, sc->which);
	sc->bluepixl = sc->fccolor.pixel;
	sc->redpixl = sc->redcolor.pixel;
	sc->cyanpixl = sc->cyancolor.pixel;

        sc->gray = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
            gray_bits, gray_width, gray_height,
            sc->blackpixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));

        sc->blue = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
            gray_bits, gray_width, gray_height,
            sc->bluepixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));

        sc->red = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
            gray_bits, gray_width, gray_height,
	    sc->redpixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));

	gv.foreground = sc->blackpixl^sc->whitepixl;
	gv.background = sc->whitepixl;
	gv.function = GXxor;
	gv.line_width = 1;
	gv.subwindow_mode = IncludeInferiors;
	gv.font = X_Font->fid;

	sc->gc = XCreateGC(X_Dpy, sc->rootwin,
	    GCForeground|GCBackground|GCFunction|
	    GCLineWidth|GCSubwindowMode|GCFont, &gv);

#ifdef notyet
	gv2.foreground = sc->blackpixl^sc->cyanpixl;
	gv2.background = sc->cyanpixl;
	gv2.function = GXxor;
	gv2.line_width = 1;
	gv2.subwindow_mode = IncludeInferiors;
	gv2.font = X_Font->fid;
#endif

	sc->hlgc = XCreateGC(X_Dpy, sc->rootwin,
	    GCForeground|GCBackground|GCFunction|
	    GCLineWidth|GCSubwindowMode|GCFont, &gv);

	gv1.function = GXinvert;
	gv1.subwindow_mode = IncludeInferiors;
	gv1.line_width = 1;
	gv1.font = X_Font->fid;

	sc->invgc = XCreateGC(X_Dpy, sc->rootwin,
	    GCFunction|GCSubwindowMode|GCLineWidth|GCFont, &gv1);

	font_init(sc);
	DefaultFont = font_getx(sc, DefaultFontName);

	/*
	 * XXX - this should *really* be in screen_init().  ordering
	 * problem.
	 */
	TAILQ_INIT(&sc->mruq);

	/* Initialize menu window. */
	grab_menuinit(sc);
	search_init(sc);

	/* Deal with existing clients. */
	XQueryTree(X_Dpy, sc->rootwin, &w0, &w1, &wins, &nwins);	

	for (i = 0; i < nwins; i++) {
		XGetWindowAttributes(X_Dpy, wins[i], &winattr);
		if (winattr.override_redirect ||
		    winattr.map_state != IsViewable) {
			char *name;
			XFetchName(X_Dpy, wins[i], &name);
			continue;
		}
		client_new(wins[i], sc, winattr.map_state != IsUnmapped);
	}
	XFree(wins);

	Curscreen = sc;	/* XXX */
	screen_init();
	screen_updatestackingorder();

	rootattr.event_mask = ChildMask|PropertyChangeMask|EnterWindowMask|
	    LeaveWindowMask|ColormapChangeMask|ButtonMask;

	/* Set the root cursor to a nice obnoxious arrow :-) */
/* 	rootattr.cursor = cursor_bigarrow(sc); */

	XChangeWindowAttributes(X_Dpy, sc->rootwin,
	    /* CWCursor| */CWEventMask, &rootattr);

	XSync(X_Dpy, False);

	return (0);
}

char *
x_screenname(int which)
{
	char *cp, *dstr, *sn;
	size_t snlen;

	if (which > 9)
		errx(1, "Can't handle more than 9 screens.  If you need it, "
		    "tell <marius@monkey.org>.  It's a trivial fix.");

	dstr = xstrdup(DisplayString(X_Dpy));

	if ((cp = rindex(dstr, ':')) == NULL)
		return (NULL);

	if ((cp = index(cp, '.')) != NULL)
		*cp = '\0';

	snlen = strlen(dstr) + 3; /* string, dot, number, null */
	sn = (char *)xmalloc(snlen);
	snprintf(sn, snlen, "%s.%d", dstr, which);
	free(dstr);

	return (sn);
}

int
x_errorhandler(Display *dpy, XErrorEvent *e)
{
#ifdef DEBUG
	{
		char msg[80], number[80], req[80];

		XGetErrorText(X_Dpy, e->error_code, msg, sizeof(msg));
		snprintf(number, sizeof(number), "%d", e->request_code);
		XGetErrorDatabaseText(X_Dpy, "XRequest", number,
		    "<unknown>", req, sizeof(req));

		warnx("%s(0x%x): %s", req, (u_int)e->resourceid, msg);
	}
#endif

	if (Starting && 
	    e->error_code == BadAccess &&                                       
	    e->request_code == X_GrabKey)                        
		errx(1, "root window unavailable - perhaps another "
		    "wm is running?");                                      

	return (0);
}

static void
_sigchld_cb(int which)
{
	pid_t pid;
	int status;

/* 	Collect dead children. */
        while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
            (pid < 0 && errno == EINTR))
		;
}