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