test/testwm.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Mar 2011 15:09:00 -0800
changeset 5459 881d1e03e381
parent 5218 572a73d71b5f
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed touches for the !IPHONE_TOUCH_EFFICIENT_DANGEROUS case
slouken@0
     1
slouken@0
     2
/* Test out the window manager interaction functions */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
slouken@0
    10
/* Is the cursor visible? */
slouken@0
    11
static int visible = 1;
slouken@0
    12
slouken@1895
    13
static Uint8 video_bpp;
slouken@826
    14
static Uint32 video_flags;
slouken@826
    15
icculus@1151
    16
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    17
static void
slouken@1895
    18
quit(int rc)
icculus@1151
    19
{
slouken@1895
    20
    SDL_Quit();
slouken@1895
    21
    exit(rc);
icculus@1151
    22
}
icculus@1151
    23
slouken@1895
    24
int
slouken@1895
    25
SetVideoMode(int w, int h)
slouken@826
    26
{
slouken@1895
    27
    SDL_Surface *screen;
slouken@1895
    28
    int i;
slouken@1895
    29
    Uint8 *buffer;
slouken@1895
    30
    SDL_Color palette[256];
slouken@826
    31
slouken@1895
    32
    screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
slouken@1895
    33
    if (screen == NULL) {
slouken@1895
    34
        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
slouken@1895
    35
                w, h, video_bpp, SDL_GetError());
slouken@1895
    36
        return (-1);
slouken@1895
    37
    }
slouken@1895
    38
    printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
slouken@1895
    39
           "fullscreen" : "windowed");
slouken@826
    40
slouken@1895
    41
    /* Set the surface pixels and refresh! */
slouken@1895
    42
    for (i = 0; i < 256; ++i) {
slouken@1895
    43
        palette[i].r = 255 - i;
slouken@1895
    44
        palette[i].g = 255 - i;
slouken@1895
    45
        palette[i].b = 255 - i;
slouken@1895
    46
    }
slouken@1895
    47
    SDL_SetColors(screen, palette, 0, 256);
slouken@1895
    48
    if (SDL_LockSurface(screen) < 0) {
slouken@1895
    49
        fprintf(stderr, "Couldn't lock display surface: %s\n",
slouken@1895
    50
                SDL_GetError());
slouken@1895
    51
        return (-1);
slouken@1895
    52
    }
slouken@1895
    53
    buffer = (Uint8 *) screen->pixels;
slouken@1895
    54
    for (i = 0; i < screen->h; ++i) {
slouken@1895
    55
        memset(buffer, (i * 255) / screen->h,
slouken@1895
    56
               screen->w * screen->format->BytesPerPixel);
slouken@1895
    57
        buffer += screen->pitch;
slouken@1895
    58
    }
slouken@1895
    59
    SDL_UnlockSurface(screen);
slouken@1895
    60
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@826
    61
slouken@1895
    62
    return (0);
slouken@826
    63
}
slouken@826
    64
slouken@1895
    65
SDL_Surface *
slouken@1895
    66
LoadIconSurface(char *file, Uint8 ** maskp)
slouken@0
    67
{
slouken@1895
    68
    SDL_Surface *icon;
slouken@1895
    69
    Uint8 *pixels;
slouken@1895
    70
    Uint8 *mask;
slouken@1895
    71
    int mlen, i, j;
slouken@0
    72
slouken@1895
    73
    *maskp = NULL;
slouken@0
    74
slouken@1895
    75
    /* Load the icon surface */
slouken@1895
    76
    icon = SDL_LoadBMP(file);
slouken@1895
    77
    if (icon == NULL) {
slouken@1895
    78
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
slouken@1895
    79
        return (NULL);
slouken@1895
    80
    }
slouken@0
    81
slouken@1895
    82
    /* Check width and height 
slouken@1895
    83
       if ( (icon->w%8) != 0 ) {
slouken@1895
    84
       fprintf(stderr, "Icon width must be a multiple of 8!\n");
slouken@1895
    85
       SDL_FreeSurface(icon);
slouken@1895
    86
       return(NULL);
slouken@1895
    87
       }
slouken@1895
    88
     */
slouken@0
    89
slouken@0
    90
slouken@1895
    91
    if (icon->format->palette == NULL) {
slouken@1895
    92
        fprintf(stderr, "Icon must have a palette!\n");
slouken@1895
    93
        SDL_FreeSurface(icon);
slouken@1895
    94
        return (NULL);
slouken@1895
    95
    }
slouken@1895
    96
slouken@1895
    97
    /* Set the colorkey */
slouken@1895
    98
    SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
slouken@1895
    99
slouken@1895
   100
    /* Create the mask */
slouken@1895
   101
    pixels = (Uint8 *) icon->pixels;
slouken@1895
   102
    printf("Transparent pixel: (%d,%d,%d)\n",
slouken@1895
   103
           icon->format->palette->colors[*pixels].r,
slouken@1895
   104
           icon->format->palette->colors[*pixels].g,
slouken@1895
   105
           icon->format->palette->colors[*pixels].b);
slouken@1895
   106
    mlen = (icon->w * icon->h + 7) / 8;
slouken@1895
   107
    mask = (Uint8 *) malloc(mlen);
slouken@1895
   108
    if (mask == NULL) {
slouken@1895
   109
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   110
        SDL_FreeSurface(icon);
slouken@1895
   111
        return (NULL);
slouken@1895
   112
    }
slouken@1895
   113
    memset(mask, 0, mlen);
slouken@1895
   114
    for (i = 0; i < icon->h; i++)
slouken@1895
   115
        for (j = 0; j < icon->w; j++) {
slouken@591
   116
            int pindex = i * icon->pitch + j;
slouken@591
   117
            int mindex = i * icon->w + j;
slouken@1895
   118
            if (pixels[pindex] != *pixels)
slouken@1895
   119
                mask[mindex >> 3] |= 1 << (7 - (mindex & 7));
slouken@591
   120
        }
slouken@1895
   121
    *maskp = mask;
slouken@1895
   122
    return (icon);
slouken@0
   123
}
slouken@0
   124
