13
13
* What it does not but should probably do:
14
14
* - Transmit events when glfwPollEvents, glfwWaitEvents or glfwSwapBuffers is
15
15
* called. Events callbacks are called as soon as event are received.
16
- * - Joystick support.
17
16
* - Input modes.
18
17
* - Gamma ramps.
19
18
* - Video modes.
@@ -81,6 +80,7 @@ var LibraryGLFW = {
81
80
return GLFW . windows [ id - 1 ] ;
82
81
} ,
83
82
83
+ joystickFunc : null , // GLFWjoystickfun
84
84
errorFunc : null , // GLFWerrorfun
85
85
monitorFunc : null , // GLFWmonitorfun
86
86
active : null , // active window
@@ -382,6 +382,14 @@ var LibraryGLFW = {
382
382
#endif
383
383
} ,
384
384
385
+ onGamepadConnected : function ( event ) {
386
+ GLFW . refreshJoysticks ( ) ;
387
+ } ,
388
+
389
+ onGamepadDisconnected : function ( event ) {
390
+ GLFW . refreshJoysticks ( ) ;
391
+ } ,
392
+
385
393
onKeydown : function ( event ) {
386
394
GLFW . onKeyChanged ( event . keyCode , 1 ) ; // GLFW_PRESS or GLFW_REPEAT
387
395
@@ -641,6 +649,68 @@ var LibraryGLFW = {
641
649
}
642
650
} ,
643
651
652
+ setJoystickCallback : function ( cbfun ) {
653
+ GLFW . joystickFunc = cbfun ;
654
+ GLFW . refreshJoysticks ( ) ;
655
+ } ,
656
+
657
+ joys : { } , // glfw joystick data
658
+ lastGamepadState : null ,
659
+ lastGamepadStateFrame : null , // The integer value of Browser.mainLoop.currentFrameNumber of when the last gamepad state was produced.
660
+
661
+ refreshJoysticks : function ( ) {
662
+ // Produce a new Gamepad API sample if we are ticking a new game frame, or if not using emscripten_set_main_loop() at all to drive animation.
663
+ if ( Browser . mainLoop . currentFrameNumber !== GLFW . lastGamepadStateFrame || ! Browser . mainLoop . currentFrameNumber ) {
664
+ GLFW . lastGamepadState = navigator . getGamepads ? navigator . getGamepads ( ) : ( navigator . webkitGetGamepads ? navigator . webkitGetGamepads : null ) ;
665
+ GLFW . lastGamepadStateFrame = Browser . mainLoop . currentFrameNumber ;
666
+
667
+ for ( var joy = 0 ; joy < GLFW . lastGamepadState . length ; ++ joy ) {
668
+ var gamepad = GLFW . lastGamepadState [ joy ] ;
669
+
670
+ if ( gamepad ) {
671
+ if ( ! GLFW . joys [ joy ] ) {
672
+ console . log ( 'glfw joystick connected:' , joy ) ;
673
+ GLFW . joys [ joy ] = {
674
+ id : allocate ( intArrayFromString ( gamepad . id ) , 'i8' , ALLOC_NORMAL ) ,
675
+ buttonsCount : gamepad . buttons . length ,
676
+ axesCount : gamepad . axes . length ,
677
+ buttons : allocate ( new Array ( gamepad . buttons . length ) , 'i8' , ALLOC_NORMAL ) ,
678
+ axes : allocate ( new Array ( gamepad . axes . length * 4 ) , 'float' , ALLOC_NORMAL )
679
+ } ;
680
+
681
+ if ( GLFW . joystickFunc ) {
682
+ Module [ 'dynCall_vii' ] ( GLFW . joystickFunc , joy , 0x00040001 ) ; // GLFW_CONNECTED
683
+ }
684
+ }
685
+
686
+ var data = GLFW . joys [ joy ] ;
687
+
688
+ for ( var i = 0 ; i < gamepad . buttons . length ; ++ i ) {
689
+ setValue ( data . buttons + i , gamepad . buttons [ i ] . pressed , 'i8' ) ;
690
+ }
691
+
692
+ for ( var i = 0 ; i < gamepad . axes . length ; ++ i ) {
693
+ setValue ( data . axes + i * 4 , gamepad . axes [ i ] , 'float' ) ;
694
+ }
695
+ } else {
696
+ if ( GLFW . joys [ joy ] ) {
697
+ console . log ( 'glfw joystick disconnected' , joy ) ;
698
+
699
+ if ( GLFW . joystickFunc ) {
700
+ Module [ 'dynCall_vii' ] ( GLFW . joystickFunc , joy , 0x00040002 ) ; // GLFW_DISCONNECTED
701
+ }
702
+
703
+ _free ( GLFW . joys [ joy ] . id ) ;
704
+ _free ( GLFW . joys [ joy ] . buttons ) ;
705
+ _free ( GLFW . joys [ joy ] . axes ) ;
706
+
707
+ delete GLFW . joys [ joy ] ;
708
+ }
709
+ }
710
+ }
711
+ }
712
+ } ,
713
+
644
714
setKeyCallback : function ( winid , cbfun ) {
645
715
var win = GLFW . WindowFromId ( winid ) ;
646
716
if ( ! win ) return ;
@@ -954,6 +1024,8 @@ var LibraryGLFW = {
954
1024
GLFW . windows = new Array ( )
955
1025
GLFW . active = null ;
956
1026
1027
+ window . addEventListener ( "gamepadconnected" , GLFW . onGamepadConnected , true ) ;
1028
+ window . addEventListener ( "gamepaddisconnected" , GLFW . onGamepadDisconnected , true ) ;
957
1029
window . addEventListener ( "keydown" , GLFW . onKeydown , true ) ;
958
1030
window . addEventListener ( "keypress" , GLFW . onKeyPress , true ) ;
959
1031
window . addEventListener ( "keyup" , GLFW . onKeyup , true ) ;
@@ -973,6 +1045,8 @@ var LibraryGLFW = {
973
1045
} ,
974
1046
975
1047
glfwTerminate : function ( ) {
1048
+ window . removeEventListener ( "gamepadconnected" , GLFW . onGamepadConnected , true ) ;
1049
+ window . removeEventListener ( "gamepaddisconnected" , GLFW . onGamepadDisconnected , true ) ;
976
1050
window . removeEventListener ( "keydown" , GLFW . onKeydown , true ) ;
977
1051
window . removeEventListener ( "keypress" , GLFW . onKeyPress , true ) ;
978
1052
window . removeEventListener ( "keyup" , GLFW . onKeyup , true ) ;
@@ -1354,15 +1428,49 @@ var LibraryGLFW = {
1354
1428
1355
1429
glfwCreateWindowSurface : function ( instance , winid , allocator , surface ) { throw "glfwCreateWindowSurface is not implemented." ; } ,
1356
1430
1357
- glfwSetJoystickCallback : function ( cbfun ) { throw "glfwSetJoystickCallback is not implemented." ; } ,
1431
+ glfwJoystickPresent : function ( joy ) {
1432
+ GLFW . refreshJoysticks ( ) ;
1358
1433
1359
- glfwJoystickPresent : function ( joy ) { throw "glfwJoystickPresent is not implemented." ; } ,
1434
+ return GLFW . joys [ joy ] !== undefined ;
1435
+ } ,
1360
1436
1361
- glfwGetJoystickAxes : function ( joy , count ) { throw "glfwGetJoystickAxes is not implemented." ; } ,
1437
+ glfwGetJoystickAxes : function ( joy , count ) {
1438
+ GLFW . refreshJoysticks ( ) ;
1439
+
1440
+ var state = GLFW . joys [ joy ] ;
1441
+ if ( ! state || ! state . axes ) {
1442
+ setValue ( count , 0 , 'i32' ) ;
1443
+ return ;
1444
+ }
1445
+
1446
+ setValue ( count , state . axesCount , 'i32' ) ;
1447
+ return state . axes ;
1448
+ } ,
1362
1449
1363
- glfwGetJoystickButtons : function ( joy , count ) { throw "glfwGetJoystickButtons is not implemented." ; } ,
1450
+ glfwGetJoystickButtons : function ( joy , count ) {
1451
+ GLFW . refreshJoysticks ( ) ;
1364
1452
1365
- glfwGetJoystickName : function ( joy ) { throw "glfwGetJoystickName is not implemented." ; } ,
1453
+ var state = GLFW . joys [ joy ] ;
1454
+ if ( ! state || ! state . buttons ) {
1455
+ setValue ( count , 0 , 'i32' ) ;
1456
+ return ;
1457
+ }
1458
+
1459
+ setValue ( count , state . buttonsCount , 'i32' ) ;
1460
+ return state . buttons ;
1461
+ } ,
1462
+
1463
+ glfwGetJoystickName : function ( joy ) {
1464
+ if ( GLFW . joys [ joy ] ) {
1465
+ return GLFW . joys [ joy ] . id ;
1466
+ } else {
1467
+ return 0 ;
1468
+ }
1469
+ } ,
1470
+
1471
+ glfwSetJoystickCallback : function ( cbfun ) {
1472
+ GLFW . setJoystickCallback ( cbfun ) ;
1473
+ } ,
1366
1474
1367
1475
glfwSetClipboardString : function ( win , string ) { } ,
1368
1476
0 commit comments