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,
59 "*-lucidatypewriter-bold-*-14-*-75-*",
60 "*-lucidatypewriter-medium-*-12-*-75-*",
70 fprintf(stderr, "usage: rio [-grey] [-font fname] [-s] [-term prog] [-version] [-virtuals num] [exit|restart]\n");
75 main(int argc, char *argv[])
77 int i, background, do_exit, do_restart;
85 myargv = argv; /* for restart */
87 do_exit = do_restart = 0;
91 for (i = 1; i < argc; i++)
92 if (strcmp(argv[i], "-nostalgia") == 0)
94 else if (strcmp(argv[i], "-grey") == 0)
96 else if (strcmp(argv[i], "-debug") == 0)
98 else if (strcmp(argv[i], "-font") == 0 && i+1<argc) {
102 else if (strcmp(argv[i], "-term") == 0 && i+1<argc)
103 termprog = argv[++i];
104 else if (strcmp(argv[i], "-virtuals") == 0 && i+1<argc) {
105 numvirtuals = atoi(argv[++i]);
106 if(numvirtuals < 0 || numvirtuals > 12) {
107 fprintf(stderr, "rio: wrong number of virtual displays, defaulting to 4\n");
110 } else if (strcmp(argv[i], "-version") == 0) {
111 fprintf(stderr, "%s", version[0]);
113 fprintf(stderr, "; patch level %d", PATCHLEVEL);
114 fprintf(stderr, "\n");
117 else if (strcmp(argv[i], "-s") == 0) {
120 else if (argv[i][0] == '-')
124 for (; i < argc; i++)
125 if (strcmp(argv[i], "exit") == 0)
127 else if (strcmp(argv[i], "restart") == 0)
132 if (do_exit && do_restart)
135 shell = (char *)getenv("SHELL");
139 dpy = XOpenDisplay("");
141 fatal("can't open display");
144 XSetErrorHandler(handler);
145 if (signal(SIGTERM, sighandler) == SIG_IGN)
146 signal(SIGTERM, SIG_IGN);
147 if (signal(SIGINT, sighandler) == SIG_IGN)
148 signal(SIGINT, SIG_IGN);
149 if (signal(SIGHUP, sighandler) == SIG_IGN)
150 signal(SIGHUP, SIG_IGN);
152 exit_rio = XInternAtom(dpy, "9WM_EXIT", False);
153 restart_rio = XInternAtom(dpy, "9WM_RESTART", False);
155 curtime = -1; /* don't care */
157 sendcmessage(DefaultRootWindow(dpy), exit_rio, 0L, 1, 1);
162 sendcmessage(DefaultRootWindow(dpy), restart_rio, 0L, 1, 1);
167 if (0) XSynchronize(dpy, True);
169 wm_state = XInternAtom(dpy, "WM_STATE", False);
170 wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
171 wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
172 wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
173 wm_take_focus = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
174 wm_lose_focus = XInternAtom(dpy, "_9WM_LOSE_FOCUS", False);
175 wm_colormaps = XInternAtom(dpy, "WM_COLORMAP_WINDOWS", False);
176 _rio_running = XInternAtom(dpy, "_9WM_RUNNING", False);
177 _rio_hold_mode = XInternAtom(dpy, "_9WM_HOLD_MODE", False);
180 if ((font = XLoadQueryFont(dpy, fname)) == 0)
181 fprintf(stderr, "rio: warning: can't load font %s\n", fname);
186 fname = fontlist[i++];
188 fprintf(stderr, "rio: warning: can't find a font\n");
191 font = XLoadQueryFont(dpy, fname);
202 shape = XShapeQueryExtension(dpy, &shape_event, &dummy);
205 num_screens = ScreenCount(dpy);
206 screens = (ScreenInfo *)malloc(sizeof(ScreenInfo) * num_screens);
208 for (i = 0; i < num_screens; i++)
209 initscreen(&screens[i], i, background);
211 initb2menu(numvirtuals);
213 /* set selection so that 9term knows we're running */
214 curtime = CurrentTime;
215 XSetSelectionOwner(dpy, _rio_running, screens[0].menuwin, timestamp());
222 for (i = 0; i < num_screens; i++)
223 scanwins(&screens[i]);
225 mainloop(shape_event);
230 initscreen(ScreenInfo *s, int i, int background)
232 char *ds, *colon, *dot1;
236 XSetWindowAttributes attr;
238 XSetWindowAttributes attrs;
241 s->root = RootWindow(dpy, i);
242 s->def_cmap = DefaultColormap(dpy, i);
243 s->min_cmaps = MinCmapsOfScreen(ScreenOfDisplay(dpy, i));
244 s->depth = DefaultDepth(dpy, i);
247 * Figure out underlying screen format.
249 if(XMatchVisualInfo(dpy, i, 16, TrueColor, &xvi)
250 || XMatchVisualInfo(dpy, i, 16, DirectColor, &xvi)){
255 if(XMatchVisualInfo(dpy, i, 15, TrueColor, &xvi)
256 || XMatchVisualInfo(dpy, i, 15, DirectColor, &xvi)){
261 if(XMatchVisualInfo(dpy, i, 24, TrueColor, &xvi)
262 || XMatchVisualInfo(dpy, i, 24, DirectColor, &xvi)){
267 if(XMatchVisualInfo(dpy, i, 8, PseudoColor, &xvi)
268 || XMatchVisualInfo(dpy, i, 8, StaticColor, &xvi)){
273 s->depth = DefaultDepth(dpy, i);
275 fprintf(stderr, "can't understand depth %d screen", s->depth);
278 s->vis = DefaultVisual(dpy, i);
280 if(DefaultDepth(dpy, i) != s->depth) {
281 s->def_cmap = XCreateColormap(dpy, s->root, s->vis, AllocNone);
284 ds = DisplayString(dpy);
285 colon = rindex(ds, ':');
286 if (colon && num_screens > 1) {
287 strcpy(s->display, "DISPLAY=");
288 strcat(s->display, ds);
289 colon = s->display + 8 + (colon - ds); /* use version in buf */
290 dot1 = index(colon, '.'); /* first period after colon */
292 dot1 = colon + strlen(colon); /* if not there, append */
293 sprintf(dot1, ".%d", i);
296 s->display[0] = '\0';
298 s->black = BlackPixel(dpy, i);
299 s->white = WhitePixel(dpy, i);
300 s->activeholdborder = colorpixel(dpy, s, s->depth, 0x000099, s->white);
301 s->inactiveholdborder = colorpixel(dpy, s, s->depth, 0x005DBB, s->black);
302 s->activeborder = colorpixel(dpy, s, s->depth, 0x55AAAA, s->black);
303 s->inactiveborder = colorpixel(dpy, s, s->depth, 0x9EEEEE, s->white);
304 s->red = colorpixel(dpy, s, s->depth, 0xDD0000, s->white);
305 s->width = WidthOfScreen(ScreenOfDisplay(dpy, i));
306 s->height = HeightOfScreen(ScreenOfDisplay(dpy, i));
307 s->bkup[0] = XCreatePixmap(dpy, s->root, 2*s->width, BORDER, DefaultDepth(dpy, i));
308 s->bkup[1] = XCreatePixmap(dpy, s->root, BORDER, 2*s->height, DefaultDepth(dpy, i));
310 gv.foreground = s->black^s->white;
311 gv.background = s->white;
314 gv.subwindow_mode = IncludeInferiors;
315 gmask = GCForeground | GCBackground | GCFunction | GCLineWidth
321 s->gc = XCreateGC(dpy, s->root, gmask, &gv);
323 gv.function = GXcopy;
324 s->gccopy = XCreateGC(dpy, s->root, gmask, &gv);
326 gv.foreground = s->red;
327 s->gcred = XCreateGC(dpy, s->root, gmask, &gv);
329 gv.foreground = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
330 s->gcsweep = XCreateGC(dpy, s->root, gmask, &gv);
334 attr.cursor = s->arrow;
335 attr.event_mask = SubstructureRedirectMask
336 | SubstructureNotifyMask | ColormapChangeMask
337 | ButtonPressMask | ButtonReleaseMask | PropertyChangeMask;
338 mask = CWCursor|CWEventMask;
339 XChangeWindowAttributes(dpy, s->root, mask, &attr);
343 XSetWindowBackgroundPixmap(dpy, s->root, s->root_pixmap);
344 XClearWindow(dpy, s->root);
346 system("xsetroot -solid grey30");
348 attrs.border_pixel = colorpixel(dpy, s, s->depth, 0x88CC88, s->black);
349 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
350 attrs.colormap = s->def_cmap;
352 s->menuwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 2,
356 CWBackPixel | CWBorderPixel | CWColormap,
361 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
362 s->gcmenubg = XCreateGC(dpy, s->menuwin, gmask, &gv);
364 gv.foreground = colorpixel(dpy, s, s->depth, 0x448844, s->black);
365 s->gcmenubgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
367 gv.foreground = s->black;
368 gv.background = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
369 s->gcmenufg = XCreateGC(dpy, s->menuwin, gmask, &gv);
371 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
372 gv.background = colorpixel(dpy, s, s->depth, 0x448844, s->black);
373 s->gcmenufgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
375 attrs.border_pixel = s->red;
376 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
377 attrs.colormap = s->def_cmap;
378 s->sweepwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 4,
382 CWBackPixel | CWBorderPixel | CWColormap,
392 for (i = 0; i < num_screens; i++)
393 if (screens[i].root == w)
404 if (curtime == CurrentTime) {
405 XChangeProperty(dpy, screens[0].root, _rio_running, _rio_running, 8,
406 PropModeAppend, (unsigned char *)"", 0);
407 XMaskEvent(dpy, PropertyChangeMask, &ev);
408 curtime = ev.xproperty.time;
414 sendcmessage(Window w, Atom a, long x, int isroot, int usemask)
420 memset(&ev, 0, sizeof(ev));
421 ev.xclient.type = ClientMessage;
422 ev.xclient.window = w;
423 ev.xclient.message_type = a;
424 ev.xclient.format = 32;
425 ev.xclient.data.l[0] = x;
426 ev.xclient.data.l[1] = timestamp();
429 mask |= KeyPressMask; /* seems to be necessary */
431 mask |= SubstructureRedirectMask; /* magic! */
433 mask |= ExposureMask; /* not really correct but so be it */
435 status = XSendEvent(dpy, w, False, mask, &ev);
437 fprintf(stderr, "rio: sendcmessage failed\n");
441 sendconfig(Client *c)
445 ce.type = ConfigureNotify;
446 ce.event = c->window;
447 ce.window = c->window;
452 ce.border_width = c->border;
454 ce.override_redirect = 0;
455 XSendEvent(dpy, c->window, False, StructureNotifyMask, (XEvent*)&ce);
472 if (QLength(dpy) > 0) {
476 fd = ConnectionNumber(dpy);
479 t.tv_sec = t.tv_usec = 0;
480 if (select(fd+1, &rfds, NULL, NULL, &t) == 1) {
486 if (select(fd+1, &rfds, NULL, NULL, NULL) == 1) {
490 if (errno != EINTR || !signalled) {
491 perror("rio: select failed");
495 fprintf(stderr, "rio: exiting on signal\n");
503 Client *c, *cc[2], *next;
507 /* order of un-reparenting determines final stacking order... */
509 for (c = clients; c; c = next) {
516 for (i = 0; i < 2; i++) {
517 for (c = cc[i]; c; c = c->next) {
520 XReparentWindow(dpy, c->window, c->screen->root,
523 wc.border_width = c->border;
524 XConfigureWindow(dpy, c->window, CWBorderWidth, &wc);
528 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, timestamp());
529 for (i = 0; i < num_screens; i++)
530 cmapnofocus(&screens[i]);