/basis/game/input/x11/x11.factor

http://github.com/x6j8x/factor · Factor · 108 lines · 86 code · 20 blank · 2 comment · 1 complexity · c8b187574e4a554ca5e80d54cf1c1250 MD5 · raw file

  1. ! Copyright (C) 2010 Erik Charlebois, William Schlieper.
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: accessors alien.c-types alien.data arrays kernel game.input
  4. namespaces math classes bit-arrays system sequences vectors
  5. x11 x11.xlib assocs generalizations ;
  6. IN: game.input.x11
  7. SINGLETON: x11-game-input-backend
  8. x11-game-input-backend game-input-backend set-global
  9. M: x11-game-input-backend (open-game-input)
  10. ;
  11. M: x11-game-input-backend (close-game-input)
  12. ;
  13. M: x11-game-input-backend (reset-game-input)
  14. ;
  15. M: x11-game-input-backend get-controllers
  16. { } ;
  17. M: x11-game-input-backend product-string
  18. drop "" ;
  19. M: x11-game-input-backend product-id
  20. drop f ;
  21. M: x11-game-input-backend instance-id
  22. drop f ;
  23. M: x11-game-input-backend read-controller
  24. drop controller-state new ;
  25. M: x11-game-input-backend calibrate-controller
  26. drop ;
  27. M: x11-game-input-backend vibrate-controller
  28. 3drop ;
  29. HOOK: x>hid-bit-order os ( -- x )
  30. M: linux x>hid-bit-order
  31. {
  32. 0 0 0 0 0 0 0 0
  33. 0 41 30 31 32 33 34 35
  34. 36 37 38 39 45 46 42 43
  35. 20 26 8 21 23 28 24 12
  36. 18 19 47 48 40 224 4 22
  37. 7 9 10 11 13 14 15 51
  38. 52 53 225 49 29 27 6 25
  39. 5 17 16 54 55 56 229 85
  40. 226 44 57 58 59 60 61 62
  41. 63 64 65 66 67 83 71 95
  42. 96 97 86 92 93 94 87 91
  43. 90 89 98 99 0 0 0 68
  44. 69 0 0 0 0 0 0 0
  45. 88 228 84 70 0 0 74 82
  46. 75 80 79 77 81 78 73 76
  47. 127 129 128 102 103 0 72 0
  48. 0 0 0 227 231 0 0 0
  49. 0 0 0 0 0 0 0 0
  50. 0 0 0 0 0 0 0 0
  51. 0 0 0 0 0 0 0 0
  52. 0 0 0 0 0 0 0 0
  53. 0 0 0 0 0 0 0 0
  54. 0 0 0 0 0 0 0 0
  55. 0 0 0 0 0 0 0 0
  56. 0 0 0 0 0 0 0 0
  57. 0 0 0 0 0 0 0 0
  58. 0 0 0 0 0 0 0 0
  59. 0 0 0 0 0 0 0 0
  60. 0 0 0 0 0 0 0 0
  61. 0 0 0 0 0 0 0 0
  62. 0 0 0 0 0 0 0 0
  63. 0 0 0 0 0 0 0 0
  64. } ; inline
  65. : x-bits>hid-bits ( bit-array -- bit-array )
  66. 256 iota [ 2array ] { } 2map-as [ first ] filter values
  67. x>hid-bit-order [ nth ] curry map
  68. 256 <bit-array> swap [ t swap pick set-nth ] each ;
  69. M: x11-game-input-backend read-keyboard
  70. dpy get 256 <bit-array> [ XQueryKeymap drop ] keep
  71. x-bits>hid-bits keyboard-state boa ;
  72. : query-pointer ( -- x y buttons )
  73. dpy get dup XDefaultRootWindow
  74. { int int int int int int int }
  75. [ XQueryPointer drop ] with-out-parameters
  76. [ 4 ndrop ] 3dip ;
  77. SYMBOL: mouse-reset?
  78. M: x11-game-input-backend read-mouse
  79. mouse-reset? get [ reset-mouse ] unless
  80. query-pointer
  81. mouse-state new
  82. swap 256 /i >>buttons
  83. swap 400 - >>dy
  84. swap 400 - >>dx
  85. 0 >>scroll-dy 0 >>scroll-dx ;
  86. M: x11-game-input-backend reset-mouse
  87. dpy get dup XDefaultRootWindow dup
  88. 0 0 0 0 400 400 XWarpPointer drop t mouse-reset? set-global ;