Configuration file for DWM on MacBook Air
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.

226 lines
4.7 KiB

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 <stdlib.h>
  6. #include <string.h>
  7. #include <X11/Xatom.h>
  8. #include "util.h"
  9. #include "wm.h"
  10. #define CLIENT_MASK (StructureNotifyMask | PropertyChangeMask | EnterWindowMask)
  11. void
  12. update_name(Client *c)
  13. {
  14. XTextProperty name;
  15. int n;
  16. char **list = NULL;
  17. name.nitems = 0;
  18. c->name[0] = 0;
  19. XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
  20. if(!name.nitems)
  21. XGetWMName(dpy, c->win, &name);
  22. if(!name.nitems)
  23. return;
  24. if(name.encoding == XA_STRING)
  25. strncpy(c->name, (char *)name.value, sizeof(c->name));
  26. else {
  27. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  28. && n > 0 && *list)
  29. {
  30. strncpy(c->name, *list, sizeof(c->name));
  31. XFreeStringList(list);
  32. }
  33. }
  34. XFree(name.value);
  35. }
  36. void
  37. update_size(Client *c)
  38. {
  39. XSizeHints size;
  40. long msize;
  41. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  42. size.flags = PSize;
  43. c->flags = size.flags;
  44. if(c->flags & PBaseSize) {
  45. c->basew = size.base_width;
  46. c->baseh = size.base_height;
  47. }
  48. else
  49. c->basew = c->baseh = 0;
  50. if(c->flags & PResizeInc) {
  51. c->incw = size.width_inc;
  52. c->inch = size.height_inc;
  53. }
  54. else
  55. c->incw = c->inch = 0;
  56. if(c->flags & PMaxSize) {
  57. c->maxw = size.max_width;
  58. c->maxh = size.max_height;
  59. }
  60. else
  61. c->maxw = c->maxh = 0;
  62. if(c->flags & PMinSize) {
  63. c->minw = size.min_width;
  64. c->minh = size.min_height;
  65. }
  66. else
  67. c->minw = c->minh = 0;
  68. }
  69. void
  70. focus(Client *c)
  71. {
  72. Client **l, *old;
  73. old = stack;
  74. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  75. eassert(*l == c);
  76. *l = c->snext;
  77. c->snext = stack;
  78. stack = c;
  79. XRaiseWindow(dpy, c->win);
  80. XRaiseWindow(dpy, c->title);
  81. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  82. if(old && old != c) {
  83. XMapWindow(dpy, old->title);
  84. draw_client(old);
  85. }
  86. XUnmapWindow(dpy, c->title);
  87. draw_bar();
  88. XFlush(dpy);
  89. }
  90. void
  91. manage(Window w, XWindowAttributes *wa)
  92. {
  93. Client *c, **l;
  94. XSetWindowAttributes twa;
  95. c = emallocz(sizeof(Client));
  96. c->win = w;
  97. c->x = wa->x;
  98. c->y = wa->y;
  99. c->w = wa->width;
  100. c->h = wa->height;
  101. update_size(c);
  102. XSetWindowBorderWidth(dpy, c->win, 1);
  103. XSelectInput(dpy, c->win, CLIENT_MASK);
  104. XGetTransientForHint(dpy, c->win, &c->trans);
  105. twa.override_redirect = 1;
  106. twa.background_pixmap = ParentRelative;
  107. twa.event_mask = ExposureMask;
  108. c->title = XCreateWindow(dpy, root, c->x, c->y, c->w, barrect.height,
  109. 0, DefaultDepth(dpy, screen), CopyFromParent,
  110. DefaultVisual(dpy, screen),
  111. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  112. update_name(c);
  113. for(l=&clients; *l; l=&(*l)->next);
  114. c->next = *l; /* *l == nil */
  115. *l = c;
  116. c->snext = stack;
  117. stack = c;
  118. XMapWindow(dpy, c->win);
  119. XMapWindow(dpy, c->title);
  120. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  121. GrabModeAsync, GrabModeSync, None, None);
  122. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  123. GrabModeAsync, GrabModeSync, None, None);
  124. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  125. GrabModeAsync, GrabModeSync, None, None);
  126. resize(c);
  127. focus(c);
  128. }
  129. void
  130. resize(Client *c)
  131. {
  132. XConfigureEvent e;
  133. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  134. XMoveResizeWindow(dpy, c->title, c->x + c->w / 3, c->y, 2 * c->w / 3, barrect.height);
  135. e.type = ConfigureNotify;
  136. e.event = c->win;
  137. e.window = c->win;
  138. e.x = c->x;
  139. e.y = c->y;
  140. e.width = c->w;
  141. e.height = c->h;
  142. e.border_width = 0;
  143. e.above = None;
  144. e.override_redirect = False;
  145. XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
  146. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  147. XSelectInput(dpy, c->win, CLIENT_MASK);
  148. XFlush(dpy);
  149. }
  150. static int
  151. dummy_error_handler(Display *dpy, XErrorEvent *error)
  152. {
  153. return 0;
  154. }
  155. void
  156. unmanage(Client *c)
  157. {
  158. Client **l;
  159. XGrabServer(dpy);
  160. XSetErrorHandler(dummy_error_handler);
  161. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  162. XUnmapWindow(dpy, c->win);
  163. XDestroyWindow(dpy, c->title);
  164. for(l=&clients; *l && *l != c; l=&(*l)->next);
  165. eassert(*l == c);
  166. *l = c->next;
  167. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  168. eassert(*l == c);
  169. *l = c->snext;
  170. free(c);
  171. XFlush(dpy);
  172. XSetErrorHandler(error_handler);
  173. XUngrabServer(dpy);
  174. discard_events(EnterWindowMask);
  175. if(stack)
  176. focus(stack);
  177. }
  178. Client *
  179. getclient(Window w)
  180. {
  181. Client *c;
  182. for(c = clients; c; c = c->next)
  183. if(c->win == w)
  184. return c;
  185. return NULL;
  186. }
  187. void
  188. draw_client(Client *c)
  189. {
  190. if(!c)
  191. return;
  192. if(c == stack)
  193. draw_bar();
  194. brush.rect.x = brush.rect.y = 0;
  195. brush.rect.width = 2 * c->w / 3;
  196. brush.rect.height = barrect.height;
  197. draw(dpy, &brush, True, c->name);
  198. XCopyArea(dpy, brush.drawable, c->title, brush.gc, 0, 0,
  199. brush.rect.width, brush.rect.height, 0, 0);
  200. XFlush(dpy);
  201. }