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.

238 lines
4.9 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. discard_events(EnterWindowMask);
  89. XFlush(dpy);
  90. }
  91. void
  92. manage(Window w, XWindowAttributes *wa)
  93. {
  94. Client *c, **l;
  95. XSetWindowAttributes twa;
  96. c = emallocz(sizeof(Client));
  97. c->win = w;
  98. c->tx = c->x = wa->x;
  99. c->ty = c->y = wa->y;
  100. c->tw = c->w = wa->width;
  101. c->h = wa->height;
  102. c->th = barrect.height;
  103. update_size(c);
  104. XSetWindowBorderWidth(dpy, c->win, 1);
  105. XSetWindowBorder(dpy, c->win, brush.border);
  106. XSelectInput(dpy, c->win, CLIENT_MASK);
  107. XGetTransientForHint(dpy, c->win, &c->trans);
  108. twa.override_redirect = 1;
  109. twa.background_pixmap = ParentRelative;
  110. twa.event_mask = ExposureMask;
  111. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  112. 0, DefaultDepth(dpy, screen), CopyFromParent,
  113. DefaultVisual(dpy, screen),
  114. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  115. update_name(c);
  116. for(l=&clients; *l; l=&(*l)->next);
  117. c->next = *l; /* *l == nil */
  118. *l = c;
  119. c->snext = stack;
  120. stack = c;
  121. XMapWindow(dpy, c->win);
  122. XMapWindow(dpy, c->title);
  123. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  124. GrabModeAsync, GrabModeSync, None, None);
  125. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  126. GrabModeAsync, GrabModeSync, None, None);
  127. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  128. GrabModeAsync, GrabModeSync, None, None);
  129. resize(c);
  130. focus(c);
  131. }
  132. void
  133. resize(Client *c)
  134. {
  135. XConfigureEvent e;
  136. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  137. e.type = ConfigureNotify;
  138. e.event = c->win;
  139. e.window = c->win;
  140. e.x = c->x;
  141. e.y = c->y;
  142. e.width = c->w;
  143. e.height = c->h;
  144. e.border_width = 0;
  145. e.above = None;
  146. e.override_redirect = False;
  147. XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
  148. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  149. XSelectInput(dpy, c->win, CLIENT_MASK);
  150. XFlush(dpy);
  151. }
  152. static int
  153. dummy_error_handler(Display *dpy, XErrorEvent *error)
  154. {
  155. return 0;
  156. }
  157. void
  158. unmanage(Client *c)
  159. {
  160. Client **l;
  161. XGrabServer(dpy);
  162. XSetErrorHandler(dummy_error_handler);
  163. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  164. XDestroyWindow(dpy, c->title);
  165. for(l=&clients; *l && *l != c; l=&(*l)->next);
  166. eassert(*l == c);
  167. *l = c->next;
  168. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  169. eassert(*l == c);
  170. *l = c->snext;
  171. free(c);
  172. XFlush(dpy);
  173. XSetErrorHandler(error_handler);
  174. XUngrabServer(dpy);
  175. if(stack)
  176. focus(stack);
  177. }
  178. Client *
  179. gettitle(Window w)
  180. {
  181. Client *c;
  182. for(c = clients; c; c = c->next)
  183. if(c->title == w)
  184. return c;
  185. return NULL;
  186. }
  187. Client *
  188. getclient(Window w)
  189. {
  190. Client *c;
  191. for(c = clients; c; c = c->next)
  192. if(c->win == w)
  193. return c;
  194. return NULL;
  195. }
  196. void
  197. draw_client(Client *c)
  198. {
  199. if(c == stack)
  200. draw_bar();
  201. c->tw = textwidth(&brush.font, c->name) + labelheight(&brush.font);
  202. c->tx = c->x + c->w - c->tw + 2;
  203. c->ty = c->y;
  204. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  205. brush.rect.x = brush.rect.y = 0;
  206. brush.rect.width = c->tw;
  207. brush.rect.height = c->th;
  208. draw(dpy, &brush, True, c->name);
  209. XCopyArea(dpy, brush.drawable, c->title, brush.gc,
  210. 0, 0, c->tw, c->th, 0, 0);
  211. XFlush(dpy);
  212. }