slouken@1895
   125
void
slouken@1895
   126
HotKey_ToggleFullScreen(void)
slouken@0
   127
{
slouken@1895
   128
    SDL_Surface *screen;
slouken@0
   129
slouken@1895
   130
    screen = SDL_GetVideoSurface();
slouken@1895
   131
    if (SDL_WM_ToggleFullScreen(screen)) {
slouken@1895
   132
        printf("Toggled fullscreen mode - now %s\n",
slouken@1895
   133
               (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
slouken@1895
   134
    } else {
slouken@1895
   135
        printf("Unable to toggle fullscreen mode\n");
slouken@1895
   136
        video_flags ^= SDL_FULLSCREEN;
slouken@1895
   137
        SetVideoMode(screen->w, screen->h);
slouken@1895
   138
    }
slouken@0
   139
}
slouken@0
   140
slouken@1895
   141
void
slouken@1895
   142
HotKey_ToggleGrab(void)
slouken@0
   143
{
slouken@1895
   144
    SDL_GrabMode mode;
slouken@0
   145
slouken@1895
   146
    printf("Ctrl-G: toggling input grab!\n");
slouken@1895
   147
    mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
slouken@1895
   148
    if (mode == SDL_GRAB_ON) {
slouken@1895
   149
        printf("Grab was on\n");
slouken@1895
   150
    } else {
slouken@1895
   151
        printf("Grab was off\n");
slouken@1895
   152
    }
slouken@1895
   153
    mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
slouken@1895
   154
    if (mode == SDL_GRAB_ON) {
slouken@1895
   155
        printf("Grab is now on\n");
slouken@1895
   156
    } else {
slouken@1895
   157
        printf("Grab is now off\n");
slouken@1895
   158
    }
slouken@0
   159
}
slouken@0
   160
slouken@1895
   161
void
slouken@1895
   162
HotKey_Iconify(void)
slouken@0
   163
{
slouken@1895
   164
    printf("Ctrl-Z: iconifying window!\n");
slouken@1895
   165
    SDL_WM_IconifyWindow();
slouken@0
   166
}
slouken@0
   167
slouken@1895
   168
void
slouken@1895
   169
HotKey_Quit(void)
slouken@0
   170
{
slouken@1895
   171
    SDL_Event event;
slouken@0
   172
slouken@1895
   173
    printf("Posting internal quit request\n");
slouken@1895
   174
    event.type = SDL_USEREVENT;
slouken@1895
   175
    SDL_PushEvent(&event);
slouken@0
   176
}
slouken@0
   177
icculus@3634
   178
icculus@3634
   179
static void
icculus@3634
   180
print_modifiers(void)
icculus@3634
   181
{
icculus@3634
   182
    int mod;
icculus@3634
   183
    printf(" modifiers:");
icculus@3634
   184
    mod = SDL_GetModState();
icculus@3634
   185
    if(!mod) {
icculus@3634
   186
        printf(" (none)");
icculus@3634
   187
        return;
icculus@3634
   188
    }
icculus@3634
   189
    if(mod & KMOD_LSHIFT)
icculus@3634
   190
        printf(" LSHIFT");
icculus@3634
   191
    if(mod & KMOD_RSHIFT)
icculus@3634
   192
        printf(" RSHIFT");
icculus@3634
   193
    if(mod & KMOD_LCTRL)
icculus@3634
   194
        printf(" LCTRL");
icculus@3634
   195
    if(mod & KMOD_RCTRL)
icculus@3634
   196
        printf(" RCTRL");
icculus@3634
   197
    if(mod & KMOD_LALT)
icculus@3634
   198
        printf(" LALT");
icculus@3634
   199
    if(mod & KMOD_RALT)
icculus@3634
   200
        printf(" RALT");
icculus@3634
   201
    if(mod & KMOD_LMETA)
icculus@3634
   202
        printf(" LMETA");
icculus@3634
   203
    if(mod & KMOD_RMETA)
icculus@3634
   204
        printf(" RMETA");
icculus@3634
   205
    if(mod & KMOD_NUM)
icculus@3634
   206
        printf(" NUM");
icculus@3634
   207
    if(mod & KMOD_CAPS)
icculus@3634
   208
        printf(" CAPS");
icculus@3634
   209
    if(mod & KMOD_MODE)
icculus@3634
   210
        printf(" MODE");
icculus@3634
   211
}
icculus@3634
   212
slouken@5218
   213
static void PrintKey(const SDL_Keysym *sym, int pressed)
icculus@3634
   214
{
icculus@3634
   215
    /* Print the keycode, name and state */
icculus@3634
   216
    if ( sym->sym ) {
icculus@3634
   217
        printf("Key %s:  %d-%s ", pressed ?  "pressed" : "released",
icculus@3634
   218
                    sym->sym, SDL_GetKeyName(sym->sym));
icculus@3634
   219
    } else {
icculus@3634
   220
        printf("Unknown Key (scancode = %d) %s ", sym->scancode,
icculus@3634
   221
                    pressed ?  "pressed" : "released");
icculus@3634
   222
    }
icculus@3634
   223
icculus@3634
   224
    /* Print the translated character, if one exists */
icculus@3634
   225
    if ( sym->unicode ) {
icculus@3634
   226
        /* Is it a control-character? */
icculus@3634
   227
        if ( sym->unicode < ' ' ) {
icculus@3634
   228
            printf(" (^%c)", sym->unicode+'@');
icculus@3634
   229
        } else {
icculus@3634
   230
#ifdef UNICODE
icculus@3634
   231
            printf(" (%c)", sym->unicode);
icculus@3634
   232
#else
icculus@3634
   233
            /* This is a Latin-1 program, so only show 8-bits */
icculus@3634
   234
            if ( !(sym->unicode & 0xFF00) )
icculus@3634
   235
                printf(" (%c)", sym->unicode);
icculus@3634
   236
            else
icculus@3634
   237
                printf(" (0x%X)", sym->unicode);
icculus@3634
   238
#endif
icculus@3634
   239
        }
icculus@3634
   240
    }
icculus@3634
   241
    print_modifiers();
icculus@3634
   242
    printf("\n");
icculus@3634
   243
}
icculus@3634
   244
icculus@3634
   245
bob@2185
   246
static int (SDLCALL * old_filterfunc) (void *, SDL_Event *);
slouken@1895
   247
static void *old_filterdata;
slouken@1895
   248
slouken@1895
   249
int SDLCALL
slouken@1895
   250
FilterEvents(void *userdata, SDL_Event * event)
slouken@0
   251
{
slouken@1895
   252
    static int reallyquit = 0;
slouken@0
   253
slouken@1895
   254
    if (old_filterfunc) {
slouken@1895
   255
        old_filterfunc(old_filterdata, event);
slouken@1895
   256
    }
slouken@0
   257
slouken@1895
   258
    switch (event->type) {
slouken@0
   259
slouken@1895
   260
    case SDL_ACTIVEEVENT:
slouken@1895
   261
        /* See what happened */
slouken@1895
   262
        printf("App %s ", event->active.gain ? "gained" : "lost");
slouken@1895
   263
        if (event->active.state & SDL_APPACTIVE)
slouken@1895
   264
            printf("active ");
slouken@1895
   265
        if (event->active.state & SDL_APPINPUTFOCUS)
slouken@1895
   266
            printf("input ");
slouken@1895
   267
        if (event->active.state & SDL_APPMOUSEFOCUS)
slouken@1895
   268
            printf("mouse ");
slouken@1895
   269
        printf("focus\n");
slouken@0
   270
slouken@1895
   271
        /* See if we are iconified or restored */
slouken@1895
   272
        if (event->active.state & SDL_APPACTIVE) {
slouken@1895
   273
            printf("App has been %s\n",
slouken@1895
   274
                   event->active.gain ? "restored" : "iconified");
slouken@1895
   275
        }
slouken@1895
   276
        return (0);
slouken@0
   277
slouken@1895
   278
        /* We want to toggle visibility on buttonpress */
slouken@1895
   279
    case SDL_MOUSEBUTTONDOWN:
slouken@1895
   280
    case SDL_MOUSEBUTTONUP:
slouken@1895
   281
        if (event->button.state == SDL_PRESSED) {
slouken@1895
   282
            visible = !visible;
slouken@1895
   283
            SDL_ShowCursor(visible);
slouken@1895
   284
        }
slouken@3545
   285
        printf("Mouse button %d has been %s at %d,%d\n",
slouken@1895
   286
               event->button.button,
slouken@3545
   287
               (event->button.state == SDL_PRESSED) ? "pressed" : "released",
slouken@3545
   288
               event->button.x, event->button.y);
slouken@1895
   289
        return (0);
slouken@1895
   290
slouken@1895
   291
        /* Show relative mouse motion */
slouken@1895
   292
    case SDL_MOUSEMOTION:
slouken@0
   293
#if 0
slouken@1895
   294
        printf("Mouse motion: {%d,%d} (%d,%d)\n",
slouken@1895
   295
               event->motion.x, event->motion.y,
slouken@1895
   296
               event->motion.xrel, event->motion.yrel);
slouken@0
   297
#endif
slouken@1895
   298
        return (0);
slouken@0
   299
slouken@1895
   300
    case SDL_KEYDOWN:
icculus@3634
   301
        PrintKey(&event->key.keysym, 1);
slouken@1895
   302
        if (event->key.keysym.sym == SDLK_ESCAPE) {
slouken@1895
   303
            HotKey_Quit();
slouken@1895
   304
        }
slouken@1895
   305
        if ((event->key.keysym.sym == SDLK_g) &&
slouken@1895
   306
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1895
   307
            HotKey_ToggleGrab();
slouken@1895
   308
        }
slouken@1895
   309
        if ((event->key.keysym.sym == SDLK_z) &&
slouken@1895
   310
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1895
   311
            HotKey_Iconify();
slouken@1895
   312
        }
slouken@1895
   313
        if ((event->key.keysym.sym == SDLK_RETURN) &&
slouken@4942
   314
            (event->key.keysym.mod & (KMOD_ALT|KMOD_META))) {
slouken@1895
   315
            HotKey_ToggleFullScreen();
slouken@1895
   316
        }
slouken@1895
   317
        return (0);
slouken@0
   318
icculus@3634
   319
	case SDL_KEYUP:
icculus@3634
   320
		PrintKey(&event->key.keysym, 0);
icculus@3634
   321
		return(0);
icculus@3634
   322
slouken@1895
   323
        /* Pass the video resize event through .. */
slouken@1895
   324
    case SDL_VIDEORESIZE:
slouken@1895
   325
        return (1);
slouken@0
   326
slouken@1895
   327
        /* This is important!  Queue it if we want to quit. */
slouken@1895
   328
    case SDL_QUIT:
slouken@1895
   329
        if (!reallyquit) {
slouken@1895
   330
            reallyquit = 1;
slouken@1895
   331
            printf("Quit requested\n");
slouken@1895
   332
            return (0);
slouken@1895
   333
        }
slouken@1895
   334
        printf("Quit demanded\n");
slouken@1895
   335
        return (1);
slouken@0
   336
slouken@1895
   337
        /* This will never happen because events queued directly
slouken@1895
   338
           to the event queue are not filtered.
slouken@1895
   339
         */
slouken@1895
   340
    case SDL_USEREVENT:
slouken@1895
   341
        return (1);
slouken@0
   342
slouken@1895
   343
        /* Drop all other events */
slouken@1895
   344
    default:
slouken@1895
   345
        return (0);
slouken@1895
   346
    }
slouken@0
   347
}
slouken@0
   348
slouken@1895
   349
int
slouken@1895
   350
main(int argc, char *argv[])
slouken@0
   351
{
slouken@1895
   352
    SDL_Event event;
slouken@2060
   353
    const char *title;
slouken@1895
   354
    SDL_Surface *icon;
slouken@1895
   355
    Uint8 *icon_mask;
slouken@1895
   356
    int parsed;
slouken@1895
   357
    int w, h;
slouken@0
   358
slouken@1895
   359
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   360
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   361
        return (1);
slouken@1895
   362
    }
slouken@0
   363
slouken@1895
   364
    /* Check command line arguments */
slouken@1895
   365
    w = 640;
slouken@1895
   366
    h = 480;
slouken@1895
   367
    video_bpp = 8;
slouken@1895
   368
    video_flags = SDL_SWSURFACE;
slouken@1895
   369
    parsed = 1;
slouken@1895
   370
    while (parsed) {
slouken@1895
   371
        if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
slouken@1895
   372
            video_flags |= SDL_FULLSCREEN;
slouken@1895
   373
            argc -= 1;
slouken@1895
   374
            argv += 1;
slouken@1895
   375
        } else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
slouken@1895
   376
            video_flags |= SDL_RESIZABLE;
slouken@1895
   377
            argc -= 1;
slouken@1895
   378
            argv += 1;
slouken@1895
   379
        } else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
slouken@1895
   380
            video_flags |= SDL_NOFRAME;
slouken@1895
   381
            argc -= 1;
slouken@1895
   382
            argv += 1;
slouken@1895
   383
        } else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
