forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepad.lisp
More file actions
71 lines (59 loc) · 2.77 KB
/
gamepad.lisp
File metadata and controls
71 lines (59 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#|
This file is a part of trial
(c) 2016 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.fraf.trial)
(defvar *gamepad-handlers* ())
(defvar *gamepad-handlers-lock* (bt:make-lock))
(defvar *gamepad-input-thread* ())
(defun add-gamepad-handler (handler)
(bt:with-lock-held (*gamepad-handlers-lock*)
(pushnew handler *gamepad-handlers*)))
(defun remove-gamepad-handler (handler)
(bt:with-lock-held (*gamepad-handlers-lock*)
(setf *gamepad-handlers* (remove handler *gamepad-handlers*))))
(defun init-gamepad-system ()
(or *gamepad-input-thread*
(setf *gamepad-input-thread*
(with-thread ("gamepad event thread")
(cl-gamepad:init)
(unwind-protect
(loop for i = 0 then (1+ i)
while *gamepad-input-thread*
do (when (= 0 (mod i 60))
(cl-gamepad:detect-devices))
(cl-gamepad:process-events)
(sleep 1/60))
(cl-gamepad:shutdown))))))
(defun shutdown-gamepad-system ()
(with-thread-exit (*gamepad-input-thread*)
(setf *gamepad-input-thread* NIL)))
(init-gamepad-system)
(defun cl-gamepad:device-attached (device)
(v:info :trial.input.gamepad "Attached ~s"
(cl-gamepad:print-device device NIL))
(dolist (handler *gamepad-handlers*)
(handle (make-instance 'gamepad-attach :device device) handler)))
(defun cl-gamepad:device-removed (device)
(v:info :trial.input.gamepad "Removed ~s" (cl-gamepad:print-device device NIL))
(dolist (handler *gamepad-handlers*)
(handle (make-instance 'gamepad-remove :device device) handler)))
(defun cl-gamepad:button-pressed (button time device)
(declare (ignore time))
(let ((button (cl-gamepad:button-label device button)))
(v:trace :trial.input.gamepad "~a pressed ~a" (cl-gamepad:id device) button)
(dolist (handler *gamepad-handlers*)
(handle (make-instance 'gamepad-press :button button :device device) handler))))
(defun cl-gamepad:button-released (button time device)
(declare (ignore time))
(let ((button (cl-gamepad:button-label device button)))
(v:trace :trial.input.gamepad "~a released ~a" (cl-gamepad:id device) button)
(dolist (handler *gamepad-handlers*)
(handle (make-instance 'gamepad-release :button button :device device) handler))))
(defun cl-gamepad:axis-moved (axis last-value value time device)
(declare (ignore time))
(let ((axis (cl-gamepad:axis-label device axis))
(mult (cl-gamepad:axis-multiplier device axis)))
(dolist (handler *gamepad-handlers*)
(handle (make-instance 'gamepad-move :axis axis :old-pos (* mult last-value) :pos (* mult value) :device device) handler))))