wm.c
changeset 32 082c75b937b5
parent 31 386649deb651
child 33 e90449e03167
equal deleted inserted replaced
31:386649deb651 32:082c75b937b5
     1 /*
     1 /*
     2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
     2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
     3  * See LICENSE file for license details.
     3  * See LICENSE file for license details.
     4  */
     4  */
     5 
     5 
     6 #include <errno.h>
       
     7 
       
     8 #include <stdarg.h>
     6 #include <stdarg.h>
     9 #include <stdio.h>
     7 #include <stdio.h>
    10 #include <stdlib.h>
     8 #include <stdlib.h>
    11 
       
    12 #include <sys/types.h>
       
    13 #include <sys/time.h>
       
    14 
     9 
    15 #include <X11/cursorfont.h>
    10 #include <X11/cursorfont.h>
    16 #include <X11/Xatom.h>
    11 #include <X11/Xatom.h>
    17 #include <X11/Xproto.h>
    12 #include <X11/Xproto.h>
    18 
    13 
    26 	[Tirc] = "irc",
    21 	[Tirc] = "irc",
    27 	[Twww] = "www",
    22 	[Twww] = "www",
    28 	[Twork] = "work",
    23 	[Twork] = "work",
    29 };
    24 };
    30 
    25 
    31 /* commands */
       
    32 static char *cmdwallpaper[] = {
       
    33 	"feh", "--bg-scale", "/home/garbeam/wallpaper/bg.jpg", NULL
       
    34 };
       
    35 
       
    36 static char *cmdstatus[] = {
       
    37 	"sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`" 
       
    38 	" `uptime | sed 's/.*://; s/,//g'`"
       
    39 	" `acpi | awk '{print $4}' | sed 's/,//'`", NULL
       
    40 };
       
    41 
       
    42 /********** CUSTOMIZE **********/
    26 /********** CUSTOMIZE **********/
    43 
    27 
    44 /* X structs */
    28 /* X structs */
    45 Display *dpy;
    29 Display *dpy;
    46 Window root, barwin;
    30 Window root, barwin;
    49 Bool running = True;
    33 Bool running = True;
    50 Bool issel;
    34 Bool issel;
    51 
    35 
    52 char stext[1024];
    36 char stext[1024];
    53 int tsel = Tdev; /* default tag */
    37 int tsel = Tdev; /* default tag */
    54 int screen, sx, sy, sw, sh, bx, by, bw, bh;
    38 int screen, sx, sy, sw, sh, th;
    55 
    39 
    56 Brush brush = {0};
    40 Brush brush = {0};
    57 Client *clients = NULL;
    41 Client *clients = NULL;
    58 Client *stack = NULL;
    42 Client *stack = NULL;
    59 
    43 
   207 	int i;
   191 	int i;
   208 	XSetWindowAttributes wa;
   192 	XSetWindowAttributes wa;
   209 	unsigned int mask;
   193 	unsigned int mask;
   210 	Window w;
   194 	Window w;
   211 	XEvent ev;
   195 	XEvent ev;
   212 	fd_set fds;
       
   213 	struct timeval t, timeout = {
       
   214 		.tv_usec = 0,
       
   215 		.tv_sec = STATUSDELAY,
       
   216 	};
       
   217 
   196 
   218 	/* command line args */
   197 	/* command line args */
   219 	for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
   198 	for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
   220 		switch (argv[i][1]) {
   199 		switch (argv[i][1]) {
   221 		case 'v':
   200 		case 'v':
   243 	XFlush(dpy);
   222 	XFlush(dpy);
   244 
   223 
   245 	if(other_wm_running)
   224 	if(other_wm_running)
   246 		error("gridwm: another window manager is already running\n");
   225 		error("gridwm: another window manager is already running\n");
   247 
   226 
   248 	spawn(dpy, cmdwallpaper);
       
   249 	sx = sy = 0;
   227 	sx = sy = 0;
   250 	sw = DisplayWidth(dpy, screen);
   228 	sw = DisplayWidth(dpy, screen);
   251 	sh = DisplayHeight(dpy, screen);
   229 	sh = DisplayHeight(dpy, screen);
   252 	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
   230 	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
   253 
   231 
   273 
   251 
   274 	/* style */
   252 	/* style */
   275 	loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
   253 	loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
   276 	loadfont(dpy, &brush.font, FONT);
   254 	loadfont(dpy, &brush.font, FONT);
   277 
   255 
   278 	wa.override_redirect = 1;
   256 	th = texth(&brush.font);
   279 	wa.background_pixmap = ParentRelative;
   257 
   280 	wa.event_mask = ExposureMask;
   258 	brush.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
   281 
       
   282 	bx = by = 0;
       
   283 	bw = sw;
       
   284 	bh = texth(&brush.font);
       
   285 	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
       
   286 			CopyFromParent, DefaultVisual(dpy, screen),
       
   287 			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
       
   288 	XDefineCursor(dpy, barwin, cursor[CurNormal]);
       
   289 	XMapRaised(dpy, barwin);
       
   290 
       
   291 	brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
       
   292 	brush.gc = XCreateGC(dpy, root, 0, 0);
   259 	brush.gc = XCreateGC(dpy, root, 0, 0);
   293 
       
   294 	pipe_spawn(stext, sizeof(stext), dpy, cmdstatus);
       
   295 	draw_bar();
       
   296 
   260 
   297 	wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
   261 	wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
   298 					| LeaveWindowMask;
   262 					| LeaveWindowMask;
   299 	wa.cursor = cursor[CurNormal];
   263 	wa.cursor = cursor[CurNormal];
   300 	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
   264 	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
   301 
   265 
   302 	arrange = grid;
   266 	arrange = grid;
   303 	scan_wins();
   267 	scan_wins();
   304 
   268 
   305 	while(running) {
   269 	while(running) {
   306 		if(XPending(dpy) > 0) {
   270 		XNextEvent(dpy, &ev);
   307 			XNextEvent(dpy, &ev);
   271 		if(handler[ev.type])
   308 			if(handler[ev.type])
   272 			(handler[ev.type])(&ev); /* call handler */
   309 				(handler[ev.type]) (&ev); /* call handler */
       
   310 			continue;
       
   311 		}
       
   312 		FD_ZERO(&fds);
       
   313 		FD_SET(ConnectionNumber(dpy), &fds);
       
   314 		t = timeout;
       
   315 		if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
       
   316 			continue;
       
   317 		else if(errno != EINTR) {
       
   318 			pipe_spawn(stext, sizeof(stext), dpy, cmdstatus);
       
   319 			draw_bar();
       
   320 		}
       
   321 	}
   273 	}
   322 
   274 
   323 	cleanup();
   275 	cleanup();
   324 	XCloseDisplay(dpy);
   276 	XCloseDisplay(dpy);
   325 
   277