guile-clipboard-speaker

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 06c659251761c21868fafc18420a6efef977060f
parent a87f916ab31c586b4daf9a27391892c2af93a819
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Fri, 23 Jun 2023 19:26:58 +0300

Improve the ability to kill the speaker process.

Diffstat:
Mclipboard-speaker.scm | 50+++++++++++++++++++++++++++-----------------------
1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/clipboard-speaker.scm b/clipboard-speaker.scm @@ -15,8 +15,9 @@ ((config api) #:prefix config:api:) ((config licenses) #:prefix config:licenses:) ((config parser sexp) #:prefix config:parser:sexp:) + ) -) +;;;(define espeak-ng-pid #f) (define (dp x) ;; Debug print. @@ -28,11 +29,9 @@ x) (define (debug-notify-send x) - (waitpid (spawn "notify-send" (list "notify-send" - "--expire-time=1000" - (format #f "~a~%" x))))) - - + (spawn "notify-send" (list "notify-send" + "--expire-time=1000" + (format #f "~s~%" x)))) (define (get-pid-of-file-locking-process options) (define lslocks-output-pipe (pipe)) @@ -46,28 +45,28 @@ (close-port (cdr lslocks-output-pipe)) + (define lslocks-output (json:json-read (car lslocks-output-pipe))) + + (waitpid lslocks-pid) + (close-port (car lslocks-output-pipe)) + (define result (match (filter (lambda (x) (equal? (cdadr x) (config:option-ref options 'pid-file-path))) ((compose vector->list - cdar - json:json-read) - (car lslocks-output-pipe))) + cdar) + lslocks-output)) (((('pid . pid) ('path . path))) pid) (() #f))) - (waitpid lslocks-pid) - - (close-port (car lslocks-output-pipe)) - result) (define (open-file-locked filename) - (let ([file-port (open-file filename "w+")]) + (let ([file-port (open-file filename "w")]) (with-exception-handler (lambda (an-exception) (close-port file-port) @@ -236,11 +235,14 @@ (string-append (get-line fifo-read-port) "\n")) - (dp text-to-speak) - (define espeak-ng-pid (espeak-ng-speak text-to-speak words-per-minute)) + (sigaction SIGTERM + (lambda (x) + (kill 0 x) ;; Kill all processes in this group. + (exit EXIT_SUCCESS))) + (waitpid espeak-ng-pid) (espeak-loop fifo-read-port @@ -258,7 +260,6 @@ (define (main args) (define options (config:getopt-config-auto args config)) - (dp (config:full-command options)) (newline) ;; XXX: I don't understand how one may retrieve these values. (define configuration-file-path (string-append @@ -273,10 +274,14 @@ ((file-exists? (config:option-ref options 'pid-file-path)) (let ([pid (get-pid-of-file-locking-process options)]) - (delete-file (config:option-ref options 'pid-file-path)) - (format #t "Kill ~a\n" pid) - (kill pid SIGHUP) - (exit EXIT_SUCCESS))) + (cond + ((number? pid) + (kill pid SIGTERM) + (format #t "Killed ~s.~%" pid) + (exit EXIT_SUCCESS)) + (else + (format #t "clipboard-speaker is not running.~%") + (exit EXIT_FAILURE))))) (else (format #t "No pid file: ~a~%" (config:option-ref options 'pid-file-path)) (exit EXIT_FAILURE)))) @@ -289,7 +294,7 @@ (read-clipboard (config:option-ref options 'clipboard-type))) - (format #t "Clipboard: ~a~%" clipboard-output) + (format #t "Clipboard: ~s~%" clipboard-output) ;; Write lines into fifo write port and flush. (put-string (cdr fifo-r+w) @@ -297,7 +302,6 @@ (define pid-file-port (open-file-locked (config:option-ref options 'pid-file-path))) - (format #t "pid file port: ~a\n" pid-file-port) (when pid-file-port ;; We're the speaker server. (put-string pid-file-port (number->string (getpid)))