test/testwm.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 05 Jan 2007 19:12:50 +0000
changeset 2082 e6cd882e3ac0
parent 2060 866052b01ee5
child 2179 8b76cc268771
permissions -rw-r--r--
Alpha sort the test list, add missing ones, remove removed ones
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
slouken@1895
   178
static int SDLCALL(*old_filterfunc) (void *, SDL_Event *);
slouken@1895
   179
static void *old_filterdata;
slouken@1895
   180
slouken@1895
   181
int SDLCALL
slouken@1895
   182
FilterEvents(void *userdata, SDL_Event * event)
slouken@0
   183
{
slouken@1895
   184
    static int reallyquit = 0;
slouken@0
   185
slouken@1895
   186
    if (old_filterfunc) {
slouken@1895
   187
        old_filterfunc(old_filterdata, event);
slouken@1895
   188
    }
slouken@0
   189
slouken@1895
   190
    switch (event->type) {
slouken@0
   191
slouken@1895
   192
    case SDL_ACTIVEEVENT:
slouken@1895
   193
        /* See what happened */
slouken@1895
   194
        printf("App %s ", event->active.gain ? "gained" : "lost");
slouken@1895
   195
        if (event->active.state & SDL_APPACTIVE)
slouken@1895
   196
            printf("active ");
slouken@1895
   197
        if (event->active.state & SDL_APPINPUTFOCUS)
slouken@1895
   198
            printf("input ");
slouken@1895
   199
        if (event->active.state & SDL_APPMOUSEFOCUS)
slouken@1895
   200
            printf("mouse ");
slouken@1895
   201
        printf("focus\n");
slouken@0
   202
slouken@1895
   203
        /* See if we are iconified or restored */
slouken@1895
   204
        if (event->active.state & SDL_APPACTIVE) {
slouken@1895
   205
            printf("App has been %s\n",
slouken@1895
   206
                   event->active.gain ? "restored" : "iconified");
slouken@1895
   207
        }
slouken@1895
   208
        return (0);
slouken@0
   209
slouken@1895
   210
        /* We want to toggle visibility on buttonpress */
slouken@1895
   211
    case SDL_MOUSEBUTTONDOWN:
slouken@1895
   212
    case SDL_MOUSEBUTTONUP:
slouken@1895
   213
        if (event->button.state == SDL_PRESSED) {
slouken@1895
   214
            visible = !visible;
slouken@1895
   215
            SDL_ShowCursor(visible);
slouken@1895
   216
        }
slouken@1895
   217
        printf("Mouse button %d has been %s\n",
slouken@1895
   218
               event->button.button,
slouken@1895
   219
               (event->button.state == SDL_PRESSED) ? "pressed" : "released");
slouken@1895
   220
        return (0);
slouken@1895
   221
slouken@1895
   222
        /* Show relative mouse motion */
slouken@1895
   223
    case SDL_MOUSEMOTION:
slouken@0
   224
#if 0
slouken@1895
   225
        printf("Mouse motion: {%d,%d} (%d,%d)\n",
slouken@1895
   226
               event->motion.x, event->motion.y,
slouken@1895
   227
               event->motion.xrel, event->motion.yrel);
slouken@0
   228
#endif
slouken@1895
   229
        return (0);
slouken@0
   230
slouken@1895
   231
    case SDL_KEYDOWN:
slouken@1895
   232
        if (event->key.keysym.sym == SDLK_ESCAPE) {
slouken@1895
   233
            HotKey_Quit();
slouken@1895
   234
        }
slouken@1895
   235
        if ((event->key.keysym.sym == SDLK_g) &&
slouken@1895
   236
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1895
   237
            HotKey_ToggleGrab();
slouken@1895
   238
        }
slouken@1895
   239
        if ((event->key.keysym.sym == SDLK_z) &&
slouken@1895
   240
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1895
   241
            HotKey_Iconify();
slouken@1895
   242
        }
slouken@1895
   243
        if ((event->key.keysym.sym == SDLK_RETURN) &&
slouken@1895
   244
            (event->key.keysym.mod & KMOD_ALT)) {
slouken@1895
   245
            HotKey_ToggleFullScreen();
slouken@1895
   246
        }
slouken@1895
   247
        return (0);
slouken@0
   248
slouken@1895
   249
        /* Pass the video resize event through .. */
slouken@1895
   250
    case SDL_VIDEORESIZE:
slouken@1895
   251
        return (1);
slouken@0
   252
slouken@1895
   253
        /* This is important!  Queue it if we want to quit. */
slouken@1895
   254
    case SDL_QUIT:
slouken@1895
   255
        if (!reallyquit) {
slouken@1895
   256
            reallyquit = 1;
slouken@1895
   257
            printf("Quit requested\n");
slouken@1895
   258
            return (0);
slouken@1895
   259
        }
slouken@1895
   260
        printf("Quit demanded\n");
slouken@1895
   261
        return (1);
slouken@0
   262
slouken@1895
   263
        /* This will never happen because events queued directly
slouken@1895
   264
           to the event queue are not filtered.
slouken@1895
   265
         */
slouken@1895
   266
    case SDL_USEREVENT:
slouken@1895
   267
        return (1);
slouken@0
   268
slouken@1895
   269
        /* Drop all other events */
slouken@1895
   270
    default:
slouken@1895
   271
        return (0);
slouken@1895
   272
    }
slouken@0
   273
}
slouken@0
   274
slouken@1895
   275
