dwm-meillo

view client.c @ 26:e8f627998d6f

simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
author Anselm R. Garbe <garbeam@wmii.de>
date Wed, 12 Jul 2006 15:17:22 +0200
parents 95ffdfd0a819
children f96fb3fd8203
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/Xatom.h>
10 #include "util.h"
11 #include "wm.h"
13 static void
14 resize_title(Client *c)
15 {
16 c->tw = textw(&brush.font, c->name) + bh;
17 if(c->tw > c->w)
18 c->tw = c->w + 2;
19 c->tx = c->x + c->w - c->tw + 2;
20 c->ty = c->y;
21 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
22 }
24 void
25 update_name(Client *c)
26 {
27 XTextProperty name;
28 int n;
29 char **list = NULL;
31 name.nitems = 0;
32 c->name[0] = 0;
33 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
34 if(!name.nitems)
35 XGetWMName(dpy, c->win, &name);
36 if(!name.nitems)
37 return;
38 if(name.encoding == XA_STRING)
39 strncpy(c->name, (char *)name.value, sizeof(c->name));
40 else {
41 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
42 && n > 0 && *list)
43 {
44 strncpy(c->name, *list, sizeof(c->name));
45 XFreeStringList(list);
46 }
47 }
48 XFree(name.value);
49 resize_title(c);
50 }
52 void
53 update_size(Client *c)
54 {
55 XSizeHints size;
56 long msize;
57 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
58 size.flags = PSize;
59 c->flags = size.flags;
60 if(c->flags & PBaseSize) {
61 c->basew = size.base_width;
62 c->baseh = size.base_height;
63 }
64 else
65 c->basew = c->baseh = 0;
66 if(c->flags & PResizeInc) {
67 c->incw = size.width_inc;
68 c->inch = size.height_inc;
69 }
70 else
71 c->incw = c->inch = 0;
72 if(c->flags & PMaxSize) {
73 c->maxw = size.max_width;
74 c->maxh = size.max_height;
75 }
76 else
77 c->maxw = c->maxh = 0;
78 if(c->flags & PMinSize) {
79 c->minw = size.min_width;
80 c->minh = size.min_height;
81 }
82 else
83 c->minw = c->minh = 0;
84 }
86 void
87 raise(Client *c)
88 {
89 XRaiseWindow(dpy, c->win);
90 XRaiseWindow(dpy, c->title);
91 }
93 void
94 lower(Client *c)
95 {
96 XLowerWindow(dpy, c->title);
97 XLowerWindow(dpy, c->win);
98 }
100 void
101 focus(Client *c)
102 {
103 Client **l, *old;
105 old = stack;
106 for(l = &stack; *l && *l != c; l = &(*l)->snext);
107 eassert(*l == c);
108 *l = c->snext;
109 c->snext = stack;
110 stack = c;
111 if(old && old != c) {
112 XMapWindow(dpy, old->title);
113 draw_client(old);
114 }
115 XUnmapWindow(dpy, c->title);
116 draw_client(old);
117 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
118 XFlush(dpy);
119 }
121 void
122 manage(Window w, XWindowAttributes *wa)
123 {
124 Client *c, **l;
125 XSetWindowAttributes twa;
127 c = emallocz(sizeof(Client));
128 c->win = w;
129 c->tx = c->x = wa->x;
130 c->ty = c->y = wa->y;
131 if(c->y < bh)
132 c->ty = c->y += bh;
133 c->tw = c->w = wa->width;
134 c->h = wa->height;
135 c->th = bh;
136 update_size(c);
137 XSetWindowBorderWidth(dpy, c->win, 1);
138 XSetWindowBorder(dpy, c->win, brush.border);
139 XSelectInput(dpy, c->win,
140 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
141 XGetTransientForHint(dpy, c->win, &c->trans);
142 twa.override_redirect = 1;
143 twa.background_pixmap = ParentRelative;
144 twa.event_mask = ExposureMask;
146 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
147 0, DefaultDepth(dpy, screen), CopyFromParent,
148 DefaultVisual(dpy, screen),
149 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
150 update_name(c);
152 for(l=&clients; *l; l=&(*l)->next);
153 c->next = *l; /* *l == nil */
154 *l = c;
155 c->snext = stack;
156 stack = c;
157 XMapRaised(dpy, c->win);
158 XMapRaised(dpy, c->title);
159 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
160 GrabModeAsync, GrabModeSync, None, None);
161 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
162 GrabModeAsync, GrabModeSync, None, None);
163 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
164 GrabModeAsync, GrabModeSync, None, None);
165 resize(c);
166 focus(c);
167 }
169 void
170 resize(Client *c)
171 {
172 XConfigureEvent e;
174 resize_title(c);
175 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
176 e.type = ConfigureNotify;
177 e.event = c->win;
178 e.window = c->win;
179 e.x = c->x;
180 e.y = c->y;
181 e.width = c->w;
182 e.height = c->h;
183 e.border_width = 0;
184 e.above = None;
185 e.override_redirect = False;
186 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
187 XFlush(dpy);
188 }
190 static int
191 dummy_error_handler(Display *dpy, XErrorEvent *error)
192 {
193 return 0;
194 }
196 void
197 unmanage(Client *c)
198 {
199 Client **l;
201 XGrabServer(dpy);
202 XSetErrorHandler(dummy_error_handler);
204 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
205 XDestroyWindow(dpy, c->title);
207 for(l=&clients; *l && *l != c; l=&(*l)->next);
208 eassert(*l == c);
209 *l = c->next;
210 for(l=&stack; *l && *l != c; l=&(*l)->snext);
211 eassert(*l == c);
212 *l = c->snext;
213 free(c);
215 XFlush(dpy);
216 XSetErrorHandler(error_handler);
217 XUngrabServer(dpy);
218 if(stack)
219 focus(stack);
220 }
222 Client *
223 gettitle(Window w)
224 {
225 Client *c;
226 for(c = clients; c; c = c->next)
227 if(c->title == w)
228 return c;
229 return NULL;
230 }
232 Client *
233 getclient(Window w)
234 {
235 Client *c;
236 for(c = clients; c; c = c->next)
237 if(c->win == w)
238 return c;
239 return NULL;
240 }
242 void
243 draw_client(Client *c)
244 {
245 if(c == stack) {
246 draw_bar();
247 return;
248 }
250 brush.x = brush.y = 0;
251 brush.w = c->tw;
252 brush.h = c->th;
254 draw(dpy, &brush, True, c->name);
255 XCopyArea(dpy, brush.drawable, c->title, brush.gc,
256 0, 0, c->tw, c->th, 0, 0);
257 XFlush(dpy);
258 }