test/testwm.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 06 Jul 2006 07:17:11 +0000
branchSDL-1.3
changeset 1724 6c63fc2bd986
parent 1686 8d7fecceb9ef
child 1731 875c3cf1a12c
permissions -rw-r--r--
Proof of concept done - Win32 GDI implementation mostly complete.
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@1662
    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@1662
    17
static void
slouken@1668
    18
quit(int rc)
icculus@1151
    19
{
slouken@1668
    20
    SDL_Quit();
slouken@1668
    21
    exit(rc);
icculus@1151
    22
}
icculus@1151
    23
slouken@1662
    24
int
slouken@1668
    25
SetVideoMode(int w, int h)
slouken@826
    26
{
slouken@1662
    27
    SDL_Surface *screen;
slouken@1662
    28
    int i;
slouken@1662
    29
    Uint8 *buffer;
slouken@1662
    30
    SDL_Color palette[256];
slouken@826
    31
slouken@1668
    32
    screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
slouken@1662
    33
    if (screen == NULL) {
slouken@1668
    34
        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
slouken@1668
    35
                w, h, video_bpp, SDL_GetError());
slouken@1662
    36
        return (-1);
slouken@1662
    37
    }
slouken@1668
    38
    printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
slouken@1668
    39
           "fullscreen" : "windowed");
slouken@826
    40
slouken@1662
    41
    /* Set the surface pixels and refresh! */
slouken@1662
    42
    for (i = 0; i < 256; ++i) {
slouken@1662
    43
        palette[i].r = 255 - i;
slouken@1662
    44
        palette[i].g = 255 - i;
slouken@1662
    45
        palette[i].b = 255 - i;
slouken@1662
    46
    }
slouken@1668
    47
    SDL_SetColors(screen, palette, 0, 256);
slouken@1668
    48
    if (SDL_LockSurface(screen) < 0) {
slouken@1668
    49
        fprintf(stderr, "Couldn't lock display surface: %s\n",
slouken@1668
    50
                SDL_GetError());
slouken@1662
    51
        return (-1);
slouken@1662
    52
    }
slouken@1662
    53
    buffer = (Uint8 *) screen->pixels;
slouken@1662
    54
    for (i = 0; i < screen->h; ++i) {
slouken@1668
    55
        memset(buffer, (i * 255) / screen->h,
slouken@1668
    56
               screen->w * screen->format->BytesPerPixel);
slouken@1662
    57
        buffer += screen->pitch;
slouken@1662
    58
    }
slouken@1668
    59
    SDL_UnlockSurface(screen);
slouken@1668
    60
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@826
    61
slouken@1662
    62
    return (0);
slouken@826
    63
}
slouken@826
    64
slouken@1662
    65
SDL_Surface *
slouken@1668
    66
LoadIconSurface(char *file, Uint8 ** maskp)
slouken@0
    67
{
slouken@1662
    68
    SDL_Surface *icon;
slouken@1662
    69
    Uint8 *pixels;
slouken@1662
    70
    Uint8 *mask;
slouken@1662
    71
    int mlen, i, j;
slouken@1662
    72
slouken@1662
    73
    *maskp = NULL;
slouken@0
    74
slouken@1662
    75
    /* Load the icon surface */
slouken@1668
    76
    icon = SDL_LoadBMP(file);
slouken@1662
    77
    if (icon == NULL) {
slouken@1668
    78
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
slouken@1662
    79
        return (NULL);
slouken@1662
    80
    }
slouken@0
    81
slouken@1662
    82
    /* Check width and height 
slouken@1662
    83
       if ( (icon->w%8) != 0 ) {
slouken@1662
    84
       fprintf(stderr, "Icon width must be a multiple of 8!\n");
slouken@1662
    85
       SDL_FreeSurface(icon);
slouken@1662
    86
       return(NULL);
slouken@1662
    87
       }
slouken@1662
    88
     */
slouken@1662
    89
slouken@0
    90
slouken@1662
    91
    if (icon->format->palette == NULL) {
slouken@1668
    92
        fprintf(stderr, "Icon must have a palette!\n");
slouken@1668
    93
        SDL_FreeSurface(icon);
slouken@1662
    94
        return (NULL);
slouken@1662
    95
    }
slouken@1662
    96
slouken@1662
    97
    /* Set the colorkey */
slouken@1668
    98
    SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
slouken@0
    99
slouken@1662
   100
    /* Create the mask */
slouken@1662
   101
    pixels = (Uint8 *) icon->pixels;
slouken@1668
   102
    printf("Transparent pixel: (%d,%d,%d)\n",
slouken@1668
   103
           icon->format->palette->colors[*pixels].r,
slouken@1668
   104
           icon->format->palette->colors[*pixels].g,
slouken@1668
   105
           icon->format->palette->colors[*pixels].b);
slouken@1662
   106
    mlen = (icon->w * icon->h + 7) / 8;
slouken@1668
   107
    mask = (Uint8 *) malloc(mlen);
slouken@1662
   108
    if (mask == NULL) {
slouken@1668
   109
        fprintf(stderr, "Out of memory!\n");
slouken@1668
   110
        SDL_FreeSurface(icon);
slouken@1662
   111
        return (NULL);
slouken@1662
   112
    }
slouken@1668
   113
    memset(mask, 0, mlen);
slouken@1662
   114
    for (i = 0; i < icon->h; i++)
slouken@1662
   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@1662
   118
            if (pixels[pindex] != *pixels)
slouken@1662
   119
                mask[mindex >> 3] |= 1 << (7 - (mindex & 7));
slouken@591
   120
        }
