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