slouken@1895
   384
            w = atoi(argv[2]);
slouken@1895
   385
            argc -= 2;
slouken@1895
   386
            argv += 2;
slouken@1895
   387
        } else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
slouken@1895
   388
            h = atoi(argv[2]);
slouken@1895
   389
            argc -= 2;
slouken@1895
   390
            argv += 2;
slouken@1895
   391
        } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
slouken@1895
   392
            video_bpp = atoi(argv[2]);
slouken@1895
   393
            argc -= 2;
slouken@1895
   394
            argv += 2;
slouken@1895
   395
        } else {
slouken@1895
   396
            parsed = 0;
slouken@1895
   397
        }
slouken@1895
   398
    }
slouken@0
   399
slouken@1895
   400
    /* Set the icon -- this must be done before the first mode set */
slouken@1895
   401
    icon = LoadIconSurface("icon.bmp", &icon_mask);
slouken@1895
   402
    if (icon != NULL) {
slouken@1895
   403
        SDL_WM_SetIcon(icon, icon_mask);
slouken@1895
   404
    }
slouken@1895
   405
    if (icon_mask != NULL)
slouken@1895
   406
        free(icon_mask);
slouken@0
   407
slouken@1895
   408
    /* Set the title bar */
slouken@1895
   409
    if (argv[1] == NULL)
