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.

182 lines
3.9 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. if(c == stack)
  36. draw_bar();
  37. else
  38. draw_client(c);
  39. }
  40. void
  41. focus(Client *c)
  42. {
  43. Client **l;
  44. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  45. eassert(*l == c);
  46. *l = c->snext;
  47. c->snext = stack;
  48. stack = c;
  49. XRaiseWindow(dpy, c->win);
  50. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  51. XFlush(dpy);
  52. }
  53. void
  54. manage(Window w, XWindowAttributes *wa)
  55. {
  56. Client *c, **l;
  57. XSetWindowAttributes twa;
  58. long msize;
  59. c = emallocz(sizeof(Client));
  60. c->win = w;
  61. c->r[RFloat].x = wa->x;
  62. c->r[RFloat].y = wa->y;
  63. c->r[RFloat].width = wa->width;
  64. c->r[RFloat].height = wa->height;
  65. XSetWindowBorderWidth(dpy, c->win, 1);
  66. XSelectInput(dpy, c->win, CLIENT_MASK);
  67. XGetTransientForHint(dpy, c->win, &c->trans);
  68. if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags)
  69. c->size.flags = PSize;
  70. c->fixedsize =
  71. (c->size.flags & PMinSize && c->size.flags & PMaxSize
  72. && c->size.min_width == c->size.max_width
  73. && c->size.min_height == c->size.max_height);
  74. update_name(c);
  75. twa.override_redirect = 1;
  76. twa.background_pixmap = ParentRelative;
  77. twa.event_mask = ExposureMask;
  78. c->title = XCreateWindow(dpy, root, c->r[RFloat].x, c->r[RFloat].y,
  79. c->r[RFloat].width, barrect.height, 0,
  80. DefaultDepth(dpy, screen), CopyFromParent,
  81. DefaultVisual(dpy, screen),
  82. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  83. for(l=&clients; *l; l=&(*l)->next);
  84. c->next = *l; /* *l == nil */
  85. *l = c;
  86. c->snext = stack;
  87. stack = c;
  88. XMapWindow(dpy, c->win);
  89. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  90. GrabModeAsync, GrabModeSync, None, None);
  91. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  92. GrabModeAsync, GrabModeSync, None, None);
  93. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  94. GrabModeAsync, GrabModeSync, None, None);
  95. focus(c);
  96. }
  97. void
  98. resize(Client *c)
  99. {
  100. XConfigureEvent e;
  101. XMoveResizeWindow(dpy, c->win, c->r[RFloat].x, c->r[RFloat].y,
  102. c->r[RFloat].width, c->r[RFloat].height);
  103. e.type = ConfigureNotify;
  104. e.event = c->win;
  105. e.window = c->win;
  106. e.x = c->r[RFloat].x;
  107. e.y = c->r[RFloat].y;
  108. e.width = c->r[RFloat].width;
  109. e.height = c->r[RFloat].height;
  110. e.border_width = 0;
  111. e.above = None;
  112. e.override_redirect = False;
  113. XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
  114. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  115. XSelectInput(dpy, c->win, CLIENT_MASK);
  116. XFlush(dpy);
  117. }
  118. static int
  119. dummy_error_handler(Display *dpy, XErrorEvent *error)
  120. {
  121. return 0;
  122. }
  123. void
  124. unmanage(Client *c)
  125. {
  126. Client **l;
  127. XGrabServer(dpy);
  128. XSetErrorHandler(dummy_error_handler);
  129. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  130. XUnmapWindow(dpy, c->win);
  131. XDestroyWindow(dpy, c->title);
  132. for(l=&clients; *l && *l != c; l=&(*l)->next);
  133. eassert(*l == c);
  134. *l = c->next;
  135. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  136. eassert(*l == c);
  137. *l = c->snext;
  138. free(c);
  139. XFlush(dpy);
  140. XSetErrorHandler(error_handler);
  141. XUngrabServer(dpy);
  142. discard_events(EnterWindowMask);
  143. if(stack)
  144. focus(stack);
  145. }
  146. Client *
  147. getclient(Window w)
  148. {
  149. Client *c;
  150. for(c = clients; c; c = c->next)
  151. if(c->win == w)
  152. return c;
  153. return NULL;
  154. }
  155. void
  156. draw_client(Client *c)
  157. {
  158. }