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