all repos — dwm @ 83d23908d3438d7f1f62533a7c8d96fc1019df55

fork of suckless dynamic window manager

client.c (view raw)

  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
 15void
 16update_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}
 41
 42void
 43update_size(Client *c)
 44{
 45	XSizeHints size;
 46	long msize;
 47	if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
 48		size.flags = PSize;
 49	c->flags = size.flags;
 50	if(c->flags & PBaseSize) {
 51		c->basew = size.base_width;
 52		c->baseh = size.base_height;
 53	}
 54	else
 55		c->basew = c->baseh = 0;
 56	if(c->flags & PResizeInc) {
 57		c->incw = size.width_inc;
 58		c->inch = size.height_inc;
 59	}
 60	else
 61		c->incw = c->inch = 0;
 62	if(c->flags & PMaxSize) {
 63		c->maxw = size.max_width;
 64		c->maxh = size.max_height;
 65	}
 66	else
 67		c->maxw = c->maxh = 0;
 68	if(c->flags & PMinSize) {
 69		c->minw = size.min_width;
 70		c->minh = size.min_height;
 71	}
 72	else
 73		c->minw = c->minh = 0;
 74}
 75
 76void
 77focus(Client *c)
 78{
 79	Client **l, *old;
 80
 81	old = stack;
 82	for(l=&stack; *l && *l != c; l=&(*l)->snext);
 83	eassert(*l == c);
 84	*l = c->snext;
 85	c->snext = stack;
 86	stack = c;
 87	XRaiseWindow(dpy, c->win);
 88	XRaiseWindow(dpy, c->title);
 89	XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
 90	if(old && old != c) {
 91		XMapWindow(dpy, old->title);
 92		draw_client(old);
 93	}
 94	XUnmapWindow(dpy, c->title);
 95	draw_bar();
 96	XFlush(dpy);
 97}
 98
 99void
100manage(Window w, XWindowAttributes *wa)
101{
102	Client *c, **l;
103	XSetWindowAttributes twa;
104
105	c = emallocz(sizeof(Client));
106	c->win = w;
107	c->x = wa->x;
108	c->y = wa->y;
109	c->w = wa->width;
110	c->h = wa->height;
111	update_size(c);
112	XSetWindowBorderWidth(dpy, c->win, 1);
113	XSelectInput(dpy, c->win, CLIENT_MASK);
114	XGetTransientForHint(dpy, c->win, &c->trans);
115	twa.override_redirect = 1;
116	twa.background_pixmap = ParentRelative;
117	twa.event_mask = ExposureMask;
118
119	c->title = XCreateWindow(dpy, root, c->x, c->y, c->w, barrect.height,
120			0, DefaultDepth(dpy, screen), CopyFromParent,
121			DefaultVisual(dpy, screen),
122			CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
123	update_name(c);
124
125	for(l=&clients; *l; l=&(*l)->next);
126	c->next = *l; /* *l == nil */
127	*l = c;
128	c->snext = stack;
129	stack = c;
130	XMapWindow(dpy, c->win);
131	XMapWindow(dpy, c->title);
132	XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
133			GrabModeAsync, GrabModeSync, None, None);
134	XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
135			GrabModeAsync, GrabModeSync, None, None);
136	XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
137			GrabModeAsync, GrabModeSync, None, None);
138	resize(c);
139	focus(c);
140}
141
142void
143resize(Client *c)
144{
145	XConfigureEvent e;
146
147	XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
148	XMoveResizeWindow(dpy, c->title, c->x + c->w / 3, c->y, 2 * c->w / 3, barrect.height);
149	e.type = ConfigureNotify;
150	e.event = c->win;
151	e.window = c->win;
152	e.x = c->x;
153	e.y = c->y;
154	e.width = c->w;
155	e.height = c->h;
156	e.border_width = 0;
157	e.above = None;
158	e.override_redirect = False;
159	XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
160	XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
161	XSelectInput(dpy, c->win, CLIENT_MASK);
162	XFlush(dpy);
163}
164
165static int
166dummy_error_handler(Display *dpy, XErrorEvent *error)
167{
168	return 0;
169}
170
171void
172unmanage(Client *c)
173{
174	Client **l;
175
176	XGrabServer(dpy);
177	XSetErrorHandler(dummy_error_handler);
178
179	XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
180	XUnmapWindow(dpy, c->win);
181	XDestroyWindow(dpy, c->title);
182
183	for(l=&clients; *l && *l != c; l=&(*l)->next);
184	eassert(*l == c);
185	*l = c->next;
186	for(l=&stack; *l && *l != c; l=&(*l)->snext);
187	eassert(*l == c);
188	*l = c->snext;
189	free(c);
190
191	XFlush(dpy);
192	XSetErrorHandler(error_handler);
193	XUngrabServer(dpy);
194	discard_events(EnterWindowMask);
195	if(stack)
196		focus(stack);
197}
198
199
200Client *
201getclient(Window w)
202{
203	Client *c;
204	for(c = clients; c; c = c->next)
205		if(c->win == w)
206			return c;
207	return NULL;
208}
209
210void
211draw_client(Client *c)
212{
213	if(!c)
214		return;
215	if(c == stack)
216		draw_bar();
217
218	brush.rect.x = brush.rect.y = 0;
219	brush.rect.width = 2 * c->w / 3;
220	brush.rect.height = barrect.height;
221
222	draw(dpy, &brush, True, c->name);
223	XCopyArea(dpy, brush.drawable, c->title, brush.gc, 0, 0,
224			brush.rect.width, brush.rect.height, 0, 0);
225	XFlush(dpy);
226}