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