Blob


1 /* Copyright (c) 1994-1996 David Hogan, see README for licence details */
2 #include <stdio.h>
3 #include <signal.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <X11/X.h>
8 #include <X11/Xos.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include <X11/Xatom.h>
12 #ifdef SHAPE
13 #include <X11/extensions/shape.h>
14 #endif
15 #include "dat.h"
16 #include "fns.h"
17 #include "patchlevel.h"
19 char *version[] =
20 {
21 "rio version 1.0, Copyright (c) 1994-1996 David Hogan, (c) 2004 Russ Cox", 0,
22 };
24 Display *dpy;
25 ScreenInfo *screens;
26 int initting;
27 XFontStruct *font;
28 int nostalgia;
29 char **myargv;
30 char *termprog;
31 char *shell;
32 Bool shape;
33 int _border = 4;
34 int _corner = 25;
35 int _inset = 1;
36 int curtime;
37 int debug;
38 int signalled;
39 int scrolling;
40 int num_screens;
41 int solidsweep = 0;
42 int numvirtuals = 0;
43 int ffm = 0;
45 Atom exit_rio;
46 Atom restart_rio;
47 Atom wm_state;
48 Atom wm_change_state;
49 Atom wm_protocols;
50 Atom wm_delete;
51 Atom wm_take_focus;
52 Atom wm_lose_focus;
53 Atom wm_colormaps;
54 Atom _rio_running;
55 Atom _rio_hold_mode;
57 char *fontlist[] = {
58 "lucm.latin1.9",
59 "blit",
60 "*-lucidatypewriter-bold-*-14-*-75-*",
61 "*-lucidatypewriter-medium-*-12-*-75-*",
62 "9x15bold",
63 "fixed",
64 "*",
65 0,
66 };
68 void
69 usage(void)
70 {
71 fprintf(stderr, "usage: rio [-grey] [-font fname] [-s] [-term prog] [-version] [-virtuals num] [exit|restart]\n");
72 exit(1);
73 }
75 int
76 main(int argc, char *argv[])
77 {
78 int i, background, do_exit, do_restart;
79 char *fname;
80 int shape_event;
81 #ifdef SHAPE
82 int dummy;
83 #endif
85 shape_event = 0;
86 myargv = argv; /* for restart */
88 do_exit = do_restart = 0;
89 background = 0;
90 font = 0;
91 fname = 0;
92 for(i = 1; i < argc; i++)
93 if(strcmp(argv[i], "-nostalgia") == 0)
94 nostalgia++;
95 else if(strcmp(argv[i], "-grey") == 0)
96 background = 1;
97 else if(strcmp(argv[i], "-debug") == 0)
98 debug++;
99 /*
100 else if(strcmp(argv[i], "-ffm") == 0)
101 ffm++;
102 */
103 else if(strcmp(argv[i], "-font") == 0 && i+1<argc){
104 i++;
105 fname = argv[i];
107 else if(strcmp(argv[i], "-term") == 0 && i+1<argc)
108 termprog = argv[++i];
109 else if(strcmp(argv[i], "-virtuals") == 0 && i+1<argc){
110 numvirtuals = atoi(argv[++i]);
111 if(numvirtuals < 0 || numvirtuals > 12){
112 fprintf(stderr, "rio: wrong number of virtual displays, defaulting to 4\n");
113 numvirtuals = 4;
115 } else if(strcmp(argv[i], "-version") == 0){
116 fprintf(stderr, "%s", version[0]);
117 if(PATCHLEVEL > 0)
118 fprintf(stderr, "; patch level %d", PATCHLEVEL);
119 fprintf(stderr, "\n");
120 exit(0);
122 else if(strcmp(argv[i], "-s") == 0){
123 scrolling = 1;
125 else if(argv[i][0] == '-')
126 usage();
127 else
128 break;
129 for(; i < argc; i++)
130 if(strcmp(argv[i], "exit") == 0)
131 do_exit++;
132 else if(strcmp(argv[i], "restart") == 0)
133 do_restart++;
134 else
135 usage();
137 if(do_exit && do_restart)
138 usage();
140 shell = (char *)getenv("SHELL");
141 if(shell == NULL)
142 shell = DEFSHELL;
144 dpy = XOpenDisplay("");
145 if(dpy == 0)
146 fatal("can't open display");
148 initting = 1;
149 XSetErrorHandler(handler);
150 if(signal(SIGTERM, sighandler) == SIG_IGN)
151 signal(SIGTERM, SIG_IGN);
152 if(signal(SIGINT, sighandler) == SIG_IGN)
153 signal(SIGINT, SIG_IGN);
154 if(signal(SIGHUP, sighandler) == SIG_IGN)
155 signal(SIGHUP, SIG_IGN);
157 exit_rio = XInternAtom(dpy, "9WM_EXIT", False);
158 restart_rio = XInternAtom(dpy, "9WM_RESTART", False);
160 curtime = -1; /* don't care */
161 if(do_exit){
162 sendcmessage(DefaultRootWindow(dpy), exit_rio, 0L, 1, 1);
163 XSync(dpy, False);
164 exit(0);
166 if(do_restart){
167 sendcmessage(DefaultRootWindow(dpy), restart_rio, 0L, 1, 1);
168 XSync(dpy, False);
169 exit(0);
172 if(0) XSynchronize(dpy, True);
174 wm_state = XInternAtom(dpy, "WM_STATE", False);
175 wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
176 wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
177 wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
178 wm_take_focus = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
179 wm_lose_focus = XInternAtom(dpy, "_9WM_LOSE_FOCUS", False);
180 wm_colormaps = XInternAtom(dpy, "WM_COLORMAP_WINDOWS", False);
181 _rio_running = XInternAtom(dpy, "_9WM_RUNNING", False);
182 _rio_hold_mode = XInternAtom(dpy, "_9WM_HOLD_MODE", False);
184 if(fname != 0)
185 if((font = XLoadQueryFont(dpy, fname)) == 0)
186 fprintf(stderr, "rio: warning: can't load font %s\n", fname);
188 if(font == 0){
189 i = 0;
190 for(;;){
191 fname = fontlist[i++];
192 if(fname == 0){
193 fprintf(stderr, "rio: warning: can't find a font\n");
194 break;
196 font = XLoadQueryFont(dpy, fname);
197 if(font != 0)
198 break;
201 if(nostalgia){
202 _border--;
203 _inset--;
206 #ifdef SHAPE
207 shape = XShapeQueryExtension(dpy, &shape_event, &dummy);
208 #endif
210 num_screens = ScreenCount(dpy);
211 screens = (ScreenInfo *)malloc(sizeof(ScreenInfo) * num_screens);
213 for(i = 0; i < num_screens; i++)
214 initscreen(&screens[i], i, background);
216 initb2menu(numvirtuals);
218 /* set selection so that 9term knows we're running */
219 curtime = CurrentTime;
220 XSetSelectionOwner(dpy, _rio_running, screens[0].menuwin, timestamp());
222 XSync(dpy, False);
223 initting = 0;
225 nofocus();
227 for(i = 0; i < num_screens; i++)
228 scanwins(&screens[i]);
230 keysetup();
231 mainloop(shape_event);
232 return 0;
235 void
236 initscreen(ScreenInfo *s, int i, int background)
238 char *ds, *colon, *dot1;
239 unsigned long mask;
240 unsigned long gmask;
241 XGCValues gv;
242 XSetWindowAttributes attr;
243 XVisualInfo xvi;
244 XSetWindowAttributes attrs;
246 s->num = i;
247 s->root = RootWindow(dpy, i);
248 s->def_cmap = DefaultColormap(dpy, i);
249 s->min_cmaps = MinCmapsOfScreen(ScreenOfDisplay(dpy, i));
250 s->depth = DefaultDepth(dpy, i);
252 /*
253 * Figure out underlying screen format.
254 */
255 if(XMatchVisualInfo(dpy, i, 16, TrueColor, &xvi)
256 || XMatchVisualInfo(dpy, i, 16, DirectColor, &xvi)){
257 s->vis = xvi.visual;
258 s->depth = 16;
260 else
261 if(XMatchVisualInfo(dpy, i, 15, TrueColor, &xvi)
262 || XMatchVisualInfo(dpy, i, 15, DirectColor, &xvi)){
263 s->vis = xvi.visual;
264 s->depth = 15;
266 else
267 if(XMatchVisualInfo(dpy, i, 24, TrueColor, &xvi)
268 || XMatchVisualInfo(dpy, i, 24, DirectColor, &xvi)){
269 s->vis = xvi.visual;
270 s->depth = 24;
272 else
273 if(XMatchVisualInfo(dpy, i, 8, PseudoColor, &xvi)
274 || XMatchVisualInfo(dpy, i, 8, StaticColor, &xvi)){
275 s->vis = xvi.visual;
276 s->depth = 8;
278 else{
279 s->depth = DefaultDepth(dpy, i);
280 if(s->depth != 8){
281 fprintf(stderr, "can't understand depth %d screen", s->depth);
282 exit(1);
284 s->vis = DefaultVisual(dpy, i);
286 if(DefaultDepth(dpy, i) != s->depth){
287 s->def_cmap = XCreateColormap(dpy, s->root, s->vis, AllocNone);
290 ds = DisplayString(dpy);
291 colon = rindex(ds, ':');
292 if(colon && num_screens > 1){
293 strcpy(s->display, "DISPLAY=");
294 strcat(s->display, ds);
295 colon = s->display + 8 + (colon - ds); /* use version in buf */
296 dot1 = index(colon, '.'); /* first period after colon */
297 if(!dot1)
298 dot1 = colon + strlen(colon); /* if not there, append */
299 sprintf(dot1, ".%d", i);
301 else
302 s->display[0] = '\0';
304 s->black = BlackPixel(dpy, i);
305 s->white = WhitePixel(dpy, i);
306 s->activeholdborder = colorpixel(dpy, s, s->depth, 0x000099, s->white);
307 s->inactiveholdborder = colorpixel(dpy, s, s->depth, 0x005DBB, s->black);
308 s->activeborder = colorpixel(dpy, s, s->depth, 0x55AAAA, s->black);
309 s->inactiveborder = colorpixel(dpy, s, s->depth, 0x9EEEEE, s->white);
310 s->red = colorpixel(dpy, s, s->depth, 0xDD0000, s->white);
311 s->width = WidthOfScreen(ScreenOfDisplay(dpy, i));
312 s->height = HeightOfScreen(ScreenOfDisplay(dpy, i));
313 s->bkup[0] = XCreatePixmap(dpy, s->root, 2*s->width, BORDER, DefaultDepth(dpy, i));
314 s->bkup[1] = XCreatePixmap(dpy, s->root, BORDER, 2*s->height, DefaultDepth(dpy, i));
316 gv.foreground = s->black^s->white;
317 gv.background = s->white;
318 gv.function = GXxor;
319 gv.line_width = 0;
320 gv.subwindow_mode = IncludeInferiors;
321 gmask = GCForeground | GCBackground | GCFunction | GCLineWidth
322 | GCSubwindowMode;
323 if(font != 0){
324 gv.font = font->fid;
325 gmask |= GCFont;
327 s->gc = XCreateGC(dpy, s->root, gmask, &gv);
329 gv.function = GXcopy;
330 s->gccopy = XCreateGC(dpy, s->root, gmask, &gv);
332 gv.foreground = s->red;
333 s->gcred = XCreateGC(dpy, s->root, gmask, &gv);
335 gv.foreground = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
336 s->gcsweep = XCreateGC(dpy, s->root, gmask, &gv);
338 initcurs(s);
340 attr.cursor = s->arrow;
341 attr.event_mask = SubstructureRedirectMask
342 | SubstructureNotifyMask | ColormapChangeMask
343 | ButtonPressMask | ButtonReleaseMask | PropertyChangeMask
344 | KeyPressMask | EnterWindowMask;
345 mask = CWCursor|CWEventMask;
346 XChangeWindowAttributes(dpy, s->root, mask, &attr);
347 XSync(dpy, False);
349 if(background){
350 XSetWindowBackgroundPixmap(dpy, s->root, s->root_pixmap);
351 XClearWindow(dpy, s->root);
352 } else
353 system("xsetroot -solid grey30");
355 attrs.border_pixel = colorpixel(dpy, s, s->depth, 0x88CC88, s->black);
356 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
357 attrs.colormap = s->def_cmap;
359 s->menuwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 2,
360 s->depth,
361 CopyFromParent,
362 s->vis,
363 CWBackPixel | CWBorderPixel | CWColormap,
364 &attrs
365 );
368 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
369 s->gcmenubg = XCreateGC(dpy, s->menuwin, gmask, &gv);
371 gv.foreground = colorpixel(dpy, s, s->depth, 0x448844, s->black);
372 s->gcmenubgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
374 gv.foreground = s->black;
375 gv.background = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
376 s->gcmenufg = XCreateGC(dpy, s->menuwin, gmask, &gv);
378 gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
379 gv.background = colorpixel(dpy, s, s->depth, 0x448844, s->black);
380 s->gcmenufgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
382 attrs.border_pixel = s->red;
383 attrs.background_pixel = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
384 attrs.colormap = s->def_cmap;
385 s->sweepwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 4,
386 s->depth,
387 CopyFromParent,
388 s->vis,
389 CWBackPixel | CWBorderPixel | CWColormap,
390 &attrs
391 );
394 ScreenInfo*
395 getscreen(Window w)
397 int i;
399 for(i = 0; i < num_screens; i++)
400 if(screens[i].root == w)
401 return &screens[i];
403 return 0;
406 Time
407 timestamp(void)
409 XEvent ev;
411 if(curtime == CurrentTime){
412 XChangeProperty(dpy, screens[0].root, _rio_running, _rio_running, 8,
413 PropModeAppend, (unsigned char *)"", 0);
414 XMaskEvent(dpy, PropertyChangeMask, &ev);
415 curtime = ev.xproperty.time;
417 return curtime;
420 void
421 sendcmessage(Window w, Atom a, long x, int isroot, int usemask)
423 XEvent ev;
424 int status;
425 long mask;
427 memset(&ev, 0, sizeof(ev));
428 ev.xclient.type = ClientMessage;
429 ev.xclient.window = w;
430 ev.xclient.message_type = a;
431 ev.xclient.format = 32;
432 ev.xclient.data.l[0] = x;
433 ev.xclient.data.l[1] = timestamp();
434 mask = 0;
435 if(usemask){
436 mask |= KeyPressMask; /* seems to be necessary */
437 if(isroot)
438 mask |= SubstructureRedirectMask; /* magic! */
439 else
440 mask |= ExposureMask; /* not really correct but so be it */
442 status = XSendEvent(dpy, w, False, mask, &ev);
443 if(status == 0)
444 fprintf(stderr, "rio: sendcmessage failed\n");
447 void
448 sendconfig(Client *c)
450 XConfigureEvent ce;
452 ce.type = ConfigureNotify;
453 ce.event = c->window;
454 ce.window = c->window;
455 ce.x = c->x;
456 ce.y = c->y;
457 ce.width = c->dx;
458 ce.height = c->dy;
459 ce.border_width = c->border;
460 ce.above = None;
461 ce.override_redirect = 0;
462 XSendEvent(dpy, c->window, False, StructureNotifyMask, (XEvent*)&ce);
465 void
466 sighandler(void)
468 signalled = 1;
471 void
472 getevent(XEvent *e)
474 int fd;
475 fd_set rfds;
476 struct timeval t;
478 if(!signalled){
479 if(QLength(dpy) > 0){
480 XNextEvent(dpy, e);
481 return;
483 fd = ConnectionNumber(dpy);
484 FD_ZERO(&rfds);
485 FD_SET(fd, &rfds);
486 t.tv_sec = t.tv_usec = 0;
487 if(select(fd+1, &rfds, NULL, NULL, &t) == 1){
488 XNextEvent(dpy, e);
489 return;
491 XFlush(dpy);
492 FD_SET(fd, &rfds);
493 if(select(fd+1, &rfds, NULL, NULL, NULL) == 1){
494 XNextEvent(dpy, e);
495 return;
497 if(errno != EINTR || !signalled){
498 perror("rio: select failed");
499 exit(1);
502 fprintf(stderr, "rio: exiting on signal\n");
503 cleanup();
504 exit(1);
507 void
508 cleanup(void)
510 Client *c, *cc[2], *next;
511 XWindowChanges wc;
512 int i;
514 /* order of un-reparenting determines final stacking order... */
515 cc[0] = cc[1] = 0;
516 for(c = clients; c; c = next){
517 next = c->next;
518 i = normal(c);
519 c->next = cc[i];
520 cc[i] = c;
523 for(i = 0; i < 2; i++){
524 for(c = cc[i]; c; c = c->next){
525 if(!withdrawn(c)){
526 gravitate(c, 1);
527 XReparentWindow(dpy, c->window, c->screen->root,
528 c->x, c->y);
530 wc.border_width = c->border;
531 XConfigureWindow(dpy, c->window, CWBorderWidth, &wc);
535 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, timestamp());
536 for(i = 0; i < num_screens; i++)
537 cmapnofocus(&screens[i]);
538 XCloseDisplay(dpy);