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 if(c == stack)
41 draw_bar();
42 else
43 draw_client(c);
44}
45
46void
47focus(Client *c)
48{
49 Client **l;
50 for(l=&stack; *l && *l != c; l=&(*l)->snext);
51 eassert(*l == c);
52 *l = c->snext;
53 c->snext = stack;
54 stack = c;
55 XRaiseWindow(dpy, c->win);
56 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
57 XFlush(dpy);
58}
59
60void
61manage(Window w, XWindowAttributes *wa)
62{
63 Client *c, **l;
64 XSetWindowAttributes twa;
65 long msize;
66
67 c = emallocz(sizeof(Client));
68 c->win = w;
69 c->r[RFloat].x = wa->x;
70 c->r[RFloat].y = wa->y;
71 c->r[RFloat].width = wa->width;
72 c->r[RFloat].height = wa->height;
73 XSetWindowBorderWidth(dpy, c->win, 1);
74 XSelectInput(dpy, c->win, CLIENT_MASK);
75 XGetTransientForHint(dpy, c->win, &c->trans);
76 if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags)
77 c->size.flags = PSize;
78 c->fixedsize =
79 (c->size.flags & PMinSize && c->size.flags & PMaxSize
80 && c->size.min_width == c->size.max_width
81 && c->size.min_height == c->size.max_height);
82 update_name(c);
83 twa.override_redirect = 1;
84 twa.background_pixmap = ParentRelative;
85 twa.event_mask = ExposureMask;
86
87 c->title = XCreateWindow(dpy, root, c->r[RFloat].x, c->r[RFloat].y,
88 c->r[RFloat].width, barrect.height, 0,
89 DefaultDepth(dpy, screen), CopyFromParent,
90 DefaultVisual(dpy, screen),
91 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
92
93 for(l=&clients; *l; l=&(*l)->next);
94 c->next = *l; /* *l == nil */
95 *l = c;
96 c->snext = stack;
97 stack = c;
98 XMapWindow(dpy, c->win);
99 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
100 GrabModeAsync, GrabModeSync, None, None);
101 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
102 GrabModeAsync, GrabModeSync, None, None);
103 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
104 GrabModeAsync, GrabModeSync, None, None);
105 focus(c);
106}
107
108void
109resize(Client *c)
110{
111 XConfigureEvent e;
112
113 XMoveResizeWindow(dpy, c->win, c->r[RFloat].x, c->r[RFloat].y,
114 c->r[RFloat].width, c->r[RFloat].height);
115 e.type = ConfigureNotify;
116 e.event = c->win;
117 e.window = c->win;
118 e.x = c->r[RFloat].x;
119 e.y = c->r[RFloat].y;
120 e.width = c->r[RFloat].width;
121 e.height = c->r[RFloat].height;
122 e.border_width = 0;
123 e.above = None;
124 e.override_redirect = False;
125 XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
126 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
127 XSelectInput(dpy, c->win, CLIENT_MASK);
128 XFlush(dpy);
129}
130
131static int
132dummy_error_handler(Display *dpy, XErrorEvent *error)
133{
134 return 0;
135}
136
137void
138unmanage(Client *c)
139{
140 Client **l;
141
142 XGrabServer(dpy);
143 XSetErrorHandler(dummy_error_handler);
144
145 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
146 XUnmapWindow(dpy, c->win);
147 XDestroyWindow(dpy, c->title);
148
149 for(l=&clients; *l && *l != c; l=&(*l)->next);
150 eassert(*l == c);
151 *l = c->next;
152 for(l=&stack; *l && *l != c; l=&(*l)->snext);
153 eassert(*l == c);
154 *l = c->snext;
155 free(c);
156
157 XFlush(dpy);
158 XSetErrorHandler(error_handler);
159 XUngrabServer(dpy);
160 discard_events(EnterWindowMask);
161 if(stack)
162 focus(stack);
163}
164
165
166Client *
167getclient(Window w)
168{
169 Client *c;
170 for(c = clients; c; c = c->next)
171 if(c->win == w)
172 return c;
173 return NULL;
174}
175
176void
177draw_client(Client *c)
178{
179
180
181
182}