int
slouken@1895
   276
main(int argc, char *argv[])
slouken@0
   277
{
slouken@1895
   278
    SDL_Event event;
slouken@2060
   279
    const char *title;
slouken@1895
   280
    SDL_Surface *icon;
slouken@1895
   281
    Uint8 *icon_mask;
slouken@1895
   282
    int parsed;
slouken@1895
   283
    int w, h;
slouken@0
   284
slouken@1895
   285
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   286
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   287
        return (1);
slouken@1895
   288
    }
slouken@0
   289
slouken@1895
   290
    /* Check command line arguments */
slouken@1895
   291
    w = 640;
slouken@1895
   292
    h = 480;
slouken@1895
   293
    video_bpp = 8;
slouken@1895
   294
    video_flags = SDL_SWSURFACE;
slouken@1895
   295
    parsed = 1;
slouken@1895
   296
    while (parsed) {
slouken@1895
   297
        if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
slouken@1895
   298
            video_flags |= SDL_FULLSCREEN;
slouken@1895
   299
            argc -= 1;
slouken@1895
   300
            argv += 1;
slouken@1895
   301
        } else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
slouken@1895
   302
            video_flags |= SDL_RESIZABLE;
slouken@1895
   303
            argc -= 1;
slouken@1895
   304
            argv += 1;
slouken@1895
   305
        } else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
slouken@1895
   306
            video_flags |= SDL_NOFRAME;
slouken@1895
   307
            argc -= 1;
slouken@1895
   308
            argv += 1;
slouken@1895
   309
        } else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
slouken@1895
   310
            w = atoi(argv[2]);
slouken@1895
   311
            argc -= 2;
slouken@1895
   312
            argv += 2;
slouken@1895
   313
        } else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
slouken@1895
   314
            h = atoi(argv[2]);
slouken@1895
   315
            argc -= 2;
slouken@1895
   316
            argv += 2;
slouken@1895
   317
        } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
slouken@1895
   318
            video_bpp = atoi(argv[2]);
slouken@1895
   319
            argc -= 2;
slouken@1895
   320
            argv += 2;
slouken@1895
   321
        } else {
slouken@1895
   322
            parsed = 0;
slouken@1895
   323
        }
slouken@1895
   324
    }
slouken@0
   325
slouken@1895
   326
    /* Set the icon -- this must be done before the first mode set */
slouken@1895
   327
    icon = LoadIconSurface("icon.bmp", &icon_mask);
slouken@1895
   328
    if (icon != NULL) {
slouken@1895
   329
        SDL_WM_SetIcon(icon, icon_mask);
slouken@1895
   330
    }
slouken@1895
   331
    if (icon_mask != NULL)
slouken@1895
   332
        free(icon_mask);
slouken@0
   333
slouken@1895
   334
    /* Set the title bar */
slouken@1895
   335
    if (argv[1] == NULL)
slouken@1895
   336
        title = "Testing  1.. 2.. 3...";
slouken@1895
   337
    else
slouken@1895
   338
        title = argv[1];
slouken@1895
   339
    SDL_WM_SetCaption(title, "testwm");
slouken@0
   340
slouken@1895
   341
    /* See if it's really set */
slouken@1895
   342
    SDL_WM_GetCaption(&title, NULL);
slouken@1895
   343
    if (title)
slouken@1895
   344
        printf("Title was set to: %s\n", title);
slouken@1895
   345
    else
slouken@1895
   346
        printf("No window title was set!\n");
slouken@0
   347
slouken@1895
   348
    /* Initialize the display */
slouken@1895
   349
    if (SetVideoMode(w, h) < 0) {
slouken@1895
   350
        quit(1);
slouken@1895
   351
    }
slouken@0
   352
slouken@1895
   353
    /* Set an event filter that discards everything but QUIT */
slouken@1895
   354
    SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
slouken@1895
   355
    SDL_SetEventFilter(FilterEvents, NULL);
slouken@0
   356
slouken@1895
   357
    /* Ignore key up events, they don't even get filtered */
slouken@1895
   358
    SDL_EventState(SDL_KEYUP, SDL_IGNORE);
slouken@0
   359
slouken@1895
   360
    /* Loop, waiting for QUIT */
slouken@1895
   361
    while (SDL_WaitEvent(&event)) {
slouken@1895
   362
        switch (event.type) {
slouken@1895
   363
        case SDL_VIDEORESIZE:
slouken@1895
   364
            printf("Got a resize event: %dx%d\n",
slouken@1895
   365
                   event.resize.w, event.resize.h);
slouken@1895
   366
            SetVideoMode(event.resize.w, event.resize.h);
slouken@1895
   367
            break;
slouken@1895
   368
        case SDL_USEREVENT:
slouken@1895
   369
            printf("Handling internal quit request\n");
slouken@1895
   370
            /* Fall through to the quit handler */
slouken@1895
   371
        case SDL_QUIT:
slouken@1895
   372
            printf("Bye bye..\n");
slouken@1895
   373
            quit(0);
slouken@1895
   374
        default:
slouken@1895
   375
            /* This should never happen */
slouken@1895
   376
            printf("Warning: Event %d wasn't filtered\n", event.type);
slouken@1895
   377
            break;
slouken@1895
   378
        }
slouken@1895
   379
    }
slouken@1895
   380
    printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
slouken@1895
   381
    SDL_Quit();
slouken@1895
   382
    return (255);
slouken@0
   383
}