home *** CD-ROM | disk | FTP | other *** search
/ ftp.freefriends.org / ftp.freefriends.org.tar / ftp.freefriends.org / arnold / Source / 9wm-1.1.shar.gz / 9wm-1.1.shar / client.c < prev    next >
C/C++ Source or Header  |  1995-01-15  |  5KB  |  211 lines

  1. /* Copyright (c) 1994 David Hogan, see README for licence details */
  2. #include <stdio.h>
  3. #include <X11/X.h>
  4. #include <X11/Xlib.h>
  5. #include <X11/Xutil.h>
  6. #include "dat.h"
  7. #include "fns.h"
  8.  
  9. Client      *clients;
  10. Client      *current;
  11.  
  12. void
  13. setactive(c, on)
  14. Client *c;
  15. int on;
  16. {
  17.     if (on) {
  18.         XUngrabButton(dpy, AnyButton, AnyModifier, c->parent);
  19.         XSetInputFocus(dpy, c->window, RevertToPointerRoot, timestamp());
  20.         if (c->proto & Ptakefocus)
  21.             sendcmessage(c->window, wm_protocols, wm_take_focus);
  22.         cmapfocus(c);
  23.     }
  24.     else
  25.         XGrabButton(dpy, AnyButton, AnyModifier, c->parent, False,
  26.             ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  27.     draw_border(c, on);
  28. }
  29.  
  30. void
  31. draw_border(c, active)
  32. Client *c;
  33. int active;
  34. {
  35.     XSetWindowBackground(dpy, c->parent, active ? black : white);
  36.     XClearWindow(dpy, c->parent);
  37.     if (c->hold && active)
  38.         XDrawRectangle(dpy, c->parent, gc, 1, 1, c->dx+BORDER-1, c->dy+BORDER-1);
  39. }
  40.  
  41. #ifdef  DEBUG
  42. void
  43. dump_revert()
  44. {
  45.     Client *c;
  46.     int i;
  47.  
  48.     i = 0;
  49.     for (c = current; c; c = c->revert) {
  50.         fprintf(stderr, "%s(%x:%d)", c->label ? c->label : "?", c->window, c->state);
  51.         if (i++ > 100)
  52.             break;
  53.         if (c->revert)
  54.             fprintf(stderr, " -> ");
  55.     }
  56.     if (current == 0)
  57.         fprintf(stderr, "empty");
  58.     fprintf(stderr, "\n");
  59. }
  60.  
  61. void
  62. dump_clients()
  63. {
  64.     Client *c;
  65.  
  66.     for (c = clients; c; c = c->next)
  67.         fprintf(stderr, "w 0x%x parent 0x%x @ (%d, %d)\n", c->window, c->parent, c->x, c->y);
  68. }
  69. #endif
  70.  
  71. void
  72. active(c)
  73. Client *c;
  74. {
  75.     Client *cc;
  76.  
  77.     if (c == 0) {
  78.         fprintf(stderr, "9wm: active(c==0)\n");
  79.         return;
  80.     }
  81.     if (c == current)
  82.         return;
  83.     if (current)
  84.         setactive(current, 0);
  85.     setactive(c, 1);
  86.     for (cc = clients; cc; cc = cc->next)
  87.         if (cc->revert == c)
  88.             cc->revert = c->revert;
  89.     c->revert = current;
  90.     while (c->revert && !normal(c->revert))
  91.         c->revert = c->revert->revert;
  92.     current = c;
  93. #ifdef  DEBUG
  94.     if (debug)
  95.         dump_revert();
  96. #endif
  97. }
  98.  
  99. void
  100. nofocus()
  101. {
  102.     static Window w = 0;
  103.     int mask;
  104.     XSetWindowAttributes attr;
  105.     Client *c;
  106.  
  107.     if (current) {
  108.         setactive(current, 0);
  109.         for (c = current->revert; c; c = c->revert)
  110.             if (normal(c)) {
  111.                 active(c);
  112.                 return;
  113.             }
  114.         /* if no candidates to revert to, fall through */
  115.     }
  116.     current = 0;
  117.     if (w == 0) {
  118.         mask = CWOverrideRedirect;
  119.         attr.override_redirect = 1;
  120.         w = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, CopyFromParent,
  121.             InputOnly, CopyFromParent, mask, &attr);
  122.         XMapWindow(dpy, w);
  123.     }
  124.     XSetInputFocus(dpy, w, RevertToPointerRoot, timestamp());
  125.     cmapfocus(0);
  126. }
  127.  
  128. Client *
  129. getclient(w, create)
  130. Window w;
  131. int create;
  132. {
  133.     Client *c;
  134.  
  135.     if (w == 0 || w == root)
  136.         return 0;
  137.  
  138.     for (c = clients; c; c = c->next)
  139.         if (c->window == w || c->parent == w)
  140.             return c;
  141.  
  142.     if (!create)
  143.         return 0;
  144.  
  145.     c = (Client *)malloc(sizeof(Client));
  146.     memset(c, 0, sizeof(Client));
  147.     c->window = w;
  148.     c->parent = root;
  149.     c->reparenting = 0;
  150.     c->state = WithdrawnState;
  151.     c->init = 0;
  152.     c->cmap = None;
  153.     c->label = c->class = 0;
  154.     c->revert = 0;
  155.     c->is9term = 0;
  156.     c->hold = 0;
  157.     c->ncmapwins = 0;
  158.     c->cmapwins = 0;
  159.     c->wmcmaps = 0;
  160.     c->next = clients;
  161.     clients = c;
  162.     return c;
  163. }
  164.  
  165. void
  166. rmclient(c)
  167. Client *c;
  168. {
  169.     Client *cc;
  170.     int i;
  171.  
  172.     for (cc = current; cc && cc->revert; cc = cc->revert)
  173.         if (cc->revert == c)
  174.             cc->revert = cc->revert->revert;
  175.  
  176.     if (c == clients)
  177.         clients = c->next;
  178.     for (cc = clients; cc && cc->next; cc = cc->next)
  179.         if (cc->next == c)
  180.             cc->next = cc->next->next;
  181.  
  182.     if (hidden(c))
  183.         unhidec(c, 0);
  184.  
  185.     if (c->parent != root)
  186.         XDestroyWindow(dpy, c->parent);
  187.  
  188.     c->parent = c->window = None;       /* paranoia */
  189.     if (current == c) {
  190.         current = c->revert;
  191.         if (current == 0)
  192.             nofocus();
  193.         else
  194.             setactive(current, 1);
  195.     }
  196.     if (c->ncmapwins != 0) {
  197.         XFree((char *)c->cmapwins);
  198.         free((char *)c->wmcmaps);
  199.     }
  200.     if (c->iconname != 0)
  201.         XFree((char*) c->iconname);
  202.     if (c->name != 0)
  203.         XFree((char*) c->name);
  204.     if (c->instance != 0)
  205.         XFree((char*) c->instance);
  206.     if (c->class != 0)
  207.         XFree((char*) c->class);
  208.     memset(c, 0, sizeof(Client));       /* paranoia */
  209.     free(c);
  210. }
  211.