JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made code more readable
[dwm.git] / client.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/Xatom.h>
9
10 #include "util.h"
11 #include "wm.h"
12
13 #define CLIENT_MASK             (StructureNotifyMask | PropertyChangeMask | EnterWindowMask)
14
15 void
16 update_name(Client *c)
17 {
18         XTextProperty name;
19         int n;
20         char **list = NULL;
21
22         name.nitems = 0;
23         c->name[0] = 0;
24         XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
25         if(!name.nitems)
26                 XGetWMName(dpy, c->win, &name);
27         if(!name.nitems)
28                 return;
29         if(name.encoding == XA_STRING)
30                 strncpy(c->name, (char *)name.value, sizeof(c->name));
31         else {
32                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
33                                 && n > 0 && *list)
34                 {
35                         strncpy(c->name, *list, sizeof(c->name));
36                         XFreeStringList(list);
37                 }
38         }
39         XFree(name.value);
40         if(c == stack)
41                 draw_bar();
42         else
43                 draw_client(c);
44 }
45
46 void
47 update_size(Client *c)
48 {
49         XSizeHints size;
50         long msize;
51         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
52                 size.flags = PSize;
53         c->flags = size.flags;
54         c->basew = size.base_width;
55         c->baseh = size.base_height;
56         c->incw = size.width_inc;
57         c->inch = size.height_inc;
58         c->maxw = size.max_width;
59         c->maxh = size.max_height;
60         c->minw = size.min_width;
61         c->minh = size.min_height;
62 }
63
64 void
65 focus(Client *c)
66 {
67         Client **l;
68         for(l=&stack; *l && *l != c; l=&(*l)->snext);
69         eassert(*l == c);
70         *l = c->snext;
71         c->snext = stack;
72         stack = c;
73         XRaiseWindow(dpy, c->win);
74         XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
75         XFlush(dpy);
76 }
77
78 void
79 manage(Window w, XWindowAttributes *wa)
80 {
81         Client *c, **l;
82         XSetWindowAttributes twa;
83
84         c = emallocz(sizeof(Client));
85         c->win = w;
86         c->x = wa->x;
87         c->y = wa->y;
88         c->w = wa->width;
89         c->h = wa->height;
90         update_size(c);
91         XSetWindowBorderWidth(dpy, c->win, 1);
92         XSelectInput(dpy, c->win, CLIENT_MASK);
93         XGetTransientForHint(dpy, c->win, &c->trans);
94         update_name(c);
95         twa.override_redirect = 1;
96         twa.background_pixmap = ParentRelative;
97         twa.event_mask = ExposureMask;
98
99         c->title = XCreateWindow(dpy, root, c->x, c->y, c->w, barrect.height,
100                         0, DefaultDepth(dpy, screen), CopyFromParent,
101                         DefaultVisual(dpy, screen),
102                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
103
104         for(l=&clients; *l; l=&(*l)->next);
105         c->next = *l; /* *l == nil */
106         *l = c;
107         c->snext = stack;
108         stack = c;
109         XMapWindow(dpy, c->win);
110         XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
111                         GrabModeAsync, GrabModeSync, None, None);
112         XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
113                         GrabModeAsync, GrabModeSync, None, None);
114         XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
115                         GrabModeAsync, GrabModeSync, None, None);
116         focus(c);
117 }
118
119 void
120 resize(Client *c)
121 {
122         XConfigureEvent e;
123
124         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
125         e.type = ConfigureNotify;
126         e.event = c->win;
127         e.window = c->win;
128         e.x = c->x;
129         e.y = c->y;
130         e.width = c->w;
131         e.height = c->h;
132         e.border_width = 0;
133         e.above = None;
134         e.override_redirect = False;
135         XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
136         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
137         XSelectInput(dpy, c->win, CLIENT_MASK);
138         XFlush(dpy);
139 }
140
141 static int
142 dummy_error_handler(Display *dpy, XErrorEvent *error)
143 {
144         return 0;
145 }
146
147 void
148 unmanage(Client *c)
149 {
150         Client **l;
151
152         XGrabServer(dpy);
153         XSetErrorHandler(dummy_error_handler);
154
155         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
156         XUnmapWindow(dpy, c->win);
157         XDestroyWindow(dpy, c->title);
158
159         for(l=&clients; *l && *l != c; l=&(*l)->next);
160         eassert(*l == c);
161         *l = c->next;
162         for(l=&stack; *l && *l != c; l=&(*l)->snext);
163         eassert(*l == c);
164         *l = c->snext;
165         free(c);
166
167         XFlush(dpy);
168         XSetErrorHandler(error_handler);
169         XUngrabServer(dpy);
170         discard_events(EnterWindowMask);
171         if(stack)
172                 focus(stack);
173 }
174
175
176 Client *
177 getclient(Window w)
178 {
179         Client *c;
180         for(c = clients; c; c = c->next)
181                 if(c->win == w)
182                         return c;
183         return NULL;
184 }
185
186 void
187 draw_client(Client *c)
188 {
189         
190
191
192 }