~ubuntu-branches/ubuntu/trusty/emscripten/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/sdl_joystick.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2014-01-19 14:12:40 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20140119141240-jg1l42cc158j59tn
Tags: 1.9.0~20140119~7dc8c2f-1
* New snapshot release (Closes: #733714)
* Provide sources for javascript and flash. Done in orig-tar.sh
  Available in third_party/websockify/include/web-socket-js/src/
  (Closes: #735903)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <SDL/SDL.h>
 
3
#include <SDL/SDL_ttf.h>
 
4
#include <assert.h>
 
5
#include <string.h>
 
6
#include <emscripten.h>
 
7
 
 
8
int result = 1;
 
9
 
 
10
void assertJoystickEvent(int expectedGamepad, int expectedType, int expectedIndex, int expectedValue) {
 
11
  SDL_Event event;
 
12
  while(1) {
 
13
    // Loop ends either when assertion fails (we run out of events), or we find
 
14
    // the event we're looking for.
 
15
    assert(SDL_PollEvent(&event) == 1);
 
16
    if (event.type != expectedType) {
 
17
      continue;
 
18
    }
 
19
    switch(event.type) {
 
20
      case SDL_JOYAXISMOTION: {
 
21
        assert(event.jaxis.which == expectedGamepad);
 
22
        assert(event.jaxis.axis == expectedIndex);
 
23
        assert(event.jaxis.value == expectedValue);
 
24
        break;
 
25
      }
 
26
      case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: {
 
27
        assert(event.jbutton.which == expectedGamepad);
 
28
        assert(event.jbutton.button == expectedIndex);
 
29
        assert(event.jbutton.state == expectedValue);
 
30
        break;
 
31
      }
 
32
    }
 
33
    // Break out of while loop.
 
34
    break;
 
35
  }
 
36
}
 
37
 
 
38
void assertNoJoystickEvent() {
 
39
  SDL_Event event;
 
40
  while(SDL_PollEvent(&event)) {
 
41
    switch(event.type) {
 
42
      case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: case SDL_JOYAXISMOTION: {
 
43
        // Fail.
 
44
        assert(0);
 
45
      }
 
46
    }
 
47
  }
 
48
}
 
49
 
 
50
void main_2(void* arg);
 
51
 
 
52
int main() {
 
53
  SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
 
54
  SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
 
55
  emscripten_async_call(main_2, NULL, 3000); // avoid startup delays and intermittent errors
 
56
  return 0;
 
57
}
 
58
 
 
59
void main_2(void* arg) {
 
60
  // TODO: At the moment, we only support joystick support through polling.
 
61
  emscripten_run_script("window.addNewGamepad('Pad Thai', 4, 16)");
 
62
  emscripten_run_script("window.addNewGamepad('Pad Kee Mao', 0, 4)");
 
63
  // Check that the joysticks exist properly.
 
64
  assert(SDL_NumJoysticks() == 2);
 
65
  assert(!SDL_JoystickOpened(0));
 
66
  assert(!SDL_JoystickOpened(1));
 
67
  SDL_Joystick* pad1 = SDL_JoystickOpen(0);
 
68
  assert(SDL_JoystickOpened(0));
 
69
  assert(SDL_JoystickIndex(pad1) == 0);
 
70
  assert(strncmp(SDL_JoystickName(0), "Pad Thai", 9) == 0);
 
71
  assert(strncmp(SDL_JoystickName(1), "Pad Kee Mao", 12) == 0);
 
72
  assert(SDL_JoystickNumAxes(pad1) == 4);
 
73
  assert(SDL_JoystickNumButtons(pad1) == 16);
 
74
 
 
75
  // By default, SDL will automatically process events. Test this behavior, and then disable it.
 
76
  assert(SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE);
 
77
  SDL_JoystickEventState(SDL_DISABLE);
 
78
  assert(SDL_JoystickEventState(SDL_QUERY) == SDL_DISABLE);
 
79
  // Button events.
 
80
  emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");
 
81
  // We didn't tell SDL to automatically update this joystick's state.
 
82
  assertNoJoystickEvent();
 
83
  SDL_JoystickUpdate();
 
84
  assertJoystickEvent(0, SDL_JOYBUTTONDOWN, 1, SDL_PRESSED);
 
85
  assert(SDL_JoystickGetButton(pad1, 1) == 1);
 
86
  // Enable automatic updates.
 
87
  SDL_JoystickEventState(SDL_ENABLE);
 
88
  assert(SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE);
 
89
  emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");
 
90
  assertJoystickEvent(0, SDL_JOYBUTTONUP, 1, SDL_RELEASED);
 
91
  assert(SDL_JoystickGetButton(pad1, 1) == 0);
 
92
  // No button change: Should not result in a new event.
 
93
  emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");
 
94
  assertNoJoystickEvent();
 
95
  // Joystick 1 is not opened; should not result in a new event.
 
96
  emscripten_run_script("window.simulateGamepadButtonDown(1, 1)");
 
97
  assertNoJoystickEvent();
 
98
 
 
99
  // Joystick wiggling
 
100
  emscripten_run_script("window.simulateAxisMotion(0, 0, 1)");
 
101
  assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 32767);
 
102
  assert(SDL_JoystickGetAxis(pad1, 0) == 32767);
 
103
  emscripten_run_script("window.simulateAxisMotion(0, 0, 0)");
 
104
  assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 0);
 
105
  assert(SDL_JoystickGetAxis(pad1, 0) == 0);
 
106
  emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");
 
107
  assertJoystickEvent(0, SDL_JOYAXISMOTION, 1, -32768);
 
108
  assert(SDL_JoystickGetAxis(pad1, 1) == -32768);
 
109
  emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");
 
110
  // No joystick change: Should not result in a new event.
 
111
  assertNoJoystickEvent();
 
112
  // Joystick 1 is not opened; should not result in a new event.
 
113
  emscripten_run_script("window.simulateAxisMotion(1, 1, -1)");
 
114
  assertNoJoystickEvent();
 
115
 
 
116
  SDL_JoystickClose(pad1);
 
117
  assert(!SDL_JoystickOpened(0));
 
118
 
 
119
  // Joystick 0 is closed; we should not process any new gamepad events from it.
 
120
  emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");
 
121
  assertNoJoystickEvent();
 
122
 
 
123
  // End test.
 
124
  result = 2;
 
125
  printf("Test passed!\n");
 
126
  REPORT_RESULT();
 
127
}
 
128