slouken@1662
   121
    *maskp = mask;
slouken@1662
   122
    return (icon);
slouken@0
   123
}
slouken@0
   124
slouken@1662
   125
void
slouken@1668
   126
HotKey_ToggleFullScreen(void)
slouken@0
   127
{
slouken@1662
   128
    SDL_Surface *screen;
slouken@0
   129
slouken@1668
   130
    screen = SDL_GetVideoSurface();
slouken@1668
   131
    if (SDL_WM_ToggleFullScreen(screen)) {
slouken@1668
   132
        printf("Toggled fullscreen mode - now %s\n",
slouken@1668
   133
               (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
slouken@1662
   134
    } else {
slouken@1668
   135
        printf("Unable to toggle fullscreen mode\n");
slouken@1662
   136
        video_flags ^= SDL_FULLSCREEN;
slouken@1668
   137
        SetVideoMode(screen->w, screen->h);
slouken@1662
   138
    }
slouken@0
   139
}
slouken@0
   140
slouken@1662
   141
void
slouken@1668
   142
HotKey_ToggleGrab(void)
slouken@0
   143
{
slouken@1662
   144
    SDL_GrabMode mode;
slouken@0
   145
slouken@1668
   146
    printf("Ctrl-G: toggling input grab!\n");
slouken@1668
   147
    mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
slouken@1662
   148
    if (mode == SDL_GRAB_ON) {
slouken@1668
   149
        printf("Grab was on\n");
slouken@1662
   150
    } else {
slouken@1668
   151
        printf("Grab was off\n");
slouken@1662
   152
    }
slouken@1668
   153
    mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
slouken@1662
   154
    if (mode == SDL_GRAB_ON) {
slouken@1668
   155
        printf("Grab is now on\n");
slouken@1662
   156
    } else {
slouken@1668
   157
        printf("Grab is now off\n");
slouken@1662
   158
    }
slouken@0
   159
}
slouken@0
   160
slouken@1662
   161
void
slouken@1668
   162
HotKey_Iconify(void)
slouken@0
   163
{
slouken@1668
   164
    printf("Ctrl-Z: iconifying window!\n");
slouken@1668
   165
    SDL_WM_IconifyWindow();
slouken@0
   166
}
slouken@0
   167
slouken@1662
   168
void
slouken@1668
   169
HotKey_Quit(void)
slouken@0
   170
{
slouken@1662
   171
    SDL_Event event;
slouken@0
   172
slouken@1668
   173
    printf("Posting internal quit request\n");
slouken@1662
   174
    event.type = SDL_USEREVENT;
slouken@1668
   175
    SDL_PushEvent(&event);
slouken@0
   176
}
slouken@0
   177
slouken@1662
   178
int SDLCALL
slouken@1724
   179
FilterEvents(void *userdata, SDL_Event * event)
slouken@0
   180
{
slouken@1662
   181
    static int reallyquit = 0;
slouken@0
   182
slouken@1662
   183
    switch (event->type) {
slouken@0
   184
slouken@1662
   185
    case SDL_ACTIVEEVENT:
slouken@1662
   186
        /* See what happened */
slouken@1668
   187
        printf("App %s ", event->active.gain ? "gained" : "lost");
slouken@1662
   188
        if (event->active.state & SDL_APPACTIVE)
slouken@1668
   189
            printf("active ");
slouken@1662
   190
        if (event->active.state & SDL_APPINPUTFOCUS)
slouken@1668
   191
            printf("input ");
slouken@1662
   192
        if (event->active.state & SDL_APPMOUSEFOCUS)
slouken@1668
   193
            printf("mouse ");
slouken@1668
   194
        printf("focus\n");
slouken@0
   195
slouken@1662
   196
        /* See if we are iconified or restored */
slouken@1662
   197
        if (event->active.state & SDL_APPACTIVE) {
slouken@1668
   198
            printf("App has been %s\n",
slouken@1668
   199
                   event->active.gain ? "restored" : "iconified");
slouken@1662
   200
        }
slouken@1662
   201
        return (0);
slouken@0
   202
slouken@1662
   203
        /* We want to toggle visibility on buttonpress */
slouken@1662
   204
    case SDL_MOUSEBUTTONDOWN:
slouken@1662
   205
    case SDL_MOUSEBUTTONUP:
slouken@1662
   206
        if (event->button.state == SDL_PRESSED) {
slouken@1662
   207
            visible = !visible;
slouken@1668
   208
            SDL_ShowCursor(visible);
slouken@1662
   209
        }
slouken@1668
   210
        printf("Mouse button %d has been %s\n",
slouken@1668
   211
               event->button.button,
slouken@1668
   212
               (event->button.state == SDL_PRESSED) ? "pressed" : "released");
slouken@1662
   213
        return (0);
slouken@0
   214
slouken@1662
   215
        /* Show relative mouse motion */
slouken@1662
   216
    case SDL_MOUSEMOTION:
slouken@0
   217
#if 0
slouken@1668
   218
        printf("Mouse motion: {%d,%d} (%d,%d)\n",
slouken@1668
   219
               event->motion.x, event->motion.y,
slouken@1668
   220
               event->motion.xrel, event->motion.yrel);
slouken@0
   221
#endif
slouken@1662
   222
        return (0);
slouken@0
   223
slouken@1662
   224
    case SDL_KEYDOWN:
slouken@1662
   225
        if (event->key.keysym.sym == SDLK_ESCAPE) {
slouken@1668
   226
            HotKey_Quit();
slouken@1662
   227
        }
slouken@1662
   228
        if ((event->key.keysym.sym == SDLK_g) &&
slouken@1662
   229
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1668
   230
            HotKey_ToggleGrab();
slouken@1662
   231
        }
slouken@1662
   232
        if ((event->key.keysym.sym == SDLK_z) &&
slouken@1662
   233
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1668
   234
            HotKey_Iconify();
slouken@1662
   235
        }
slouken@1662
   236
        if ((event->key.keysym.sym == SDLK_RETURN) &&
slouken@1662
   237
            (event->key.keysym.mod & KMOD_ALT)) {
slouken@1668
   238
            HotKey_ToggleFullScreen();
slouken@1662
   239
        }
slouken@1662
   240
        return (0);
slouken@0
   241
slouken@1662
   242
        /* Pass the video resize event through .. */
slouken@1662
   243
    case SDL_VIDEORESIZE:
slouken@1662
   244
        return (1);
slouken@0
   245
slouken@1662
   246
        /* This is important!  Queue it if we want to quit. */
slouken@1662
   247
    case SDL_QUIT:
slouken@1662
   248
        if (!reallyquit) {
slouken@1662
   249
            reallyquit = 1;
slouken@1668
   250
            printf("Quit requested\n");
slouken@1662
   251
            return (0);
slouken@1662
   252
        }
slouken@1668
   253
        printf("Quit demanded\n");
slouken@1662
   254
        return (1);
slouken@0
   255
slouken@1662
   256
        /* This will never happen because events queued directly
slouken@1662
   257
           to the event queue are not filtered.
slouken@1662
   258
         */
slouken@1662
   259
    case SDL_USEREVENT:
slouken@1662
   260
        return (1);
slouken@0
   261
slouken@1662
   262
        /* Drop all other events */
slouken@1662
   263
    default:
slouken@1662
   264
        return (0);
slouken@1662
   265
    }
slouken@0
   266
}
slouken@0
   267
slouken@1662
   268
int
slouken@1668
   269
main(int argc, char *argv[])
slouken@0
   270
{
slouken@1662
   271
    SDL_Event event;
slouken@1662
   272
    char *title;
slouken@1662
   273
    SDL_Surface *icon;
slouken@1662
   274
    Uint8 *icon_mask;
slouken@1662
   275
    int parsed;
slouken@1662
   276
    int w, h;
slouken@0
   277
slouken@1668
   278
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1668
   279
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1662
   280
        return (1);
slouken@1662
   281
    }
slouken@0
   282
slouken@1662
   283
    /* Check command line arguments */
slouken@1662
   284
    w = 640;
slouken@1662
   285
    h = 480;
slouken@1662
   286
    video_bpp = 8;
slouken@1662
   287
    video_flags = SDL_SWSURFACE;
slouken@1662
   288
    parsed = 1;
slouken@1662
   289
    while (parsed) {
slouken@1668
   290
        if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
slouken@1662
   291
            video_flags |= SDL_FULLSCREEN;
slouken@1662
   292
            argc -= 1;
slouken@1662
   293
            argv += 1;
slouken@1668
   294
        } else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
slouken@1662
   295
            video_flags |= SDL_RESIZABLE;
slouken@1662
   296
            argc -= 1;
slouken@1662
   297
            argv += 1;
slouken@1668
   298
        } else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
slouken@1662
   299
            video_flags |= SDL_NOFRAME;
slouken@1662
   300
            argc -= 1;
slouken@1662
   301
            argv += 1;
slouken@1668
   302
        } else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
