Configuration of dwm for Mac Computers
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.

229 lines
4.8 KiB

18 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->tx = c->x = wa->x;
  98. c->ty = c->y = wa->y;
  99. c->tw = c->w = wa->width;
  100. c->h = wa->height;
  101. c->th = barrect.height;
  102. update_size(c);
  103. XSetWindowBorderWidth(dpy, c->win, 1);
  104. XSetWindowBorder(dpy, c->win, brush.border);
  105. XSelectInput(dpy, c->win, CLIENT_MASK);
  106. XGetTransientForHint(dpy, c->win, &c->trans);
  107. twa.override_redirect = 1;
  108. twa.background_pixmap = ParentRelative;
  109. twa.event_mask = SubstructureNotifyMask | ExposureMask;
  110. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  111. 0, DefaultDepth(dpy, screen), CopyFromParent,
  112. DefaultVisual(dpy, screen),
  113. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  114. update_name(c);
  115. for(l=&clients; *l; l=&(*l)->next);
  116. c->next = *l; /* *l == nil */
  117. *l = c;
  118. c->snext = stack;
  119. stack = c;
  120. XMapWindow(dpy, c->win);
  121. XMapWindow(dpy, c->title);
  122. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  123. GrabModeAsync, GrabModeSync, None, None);
  124. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  125. GrabModeAsync, GrabModeSync, None, None);
  126. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  127. GrabModeAsync, GrabModeSync, None, None);
  128. resize(c);
  129. focus(c);
  130. }
  131. void
  132. resize(Client *c)
  133. {
  134. XConfigureEvent e;
  135. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  136. e.type = ConfigureNotify;
  137. e.event = c->win;
  138. e.window = c->win;
  139. e.x = c->x;
  140. e.y = c->y;
  141. e.width = c->w;
  142. e.height = c->h;
  143. e.border_width = 0;
  144. e.above = None;
  145. e.override_redirect = False;
  146. XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
  147. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  148. XSelectInput(dpy, c->win, CLIENT_MASK);
  149. XFlush(dpy);
  150. }
  151. static int
  152. dummy_error_handler(Display *dpy, XErrorEvent *error)
  153. {
  154. return 0;
  155. }
  156. void
  157. unmanage(Client *c)
  158. {
  159. Client **l;
  160. XGrabServer(dpy);
  161. XSetErrorHandler(dummy_error_handler);
  162. XUngrabButton(dpy, AnyButton, AnyModifier, 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 == stack)
  191. draw_bar();
  192. c->tw = textwidth(&brush.font, c->name) + labelheight(&brush.font);
  193. c->tx = c->x + c->w - c->tw + 2;
  194. c->ty = c->y;
  195. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  196. brush.rect.x = brush.rect.y = 0;
  197. brush.rect.width = c->tw;
  198. brush.rect.height = c->th;
  199. draw(dpy, &brush, True, c->name);
  200. XCopyArea(dpy, brush.drawable, c->title, brush.gc,
  201. 0, 0, c->tw, c->th, 0, 0);
  202. XFlush(dpy);
  203. }