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