diff options
author | Samuel Fadel <samuel@nihil.ws> | 2022-12-08 18:25:18 +0100 |
---|---|---|
committer | Samuel Fadel <samuel@nihil.ws> | 2022-12-08 18:25:18 +0100 |
commit | 6f54084c0ee63f4b341f8f92efbc8b82d3dfa224 (patch) | |
tree | 454e8d7cf51aa4cded69535d60ec56a753325034 | |
parent | 6bdaee03c0fd3caac390dffdaaf52d54653b1430 (diff) |
Working example with keybindings done from Scheme.
* main.scm: Added utility and keybinding calls
* wm.scm: Added necessary bindings to C code
* schewm.c: Some utility functions for grabbing keys
-rw-r--r-- | main.scm | 30 | ||||
-rw-r--r-- | schewm.c | 42 | ||||
-rw-r--r-- | wm.scm | 15 |
3 files changed, 64 insertions, 23 deletions
@@ -5,7 +5,7 @@ (make-config inner-border-width outer-border-width - manger-border-width + magnet-border-width offset-x offset-y offset-width @@ -30,16 +30,34 @@ 0 ; offset-height )) -(define keys - '(((make-key-chord (key-from-str "q")) . wm-quit))) +(define (make-key key) + (list #f (key-from-str key))) + +(define (make-shift-key key) + (list #t (key-from-str key))) + +(define wm-keybindings + `((,(make-shift-key "q") . ,wm-quit) + (,(make-key "Tab") . ,wm-focus-next))) + +(define (grab-keys keybindings) + (unless (null? keybindings) + (let* ((keybinding (car keybindings)) + (chord (car keybinding)) + (func (cdr keybinding)) + (with-shift (car chord)) + (key (car (cdr chord)))) + (if with-shift + (wm-grab-key-with-mod-shift key) + (wm-grab-key-with-mod key)) + (grab-keys (cdr keybindings))))) (when (wm-init) + (grab-keys wm-keybindings) (wm-set-key-press-handler! (lambda (mod keysym) (display (list mod keysym)) - (display "\n"))) - (display config) - (display "\n") + (newline))) (wm-run) (wm-quit)) @@ -465,6 +465,7 @@ typedef void (*generic_event_handler_t)(xcb_generic_event_t *); static struct { uint32_t cur_workspace, num_workspaces; bool is_running; + uint16_t mod_key; struct Client *focus; struct ClientsMap *clients; struct Workspace *workspaces; @@ -1778,6 +1779,29 @@ wm_fit_client(struct Client *client) { return should_move || should_resize; } +void +wm_set_mod_key(uint16_t mod) { + wm.mod_key = mod; +} + +uint16_t +wm_get_mod_key(bool with_shift) { + if (with_shift) { + return wm.mod_key | MOD_SHIFT; + } + return wm.mod_key; +} + +void +wm_grab_key_with_mod(xcb_keysym_t keysym) { + dpy_grab_key(wm_get_mod_key(false), keysym); +} + +void +wm_grab_key_with_mod_shift(xcb_keysym_t keysym) { + dpy_grab_key(wm_get_mod_key(true), keysym); +} + /* * event handlers which take a generic event and know what to cast it into. */ @@ -1879,8 +1903,7 @@ wm_init() { dpy_update_window_list(wm.clients); wm_set_workspace(wm.cur_workspace); - // grab_keys(); - dpy_grab_key(XCB_MOD_MASK_ANY, XK_q); + wm.mod_key = MOD_META; dpy_flush(); memset(wm.events, 0, sizeof(wm.events)); @@ -1930,21 +1953,6 @@ wm_run() { wm_destroy(); } -static uint16_t mod_key = MOD_META; - -void -wm_set_mod_key(uint16_t mod) { - mod_key = mod; -} - -uint16_t -wm_get_mod_key(bool with_shift) { - if (with_shift) { - return mod_key | MOD_SHIFT; - } - return mod_key; -} - /* * NOTE: * Don't do the thing below. Create hooks for relevant stuff the user @@ -1,7 +1,11 @@ (define-module (wm) #:use-module (system foreign) #:export (wm-init + key-from-str + wm-grab-key-with-mod + wm-grab-key-with-mod-shift wm-set-key-press-handler! + wm-focus-next wm-run wm-quit)) @@ -36,6 +40,17 @@ (define wm-run (schewm-func void "wm_run" '())) +(define wm-grab-key-with-mod + (schewm-func void "wm_grab_key_with_mod" + (list uint32))) + +(define wm-grab-key-with-mod-shift + (schewm-func void "wm_grab_key_with_mod_shift" + (list uint32))) + +(define (wm-focus-next) + '()) + (define c/wm-set-key-press-handler (schewm-func void "wm_set_key_press_handler" |