You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
6.4 KiB

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