diff --git a/extra/game-input/backend/dinput/dinput.factor b/extra/game-input/backend/dinput/dinput.factor index 5744025d00..2041bb1047 100755 --- a/extra/game-input/backend/dinput/dinput.factor +++ b/extra/game-input/backend/dinput/dinput.factor @@ -188,15 +188,14 @@ TUPLE: window-rect < rect window-loc ; +keyboard-device+ global [ com-release f ] change-at ; -M: dinput-game-input-backend open-game-input - +dinput+ get-global [ "game-input already open" throw ] when +M: dinput-game-input-backend (open-game-input) create-dinput create-device-change-window find-keyboard set-up-controllers add-wm-devicechange ; -M: dinput-game-input-backend close-game-input +M: dinput-game-input-backend (close-game-input) remove-wm-devicechange release-controllers release-keyboard diff --git a/extra/game-input/backend/iokit/iokit.factor b/extra/game-input/backend/iokit/iokit.factor index fe2ce66789..1871569227 100755 --- a/extra/game-input/backend/iokit/iokit.factor +++ b/extra/game-input/backend/iokit/iokit.factor @@ -218,8 +218,7 @@ SYMBOLS: +hid-manager+ +keyboard-state+ +controller-states+ ; 4 +controller-states+ set-global 256 f +keyboard-state+ set-global ; -M: iokit-game-input-backend open-game-input - +hid-manager+ get-global [ "game-input already open" throw ] when +M: iokit-game-input-backend (open-game-input) hid-manager-matching-game-devices { [ initialize-variables ] [ device-matched-callback f IOHIDManagerRegisterDeviceMatchingCallback ] @@ -232,7 +231,7 @@ M: iokit-game-input-backend open-game-input ] } cleave ; -M: iokit-game-input-backend close-game-input +M: iokit-game-input-backend (close-game-input) +hid-manager+ get-global [ +hid-manager+ global [ [ diff --git a/extra/game-input/game-input-docs.factor b/extra/game-input/game-input-docs.factor index f74c135110..e4380cc0d2 100755 --- a/extra/game-input/game-input-docs.factor +++ b/extra/game-input/game-input-docs.factor @@ -25,16 +25,20 @@ ARTICLE: "game-input" "Game controller input" { $subsection keyboard-state } ; HELP: open-game-input -{ $description "Initializes the game input interface. An exception will be thrown if the initialization fails." } ; +{ $description "Initializes the game input interface. An exception will be thrown if the initialization fails. If the game input interface is already opened, nothing happens." } ; HELP: close-game-input -{ $description "Closes the game input interface, releasing any allocated resources. Once this word is called, any remaining " { $link controller } " objects are invalid." } ; +{ $description "Closes the game input interface, releasing any allocated resources. Once this word is called, any remaining " { $link controller } " objects are invalid. If the game input interface is not opened, nothing happens." } ; + +HELP: game-input-opened? +{ $values { "?" "a boolean" } } +{ $description "Returns true if the game input interface is open, false otherwise." } ; HELP: with-game-input { $values { "quot" quotation } } { $description "Initializes the game input interface for the dynamic extent of " { $snippet "quotation" } "." } ; -{ open-game-input close-game-input with-game-input } related-words +{ open-game-input close-game-input with-game-input game-input-opened? } related-words HELP: get-controllers { $values { "sequence" "A " { $link sequence } " of " { $link controller } "s" } } @@ -71,14 +75,16 @@ HELP: calibrate-controller HELP: read-controller { $values { "controller" controller } { "controller-state" controller-state } } -{ $description "Reads the current state of " { $snippet "controller" } ". See the documentation for the " { $link controller-state } " class for details of the returned value's format. If the device is no longer available, " { $link f } " is returned." } ; +{ $description "Reads the current state of " { $snippet "controller" } ". See the documentation for the " { $link controller-state } " class for details of the returned value's format. If the device is no longer available, " { $link f } " is returned." } +{ $warning "For efficiency, the implementation may reuse the returned " { $snippet "controller-state" } " object next time " { $snippet "read-controller" } " is called on the same controller. You should " { $link clone } " any values from the returned tuple you need to preserve." } ; { controller-state controller read-controller } related-words HELP: read-keyboard { $values { "keyboard-state" keyboard-state } } { $description "Reads the current raw state of the keyboard. See the documentation for the " { $link keyboard-state } " class for details on the returned value's format." } -{ $warning "The keyboard state returned by this word is unprocessed by any keymaps, modifier keys, key repeat settings, or other operating environment postprocessing. Because of this, " { $snippet "read-keyboard" } " should not be used for text entry purposes. The Factor UI's standard gesture mechanism should be used in cases where the logical meaning of keypresses is needed; see " { $link "keyboard-gestures" } "." } ; +{ $warning "For efficiency, the implementation may reuse the returned " { $snippet "keyboard-state" } " object next time " { $snippet "read-keyboard" } " is called. You should " { $link clone } " any values from the returned tuple you need to preserve." +$nl "The keyboard state returned by this word is unprocessed by any keymaps, modifier keys, key repeat settings, or other operating environment postprocessing. Because of this, " { $snippet "read-keyboard" } " should not be used for text entry purposes. The Factor UI's standard gesture mechanism should be used in cases where the logical meaning of keypresses is needed; see " { $link "keyboard-gestures" } "." } ; HELP: controller-state { $class-description "The " { $link read-controller } " word returns objects of this class. " { $snippet "controller-state" } " objects have the following slots:" diff --git a/extra/game-input/game-input.factor b/extra/game-input/game-input.factor index 62de2c5a54..bb910337ca 100755 --- a/extra/game-input/game-input.factor +++ b/extra/game-input/game-input.factor @@ -1,11 +1,25 @@ USING: arrays accessors continuations kernel symbols -combinators.lib sequences ; +combinators.lib sequences namespaces ; IN: game-input -SYMBOL: game-input-backend +SYMBOLS: game-input-backend game-input-opened ; -HOOK: open-game-input game-input-backend ( -- ) -HOOK: close-game-input game-input-backend ( -- ) +HOOK: (open-game-input) game-input-backend ( -- ) +HOOK: (close-game-input) game-input-backend ( -- ) + +: game-input-opened? ( -- ? ) + game-input-opened get ; + +: open-game-input ( -- ) + game-input-opened? [ + (open-game-input) + game-input-opened on + ] unless ; +: close-game-input ( -- ) + game-input-opened? [ + (close-game-input) + game-input-opened off + ] when ; : with-game-input ( quot -- ) open-game-input [ close-game-input ] [ ] cleanup ; diff --git a/extra/joystick-demo/joystick-demo.factor b/extra/joystick-demo/joystick-demo.factor index d8963862b7..bbcf904737 100644 --- a/extra/joystick-demo/joystick-demo.factor +++ b/extra/joystick-demo/joystick-demo.factor @@ -80,18 +80,37 @@ TUPLE: joystick-demo-gadget < pack axis raxis controller buttons alarm ; : update-buttons ( buttons button-states -- ) [ >>selected? drop ] 2each ; -: update-axes ( gadget -- ) - dup controller>> read-controller { - [ [ axis>> ] [ [ x>> ] [ y>> ] [ z>> ] tri ] bi* move-axis ] - [ [ raxis>> ] [ [ rx>> ] [ ry>> ] [ rz>> ] tri ] bi* move-axis ] - [ [ buttons>> ] [ buttons>> ] bi* update-buttons ] +: kill-update-axes ( gadget -- ) + gray >>interior + [ cancel-alarm f ] change-alarm + relayout-1 ; + +: (update-axes) ( gadget controller-state -- ) + { + [ [ axis>> ] [ [ x>> ] [ y>> ] [ z>> ] tri ] bi* move-axis ] + [ [ raxis>> ] [ [ rx>> ] [ ry>> ] [ rz>> ] tri ] bi* move-axis ] + [ [ buttons>> ] [ buttons>> ] bi* update-buttons ] [ drop relayout-1 ] } 2cleave ; +: update-axes ( gadget -- ) + dup controller>> read-controller + [ (update-axes) ] [ kill-update-axes ] if* ; + M: joystick-demo-gadget graft* dup '[ , update-axes ] FREQUENCY every >>alarm drop ; M: joystick-demo-gadget ungraft* - alarm>> cancel-alarm ; + alarm>> [ cancel-alarm ] when* ; +: joystick-window ( controller -- ) + [ ] [ product-string ] bi + open-window ; + +: joystick-demo ( -- ) + open-game-input [ + get-controllers [ joystick-window ] each + ] with-ui ; + +MAIN: joystick-demo