slouken@1668
   303
            w = atoi(argv[2]);
slouken@1662
   304
            argc -= 2;
slouken@1662
   305
            argv += 2;
slouken@1668
   306
        } else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
slouken@1668
   307
            h = atoi(argv[2]);
slouken@1662
   308
            argc -= 2;
slouken@1662
   309
            argv += 2;
slouken@1668
   310
        } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
slouken@1668
   311
            video_bpp = atoi(argv[2]);
slouken@1662
   312
            argc -= 2;
slouken@1662
   313
            argv += 2;
slouken@1662
   314
        } else {
slouken@1662
   315
            parsed = 0;
slouken@1662
   316
        }
slouken@1662
   317
    }
slouken@0
   318
slouken@1662
   319
    /* Set the icon -- this must be done before the first mode set */
slouken@1668
   320
    icon = LoadIconSurface("icon.bmp", &icon_mask);
slouken@1662
   321
    if (icon != NULL) {
slouken@1668
   322
        SDL_WM_SetIcon(icon, icon_mask);
slouken@1662
   323
    }
slouken@1662
   324
    if (icon_mask != NULL)
slouken@1668
   325
        free(icon_mask);
slouken@0
   326
slouken@1662
   327
    /* Set the title bar */
slouken@1662
   328
    if (argv[1] == NULL)
