add auto-reload module

This commit is contained in:
Almarhoon Ibraheem 2024-06-18 20:45:41 +03:00
parent c1e67d46bf
commit ae09bf465e
3 changed files with 108 additions and 5 deletions

View file

@ -135,12 +135,12 @@ Clone this repository to your =~/.config/sway=
| sjson | A patched version of =guile-json=; planned to be a separate dependency in the future (not embedded). | | sjson | A patched version of =guile-json=; planned to be a separate dependency in the future (not embedded). |
|------------------------------+-----------------------------------------------------------------------------------------------------------| |------------------------------+-----------------------------------------------------------------------------------------------------------|
| modules/ | Directory containing modules for extending Sway using =swayipc=. | | modules/ | Directory containing modules for extending Sway using =swayipc=. |
| modules/auto-reload.scm | TODO: Watcher to automatically reload Sway when Guile files change. | | modules/auto-reload.scm | Watcher to automatically reload Sway when Guile files change. |
| modules/general.scm | Inspired by Emacs =general= package; provides an easy interface for assigning keybindings. | | modules/general.scm | Inspired by Emacs =general= package; provides an easy interface for assigning keybindings. |
| modules/kbd.scm | WIP: Translates Emacs-like keybindings to be compatible with =Sway=. | | modules/kbd.scm | Translates Emacs-like keybindings to be compatible with =Sway=. |
| modules/which-key.scm | TODO: Inspired by Emacs =which-key= package; enhances keybinding discovery and management. | | modules/which-key.scm | TODO: Inspired by Emacs =which-key= package; enhances keybinding discovery and management. |
| modules/workspace-grid.scm | Configures workspaces in a grid and enables movement between them in specified directions (see workflow). | | modules/workspace-grid.scm | Configures workspaces in a grid and enables movement between them in specified directions (see workflow). |
| modules/workspace-groups.scm | WIP: Spans/synchronizes workspaces across monitors (see workflow). | | modules/workspace-groups.scm | Spans/synchronizes workspaces across monitors (see workflow). |
|------------------------------+-----------------------------------------------------------------------------------------------------------| |------------------------------+-----------------------------------------------------------------------------------------------------------|
| swayipc/ | Directory containing the core code for =swayipc=, facilitating communication with Sway. | | swayipc/ | Directory containing the core code for =swayipc=, facilitating communication with Sway. |
| swayipc/connection | Establishes =IPC= connection for handling events and commands with Sway. | | swayipc/connection | Establishes =IPC= connection for handling events and commands with Sway. |

View file

@ -16,6 +16,7 @@
(swayipc dispatcher)) (swayipc dispatcher))
(load "behavior.scm") (load "behavior.scm")
;; init keybindings ;; init keybindings
(load "keybindings.scm") (load "keybindings.scm")
(keybindings-init) (keybindings-init)

View file

@ -1,5 +1,107 @@
;; TODO ;; use example:
;; NOTE: use full path for your home, don't use tilde (~)
;; (auto-reload-configure #:directories '("/home/ebeem/.config/sway/"))
;; (auto-reload-init)
(define-module (modules auto-reload) (define-module (modules auto-reload)
#:use-module (swayipc dispatcher) #:use-module (swayipc dispatcher)
#:export ()) #:use-module (system foreign)
#:use-module (system foreign-library)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-18)
#:use-module (rnrs bytevectors)
#:use-module (ice-9 rdelim)
#:export (auto-reload-configure
auto-reload-init))
;; The list directories to watch for changes
(define DIRECTORIES '())
;; Load the inotify library
(define inotify (dynamic-link "libc.so.6"))
;; Import the necessary functions from the inotify library
(define inotify-init
(pointer->procedure int (dynamic-func "inotify_init" inotify) '()))
(define inotify-add-watch
(pointer->procedure int (dynamic-func "inotify_add_watch" inotify)
(list int '* uint32)))
(define inotify-rm-watch
(pointer->procedure int (dynamic-func "inotify_rm_watch" inotify)
(list int int)))
(define read
(pointer->procedure int (dynamic-func "read" inotify)
(list int '* size_t)))
;; Define constants for inotify events
(define IN_ACCESS #x00000001)
(define IN_MODIFY #x00000002)
(define IN_ATTRIB #x00000004)
(define IN_CLOSE_WRITE #x00000008)
(define IN_CLOSE_NOWRITE #x00000010)
(define IN_OPEN #x00000020)
(define IN_MOVED_FROM #x00000040)
(define IN_MOVED_TO #x00000080)
(define IN_CREATE #x00000100)
(define IN_DELETE #x00000200)
(define IN_DELETE_SELF #x00000400)
(define IN_MOVE_SELF #x00000800)
(define IN_UNMOUNT #x00002000)
(define IN_Q_OVERFLOW #x00004000)
(define IN_IGNORED #x00008000)
(define IN_ONLYDIR #x01000000)
(define IN_DONT_FOLLOW #x02000000)
(define IN_EXCL_UNLINK #x04000000)
(define IN_MASK_ADD #x20000000)
(define IN_ISDIR #x40000000)
(define IN_ONESHOT #x80000000)
;; Create an inotify instance
(define fd (inotify-init))
(define (watch-directory dir)
"Watch the directory DIR for file changes."
(inotify-add-watch fd dir (+ IN_CREATE IN_DELETE IN_MODIFY)))
(define (event-loop)
"Event loop to process inotify events."
(let* ((buffer (make-bytevector 1024))
(event-size 4)
(event (bytevector->pointer buffer)))
(let loop ()
(let ((length (read fd event 1024)))
(if (> length 0)
(let ((event-type (bytevector-s32-native-ref
(pointer->bytevector event 4 0) 0)))
;; TODO: should check for event, currently it always returns 1 for some reason
;; there might be something wrong with the way the point is converted
;; (cond
;; ((logand event-type IN_CREATE)
;; (format #t "File created: ~a\n" name))
;; ((logand event-type IN_DELETE)
;; (format #t "File deleted: ~a\n" name))
;; ((logand event-type IN_MODIFY)
;; (format #t "File modified: ~a\n" name)))
(sway-reload))
(loop))))))
(define* (auto-reload-configure #:key directories)
"Configure auto reload parameters.
Parameters:
- directories: list of directories to watch for changes."
(set! DIRECTORIES directories))
(define (auto-reload-init)
"Start listening for change events on the configured directories"
(for-each (lambda (dir)
(format #t "watching directory ~a for auto reload\n" dir)
(watch-directory (string->pointer dir)))
DIRECTORIES)
;; Start the event loop
(thread-start! (make-thread event-loop)))