wm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <X11/cursorfont.h>
  9. #include <X11/Xatom.h>
  10. #include <X11/Xproto.h>
  11. #include "wm.h"
  12. /********** CUSTOMIZE **********/
  13. char *tags[TLast] = {
  14. [Tscratch] = "scratch",
  15. [Tdev] = "dev",
  16. [Tirc] = "irc",
  17. [Twww] = "www",
  18. [Twork] = "work",
  19. };
  20. /********** CUSTOMIZE **********/
  21. /* X structs */
  22. Display *dpy;
  23. Window root, barwin;
  24. Atom wm_atom[WMLast], net_atom[NetLast];
  25. Cursor cursor[CurLast];
  26. Bool running = True;
  27. Bool issel;
  28. char stext[1024];
  29. int tsel = Tdev; /* default tag */
  30. int screen, sx, sy, sw, sh, th;
  31. DC dc = {0};
  32. Client *clients = NULL;
  33. Client *stack = NULL;
  34. static Bool other_wm_running;
  35. static const char version[] =
  36. "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  37. static int (*x_error_handler) (Display *, XErrorEvent *);
  38. static void
  39. usage() { error("usage: dwm [-v]\n"); }
  40. static void
  41. scan_wins()
  42. {
  43. unsigned int i, num;
  44. Window *wins;
  45. XWindowAttributes wa;
  46. Window d1, d2;
  47. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  48. for(i = 0; i < num; i++) {
  49. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  50. continue;
  51. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  52. continue;
  53. if(wa.map_state == IsViewable)
  54. manage(wins[i], &wa);
  55. }
  56. }
  57. if(wins)
  58. XFree(wins);
  59. }
  60. static int
  61. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  62. {
  63. Atom real;
  64. int format;
  65. unsigned long res, extra;
  66. int status;
  67. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  68. &res, &extra, prop);
  69. if(status != Success || *prop == 0) {
  70. return 0;
  71. }
  72. if(res == 0) {
  73. free((void *) *prop);
  74. }
  75. return res;
  76. }
  77. int
  78. win_proto(Window w)
  79. {
  80. unsigned char *protocols;
  81. long res;
  82. int protos = 0;
  83. int i;
  84. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
  85. if(res <= 0) {
  86. return protos;
  87. }
  88. for(i = 0; i < res; i++) {
  89. if(protocols[i] == wm_atom[WMDelete])
  90. protos |= WM_PROTOCOL_DELWIN;
  91. }
  92. free((char *) protocols);
  93. return protos;
  94. }
  95. void
  96. send_message(Window w, Atom a, long value)
  97. {
  98. XEvent e;
  99. e.type = ClientMessage;
  100. e.xclient.window = w;
  101. e.xclient.message_type = a;
  102. e.xclient.format = 32;
  103. e.xclient.data.l[0] = value;
  104. e.xclient.data.l[1] = CurrentTime;
  105. XSendEvent(dpy, w, False, NoEventMask, &e);
  106. XFlush(dpy);
  107. }
  108. /*
  109. * There's no way to check accesses to destroyed windows, thus
  110. * those cases are ignored (especially on UnmapNotify's).
  111. * Other types of errors call Xlib's default error handler, which
  112. * calls exit().
  113. */
  114. int
  115. error_handler(Display *dpy, XErrorEvent *error)
  116. {
  117. if(error->error_code == BadWindow
  118. || (error->request_code == X_SetInputFocus
  119. && error->error_code == BadMatch)
  120. || (error->request_code == X_PolyText8
  121. && error->error_code == BadDrawable)
  122. || (error->request_code == X_PolyFillRectangle
  123. && error->error_code == BadDrawable)
  124. || (error->request_code == X_PolySegment
  125. && error->error_code == BadDrawable)
  126. || (error->request_code == X_ConfigureWindow
  127. && error->error_code == BadMatch)
  128. || (error->request_code == X_GrabKey
  129. && error->error_code == BadAccess))
  130. return 0;
  131. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  132. error->request_code, error->error_code);
  133. return x_error_handler(dpy, error); /* may call exit() */
  134. }
  135. /*
  136. * Startup Error handler to check if another window manager
  137. * is already running.
  138. */
  139. static int
  140. startup_error_handler(Display *dpy, XErrorEvent *error)
  141. {
  142. other_wm_running = True;
  143. return -1;
  144. }
  145. static void
  146. cleanup()
  147. {
  148. while(clients)
  149. unmanage(clients);
  150. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  151. }
  152. void
  153. quit(void *aux)
  154. {
  155. running = False;
  156. }
  157. int
  158. main(int argc, char *argv[])
  159. {
  160. int i;
  161. XSetWindowAttributes wa;
  162. unsigned int mask;
  163. Window w;
  164. XEvent ev;
  165. /* command line args */
  166. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  167. switch (argv[i][1]) {
  168. case 'v':
  169. fprintf(stdout, "%s", version);
  170. exit(0);
  171. break;
  172. default:
  173. usage();
  174. break;
  175. }
  176. }
  177. dpy = XOpenDisplay(0);
  178. if(!dpy)
  179. error("dwm: cannot connect X server\n");
  180. screen = DefaultScreen(dpy);
  181. root = RootWindow(dpy, screen);
  182. /* check if another WM is already running */
  183. other_wm_running = False;
  184. XSetErrorHandler(startup_error_handler);
  185. /* this causes an error if some other WM is running */
  186. XSelectInput(dpy, root, SubstructureRedirectMask);
  187. XFlush(dpy);
  188. if(other_wm_running)
  189. error("dwm: another window manager is already running\n");
  190. sx = sy = 0;
  191. sw = DisplayWidth(dpy, screen);
  192. sh = DisplayHeight(dpy, screen);
  193. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  194. XSetErrorHandler(0);
  195. x_error_handler = XSetErrorHandler(error_handler);
  196. /* init atoms */
  197. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  198. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  199. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  200. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  201. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  202. PropModeReplace, (unsigned char *) net_atom, NetLast);
  203. /* init cursors */
  204. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  205. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  206. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  207. update_keys();
  208. /* style */
  209. initcolors(BGCOLOR, FGCOLOR, BORDERCOLOR);
  210. initfont(&dc.font, FONT);
  211. th = texth(&dc.font);
  212. dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
  213. dc.gc = XCreateGC(dpy, root, 0, 0);
  214. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  215. | LeaveWindowMask;
  216. wa.cursor = cursor[CurNormal];
  217. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  218. scan_wins();
  219. while(running) {
  220. XNextEvent(dpy, &ev);
  221. if(handler[ev.type])
  222. (handler[ev.type])(&ev); /* call handler */
  223. }
  224. cleanup();
  225. XCloseDisplay(dpy);
  226. return 0;
  227. }