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