Mercurial > gbwm
annotate gbwm.c @ 12:1f03f3ca1a96
remember last focused window per workspace
author | Atarwn Gard <a@qwa.su> |
---|---|
date | Tue, 14 Oct 2025 18:09:23 +0500 |
parents | 90d8c905af50 |
children | 7d783901ff7d |
rev | line source |
---|---|
8 | 1 #define _POSIX_C_SOURCE 200809L |
1 | 2 /* gbwm - grid-based tiling window manager */ |
0 | 3 #include <X11/Xlib.h> |
4 #include <X11/Xatom.h> | |
5 #include <X11/Xft/Xft.h> | |
6 #include <X11/Xutil.h> | |
7 #include <X11/keysym.h> | |
2
3ad7c3ab949e
improve config and set default root cursor
Atarwn Gard <a@qwa.su>
parents:
1
diff
changeset
|
8 #include <X11/cursorfont.h> |
8 | 9 #include <X11/extensions/XTest.h> |
0 | 10 #include <stdio.h> |
11 #include <stdlib.h> | |
7 | 12 #include <stdarg.h> |
0 | 13 #include <string.h> |
14 #include <unistd.h> | |
15 #include <signal.h> | |
16 #include <sys/wait.h> | |
8 | 17 #include <time.h> |
0 | 18 |
19 typedef struct Client Client; | |
20 struct Client { | |
7 | 21 Window win; |
22 int x, y, w, h; | |
23 int saved_x, saved_y, saved_w, saved_h; // Saved position before fullscreen | |
24 int isfullscreen; | |
25 int workspace; | |
26 Client *next; | |
0 | 27 }; |
28 | |
29 typedef union { | |
7 | 30 int i; |
31 unsigned int ui; | |
32 float f; | |
33 const void *v; | |
0 | 34 } Arg; |
35 | |
36 typedef struct { | |
7 | 37 unsigned int mod; |
38 KeySym keysym; | |
39 void (*func)(const Arg *); | |
40 const Arg arg; | |
0 | 41 } Key; |
42 | |
43 static Display *dpy; | |
44 static Window root; | |
1 | 45 static Client *workspaces[9] = {NULL}; // 9 workspaces |
12
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
46 static Client *last_focused[9] = {NULL}; |
1 | 47 static int current_ws = 0; |
0 | 48 static Client *focused = NULL; |
49 static int sw, sh; | |
50 static int overlay_mode = 0; | |
51 static char overlay_input[3] = {0}; | |
52 static Window overlay_win = 0; | |
53 static GC gc; | |
54 static XftDraw *xftdraw = NULL; | |
55 static XftFont *font = NULL; | |
56 static XftColor xft_col_bg, xft_col_fg, xft_col_sel; | |
57 static unsigned long border_normal, border_focused; | |
58 | |
59 // ICCCM atoms | |
60 static Atom wm_protocols, wm_delete_window, wm_state, wm_take_focus; | |
61 | |
62 // Forward decls | |
63 static void arrange(void); | |
64 static void resize(Client *c, int x, int y, int w, int h); | |
8 | 65 static void focus(Client *c, int warp); |
0 | 66 static void spawn(const Arg *arg); |
67 static void killclient(const Arg *arg); | |
68 static void toggle_fullscreen(const Arg *arg); | |
69 static void enter_overlay(const Arg *arg); | |
70 static void process_overlay_input(void); | |
71 static void draw_overlay(void); | |
72 static void hide_overlay(void); | |
73 static void quit(const Arg *arg); | |
74 static void cycle_focus(const Arg *arg); | |
75 static void grabkeys(void); | |
76 static void setfullscreen(Client *c, int fullscreen); | |
77 static int sendevent(Client *c, Atom proto); | |
78 static void updateborder(Client *c); | |
79 static void find_next_free_cell(int *out_r, int *out_c); | |
1 | 80 static void switchws(const Arg *arg); |
81 static void movewin_to_ws(const Arg *arg); | |
8 | 82 static void die(const char *fmt, ...); |
0 | 83 |
3 | 84 #include "config.h" |
0 | 85 |
86 // Event handlers | |
87 static void buttonpress(XEvent *e) { | |
7 | 88 for (Client *c = workspaces[current_ws]; c; c = c->next) |
89 if (c->win == e->xbutton.subwindow) { | |
8 | 90 focus(c, 1); |
7 | 91 break; |
92 } | |
0 | 93 } |
94 | |
95 static void clientmessage(XEvent *e) { | |
7 | 96 XClientMessageEvent *cme = &e->xclient; |
97 Client *c; | |
98 for (c = workspaces[current_ws]; c; c = c->next) | |
99 if (c->win == cme->window) | |
100 break; | |
101 if (!c) | |
102 return; | |
0 | 103 |
7 | 104 if (cme->message_type == wm_state && cme->data.l[1] == (long)XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False)) { |
105 setfullscreen(c, cme->data.l[0] == 1 || (cme->data.l[0] == 2 && !c->isfullscreen)); | |
106 } | |
0 | 107 } |
108 | |
109 static void maprequest(XEvent *e) { | |
7 | 110 XWindowAttributes wa; |
111 if (!XGetWindowAttributes(dpy, e->xmaprequest.window, &wa)) return; | |
112 if (wa.override_redirect) return; | |
0 | 113 |
7 | 114 Client *c = calloc(1, sizeof(Client)); |
115 c->win = e->xmaprequest.window; | |
116 c->workspace = current_ws; | |
117 c->next = workspaces[current_ws]; | |
118 workspaces[current_ws] = c; | |
0 | 119 |
7 | 120 // ICCCM setup |
121 XSetWindowBorderWidth(dpy, c->win, border_width); | |
122 XSelectInput(dpy, c->win, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask); | |
0 | 123 |
7 | 124 // Set WM_STATE |
125 long data[] = { NormalState, None }; | |
126 XChangeProperty(dpy, c->win, wm_state, wm_state, 32, PropModeReplace, (unsigned char *)data, 2); | |
0 | 127 |
7 | 128 XMapWindow(dpy, c->win); |
8 | 129 focus(c, 1); |
7 | 130 arrange(); |
0 | 131 } |
132 | |
1 | 133 static void removeclient(Window win) { |
7 | 134 Client *c, **prev; |
135 for (prev = &workspaces[current_ws]; (c = *prev); prev = &c->next) { | |
136 if (c->win == win) { | |
137 *prev = c->next; | |
138 if (focused == c) { | |
139 focused = workspaces[current_ws]; | |
140 if (focused) | |
8 | 141 focus(focused, 1); |
7 | 142 } |
143 free(c); | |
144 arrange(); | |
145 return; | |
146 } | |
147 } | |
0 | 148 } |
149 | |
1 | 150 static void unmapnotify(XEvent *e) { |
7 | 151 removeclient(e->xunmap.window); |
1 | 152 } |
153 | |
0 | 154 static void destroynotify(XEvent *e) { |
7 | 155 removeclient(e->xdestroywindow.window); |
0 | 156 } |
157 | |
158 static void enternotify(XEvent *e) { | |
7 | 159 if (e->xcrossing.mode != NotifyNormal || e->xcrossing.detail == NotifyInferior) |
160 return; | |
161 for (Client *c = workspaces[current_ws]; c; c = c->next) | |
162 if (c->win == e->xcrossing.window) { | |
8 | 163 focus(c, 0); |
7 | 164 break; |
165 } | |
0 | 166 } |
167 | |
168 static void expose(XEvent *e) { | |
7 | 169 if (e->xexpose.window == overlay_win && overlay_mode) { |
170 draw_overlay(); | |
171 } | |
0 | 172 } |
173 | |
174 static void keypress(XEvent *e) { | |
7 | 175 if (overlay_mode) { |
176 KeySym k = XLookupKeysym(&e->xkey, 0); | |
177 if (k == XK_Escape) { | |
178 hide_overlay(); | |
179 return; | |
180 } | |
11
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
181 if (k == XK_BackSpace) { |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
182 if (overlay_input[1] != 0) { |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
183 overlay_input[1] = 0; |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
184 } else if (overlay_input[0] != 0) { |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
185 overlay_input[0] = 0; |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
186 } |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
187 draw_overlay(); |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
188 return; |
90d8c905af50
support Backspace to correct input in grid selection
Atarwn Gard <a@qwa.su>
parents:
9
diff
changeset
|
189 } |
0 | 190 |
7 | 191 char ch = 0; |
192 if (k >= '0' && k <= '9') ch = (char)k; | |
193 else if (k >= 'a' && k <= 'z') ch = (char)k; | |
194 else if (k >= 'A' && k <= 'Z') ch = (char)(k - 'A' + 'a'); | |
195 else return; | |
0 | 196 |
7 | 197 int found = 0; |
198 for (int r = 0; r < GRID_ROWS && !found; r++) | |
199 for (int c = 0; c < GRID_COLS && !found; c++) | |
200 if (grid_chars[r][c] == ch) found = 1; | |
201 if (!found) return; | |
0 | 202 |
7 | 203 if (overlay_input[0] == 0) { |
204 overlay_input[0] = ch; | |
205 draw_overlay(); | |
206 } else if (overlay_input[1] == 0) { | |
207 overlay_input[1] = ch; | |
208 draw_overlay(); | |
8 | 209 struct timespec ts = { |
210 .tv_sec = 0, | |
211 .tv_nsec = 150000 * 1000 | |
212 }; | |
213 nanosleep(&ts, NULL); | |
7 | 214 process_overlay_input(); |
215 hide_overlay(); | |
216 } | |
217 return; | |
218 } | |
0 | 219 |
7 | 220 KeySym keysym = XLookupKeysym(&e->xkey, 0); |
221 unsigned int state = e->xkey.state & ~(LockMask | Mod2Mask); | |
0 | 222 |
7 | 223 for (unsigned int i = 0; i < sizeof(keys) / sizeof(Key); i++) { |
224 if (keysym == keys[i].keysym && state == keys[i].mod && keys[i].func) { | |
225 keys[i].func(&keys[i].arg); | |
226 return; | |
227 } | |
228 } | |
0 | 229 } |
230 | |
231 // Core logic | |
232 static void resize(Client *c, int x, int y, int w, int h) { | |
7 | 233 c->x = x; c->y = y; c->w = w; c->h = h; |
9 | 234 XMoveResizeWindow(dpy, c->win, x, y, w - 2 * border_width, h - 2 * border_width); |
0 | 235 } |
236 | |
237 static void updateborder(Client *c) { | |
7 | 238 XSetWindowBorder(dpy, c->win, c == focused ? border_focused : border_normal); |
0 | 239 } |
240 | |
8 | 241 static void focus(Client *c, int warp) { |
7 | 242 if (!c) return; |
0 | 243 |
7 | 244 Client *old = focused; |
245 focused = c; | |
0 | 246 |
12
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
247 if (old && old != c) { |
7 | 248 updateborder(old); |
12
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
249 last_focused[old->workspace] = old; |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
250 } |
0 | 251 |
7 | 252 updateborder(c); |
253 XRaiseWindow(dpy, c->win); | |
254 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); | |
255 sendevent(c, wm_take_focus); | |
8 | 256 |
257 if (warp && !c->isfullscreen) { | |
258 int cursor_x = c->x + c->w - 16; | |
259 int cursor_y = c->y + c->h - 16; | |
260 if (cursor_x < 0) cursor_x = 0; | |
261 if (cursor_y < 0) cursor_y = 0; | |
262 if (cursor_x >= sw) cursor_x = sw - 1; | |
263 if (cursor_y >= sh) cursor_y = sh - 1; | |
264 | |
265 XWarpPointer(dpy, None, root, 0, 0, 0, 0, cursor_x, cursor_y); | |
266 } | |
0 | 267 } |
268 | |
269 static int is_cell_free(int r, int c, int cell_w, int cell_h) { | |
7 | 270 int cell_x = padding + c * (cell_w + padding); |
271 int cell_y = padding + r * (cell_h + padding); | |
0 | 272 |
7 | 273 // Check if any window overlaps with this cell |
274 for (Client *cl = workspaces[current_ws]; cl; cl = cl->next) { | |
275 if (cl->isfullscreen) continue; | |
0 | 276 |
7 | 277 // Check for overlap |
278 int cl_right = cl->x + cl->w; | |
279 int cl_bottom = cl->y + cl->h; | |
280 int cell_right = cell_x + cell_w; | |
281 int cell_bottom = cell_y + cell_h; | |
0 | 282 |
7 | 283 // If rectangles overlap |
284 if (!(cl_right <= cell_x || cl->x >= cell_right || | |
285 cl_bottom <= cell_y || cl->y >= cell_bottom)) { | |
286 return 0; // Cell is occupied | |
287 } | |
288 } | |
0 | 289 |
7 | 290 return 1; // Cell is free |
0 | 291 } |
292 | |
293 static void find_next_free_cell(int *out_r, int *out_c) { | |
7 | 294 int cell_w = (sw - padding * (GRID_COLS + 1)) / GRID_COLS; |
295 int cell_h = (sh - padding * (GRID_ROWS + 1)) / GRID_ROWS; | |
0 | 296 |
7 | 297 // First pass: look for any completely free cell |
298 for (int r = 0; r < GRID_ROWS; r++) { | |
299 for (int c = 0; c < GRID_COLS; c++) { | |
300 if (is_cell_free(r, c, cell_w, cell_h)) { | |
301 *out_r = r; | |
302 *out_c = c; | |
303 return; | |
304 } | |
305 } | |
306 } | |
0 | 307 |
7 | 308 // Second pass: no free space found, check top-left for 1x1 windows |
309 int cell_x = padding; | |
310 int cell_y = padding; | |
0 | 311 |
7 | 312 for (Client *cl = workspaces[current_ws]; cl; cl = cl->next) { |
313 if (cl->isfullscreen) continue; | |
314 if (cl->x == cell_x && cl->y == cell_y && | |
315 cl->w == cell_w && cl->h == cell_h) { | |
316 // Found a 1x1 window at top-left, find next free cell | |
317 for (int r = 0; r < GRID_ROWS; r++) { | |
318 for (int c = 0; c < GRID_COLS; c++) { | |
319 int check_x = padding + c * (cell_w + padding); | |
320 int check_y = padding + r * (cell_h + padding); | |
0 | 321 |
7 | 322 int found_1x1 = 0; |
323 for (Client *check = workspaces[current_ws]; check; check = check->next) { | |
324 if (check->isfullscreen) continue; | |
325 if (check->x == check_x && check->y == check_y && | |
326 check->w == cell_w && check->h == cell_h) { | |
327 found_1x1 = 1; | |
328 break; | |
329 } | |
330 } | |
0 | 331 |
7 | 332 if (!found_1x1) { |
333 *out_r = r; | |
334 *out_c = c; | |
335 return; | |
336 } | |
337 } | |
338 } | |
339 break; | |
340 } | |
341 } | |
0 | 342 |
7 | 343 // Fallback to top-left |
344 *out_r = 0; | |
345 *out_c = 0; | |
0 | 346 } |
347 | |
348 static void arrange(void) { | |
7 | 349 if (!workspaces[current_ws]) return; |
0 | 350 |
7 | 351 if (!focused) focused = workspaces[current_ws]; |
0 | 352 |
7 | 353 if (focused->isfullscreen) { |
354 return; | |
355 } | |
0 | 356 |
7 | 357 // Default window location - find next free cell |
358 int cell_w = (sw - padding * (GRID_COLS + 1)) / GRID_COLS; | |
359 int cell_h = (sh - padding * (GRID_ROWS + 1)) / GRID_ROWS; | |
0 | 360 |
7 | 361 if (focused->w == 0 || focused->h == 0) { |
362 int r, c; | |
363 find_next_free_cell(&r, &c); | |
364 int x = padding + c * (cell_w + padding); | |
365 int y = padding + r * (cell_h + padding); | |
366 resize(focused, x, y, cell_w, cell_h); | |
367 } | |
0 | 368 |
7 | 369 // Update all borders |
370 for (Client *c = workspaces[current_ws]; c; c = c->next) | |
371 updateborder(c); | |
0 | 372 } |
373 | |
374 static void draw_overlay(void) { | |
7 | 375 if (!overlay_win) return; |
0 | 376 |
7 | 377 XClearWindow(dpy, overlay_win); |
0 | 378 |
7 | 379 int cell_w = (sw - padding * (GRID_COLS + 1)) / GRID_COLS; |
380 int cell_h = (sh - padding * (GRID_ROWS + 1)) / GRID_ROWS; | |
0 | 381 |
7 | 382 int r1 = -1, c1 = -1, r2 = -1, c2 = -1; |
383 if (overlay_input[0]) { | |
384 for (int r = 0; r < GRID_ROWS; r++) | |
385 for (int c = 0; c < GRID_COLS; c++) | |
386 if (grid_chars[r][c] == overlay_input[0]) { r1 = r; c1 = c; } | |
387 } | |
388 if (overlay_input[1]) { | |
389 for (int r = 0; r < GRID_ROWS; r++) | |
390 for (int c = 0; c < GRID_COLS; c++) | |
391 if (grid_chars[r][c] == overlay_input[1]) { r2 = r; c2 = c; } | |
392 } | |
0 | 393 |
7 | 394 for (int r = 0; r < GRID_ROWS; r++) { |
395 for (int c = 0; c < GRID_COLS; c++) { | |
396 int x = padding + c * (cell_w + padding); | |
397 int y = padding + r * (cell_h + padding); | |
0 | 398 |
7 | 399 int is_selected = 0; |
400 if (r1 >= 0 && c1 >= 0) { | |
401 if (r2 >= 0 && c2 >= 0) { | |
402 int min_r = r1 < r2 ? r1 : r2; | |
403 int max_r = r1 > r2 ? r1 : r2; | |
404 int min_c = c1 < c2 ? c1 : c2; | |
405 int max_c = c1 > c2 ? c1 : c2; | |
406 if (r >= min_r && r <= max_r && c >= min_c && c <= max_c) | |
407 is_selected = 1; | |
408 } else if (r == r1 && c == c1) { | |
409 is_selected = 1; | |
410 } | |
411 } | |
0 | 412 |
7 | 413 if (is_selected) { |
414 XSetForeground(dpy, gc, xft_col_sel.pixel); | |
415 XFillRectangle(dpy, overlay_win, gc, x, y, cell_w, cell_h); | |
416 } | |
0 | 417 |
7 | 418 XSetForeground(dpy, gc, xft_col_fg.pixel); |
419 XDrawRectangle(dpy, overlay_win, gc, x, y, cell_w, cell_h); | |
0 | 420 |
7 | 421 if (font && xftdraw) { |
422 char txt[2] = {grid_chars[r][c], 0}; | |
423 XGlyphInfo extents; | |
424 XftTextExtentsUtf8(dpy, font, (FcChar8*)txt, strlen(txt), &extents); | |
0 | 425 |
7 | 426 int tx = x + (cell_w - extents.width) / 2; |
427 int ty = y + (cell_h - extents.height) / 2 + extents.y; | |
0 | 428 |
7 | 429 XftDrawStringUtf8(xftdraw, &xft_col_fg, font, tx, ty, |
430 (FcChar8*)txt, strlen(txt)); | |
431 } | |
432 } | |
433 } | |
0 | 434 |
7 | 435 if (overlay_input[0] || overlay_input[1]) { |
436 char status[64]; | |
437 snprintf(status, sizeof(status), "Input: %c%c", | |
438 overlay_input[0] ? overlay_input[0] : ' ', | |
439 overlay_input[1] ? overlay_input[1] : ' '); | |
0 | 440 |
7 | 441 if (font && xftdraw) { |
442 XftDrawStringUtf8(xftdraw, &xft_col_fg, font, 20, sh - 20, | |
443 (FcChar8*)status, strlen(status)); | |
444 } | |
445 } | |
0 | 446 |
7 | 447 XFlush(dpy); |
0 | 448 } |
449 | |
450 static void enter_overlay(const Arg *arg) { | |
7 | 451 if (!focused) return; |
0 | 452 |
7 | 453 overlay_mode = 1; |
454 memset(overlay_input, 0, sizeof(overlay_input)); | |
0 | 455 |
7 | 456 if (!overlay_win) { |
457 XSetWindowAttributes wa = { | |
458 .override_redirect = True, | |
459 .background_pixel = xft_col_bg.pixel, | |
460 .event_mask = ExposureMask | KeyPressMask | |
461 }; | |
462 overlay_win = XCreateWindow(dpy, root, 0, 0, sw, sh, 0, | |
463 CopyFromParent, InputOutput, CopyFromParent, | |
464 CWOverrideRedirect | CWBackPixel | CWEventMask, &wa); | |
0 | 465 |
7 | 466 gc = XCreateGC(dpy, overlay_win, 0, NULL); |
0 | 467 |
7 | 468 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy)); |
469 Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy)); | |
470 xftdraw = XftDrawCreate(dpy, overlay_win, visual, cmap); | |
0 | 471 |
7 | 472 unsigned long opacity = (unsigned long)(0.85 * 0xffffffff); |
473 Atom atom = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False); | |
474 XChangeProperty(dpy, overlay_win, atom, XA_CARDINAL, 32, | |
475 PropModeReplace, (unsigned char *)&opacity, 1); | |
476 } | |
0 | 477 |
7 | 478 XMapRaised(dpy, overlay_win); |
479 XSetInputFocus(dpy, overlay_win, RevertToPointerRoot, CurrentTime); | |
480 draw_overlay(); | |
0 | 481 } |
482 | |
483 static void hide_overlay(void) { | |
7 | 484 overlay_mode = 0; |
485 memset(overlay_input, 0, sizeof(overlay_input)); | |
486 if (overlay_win) { | |
487 XUnmapWindow(dpy, overlay_win); | |
488 } | |
489 if (focused) { | |
490 XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); | |
491 } | |
0 | 492 } |
493 | |
494 static void process_overlay_input(void) { | |
7 | 495 if (!focused || overlay_input[0] == 0 || overlay_input[1] == 0) return; |
0 | 496 |
7 | 497 int r1 = -1, c1 = -1, r2 = -1, c2 = -1; |
498 for (int r = 0; r < GRID_ROWS; r++) { | |
499 for (int c = 0; c < GRID_COLS; c++) { | |
500 if (grid_chars[r][c] == overlay_input[0]) { r1 = r; c1 = c; } | |
501 if (grid_chars[r][c] == overlay_input[1]) { r2 = r; c2 = c; } | |
502 } | |
503 } | |
504 if (r1 == -1 || r2 == -1) return; | |
0 | 505 |
7 | 506 if (r1 > r2) { int t = r1; r1 = r2; r2 = t; } |
507 if (c1 > c2) { int t = c1; c1 = c2; c2 = t; } | |
0 | 508 |
7 | 509 int cols_span = c2 - c1 + 1; |
510 int rows_span = r2 - r1 + 1; | |
0 | 511 |
7 | 512 int cell_w = (sw - padding * (GRID_COLS + 1)) / GRID_COLS; |
513 int cell_h = (sh - padding * (GRID_ROWS + 1)) / GRID_ROWS; | |
0 | 514 |
7 | 515 int x = padding + c1 * (cell_w + padding); |
516 int y = padding + r1 * (cell_h + padding); | |
517 int w = cols_span * cell_w + (cols_span - 1) * padding; | |
518 int h = rows_span * cell_h + (rows_span - 1) * padding; | |
0 | 519 |
7 | 520 resize(focused, x, y, w, h); |
8 | 521 if (focused) focus(focused, 1); |
0 | 522 } |
523 | |
1 | 524 // Workspace functions |
525 static void switchws(const Arg *arg) { | |
7 | 526 int ws = arg->i; |
527 if (ws < 0 || ws >= 9 || ws == current_ws) return; | |
528 | |
12
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
529 if (focused) { |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
530 last_focused[current_ws] = focused; |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
531 } |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
532 |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
533 |
7 | 534 current_ws = ws; |
535 | |
536 // Hide all windows from all workspaces | |
537 for (int i = 0; i < 9; i++) { | |
538 for (Client *c = workspaces[i]; c; c = c->next) { | |
539 XUnmapWindow(dpy, c->win); | |
540 } | |
541 } | |
542 | |
543 // Show current workspace windows | |
544 for (Client *c = workspaces[current_ws]; c; c = c->next) { | |
545 XMapWindow(dpy, c->win); | |
546 } | |
547 | |
12
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
548 // Restore the last focus on this WS, otherwise the first one |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
549 focused = last_focused[current_ws]; |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
550 if (!focused) focused = workspaces[current_ws]; |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
551 |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
552 if (focused) { |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
553 focus(focused, 1); |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
554 } else { |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
555 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
556 } |
1f03f3ca1a96
remember last focused window per workspace
Atarwn Gard <a@qwa.su>
parents:
11
diff
changeset
|
557 |
7 | 558 arrange(); |
1 | 559 } |
560 | |
561 static void movewin_to_ws(const Arg *arg) { | |
7 | 562 int ws = arg->i; |
563 if (!focused || ws < 0 || ws >= 9 || ws == current_ws) return; | |
564 | |
565 Client *moving = focused; | |
566 | |
567 // Remove from current workspace | |
568 Client **prev; | |
569 for (prev = &workspaces[current_ws]; *prev; prev = &(*prev)->next) { | |
570 if (*prev == moving) { | |
571 *prev = moving->next; | |
572 break; | |
573 } | |
574 } | |
575 | |
576 // Add to target workspace | |
577 moving->workspace = ws; | |
578 moving->next = workspaces[ws]; | |
579 workspaces[ws] = moving; | |
580 moving->isfullscreen = 0; // Reset fullscreen state | |
581 | |
582 // Hide the window we just moved | |
583 XUnmapWindow(dpy, moving->win); | |
584 | |
585 // Update focus to next available window in current workspace | |
586 focused = workspaces[current_ws]; | |
587 if (focused) { | |
8 | 588 focus(focused, 0); |
7 | 589 } else { |
590 focused = NULL; | |
591 } | |
592 | |
593 // Re-arrange current workspace | |
594 arrange(); | |
1 | 595 } |
596 | |
0 | 597 // Action functions |
598 static int sendevent(Client *c, Atom proto) { | |
7 | 599 int n; |
600 Atom *protocols; | |
601 int exists = 0; | |
602 XEvent ev; | |
0 | 603 |
7 | 604 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) { |
605 while (!exists && n--) | |
606 exists = protocols[n] == proto; | |
607 XFree(protocols); | |
608 } | |
609 if (exists) { | |
610 ev.type = ClientMessage; | |
611 ev.xclient.window = c->win; | |
612 ev.xclient.message_type = wm_protocols; | |
613 ev.xclient.format = 32; | |
614 ev.xclient.data.l[0] = proto; | |
615 ev.xclient.data.l[1] = CurrentTime; | |
616 XSendEvent(dpy, c->win, False, NoEventMask, &ev); | |
617 } | |
618 return exists; | |
0 | 619 } |
620 | |
621 static void killclient(const Arg *arg) { | |
7 | 622 if (!focused) return; |
623 if (!sendevent(focused, wm_delete_window)) { | |
624 XGrabServer(dpy); | |
625 XSetCloseDownMode(dpy, DestroyAll); | |
626 XKillClient(dpy, focused->win); | |
627 XSync(dpy, False); | |
628 XUngrabServer(dpy); | |
629 } | |
0 | 630 } |
631 | |
632 static void setfullscreen(Client *c, int fullscreen) { | |
7 | 633 if (!c) return; |
0 | 634 |
7 | 635 if (fullscreen && !c->isfullscreen) { |
636 // Save current position before going fullscreen | |
637 c->saved_x = c->x; | |
638 c->saved_y = c->y; | |
639 c->saved_w = c->w; | |
640 c->saved_h = c->h; | |
0 | 641 |
7 | 642 c->isfullscreen = 1; |
0 | 643 |
7 | 644 // Remove border and set to full screen |
645 XSetWindowBorderWidth(dpy, c->win, 0); | |
646 resize(c, 0, 0, sw, sh); | |
647 XRaiseWindow(dpy, c->win); | |
0 | 648 |
7 | 649 } else if (!fullscreen && c->isfullscreen) { |
650 // Restore saved position | |
651 c->isfullscreen = 0; | |
0 | 652 |
7 | 653 // Restore border |
654 XSetWindowBorderWidth(dpy, c->win, border_width); | |
0 | 655 |
7 | 656 // Restore original position |
657 resize(c, c->saved_x, c->saved_y, c->saved_w, c->saved_h); | |
658 } | |
0 | 659 |
7 | 660 // Update _NET_WM_STATE |
661 XEvent ev; | |
662 ev.type = ClientMessage; | |
663 ev.xclient.window = c->win; | |
664 ev.xclient.message_type = XInternAtom(dpy, "_NET_WM_STATE", False); | |
665 ev.xclient.format = 32; | |
666 ev.xclient.data.l[0] = fullscreen ? 1 : 0; | |
667 ev.xclient.data.l[1] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); | |
668 ev.xclient.data.l[2] = 0; | |
669 XSendEvent(dpy, root, False, SubstructureNotifyMask | SubstructureRedirectMask, &ev); | |
0 | 670 } |
671 | |
672 static void toggle_fullscreen(const Arg *arg) { | |
7 | 673 if (!focused) return; |
674 setfullscreen(focused, !focused->isfullscreen); | |
0 | 675 } |
676 | |
677 static void spawn(const Arg *arg) { | |
7 | 678 if (fork() == 0) { |
679 if (dpy) | |
680 close(ConnectionNumber(dpy)); | |
681 setsid(); | |
682 execvp(((char **)arg->v)[0], (char **)arg->v); | |
8 | 683 die("execvp %s failed", ((char **)arg->v)[0]); |
7 | 684 } |
0 | 685 } |
686 | |
687 static void quit(const Arg *arg) { | |
7 | 688 exit(0); |
0 | 689 } |
690 | |
691 static void cycle_focus(const Arg *arg) { | |
7 | 692 if (!workspaces[current_ws]) return; |
0 | 693 |
7 | 694 if (!focused) { |
8 | 695 focus(workspaces[current_ws], 1); |
7 | 696 return; |
697 } | |
0 | 698 |
7 | 699 Client *next = focused->next; |
700 if (!next) next = workspaces[current_ws]; | |
0 | 701 |
8 | 702 focus(next, 1); |
0 | 703 } |
704 | |
705 static void grabkeys(void) { | |
7 | 706 XUngrabKey(dpy, AnyKey, AnyModifier, root); |
707 for (unsigned int i = 0; i < sizeof(keys) / sizeof(Key); i++) { | |
708 KeyCode code = XKeysymToKeycode(dpy, keys[i].keysym); | |
709 if (code) { | |
710 XGrabKey(dpy, code, keys[i].mod, root, True, | |
711 GrabModeAsync, GrabModeAsync); | |
712 XGrabKey(dpy, code, keys[i].mod | Mod2Mask, root, True, | |
713 GrabModeAsync, GrabModeAsync); | |
714 } | |
715 } | |
0 | 716 } |
717 | |
718 static void sigchld(int s) { | |
7 | 719 (void)s; |
720 while (waitpid(-1, NULL, WNOHANG) > 0); | |
0 | 721 } |
722 | |
723 int xerror_handler(Display *dpy, XErrorEvent *ee) { | |
7 | 724 return 0; |
0 | 725 } |
726 | |
727 static void setup_colors(void) { | |
7 | 728 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy)); |
729 Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy)); | |
0 | 730 |
7 | 731 XftColorAllocName(dpy, visual, cmap, col_bg, &xft_col_bg); |
732 XftColorAllocName(dpy, visual, cmap, col_fg, &xft_col_fg); | |
733 XftColorAllocName(dpy, visual, cmap, col_sel, &xft_col_sel); | |
0 | 734 |
7 | 735 font = XftFontOpenName(dpy, DefaultScreen(dpy), overlay_font); |
0 | 736 |
7 | 737 // Allocate border colors |
738 XColor color; | |
739 XParseColor(dpy, cmap, col_border_normal, &color); | |
740 XAllocColor(dpy, cmap, &color); | |
741 border_normal = color.pixel; | |
0 | 742 |
7 | 743 XParseColor(dpy, cmap, col_border_focused, &color); |
744 XAllocColor(dpy, cmap, &color); | |
745 border_focused = color.pixel; | |
0 | 746 } |
747 | |
748 static void setup_icccm(void) { | |
7 | 749 wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False); |
750 wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False); | |
751 wm_state = XInternAtom(dpy, "WM_STATE", False); | |
752 wm_take_focus = XInternAtom(dpy, "WM_TAKE_FOCUS", False); | |
0 | 753 } |
754 | |
4
f1f332156693
feat/fix: builtin root window bg/overlay bg always black
Atarwn Gard <a@qwa.su>
parents:
3
diff
changeset
|
755 static void setrootbackground(void) { |
7 | 756 Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy)); |
757 XColor color; | |
4
f1f332156693
feat/fix: builtin root window bg/overlay bg always black
Atarwn Gard <a@qwa.su>
parents:
3
diff
changeset
|
758 |
7 | 759 if (XParseColor(dpy, cmap, root_bg, &color) && |
760 XAllocColor(dpy, cmap, &color)) { | |
761 XSetWindowBackground(dpy, root, color.pixel); | |
762 XClearWindow(dpy, root); | |
763 } | |
764 } | |
765 | |
766 void die(const char *fmt, ...) { | |
767 va_list ap; | |
768 va_start(ap, fmt); | |
769 vfprintf(stderr, fmt, ap); | |
770 va_end(ap); | |
771 fputc('\n', stderr); | |
772 exit(1); | |
4
f1f332156693
feat/fix: builtin root window bg/overlay bg always black
Atarwn Gard <a@qwa.su>
parents:
3
diff
changeset
|
773 } |
f1f332156693
feat/fix: builtin root window bg/overlay bg always black
Atarwn Gard <a@qwa.su>
parents:
3
diff
changeset
|
774 |
7 | 775 int main(int argc, char *argv[]) { |
776 if (argc == 2 && !strcmp("-v", argv[1])) | |
777 die("gbwm v"VERSION); | |
778 else if (argc != 1) | |
779 die("Usage: gbwm [-v]"); | |
780 if (!getenv("DISPLAY")) | |
781 die("DISPLAY environment variable not set"); | |
782 if (!(dpy = XOpenDisplay(NULL))) | |
783 die("cannot open X11 display (is X running?)"); | |
0 | 784 |
7 | 785 signal(SIGCHLD, sigchld); |
786 if (!(dpy = XOpenDisplay(NULL))) { | |
787 fprintf(stderr, "eowm: cannot open display\n"); | |
788 exit(1); | |
789 } | |
790 XSetErrorHandler(xerror_handler); | |
0 | 791 |
7 | 792 sw = DisplayWidth(dpy, DefaultScreen(dpy)); |
793 sh = DisplayHeight(dpy, DefaultScreen(dpy)); | |
794 root = RootWindow(dpy, DefaultScreen(dpy)); | |
795 Cursor cursor = XCreateFontCursor(dpy, XC_left_ptr); | |
796 XDefineCursor(dpy, root, cursor); | |
0 | 797 |
7 | 798 setup_colors(); |
799 setrootbackground(); | |
800 setup_icccm(); | |
0 | 801 |
7 | 802 XSelectInput(dpy, root, |
803 SubstructureRedirectMask | SubstructureNotifyMask | | |
804 EnterWindowMask | LeaveWindowMask | FocusChangeMask); | |
805 | |
806 grabkeys(); | |
0 | 807 |
7 | 808 XEvent ev; |
809 while (1) { | |
810 XNextEvent(dpy, &ev); | |
811 switch (ev.type) { | |
812 case ButtonPress: buttonpress(&ev); break; | |
813 case ClientMessage: clientmessage(&ev); break; | |
814 case MapRequest: maprequest(&ev); break; | |
815 case UnmapNotify: unmapnotify(&ev); break; | |
816 case DestroyNotify: destroynotify(&ev); break; | |
817 case EnterNotify: enternotify(&ev); break; | |
818 case KeyPress: keypress(&ev); break; | |
819 case Expose: expose(&ev); break; | |
820 } | |
821 } | |
0 | 822 |
7 | 823 XCloseDisplay(dpy); |
824 return 0; | |
1 | 825 } |