slouken@1662
   329
        title = "Testing  1.. 2.. 3...";
slouken@1662
   330
    else
slouken@1662
   331
        title = argv[1];
slouken@1668
   332
    SDL_WM_SetCaption(title, "testwm");
slouken@0
   333
slouken@1662
   334
    /* See if it's really set */
slouken@1668
   335
    SDL_WM_GetCaption(&title, NULL);
slouken@1662
   336
    if (title)
slouken@1668
   337
        printf("Title was set to: %s\n", title);
slouken@1662
   338
    else
slouken@1668
   339
        printf("No window title was set!\n");
slouken@0
   340
slouken@1662
   341
    /* Initialize the display */
slouken@1668
   342
    if (SetVideoMode(w, h) < 0) {
slouken@1668
   343
        quit(1);
slouken@1662
   344
    }
slouken@0
   345
slouken@1662
   346
    /* Set an event filter that discards everything but QUIT */
slouken@1724
   347
    SDL_SetEventFilter(FilterEvents, NULL);
slouken@0
   348
slouken@1662
   349
    /* Ignore key up events, they don't even get filtered */
slouken@1668
   350
    SDL_EventState(SDL_KEYUP, SDL_IGNORE);
slouken@0
   351
slouken@1662
   352
    /* Loop, waiting for QUIT */
slouken@1668
   353
    while (SDL_WaitEvent(&event)) {
slouken@1662
   354
        switch (event.type) {
slouken@1662
   355
        case SDL_VIDEORESIZE:
slouken@1668
   356
            printf("Got a resize event: %dx%d\n",
slouken@1668
   357
                   event.resize.w, event.resize.h);
slouken@1668
   358
            SetVideoMode(event.resize.w, event.resize.h);
slouken@1662
   359
            break;
slouken@1662
   360
        case SDL_USEREVENT:
slouken@1668
   361
            printf("Handling internal quit request\n");
slouken@1662
   362
            /* Fall through to the quit handler */
slouken@1662
   363
        case SDL_QUIT:
slouken@1668
   364
            printf("Bye bye..\n");
slouken@1668
   365
            quit(0);
slouken@1662
   366
        default:
slouken@1662
   367
            /* This should never happen */
slouken@1668
   368
            printf("Warning: Event %d wasn't filtered\n", event.type);
slouken@1662
   369
            break;
slouken@1662
   370
        }
slouken@1662
   371
    }
slouken@1668
   372
    printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
slouken@1668
   373
    SDL_Quit();
slouken@1662
   374
    return (255);
slouken@0
   375
}