Added functions IN-MAIN-THREAD-P and PUSH-EVENT

This commit is contained in:
Elias Martenson 2017-07-17 15:52:30 +08:00 committed by Javier Olaechea
parent bcf76afaa0
commit 61e53b4c06
2 changed files with 21 additions and 14 deletions

View file

@ -20,9 +20,7 @@
(defpackage :stumpwm
(:use :cl
#:alexandria)
(:shadow #:yes-or-no-p #:y-or-n-p)
(:export
#:call-in-main-thread))
(:shadow #:yes-or-no-p #:y-or-n-p))
(defpackage :stumpwm-user
(:use :cl :stumpwm))

View file

@ -26,7 +26,10 @@
run-with-timer
*toplevel-io*
stumpwm
timer-p))
timer-p
call-in-main-thread
in-main-thread-p
push-event))
;;; Main
@ -236,19 +239,25 @@ The action is to call FUNCTION with arguments ARGS."
(dolist (event (reverse events))
(funcall event))))
(defun in-main-thread-p ()
*in-main-thread*)
(defun push-event (fn)
(sb-thread:with-mutex ((request-channel-lock *request-channel*))
(push fn (request-channel-queue *request-channel*)))
(let ((out (request-channel-out *request-channel*)))
;; For now, just write a single byte since all we want is for the
;; main thread to process the queue. If we want to handle
;; different types of events, we'll have to change this so that
;; the message sent indicates the event type instead.
(write-byte 0 out)
(finish-output out)))
(defun call-in-main-thread (fn)
(cond (*in-main-thread*
(cond ((in-main-thread-p)
(funcall fn))
(t
(sb-thread:with-mutex ((request-channel-lock *request-channel*))
(push fn (request-channel-queue *request-channel*)))
(let ((out (request-channel-out *request-channel*)))
;; For now, just write a single byte since all we want is for the
;; main thread to process the queue. If we want to handle
;; different types of events, we'll have to change this so that
;; the message sent indicates the event type instead.
(write-byte 0 out)
(finish-output out)))))
(push-event fn))))
(defclass display-channel ()
((display :initarg :display)))