slouken@1895
   410
        title = "Testing  1.. 2.. 3...";
slouken@1895
   411
    else
slouken@1895
   412
        title = argv[1];
slouken@1895
   413
    SDL_WM_SetCaption(title, "testwm");
slouken@0
   414
slouken@1895
   415
    /* See if it's really set */
slouken@1895
   416
    SDL_WM_GetCaption(&title, NULL);
slouken@1895
   417
    if (title)
slouken@1895
   418
        printf("Title was set to: %s\n", title);
slouken@1895
   419
    else
slouken@1895
   420
        printf("No window title was set!\n");
slouken@0
   421
slouken@1895
   422
    /* Initialize the display */
slouken@1895
   423
    if (SetVideoMode(w, h) < 0) {
slouken@1895
   424
        quit(1);
slouken@1895
   425
    }
slouken@0
   426
slouken@1895
   427
    /* Set an event filter that discards everything but QUIT */
slouken@1895
   428
    SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
slouken@1895
   429
    SDL_SetEventFilter(FilterEvents, NULL);
slouken@0
   430
slouken@1895
   431
    /* Loop, waiting for QUIT */
slouken@1895
   432
    while (SDL_WaitEvent(&event)) {
slouken@1895
   433
        switch (event.type) {
slouken@1895
   434
        case SDL_VIDEORESIZE:
slouken@1895
   435
            printf("Got a resize event: %dx%d\n",
slouken@1895
   436
                   event.resize.w, event.resize.h);
slouken@1895
   437
            SetVideoMode(event.resize.w, event.resize.h);
slouken@1895
   438
            break;
slouken@1895
   439
        case SDL_USEREVENT:
slouken@1895
   440
            printf("Handling internal quit request\n");
slouken@1895
   441
            /* Fall through to the quit handler */
slouken@1895
   442
        case SDL_QUIT:
slouken@1895
   443
            printf("Bye bye..\n");
slouken@1895
   444
            quit(0);
slouken@1895
   445
        default:
slouken@1895
   446
            /* This should never happen */
slouken@1895
   447
            printf("Warning: Event %d wasn't filtered\n", event.type);
slouken@1895
   448
            break;
slouken@1895
   449
        }
slouken@1895
   450
    }
slouken@1895
   451
    printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
slouken@1895
   452
    SDL_Quit();
slouken@1895
   453
    return (255);
slouken@0
   454
}