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.

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