main.c 5.9 KB

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