1 /* Copyright (c) 1994-1996 David Hogan, see README for licence details */
10 #include <X11/Xutil.h>
11 #include <X11/Xatom.h>
13 #include <X11/extensions/shape.h>
17 #include "patchlevel.h"
21 "rio version 1.0, Copyright (c) 1994-1996 David Hogan, (c) 2004 Russ Cox", 0,
57 "*-lucidatypewriter-bold-*-14-*-75-*",
58 "*-lucidatypewriter-medium-*-12-*-75-*",
68 fprintf(stderr, "usage: rio [-grey] [-version] [-font fname] [-term prog] [exit|restart]\n");
73 main(int argc, char *argv[])
75 int i, background, do_exit, do_restart;
83 myargv = argv; /* for restart */
85 do_exit = do_restart = 0;
89 for (i = 1; i < argc; i++)
90 if (strcmp(argv[i], "-nostalgia") == 0)
92 else if (strcmp(argv[i], "-grey") == 0)
94 else if (strcmp(argv[i], "-debug") == 0)
96 else if (strcmp(argv[i], "-font") == 0 && i+1<argc) {
100 else if (strcmp(argv[i], "-term") == 0 && i+1<argc)
101 termprog = argv[++i];
102 else if (strcmp(argv[i], "-version") == 0) {
103 fprintf(stderr, "%s", version[0]);
105 fprintf(stderr, "; patch level %d", PATCHLEVEL);
106 fprintf(stderr, "\n");
109 else if (argv[i][0] == '-')
113 for (; i < argc; i++)
114 if (strcmp(argv[i], "exit") == 0)
116 else if (strcmp(argv[i], "restart") == 0)
121 if (do_exit && do_restart)
124 shell = (char *)getenv("SHELL");
128 dpy = XOpenDisplay("");
130 fatal("can't open display");
133 XSetErrorHandler(handler);
134 if (signal(SIGTERM, sighandler) == SIG_IGN)
135 signal(SIGTERM, SIG_IGN);
136 if (signal(SIGINT, sighandler) == SIG_IGN)
137 signal(SIGINT, SIG_IGN);
138 if (signal(SIGHUP, sighandler) == SIG_IGN)
139 signal(SIGHUP, SIG_IGN);
141 exit_9wm = XInternAtom(dpy, "9WM_EXIT", False);
142 restart_9wm = XInternAtom(dpy, "9WM_RESTART", False);
144 curtime = -1; /* don't care */
146 sendcmessage(DefaultRootWindow(dpy), exit_9wm, 0L, 1);
151 sendcmessage(DefaultRootWindow(dpy), restart_9wm, 0L, 1);
156 if (0) XSynchronize(dpy, True);
158 wm_state = XInternAtom(dpy, "WM_STATE", False);
159 wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
160 wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
161 wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
162 wm_take_focus = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
163 wm_lose_focus = XInternAtom(dpy, "_9WM_LOSE_FOCUS", False);
164 wm_colormaps = XInternAtom(dpy, "WM_COLORMAP_WINDOWS", False);
165 _9wm_running = XInternAtom(dpy, "_9WM_RUNNING", False);
166 _9wm_hold_mode = XInternAtom(dpy, "_9WM_HOLD_MODE", False);
169 if ((font = XLoadQueryFont(dpy, fname)) == 0)
170 fprintf(stderr, "9wm: warning: can't load font %s\n", fname);
175 fname = fontlist[i++];
177 fprintf(stderr, "9wm: warning: can't find a font\n");
180 font = XLoadQueryFont(dpy, fname);
191 shape = XShapeQueryExtension(dpy, &shape_event, &dummy);
194 num_screens = ScreenCount(dpy);
195 screens = (ScreenInfo *)malloc(sizeof(ScreenInfo) * num_screens);
197 for (i = 0; i < num_screens; i++)
198 initscreen(&screens[i], i, background);
200 /* set selection so that 9term knows we're running */
201 curtime = CurrentTime;
202 XSetSelectionOwner(dpy, _9wm_running, screens[0].menuwin, timestamp());
209 for (i = 0; i < num_screens; i++)
210 scanwins(&screens[i]);
212 mainloop(shape_event);
217 initscreen(ScreenInfo *s, int i, int background)
219 char *ds, *colon, *dot1;
223 XSetWindowAttributes attr;
225 XSetWindowAttributes attrs;
228 s->root = RootWindow(dpy, i);
229 s->def_cmap = DefaultColormap(dpy, i);
230 s->min_cmaps = MinCmapsOfScreen(ScreenOfDisplay(dpy, i));
231 s->depth = DefaultDepth(dpy, i);
234 * Figure out underlying screen format.
236 if(XMatchVisualInfo(dpy, i, 16, TrueColor, &xvi)
237 || XMatchVisualInfo(dpy, i, 16, DirectColor, &xvi)){
242 if(XMatchVisualInfo(dpy, i, 15, TrueColor, &xvi)
243 || XMatchVisualInfo(dpy, i, 15, DirectColor, &xvi)){
248 if(XMatchVisualInfo(dpy, i, 24, TrueColor, &xvi)
249 || XMatchVisualInfo(dpy, i, 24, DirectColor, &xvi)){
254 if(XMatchVisualInfo(dpy, i, 8, PseudoColor, &xvi)
255 || XMatchVisualInfo(dpy, i, 8, StaticColor, &xvi)){
260 s->depth = DefaultDepth(dpy, i);
262 fprintf(stderr, "can't understand depth %d screen", s->depth);
265 s->vis = DefaultVisual(dpy, i);
267 if(DefaultDepth(dpy, i) != s->depth) {
268 s->def_cmap = XCreateColormap(dpy, s->root, s->vis, AllocNone);
271 ds = DisplayString(dpy);
272 colon = rindex(ds, ':');
273 if (colon && num_screens > 1) {
274 strcpy(s->display, "DISPLAY=");
275 strcat(s->display, ds);
276 colon = s->display + 8 + (colon - ds); /* use version in buf */
277 dot1 = index(colon, '.'); /* first period after colon */
279 dot1 = colon + strlen(colon); /* if not there, append */
280 sprintf(dot1, ".%d", i);
283 s->display[0] = '\0';
285 s->black = BlackPixel(dpy, i);
286 s->white = WhitePixel(dpy, i);
287 s->activeholdborder = colorpixel(dpy, s, s->depth, 0x000099, s->white);
288 s->inactiveholdborder = colorpixel(dpy, s, s->depth, 0x005DBB, s->black);
289 s->activeborder = colorpixel(dpy, s, s->depth, 0x55AAAA, s->black);
290 s->inactiveborder = colorpixel(dpy, s, s->depth, 0x9EEEEE, s->white);
291 s->red = colorpixel(dpy, s, s->depth, 0xDD0000, s->white);
292 s->width = WidthOfScreen(ScreenOfDisplay(dpy, i));
293 s->height = HeightOfScreen(ScreenOfDisplay(dpy, i));
294 s->bkup[0] = XCreatePixmap(dpy, s->root, 2*s->width, BORDER, DefaultDepth(dpy, i));
295 s->bkup[1] = XCreatePixmap(dpy, s->root, BORDER, 2*s->height, DefaultDepth(dpy, i));
297 gv.foreground = s->black^s->white;
298 gv.background = s->white;
301 gv.subwindow_mode = IncludeInferiors;
302 gmask = GCForeground | GCBackground | GCFunction | GCLineWidth
308 s->gc = XCreateGC(dpy, s->root, gmask, &gv);
310 gv.function = GXcopy;
311 s->gccopy = XCreateGC(dpy, s->root, gmask, &gv);
313 gv.foreground = s->red;
314 s->gcred = XCreateGC(dpy, s->root, gmask, &gv);
316 gv.foreground = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
317 s->gcsweep = XCreateGC(dpy, s->root, gmask, &gv);
321 attr.cursor = s->arrow;
322 attr.event_mask = SubstructureRedirectMask
323 | SubstructureNotifyMask | ColormapChangeMask
324 | ButtonPressMask | ButtonReleaseMask | PropertyChangeMask;
325 mask = CWCursor|CWEventMask;
326 XChangeWindowAttributes(dpy, s->root, mask, &attr);
330 XSetWindowBackgroundPixmap(dpy, s->root, s->root_pixmap);
331 XClearWindow(dpy, s->root);
333 system("xsetroot -solid grey30");
335 attrs.border_pixel = colorpixel(dpy, s, s->depth, 0x88CC88, s->black);
336 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
337 attrs.colormap = s->def_cmap;
339 s->menuwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 2,
343 CWBackPixel | CWBorderPixel | CWColormap,
348 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
349 s->gcmenubg = XCreateGC(dpy, s->menuwin, gmask, &gv);
351 gv.foreground = colorpixel(dpy, s, s->depth, 0x448844, s->black);
352 s->gcmenubgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
354 gv.foreground = s->black;
355 gv.background = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
356 s->gcmenufg = XCreateGC(dpy, s->menuwin, gmask, &gv);
358 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
359 gv.background = colorpixel(dpy, s, s->depth, 0x448844, s->black);
360 s->gcmenufgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
362 attrs.border_pixel = s->red;
363 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
364 attrs.colormap = s->def_cmap;
365 s->sweepwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 4,
369 CWBackPixel | CWBorderPixel | CWColormap,
379 for (i = 0; i < num_screens; i++)
380 if (screens[i].root == w)
391 if (curtime == CurrentTime) {
392 XChangeProperty(dpy, screens[0].root, _9wm_running, _9wm_running, 8,
393 PropModeAppend, (unsigned char *)"", 0);
394 XMaskEvent(dpy, PropertyChangeMask, &ev);
395 curtime = ev.xproperty.time;
401 sendcmessage(Window w, Atom a, long x, int isroot)
407 memset(&ev, 0, sizeof(ev));
408 ev.xclient.type = ClientMessage;
409 ev.xclient.window = w;
410 ev.xclient.message_type = a;
411 ev.xclient.format = 32;
412 ev.xclient.data.l[0] = x;
413 ev.xclient.data.l[1] = timestamp();
416 mask = SubstructureRedirectMask; /* magic! */
418 mask = ExposureMask; /* not really correct but so be it */
419 status = XSendEvent(dpy, w, False, mask, &ev);
421 fprintf(stderr, "9wm: sendcmessage failed\n");
425 sendconfig(Client *c)
429 ce.type = ConfigureNotify;
430 ce.event = c->window;
431 ce.window = c->window;
436 ce.border_width = c->border;
438 ce.override_redirect = 0;
439 XSendEvent(dpy, c->window, False, StructureNotifyMask, (XEvent*)&ce);
456 if (QLength(dpy) > 0) {
460 fd = ConnectionNumber(dpy);
463 t.tv_sec = t.tv_usec = 0;
464 if (select(fd+1, &rfds, NULL, NULL, &t) == 1) {
470 if (select(fd+1, &rfds, NULL, NULL, NULL) == 1) {
474 if (errno != EINTR || !signalled) {
475 perror("9wm: select failed");
479 fprintf(stderr, "9wm: exiting on signal\n");
487 Client *c, *cc[2], *next;
491 /* order of un-reparenting determines final stacking order... */
493 for (c = clients; c; c = next) {
500 for (i = 0; i < 2; i++) {
501 for (c = cc[i]; c; c = c->next) {
504 XReparentWindow(dpy, c->window, c->screen->root,
507 wc.border_width = c->border;
508 XConfigureWindow(dpy, c->window, CWBorderWidth, &wc);
512 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, timestamp());
513 for (i = 0; i < num_screens; i++)
514 cmapnofocus(&screens[i]);