dwm.c
changeset 1308 978d14197052
parent 1307 2c72f7c8826f
child 1309 3affae730034
equal deleted inserted replaced
1307:2c72f7c8826f 1308:978d14197052
   143 static void configurenotify(XEvent *e);
   143 static void configurenotify(XEvent *e);
   144 static void configurerequest(XEvent *e);
   144 static void configurerequest(XEvent *e);
   145 static void destroynotify(XEvent *e);
   145 static void destroynotify(XEvent *e);
   146 static void detach(Client *c);
   146 static void detach(Client *c);
   147 static void detachstack(Client *c);
   147 static void detachstack(Client *c);
       
   148 static void die(const char *errstr, ...);
   148 static void drawbar(void);
   149 static void drawbar(void);
   149 static void drawsquare(Bool filled, Bool empty, Bool invert, ulong col[ColLast]);
   150 static void drawsquare(Bool filled, Bool empty, Bool invert, ulong col[ColLast]);
   150 static void drawtext(const char *text, ulong col[ColLast], Bool invert);
   151 static void drawtext(const char *text, ulong col[ColLast], Bool invert);
   151 static void enternotify(XEvent *e);
   152 static void enternotify(XEvent *e);
   152 static void eprint(const char *errstr, ...);
       
   153 static void expose(XEvent *e);
   153 static void expose(XEvent *e);
   154 static void focus(Client *c);
   154 static void focus(Client *c);
   155 static void focusin(XEvent *e);
   155 static void focusin(XEvent *e);
   156 static void focusstack(const Arg *arg);
   156 static void focusstack(const Arg *arg);
   157 static Client *getclient(Window w);
   157 static Client *getclient(Window w);
   341 
   341 
   342 	/* this causes an error if some other window manager is running */
   342 	/* this causes an error if some other window manager is running */
   343 	XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
   343 	XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
   344 	XSync(dpy, False);
   344 	XSync(dpy, False);
   345 	if(otherwm)
   345 	if(otherwm)
   346 		eprint("dwm: another window manager is already running\n");
   346 		die("dwm: another window manager is already running\n");
   347 	XSetErrorHandler(NULL);
   347 	XSetErrorHandler(NULL);
   348 	xerrorxlib = XSetErrorHandler(xerror);
   348 	xerrorxlib = XSetErrorHandler(xerror);
   349 	XSync(dpy, False);
   349 	XSync(dpy, False);
   350 }
   350 }
   351 
   351 
   472 	for(tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
   472 	for(tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
   473 	*tc = c->snext;
   473 	*tc = c->snext;
   474 }
   474 }
   475 
   475 
   476 void
   476 void
       
   477 die(const char *errstr, ...) {
       
   478 	va_list ap;
       
   479 
       
   480 	va_start(ap, errstr);
       
   481 	vfprintf(stderr, errstr, ap);
       
   482 	va_end(ap);
       
   483 	exit(EXIT_FAILURE);
       
   484 }
       
   485 
       
   486 void
   477 drawbar(void) {
   487 drawbar(void) {
   478 	int i, x;
   488 	int i, x;
   479 
   489 
   480 	dc.x = 0;
   490 	dc.x = 0;
   481 	for(i = 0; i < LENGTH(tags); i++) {
   491 	for(i = 0; i < LENGTH(tags); i++) {
   579 	else
   589 	else
   580 		focus(NULL);
   590 		focus(NULL);
   581 }
   591 }
   582 
   592 
   583 void
   593 void
   584 eprint(const char *errstr, ...) {
       
   585 	va_list ap;
       
   586 
       
   587 	va_start(ap, errstr);
       
   588 	vfprintf(stderr, errstr, ap);
       
   589 	va_end(ap);
       
   590 	exit(EXIT_FAILURE);
       
   591 }
       
   592 
       
   593 void
       
   594 expose(XEvent *e) {
   594 expose(XEvent *e) {
   595 	XExposeEvent *ev = &e->xexpose;
   595 	XExposeEvent *ev = &e->xexpose;
   596 
   596 
   597 	if(ev->count == 0 && (ev->window == barwin))
   597 	if(ev->count == 0 && (ev->window == barwin))
   598 		drawbar();
   598 		drawbar();
   665 getcolor(const char *colstr) {
   665 getcolor(const char *colstr) {
   666 	Colormap cmap = DefaultColormap(dpy, screen);
   666 	Colormap cmap = DefaultColormap(dpy, screen);
   667 	XColor color;
   667 	XColor color;
   668 
   668 
   669 	if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
   669 	if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
   670 		eprint("error, cannot allocate color '%s'\n", colstr);
   670 		die("error, cannot allocate color '%s'\n", colstr);
   671 	return color.pixel;
   671 	return color.pixel;
   672 }
   672 }
   673 
   673 
   674 long
   674 long
   675 getstate(Window w) {
   675 getstate(Window w) {
   791 		if(dc.font.xfont)
   791 		if(dc.font.xfont)
   792 			XFreeFont(dpy, dc.font.xfont);
   792 			XFreeFont(dpy, dc.font.xfont);
   793 		dc.font.xfont = NULL;
   793 		dc.font.xfont = NULL;
   794 		if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
   794 		if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
   795 		&& !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
   795 		&& !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
   796 			eprint("error, cannot load font: '%s'\n", fontstr);
   796 			die("error, cannot load font: '%s'\n", fontstr);
   797 		dc.font.ascent = dc.font.xfont->ascent;
   797 		dc.font.ascent = dc.font.xfont->ascent;
   798 		dc.font.descent = dc.font.xfont->descent;
   798 		dc.font.descent = dc.font.xfont->descent;
   799 	}
   799 	}
   800 	dc.font.height = dc.font.ascent + dc.font.descent;
   800 	dc.font.height = dc.font.ascent + dc.font.descent;
   801 }
   801 }
   875 	Status rettrans;
   875 	Status rettrans;
   876 	Window trans;
   876 	Window trans;
   877 	XWindowChanges wc;
   877 	XWindowChanges wc;
   878 
   878 
   879 	if(!(c = calloc(1, sizeof(Client))))
   879 	if(!(c = calloc(1, sizeof(Client))))
   880 		eprint("fatal: could not calloc() %u bytes\n", sizeof(Client));
   880 		die("fatal: could not calloc() %u bytes\n", sizeof(Client));
   881 	c->win = w;
   881 	c->win = w;
   882 
   882 
   883 	/* geometry */
   883 	/* geometry */
   884 	c->x = wa->x;
   884 	c->x = wa->x;
   885 	c->y = wa->y;
   885 	c->y = wa->y;
  1214 			FD_SET(STDIN_FILENO, &rd);
  1214 			FD_SET(STDIN_FILENO, &rd);
  1215 		FD_SET(xfd, &rd);
  1215 		FD_SET(xfd, &rd);
  1216 		if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1216 		if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1217 			if(errno == EINTR)
  1217 			if(errno == EINTR)
  1218 				continue;
  1218 				continue;
  1219 			eprint("select failed\n");
  1219 			die("select failed\n");
  1220 		}
  1220 		}
  1221 		if(FD_ISSET(STDIN_FILENO, &rd)) {
  1221 		if(FD_ISSET(STDIN_FILENO, &rd)) {
  1222 			switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1222 			switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1223 			case -1:
  1223 			case -1:
  1224 				strncpy(stext, strerror(errno), len);
  1224 				strncpy(stext, strerror(errno), len);
  1693 }
  1693 }
  1694 
  1694 
  1695 int
  1695 int
  1696 main(int argc, char *argv[]) {
  1696 main(int argc, char *argv[]) {
  1697 	if(argc == 2 && !strcmp("-v", argv[1]))
  1697 	if(argc == 2 && !strcmp("-v", argv[1]))
  1698 		eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1698 		die("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1699 	else if(argc != 1)
  1699 	else if(argc != 1)
  1700 		eprint("usage: dwm [-v]\n");
  1700 		die("usage: dwm [-v]\n");
  1701 
  1701 
  1702 	if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1702 	if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1703 		fprintf(stderr, "warning: no locale support\n");
  1703 		fprintf(stderr, "warning: no locale support\n");
  1704 
  1704 
  1705 	if(!(dpy = XOpenDisplay(0)))
  1705 	if(!(dpy = XOpenDisplay(0)))
  1706 		eprint("dwm: cannot open display\n");
  1706 		die("dwm: cannot open display\n");
  1707 
  1707 
  1708 	checkotherwm();
  1708 	checkotherwm();
  1709 	setup();
  1709 	setup();
  1710 	scan();
  1710 	scan();
  1711 	run();
  1711 	run();