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.

296 lines
7.7 KiB

19 years ago
19 years ago
19 years ago
17 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. /* See LICENSE file for copyright and license details. */
  2. #include "dwm.h"
  3. #include <errno.h>
  4. #include <locale.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/select.h>
  10. #include <X11/cursorfont.h>
  11. #include <X11/keysym.h>
  12. #include <X11/Xatom.h>
  13. #include <X11/Xproto.h>
  14. #include <X11/Xutil.h>
  15. /* extern */
  16. char stext[256];
  17. int screen, sx, sy, sw, sh, wax, way, waw, wah;
  18. unsigned int ntags;
  19. unsigned int numlockmask = 0;
  20. Atom wmatom[WMLast], netatom[NetLast];
  21. Bool *seltags;
  22. Bool selscreen = True;
  23. Client *clients = NULL;
  24. Client *sel = NULL;
  25. Client *stack = NULL;
  26. Cursor cursor[CurLast];
  27. Display *dpy;
  28. Window root;
  29. /* static */
  30. static int (*xerrorxlib)(Display *, XErrorEvent *);
  31. static Bool otherwm, readin;
  32. static Bool running = True;
  33. static void
  34. cleanup(void) {
  35. close(STDIN_FILENO);
  36. while(stack) {
  37. unban(stack);
  38. unmanage(stack);
  39. }
  40. if(dc.font.set)
  41. XFreeFontSet(dpy, dc.font.set);
  42. else
  43. XFreeFont(dpy, dc.font.xfont);
  44. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  45. XFreePixmap(dpy, dc.drawable);
  46. XFreeGC(dpy, dc.gc);
  47. XDestroyWindow(dpy, barwin);
  48. XFreeCursor(dpy, cursor[CurNormal]);
  49. XFreeCursor(dpy, cursor[CurResize]);
  50. XFreeCursor(dpy, cursor[CurMove]);
  51. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  52. XSync(dpy, False);
  53. free(seltags);
  54. }
  55. static long
  56. getstate(Window w) {
  57. int format, status;
  58. long result = -1;
  59. unsigned char *p = NULL;
  60. unsigned long n, extra;
  61. Atom real;
  62. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  63. &real, &format, &n, &extra, (unsigned char **)&p);
  64. if(status != Success)
  65. return -1;
  66. if(n != 0)
  67. result = *p;
  68. XFree(p);
  69. return result;
  70. }
  71. static void
  72. scan(void) {
  73. unsigned int i, num;
  74. Window *wins, d1, d2;
  75. XWindowAttributes wa;
  76. wins = NULL;
  77. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  78. for(i = 0; i < num; i++) {
  79. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  80. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  81. continue;
  82. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  83. manage(wins[i], &wa);
  84. }
  85. for(i = 0; i < num; i++) { /* now the transients */
  86. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  87. continue;
  88. if(XGetTransientForHint(dpy, wins[i], &d1)
  89. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  90. manage(wins[i], &wa);
  91. }
  92. }
  93. if(wins)
  94. XFree(wins);
  95. }
  96. static void
  97. setup(void) {
  98. int i, j;
  99. unsigned int mask;
  100. Window w;
  101. XModifierKeymap *modmap;
  102. XSetWindowAttributes wa;
  103. /* init atoms */
  104. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  105. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  106. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  107. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  108. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  109. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  110. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  111. PropModeReplace, (unsigned char *) netatom, NetLast);
  112. /* init cursors */
  113. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  114. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  115. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  116. /* init modifier map */
  117. modmap = XGetModifierMapping(dpy);
  118. for (i = 0; i < 8; i++)
  119. for (j = 0; j < modmap->max_keypermod; j++) {
  120. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  121. == XKeysymToKeycode(dpy, XK_Num_Lock))
  122. numlockmask = (1 << i);
  123. }
  124. XFreeModifiermap(modmap);
  125. /* select for events */
  126. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  127. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  128. wa.cursor = cursor[CurNormal];
  129. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  130. XSelectInput(dpy, root, wa.event_mask);
  131. grabkeys();
  132. compileregs();
  133. for(ntags = 0; tags[ntags]; ntags++);
  134. seltags = emallocz(sizeof(Bool) * ntags);
  135. seltags[0] = True;
  136. /* geometry */
  137. sx = sy = 0;
  138. sw = DisplayWidth(dpy, screen);
  139. sh = DisplayHeight(dpy, screen);
  140. initstyle();
  141. initlayouts();
  142. initbar();
  143. /* multihead support */
  144. selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  145. }
  146. /*
  147. * Startup Error handler to check if another window manager
  148. * is already running.
  149. */
  150. static int
  151. xerrorstart(Display *dsply, XErrorEvent *ee) {
  152. otherwm = True;
  153. return -1;
  154. }
  155. /* extern */
  156. Bool
  157. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  158. char **list = NULL;
  159. int n;
  160. XTextProperty name;
  161. if(!text || size == 0)
  162. return False;
  163. text[0] = '\0';
  164. XGetTextProperty(dpy, w, &name, atom);
  165. if(!name.nitems)
  166. return False;
  167. if(name.encoding == XA_STRING)
  168. strncpy(text, (char *)name.value, size - 1);
  169. else {
  170. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  171. && n > 0 && *list)
  172. {
  173. strncpy(text, *list, size - 1);
  174. XFreeStringList(list);
  175. }
  176. }
  177. text[size - 1] = '\0';
  178. XFree(name.value);
  179. return True;
  180. }
  181. void
  182. quit(const char *arg) {
  183. readin = running = False;
  184. }
  185. /* There's no way to check accesses to destroyed windows, thus those cases are
  186. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  187. * default error handler, which may call exit.
  188. */
  189. int
  190. xerror(Display *dpy, XErrorEvent *ee) {
  191. if(ee->error_code == BadWindow
  192. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  193. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  194. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  195. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  196. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  197. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  198. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  199. return 0;
  200. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  201. ee->request_code, ee->error_code);
  202. return xerrorxlib(dpy, ee); /* may call exit */
  203. }
  204. int
  205. main(int argc, char *argv[]) {
  206. char *p;
  207. int r, xfd;
  208. fd_set rd;
  209. XEvent ev;
  210. if(argc == 2 && !strcmp("-v", argv[1]))
  211. eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
  212. else if(argc != 1)
  213. eprint("usage: dwm [-v]\n");
  214. setlocale(LC_CTYPE, "");
  215. if(!(dpy = XOpenDisplay(0)))
  216. eprint("dwm: cannot open display\n");
  217. xfd = ConnectionNumber(dpy);
  218. screen = DefaultScreen(dpy);
  219. root = RootWindow(dpy, screen);
  220. otherwm = False;
  221. XSetErrorHandler(xerrorstart);
  222. /* this causes an error if some other window manager is running */
  223. XSelectInput(dpy, root, SubstructureRedirectMask);
  224. XSync(dpy, False);
  225. if(otherwm)
  226. eprint("dwm: another window manager is already running\n");
  227. XSync(dpy, False);
  228. XSetErrorHandler(NULL);
  229. xerrorxlib = XSetErrorHandler(xerror);
  230. XSync(dpy, False);
  231. setup();
  232. drawbar();
  233. scan();
  234. /* main event loop, also reads status text from stdin */
  235. XSync(dpy, False);
  236. readin = True;
  237. while(running) {
  238. FD_ZERO(&rd);
  239. if(readin)
  240. FD_SET(STDIN_FILENO, &rd);
  241. FD_SET(xfd, &rd);
  242. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  243. if(errno == EINTR)
  244. continue;
  245. eprint("select failed\n");
  246. }
  247. if(FD_ISSET(STDIN_FILENO, &rd)) {
  248. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  249. case -1:
  250. strncpy(stext, strerror(errno), sizeof stext - 1);
  251. stext[sizeof stext - 1] = '\0';
  252. readin = False;
  253. break;
  254. case 0:
  255. strncpy(stext, "EOF", 4);
  256. readin = False;
  257. break;
  258. default:
  259. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  260. for(; p >= stext && *p != '\n'; --p);
  261. if(p > stext)
  262. strncpy(stext, p + 1, sizeof stext);
  263. }
  264. drawbar();
  265. }
  266. while(XPending(dpy)) {
  267. XNextEvent(dpy, &ev);
  268. if(handler[ev.type])
  269. (handler[ev.type])(&ev); /* call handler */
  270. }
  271. }
  272. cleanup();
  273. XCloseDisplay(dpy);
  274. return 0;
  275. }