diff options
author | Samuel Fadel <samuel@nihil.ws> | 2022-12-19 23:02:38 +0100 |
---|---|---|
committer | Samuel Fadel <samuel@nihil.ws> | 2022-12-19 23:02:38 +0100 |
commit | 291a0b72fbeaf757056fbb7fba0376262360d877 (patch) | |
tree | 6e5bee2e5f4dc2d8b0832f638f55ae52ba7bafab | |
parent | 86d7067f5d27d7c97e9a1de8c4103e139af66c40 (diff) |
Refactoring to reduce repeated code, fix tiling bugs.
* Makefile: Add optimization flag
* init.scm: Use correct indices for workspaces
* schewm.c: Many lines of repeated code removed
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | init.scm | 38 | ||||
-rw-r--r-- | schewm.c | 231 |
3 files changed, 129 insertions, 142 deletions
@@ -4,4 +4,4 @@ LDFLAGS=`pkg-config --libs x11 xcb xcb-ewmh xcb-icccm xcb-keysyms xcb-randr` all: libschewm.so libschewm.so: schewm.c - gcc -fPIC -Wall -g ${CFLAGS} ${LDFLAGS} -shared -o libschewm.so schewm.c + gcc -fPIC -Wall -O2 ${CFLAGS} ${LDFLAGS} -shared -o libschewm.so schewm.c @@ -32,7 +32,7 @@ (,(make-key "j") . ,wm-focus-next) ;; Packing (,(make-shift-key "Left") . ,wm-pack-left) - (,(make-shift-key "Right") . ,wm-pack-left) + (,(make-shift-key "Right") . ,wm-pack-right) (,(make-shift-key "Up") . ,wm-pack-top) (,(make-shift-key "Down") . ,wm-pack-bottom) ;; Toggle between normal and tiled in a certain position @@ -50,25 +50,25 @@ (,(make-key "z") . ,wm-toggle-bottom-left) (,(make-key "c") . ,wm-toggle-bottom-right) ;; Set current workspace - (,(make-key "1") . ,(make-callback wm-set-workspace 1)) - (,(make-key "2") . ,(make-callback wm-set-workspace 2)) - (,(make-key "3") . ,(make-callback wm-set-workspace 3)) - (,(make-key "4") . ,(make-callback wm-set-workspace 4)) - (,(make-key "5") . ,(make-callback wm-set-workspace 5)) - (,(make-key "6") . ,(make-callback wm-set-workspace 6)) - (,(make-key "7") . ,(make-callback wm-set-workspace 7)) - (,(make-key "8") . ,(make-callback wm-set-workspace 8)) - (,(make-key "9") . ,(make-callback wm-set-workspace 9)) + (,(make-key "1") . ,(make-callback wm-set-workspace 0)) + (,(make-key "2") . ,(make-callback wm-set-workspace 1)) + (,(make-key "3") . ,(make-callback wm-set-workspace 2)) + (,(make-key "4") . ,(make-callback wm-set-workspace 3)) + (,(make-key "5") . ,(make-callback wm-set-workspace 4)) + (,(make-key "6") . ,(make-callback wm-set-workspace 5)) + (,(make-key "7") . ,(make-callback wm-set-workspace 6)) + (,(make-key "8") . ,(make-callback wm-set-workspace 7)) + (,(make-key "9") . ,(make-callback wm-set-workspace 8)) ;; Send client to workspace - (,(make-shift-key "1") . ,(make-callback wm-set-focused-client-workspace 1)) - (,(make-shift-key "2") . ,(make-callback wm-set-focused-client-workspace 2)) - (,(make-shift-key "3") . ,(make-callback wm-set-focused-client-workspace 3)) - (,(make-shift-key "4") . ,(make-callback wm-set-focused-client-workspace 4)) - (,(make-shift-key "5") . ,(make-callback wm-set-focused-client-workspace 5)) - (,(make-shift-key "6") . ,(make-callback wm-set-focused-client-workspace 6)) - (,(make-shift-key "7") . ,(make-callback wm-set-focused-client-workspace 7)) - (,(make-shift-key "8") . ,(make-callback wm-set-focused-client-workspace 8)) - (,(make-shift-key "9") . ,(make-callback wm-set-focused-client-workspace 9)) + (,(make-shift-key "1") . ,(make-callback wm-set-focused-client-workspace 0)) + (,(make-shift-key "2") . ,(make-callback wm-set-focused-client-workspace 1)) + (,(make-shift-key "3") . ,(make-callback wm-set-focused-client-workspace 2)) + (,(make-shift-key "4") . ,(make-callback wm-set-focused-client-workspace 3)) + (,(make-shift-key "5") . ,(make-callback wm-set-focused-client-workspace 4)) + (,(make-shift-key "6") . ,(make-callback wm-set-focused-client-workspace 5)) + (,(make-shift-key "7") . ,(make-callback wm-set-focused-client-workspace 6)) + (,(make-shift-key "8") . ,(make-callback wm-set-focused-client-workspace 7)) + (,(make-shift-key "9") . ,(make-callback wm-set-focused-client-workspace 8)) ;; Send client to monitor (,(make-shift-key "i") . ,wm-client-monitor-prev) (,(make-shift-key "o") . ,wm-client-monitor-next))) @@ -1946,7 +1946,9 @@ wm_set_client_state(struct Client *client, enum WindowState state) { dpy_unmap_window(client->id); break; case WS_MAXIMIZED: - client_store_size(client); + if (!client->has_old_size) { + client_store_size(client); + } { struct Rect mon_rect; wm_client_monitor_size(client, true, &mon_rect); @@ -2463,184 +2465,169 @@ wm_client_ensure_state(struct Client *client, enum WindowState state) { } } +enum TileType { + TILE_HALF_LEFT, + TILE_HALF_RIGHT, + TILE_HALF_TOP, + TILE_HALF_BOTTOM, + TILE_TOP_LEFT, + TILE_TOP_RIGHT, + TILE_BOTTOM_LEFT, + TILE_BOTTOM_RIGHT, +}; + void -wm_toggle_half_left() { +wm_toggle_tile(enum TileType tile_type) { if (wm.focus == NULL) { return; } wm_client_ensure_state(wm.focus, WS_NORMAL); - // Start with monitor size, then reduce to appropriate target size - // for half the screen, accounting for border sizes - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.width = rect.width / 2 - cfg.inner_border_width * 2; - rect.height -= cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); + if (wm.focus->has_old_size) { + client_restore_size(wm.focus); + dpy_update_window_geometry(wm.focus); + } else { + struct Rect rect; + // Start with monitor size, then reduce to appropriate target size + wm_client_monitor_size(wm.focus, true, &rect); + switch (tile_type) { + case TILE_HALF_LEFT: + rect.width = rect.width / 2; + break; + case TILE_HALF_RIGHT: + rect.x += rect.width / 2; + rect.width = rect.width / 2; + break; + case TILE_HALF_TOP: + rect.height = rect.height / 2; + break; + case TILE_HALF_BOTTOM: + rect.y += rect.height / 2; + rect.height = rect.height / 2; + break; + case TILE_TOP_LEFT: + rect.width = rect.width / 2; + rect.height = rect.height / 2; + break; + case TILE_TOP_RIGHT: + rect.x += rect.width / 2; + rect.width = rect.width / 2; + rect.height = rect.height / 2; + break; + case TILE_BOTTOM_LEFT: + rect.y += rect.height / 2; + rect.width = rect.width / 2; + rect.height = rect.height / 2; + break; + case TILE_BOTTOM_RIGHT: + rect.x += rect.width / 2; + rect.y += rect.height / 2; + rect.width = rect.width / 2; + rect.height = rect.height / 2; + break; + } + // Correct width/height according to borders + rect.width -= cfg.inner_border_width * 2; + rect.height -= cfg.inner_border_width * 2; + // Stores current size so we can toggle it later + client_store_size(wm.focus); + wm_move_resize_client(wm.focus, &rect, false); + } wm_raise_and_center_cursor(wm.focus); } void -wm_toggle_half_right() { - if (wm.focus == NULL) { - return; - } +wm_toggle_half_left() { + wm_toggle_tile(TILE_HALF_LEFT); +} - wm_client_ensure_state(wm.focus, WS_NORMAL); - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.x += rect.width / 2; - rect.width = rect.width / 2 - cfg.inner_border_width * 2; - rect.height -= cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); - wm_raise_and_center_cursor(wm.focus); +void +wm_toggle_half_right() { + wm_toggle_tile(TILE_HALF_RIGHT); } void wm_toggle_half_top() { - if (wm.focus == NULL) { - return; - } - - wm_client_ensure_state(wm.focus, WS_NORMAL); - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.width -= cfg.inner_border_width * 2; - rect.height = rect.height / 2 - cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); - wm_raise_and_center_cursor(wm.focus); + wm_toggle_tile(TILE_HALF_TOP); } void wm_toggle_half_bottom() { - if (wm.focus == NULL) { - return; - } - - wm_client_ensure_state(wm.focus, WS_NORMAL); - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.y += rect.height / 2; - rect.width -= cfg.inner_border_width * 2; - rect.height = rect.height / 2 - cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); - wm_raise_and_center_cursor(wm.focus); + wm_toggle_tile(TILE_HALF_BOTTOM); } void wm_toggle_top_left() { - if (wm.focus == NULL) { - return; - } - - wm_client_ensure_state(wm.focus, WS_NORMAL); - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.width = rect.width / 2 - cfg.inner_border_width * 2; - rect.height = rect.height / 2 - cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); - wm_raise_and_center_cursor(wm.focus); + wm_toggle_tile(TILE_TOP_LEFT); } void wm_toggle_top_right() { - if (wm.focus == NULL) { - return; - } - - wm_client_ensure_state(wm.focus, WS_NORMAL); - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.x += rect.width / 2; - rect.width = rect.width / 2 - cfg.inner_border_width * 2; - rect.height = rect.height / 2 - cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); - wm_raise_and_center_cursor(wm.focus); + wm_toggle_tile(TILE_TOP_RIGHT); } void wm_toggle_bottom_left() { - if (wm.focus == NULL) { - return; - } - - wm_client_ensure_state(wm.focus, WS_NORMAL); - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.y += rect.height / 2; - rect.width = rect.width / 2 - cfg.inner_border_width * 2; - rect.height = rect.height / 2 - cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); - wm_raise_and_center_cursor(wm.focus); + wm_toggle_tile(TILE_BOTTOM_LEFT); } void wm_toggle_bottom_right() { - if (wm.focus == NULL) { - return; - } - - wm_client_ensure_state(wm.focus, WS_NORMAL); - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - rect.x += rect.width / 2; - rect.y += rect.height / 2; - rect.width = rect.width / 2 - cfg.inner_border_width * 2; - rect.height = rect.height / 2 - cfg.inner_border_width * 2; - wm_move_resize_client(wm.focus, &rect, false); - wm_raise_and_center_cursor(wm.focus); + wm_toggle_tile(TILE_BOTTOM_RIGHT); } +enum PackType { + PACK_LEFT, + PACK_RIGHT, + PACK_TOP, + PACK_BOTTOM, +}; + void -wm_pack_left() { +wm_pack_client(enum PackType pack_type) { if (wm.focus == NULL || wm.focus->state != WS_NORMAL) { return; } struct Rect rect; wm_client_monitor_size(wm.focus, true, &rect); - int16_t dx = rect.x - wm.focus->x; - wm_move_client(wm.focus, dx, 0, false); + int16_t dx = 0, dy = 0; + switch (pack_type) { + case PACK_LEFT: + dx = rect.x - wm.focus->x; + break; + case PACK_RIGHT: + dx = rect.width - (wm.focus->x - rect.x + wm.focus->width) - cfg.inner_border_width * 2; + break; + case PACK_TOP: + dy = rect.y - wm.focus->y; + break; + case PACK_BOTTOM: + dy = rect.height - (wm.focus->y - rect.y + wm.focus->height) - cfg.inner_border_width * 2; + break; + } + wm_move_client(wm.focus, dx, dy, false); wm_raise_and_center_cursor(wm.focus); } void -wm_pack_right() { - if (wm.focus == NULL || wm.focus->state != WS_NORMAL) { - return; - } +wm_pack_left() { + wm_pack_client(PACK_LEFT); +} - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - int16_t dx = rect.width - (wm.focus->x - rect.x + wm.focus->width) - cfg.inner_border_width * 2; - wm_move_client(wm.focus, dx, 0, false); - wm_raise_and_center_cursor(wm.focus); +void +wm_pack_right() { + wm_pack_client(PACK_RIGHT); } void wm_pack_top() { - if (wm.focus == NULL || wm.focus->state != WS_NORMAL) { - return; - } - - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - int16_t dy = rect.y - wm.focus->y; - wm_move_client(wm.focus, 0, dy, false); - wm_raise_and_center_cursor(wm.focus); + wm_pack_client(PACK_TOP); } void wm_pack_bottom() { - if (wm.focus == NULL || wm.focus->state != WS_NORMAL) { - return; - } - - struct Rect rect; - wm_client_monitor_size(wm.focus, true, &rect); - int16_t dy = rect.height - (wm.focus->y - rect.y + wm.focus->height) - cfg.inner_border_width * 2; - wm_move_client(wm.focus, 0, dy, false); - wm_raise_and_center_cursor(wm.focus); + wm_pack_client(PACK_BOTTOM); } void |