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