~ubuntu-branches/ubuntu/trusty/libsdl2/trusty-proposed

« back to all changes in this revision

Viewing changes to test/testjoystick.c

  • Committer: Package Import Robot
  • Author(s): Manuel A. Fernandez Montecelo
  • Date: 2013-12-28 12:31:19 UTC
  • mto: (7.1.3 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20131228123119-wehupm72qsjvh6vz
Tags: upstream-2.0.1+dfsg1
ImportĀ upstreamĀ versionĀ 2.0.1+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
53
53
                              SCREEN_HEIGHT, 0);
54
54
    if (window == NULL) {
55
 
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
 
55
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
56
56
        return SDL_FALSE;
57
57
    }
58
58
 
59
59
    screen = SDL_CreateRenderer(window, -1, 0);
60
60
    if (screen == NULL) {
61
 
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
 
61
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
62
62
        SDL_DestroyWindow(window);
63
63
        return SDL_FALSE;
64
64
    }
70
70
 
71
71
    /* Print info about the joystick we are watching */
72
72
    name = SDL_JoystickName(joystick);
73
 
    printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
 
73
    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
74
74
           name ? name : "Unknown Joystick");
75
 
    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
 
75
    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
76
76
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
77
77
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
78
78
 
85
85
        while (SDL_PollEvent(&event)) {
86
86
            switch (event.type) {
87
87
            case SDL_JOYAXISMOTION:
88
 
                printf("Joystick %d axis %d value: %d\n",
 
88
                SDL_Log("Joystick %d axis %d value: %d\n",
89
89
                       event.jaxis.which,
90
90
                       event.jaxis.axis, event.jaxis.value);
91
91
                break;
92
92
            case SDL_JOYHATMOTION:
93
 
                printf("Joystick %d hat %d value:",
 
93
                SDL_Log("Joystick %d hat %d value:",
94
94
                       event.jhat.which, event.jhat.hat);
95
95
                if (event.jhat.value == SDL_HAT_CENTERED)
96
 
                    printf(" centered");
 
96
                    SDL_Log(" centered");
97
97
                if (event.jhat.value & SDL_HAT_UP)
98
 
                    printf(" up");
 
98
                    SDL_Log(" up");
99
99
                if (event.jhat.value & SDL_HAT_RIGHT)
100
 
                    printf(" right");
 
100
                    SDL_Log(" right");
101
101
                if (event.jhat.value & SDL_HAT_DOWN)
102
 
                    printf(" down");
 
102
                    SDL_Log(" down");
103
103
                if (event.jhat.value & SDL_HAT_LEFT)
104
 
                    printf(" left");
105
 
                printf("\n");
 
104
                    SDL_Log(" left");
 
105
                SDL_Log("\n");
106
106
                break;
107
107
            case SDL_JOYBALLMOTION:
108
 
                printf("Joystick %d ball %d delta: (%d,%d)\n",
 
108
                SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
109
109
                       event.jball.which,
110
110
                       event.jball.ball, event.jball.xrel, event.jball.yrel);
111
111
                break;
112
112
            case SDL_JOYBUTTONDOWN:
113
 
                printf("Joystick %d button %d down\n",
 
113
                SDL_Log("Joystick %d button %d down\n",
114
114
                       event.jbutton.which, event.jbutton.button);
115
115
                break;
116
116
            case SDL_JOYBUTTONUP:
117
 
                printf("Joystick %d button %d up\n",
 
117
                SDL_Log("Joystick %d button %d up\n",
118
118
                       event.jbutton.which, event.jbutton.button);
119
119
                break;
120
120
            case SDL_KEYDOWN:
211
211
    int i;
212
212
    SDL_Joystick *joystick;
213
213
 
 
214
    /* Enable standard application logging */
 
215
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);    
 
216
 
214
217
    /* Initialize SDL (Note: video is required to start event loop) */
215
218
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
216
 
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
 
219
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
217
220
        exit(1);
218
221
    }
219
222
 
220
223
    /* Print information about the joysticks */
221
 
    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
 
224
    SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
222
225
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
223
226
        name = SDL_JoystickNameForIndex(i);
224
 
        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
 
227
        SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
225
228
        joystick = SDL_JoystickOpen(i);
226
229
        if (joystick == NULL) {
227
 
            fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
 
230
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
228
231
                    SDL_GetError());
229
232
        } else {
230
233
            char guid[64];
231
234
            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
232
235
                                      guid, sizeof (guid));
233
 
            printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
234
 
            printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
235
 
            printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
236
 
            printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
237
 
            printf("instance id: %d\n", SDL_JoystickInstanceID(joystick));
238
 
            printf("       guid: %s\n", guid);
 
236
            SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
 
237
            SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
 
238
            SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
 
239
            SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
 
240
            SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
 
241
            SDL_Log("       guid: %s\n", guid);
239
242
            SDL_JoystickClose(joystick);
240
243
        }
241
244
    }
256
259
        while ( keepGoing ) {
257
260
            if (joystick == NULL) {
258
261
                if ( !reportederror ) {
259
 
                    printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
 
262
                    SDL_Log("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
260
263
                    keepGoing = SDL_FALSE;
261
264
                    reportederror = SDL_TRUE;
262
265
                }
268
271
 
269
272
            joystick = NULL;
270
273
            if (keepGoing) {
271
 
                printf("Waiting for attach\n");
 
274
                SDL_Log("Waiting for attach\n");
272
275
            }
273
276
            while (keepGoing) {
274
277
                SDL_WaitEvent(&event);
296
299
int
297
300
main(int argc, char *argv[])
298
301
{
299
 
    fprintf(stderr, "SDL compiled without Joystick support.\n");
 
302
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
300
303
    exit(1);
301
304
}
302
305