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
56 Atom wm_state_fullscreen;
62 "*-lucidatypewriter-bold-*-14-*-75-*",
63 "*-lucidatypewriter-medium-*-12-*-75-*",
73 fprintf(stderr, "usage: rio [-grey] [-font fname] [-s] [-term prog] [-version] [-virtuals num] [exit|restart]\n");
78 main(int argc, char *argv[])
80 int i, background, do_exit, do_restart;
88 myargv = argv; /* for restart */
90 do_exit = do_restart = 0;
94 for(i = 1; i < argc; i++)
95 if(strcmp(argv[i], "-nostalgia") == 0)
97 else if(strcmp(argv[i], "-grey") == 0)
99 else if(strcmp(argv[i], "-debug") == 0)
102 else if(strcmp(argv[i], "-ffm") == 0)
105 else if(strcmp(argv[i], "-font") == 0 && i+1<argc){
109 else if(strcmp(argv[i], "-term") == 0 && i+1<argc)
110 termprog = argv[++i];
111 else if(strcmp(argv[i], "-virtuals") == 0 && i+1<argc){
112 numvirtuals = atoi(argv[++i]);
113 if(numvirtuals < 0 || numvirtuals > 12){
114 fprintf(stderr, "rio: wrong number of virtual displays, defaulting to 4\n");
117 } else if(strcmp(argv[i], "-version") == 0){
118 fprintf(stderr, "%s", version[0]);
120 fprintf(stderr, "; patch level %d", PATCHLEVEL);
121 fprintf(stderr, "\n");
124 else if(strcmp(argv[i], "-s") == 0){
127 else if(argv[i][0] == '-')
132 if(strcmp(argv[i], "exit") == 0)
134 else if(strcmp(argv[i], "restart") == 0)
139 if(do_exit && do_restart)
142 shell = (char *)getenv("SHELL");
146 dpy = XOpenDisplay("");
148 fatal("can't open display");
151 XSetErrorHandler(handler);
152 if(signal(SIGTERM, sighandler) == SIG_IGN)
153 signal(SIGTERM, SIG_IGN);
154 if(signal(SIGINT, sighandler) == SIG_IGN)
155 signal(SIGINT, SIG_IGN);
156 if(signal(SIGHUP, sighandler) == SIG_IGN)
157 signal(SIGHUP, SIG_IGN);
159 exit_rio = XInternAtom(dpy, "9WM_EXIT", False);
160 restart_rio = XInternAtom(dpy, "9WM_RESTART", False);
162 curtime = -1; /* don't care */
164 sendcmessage(DefaultRootWindow(dpy), exit_rio, 0L, 1, 1);
169 sendcmessage(DefaultRootWindow(dpy), restart_rio, 0L, 1, 1);
174 if(0) XSynchronize(dpy, True);
176 wm_state = XInternAtom(dpy, "WM_STATE", False);
177 wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
178 wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
179 wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
180 wm_take_focus = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
181 wm_lose_focus = XInternAtom(dpy, "_9WM_LOSE_FOCUS", False);
182 wm_colormaps = XInternAtom(dpy, "WM_COLORMAP_WINDOWS", False);
183 _rio_running = XInternAtom(dpy, "_9WM_RUNNING", False);
184 _rio_hold_mode = XInternAtom(dpy, "_9WM_HOLD_MODE", False);
185 wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
186 wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
189 if((font = XLoadQueryFont(dpy, fname)) == 0)
190 fprintf(stderr, "rio: warning: can't load font %s\n", fname);
195 fname = fontlist[i++];
197 fprintf(stderr, "rio: warning: can't find a font\n");
200 font = XLoadQueryFont(dpy, fname);
211 shape = XShapeQueryExtension(dpy, &shape_event, &dummy);
214 num_screens = ScreenCount(dpy);
215 screens = (ScreenInfo *)malloc(sizeof(ScreenInfo) * num_screens);
217 for(i = 0; i < num_screens; i++)
218 initscreen(&screens[i], i, background);
220 initb2menu(numvirtuals);
222 /* set selection so that 9term knows we're running */
223 curtime = CurrentTime;
224 XSetSelectionOwner(dpy, _rio_running, screens[0].menuwin, timestamp());
231 for(i = 0; i < num_screens; i++)
232 scanwins(&screens[i]);
235 mainloop(shape_event);
240 initscreen(ScreenInfo *s, int i, int background)
242 char *ds, *colon, *dot1;
246 XSetWindowAttributes attr;
248 XSetWindowAttributes attrs;
251 s->root = RootWindow(dpy, i);
252 s->def_cmap = DefaultColormap(dpy, i);
253 s->min_cmaps = MinCmapsOfScreen(ScreenOfDisplay(dpy, i));
254 s->depth = DefaultDepth(dpy, i);
257 * Figure out underlying screen format.
259 if(XMatchVisualInfo(dpy, i, 16, TrueColor, &xvi)
260 || XMatchVisualInfo(dpy, i, 16, DirectColor, &xvi)){
265 if(XMatchVisualInfo(dpy, i, 15, TrueColor, &xvi)
266 || XMatchVisualInfo(dpy, i, 15, DirectColor, &xvi)){
271 if(XMatchVisualInfo(dpy, i, 24, TrueColor, &xvi)
272 || XMatchVisualInfo(dpy, i, 24, DirectColor, &xvi)){
277 if(XMatchVisualInfo(dpy, i, 8, PseudoColor, &xvi)
278 || XMatchVisualInfo(dpy, i, 8, StaticColor, &xvi)){
283 s->depth = DefaultDepth(dpy, i);
285 fprintf(stderr, "can't understand depth %d screen", s->depth);
288 s->vis = DefaultVisual(dpy, i);
290 if(DefaultDepth(dpy, i) != s->depth){
291 s->def_cmap = XCreateColormap(dpy, s->root, s->vis, AllocNone);
294 ds = DisplayString(dpy);
295 colon = rindex(ds, ':');
296 if(colon && num_screens > 1){
297 strcpy(s->display, "DISPLAY=");
298 strcat(s->display, ds);
299 colon = s->display + 8 + (colon - ds); /* use version in buf */
300 dot1 = index(colon, '.'); /* first period after colon */
302 dot1 = colon + strlen(colon); /* if not there, append */
303 sprintf(dot1, ".%d", i);
306 s->display[0] = '\0';
308 s->black = BlackPixel(dpy, i);
309 s->white = WhitePixel(dpy, i);
310 s->activeholdborder = colorpixel(dpy, s, s->depth, 0x000099, s->white);
311 s->inactiveholdborder = colorpixel(dpy, s, s->depth, 0x005DBB, s->black);
312 s->activeborder = colorpixel(dpy, s, s->depth, 0x55AAAA, s->black);
313 s->inactiveborder = colorpixel(dpy, s, s->depth, 0x9EEEEE, s->white);
314 s->red = colorpixel(dpy, s, s->depth, 0xDD0000, s->white);
315 s->width = WidthOfScreen(ScreenOfDisplay(dpy, i));
316 s->height = HeightOfScreen(ScreenOfDisplay(dpy, i));
317 s->bkup[0] = XCreatePixmap(dpy, s->root, 2*s->width, BORDER, DefaultDepth(dpy, i));
318 s->bkup[1] = XCreatePixmap(dpy, s->root, BORDER, 2*s->height, DefaultDepth(dpy, i));
320 gv.foreground = s->black^s->white;
321 gv.background = s->white;
324 gv.subwindow_mode = IncludeInferiors;
325 gmask = GCForeground | GCBackground | GCFunction | GCLineWidth
331 s->gc = XCreateGC(dpy, s->root, gmask, &gv);
333 gv.function = GXcopy;
334 s->gccopy = XCreateGC(dpy, s->root, gmask, &gv);
336 gv.foreground = s->red;
337 s->gcred = XCreateGC(dpy, s->root, gmask, &gv);
339 gv.foreground = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
340 s->gcsweep = XCreateGC(dpy, s->root, gmask, &gv);
344 attr.cursor = s->arrow;
345 attr.event_mask = SubstructureRedirectMask
346 | SubstructureNotifyMask | ColormapChangeMask
347 | ButtonPressMask | ButtonReleaseMask | PropertyChangeMask
348 | KeyPressMask | EnterWindowMask;
349 mask = CWCursor|CWEventMask;
350 XChangeWindowAttributes(dpy, s->root, mask, &attr);
354 XSetWindowBackgroundPixmap(dpy, s->root, s->root_pixmap);
355 XClearWindow(dpy, s->root);
357 system("xsetroot -solid grey30");
359 attrs.border_pixel = colorpixel(dpy, s, s->depth, 0x88CC88, s->black);
360 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
361 attrs.colormap = s->def_cmap;
363 s->menuwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 2,
367 CWBackPixel | CWBorderPixel | CWColormap,
372 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
373 s->gcmenubg = XCreateGC(dpy, s->menuwin, gmask, &gv);
375 gv.foreground = colorpixel(dpy, s, s->depth, 0x448844, s->black);
376 s->gcmenubgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
378 gv.foreground = s->black;
379 gv.background = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
380 s->gcmenufg = XCreateGC(dpy, s->menuwin, gmask, &gv);
382 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
383 gv.background = colorpixel(dpy, s, s->depth, 0x448844, s->black);
384 s->gcmenufgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
386 attrs.border_pixel = s->red;
387 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
388 attrs.colormap = s->def_cmap;
389 s->sweepwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 4,
393 CWBackPixel | CWBorderPixel | CWColormap,
403 for(i = 0; i < num_screens; i++)
404 if(screens[i].root == w)
415 if(curtime == CurrentTime){
416 XChangeProperty(dpy, screens[0].root, _rio_running, _rio_running, 8,
417 PropModeAppend, (unsigned char *)"", 0);
418 XMaskEvent(dpy, PropertyChangeMask, &ev);
419 curtime = ev.xproperty.time;
425 sendcmessage(Window w, Atom a, long x, int isroot, int usemask)
431 memset(&ev, 0, sizeof(ev));
432 ev.xclient.type = ClientMessage;
433 ev.xclient.window = w;
434 ev.xclient.message_type = a;
435 ev.xclient.format = 32;
436 ev.xclient.data.l[0] = x;
437 ev.xclient.data.l[1] = timestamp();
440 mask |= KeyPressMask; /* seems to be necessary */
442 mask |= SubstructureRedirectMask; /* magic! */
444 mask |= ExposureMask; /* not really correct but so be it */
446 status = XSendEvent(dpy, w, False, mask, &ev);
448 fprintf(stderr, "rio: sendcmessage failed\n");
452 sendconfig(Client *c)
456 ce.type = ConfigureNotify;
457 ce.event = c->window;
458 ce.window = c->window;
463 ce.border_width = c->border;
465 ce.override_redirect = 0;
466 XSendEvent(dpy, c->window, False, StructureNotifyMask, (XEvent*)&ce);
483 if(QLength(dpy) > 0){
487 fd = ConnectionNumber(dpy);
490 t.tv_sec = t.tv_usec = 0;
491 if(select(fd+1, &rfds, NULL, NULL, &t) == 1){
497 if(select(fd+1, &rfds, NULL, NULL, NULL) == 1){
501 if(errno != EINTR || !signalled){
502 perror("rio: select failed");
506 fprintf(stderr, "rio: exiting on signal\n");
514 Client *c, *cc[2], *next;
518 /* order of un-reparenting determines final stacking order... */
520 for(c = clients; c; c = next){
527 for(i = 0; i < 2; i++){
528 for(c = cc[i]; c; c = c->next){
530 XReparentWindow(dpy, c->window, c->screen->root,
533 wc.border_width = c->border;
534 XConfigureWindow(dpy, c->window, CWBorderWidth, &wc);
538 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, timestamp());
539 for(i = 0; i < num_screens; i++)
540 cmapnofocus(&screens[i]);