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