test/testwm2.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 07 Jul 2019 09:10:56 -0700
changeset 12928 3c4a4b1077cd
parent 12732 0280fd2d02ca
permissions -rw-r--r--
Fixed bug 4710 - audio/alsa: avoid configuring hardware parameters with only a single period

Anthony Pesch

The previous code first configured the period size using snd_pcm_hw_par-
ams_set_period_size_near. Then, it further narrowed the configuration
space by calling snd_pcm_hw_params_set_buffer_size_near using a buffer
size of 2 times the _requested_ period size in order to try and get a
configuration with only 2 periods. If the configured period size was
larger than the requested size, the second call could inadvertently
narrow the configuration space to contain only a single period.

Rather than fixing the call to snd_pcm_hw_params_set_buffer_size_near
to use a size of 2 times the configured period size, the code has been
changed to use snd_pcm_hw_params_set_periods_min in order to more
clearly explain the intent.
slouken@5535
     1
/*
slouken@12503
     2
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@1895
    12
slouken@1933
    13
#include <stdlib.h>
slouken@1933
    14
#include <stdio.h>
slouken@1895
    15
icculus@9278
    16
#ifdef __EMSCRIPTEN__
icculus@9278
    17
#include <emscripten/emscripten.h>
icculus@9278
    18
#endif
icculus@9278
    19
slouken@6785
    20
#include "SDL_test_common.h"
slouken@1895
    21
slouken@6785
    22
static SDLTest_CommonState *state;
icculus@9278
    23
int done;
slouken@1895
    24
icculus@9278
    25
static const char *cursorNames[] = {
slouken@6677
    26
        "arrow",
slouken@6677
    27
        "ibeam",
slouken@6677
    28
        "wait",
slouken@6677
    29
        "crosshair",
slouken@6677
    30
        "waitarrow",
slouken@6677
    31
        "sizeNWSE",
slouken@6677
    32
        "sizeNESW",
slouken@6677
    33
        "sizeWE",
slouken@6677
    34
        "sizeNS",
slouken@6677
    35
        "sizeALL",
slouken@6677
    36
        "NO",
slouken@6677
    37
        "hand",
icculus@9278
    38
};
icculus@9278
    39
int system_cursor = -1;
icculus@9278
    40
SDL_Cursor *cursor = NULL;
slouken@6677
    41
icculus@9278
    42
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@9278
    43
static void
icculus@9278
    44
quit(int rc)
icculus@9278
    45
{
icculus@9278
    46
    SDLTest_CommonQuit(state);
icculus@9278
    47
    exit(rc);
icculus@9278
    48
}
icculus@9278
    49
icculus@9278
    50
void
icculus@9278
    51
loop()
icculus@9278
    52
{
brandon@10193
    53
    int i;
slouken@1895
    54
    SDL_Event event;
slouken@1895
    55
        /* Check for events */
slouken@1895
    56
        while (SDL_PollEvent(&event)) {
slouken@6785
    57
            SDLTest_CommonEvent(state, &event, &done);
slouken@5246
    58
slouken@5246
    59
            if (event.type == SDL_WINDOWEVENT) {
slouken@6788
    60
                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@6788
    61
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@6788
    62
                    if (window) {
aschiffler@7639
    63
                        SDL_Log("Window %d resized to %dx%d\n",
slouken@6788
    64
                            event.window.windowID,
slouken@6788
    65
                            event.window.data1,
slouken@6788
    66
                            event.window.data2);
slouken@6788
    67
                    }
slouken@6788
    68
                }
slouken@5246
    69
                if (event.window.event == SDL_WINDOWEVENT_MOVED) {
slouken@5246
    70
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@5246
    71
                    if (window) {
aschiffler@7639
    72
                        SDL_Log("Window %d moved to %d,%d (display %s)\n",
slouken@5246
    73
                            event.window.windowID,
slouken@5246
    74
                            event.window.data1,
slouken@5246
    75
                            event.window.data2,
slouken@6787
    76
                            SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
slouken@5246
    77
                    }
slouken@5246
    78
                }
slouken@5246
    79
            }
slouken@6677
    80
            if (event.type == SDL_KEYUP) {
slouken@6677
    81
                SDL_bool updateCursor = SDL_FALSE;
slouken@6677
    82
slouken@6677
    83
                if (event.key.keysym.sym == SDLK_LEFT) {
slouken@6677
    84
                    --system_cursor;
slouken@6677
    85
                    if (system_cursor < 0) {
slouken@6677
    86
                        system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
slouken@6677
    87
                    }
slouken@6677
    88
                    updateCursor = SDL_TRUE;
slouken@6677
    89
                } else if (event.key.keysym.sym == SDLK_RIGHT) {
slouken@6677
    90
                    ++system_cursor;
slouken@6677
    91
                    if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
slouken@6677
    92
                        system_cursor = 0;
slouken@6677
    93
                    }
slouken@6677
    94
                    updateCursor = SDL_TRUE;
slouken@6677
    95
                }
slouken@6677
    96
                if (updateCursor) {
slouken@6677
    97
                    SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
slouken@6677
    98
                    SDL_FreeCursor(cursor);
slouken@6677
    99
                    cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
slouken@6677
   100
                    SDL_SetCursor(cursor);
slouken@6677
   101
                }
slouken@6677
   102
            }
slouken@1895
   103
        }
brandon@10193
   104
brandon@10193
   105
        for (i = 0; i < state->num_windows; ++i) {
brandon@10193
   106
            SDL_Renderer *renderer = state->renderers[i];
brandon@10193
   107
            SDL_RenderClear(renderer);
brandon@10193
   108
            SDL_RenderPresent(renderer);
brandon@10193
   109
        }
philipp@9607
   110
#ifdef __EMSCRIPTEN__
philipp@9607
   111
    if (done) {
philipp@9607
   112
        emscripten_cancel_main_loop();
philipp@9607
   113
    }
philipp@9607
   114
#endif
icculus@9278
   115
}
icculus@9278
   116
icculus@9278
   117
int
icculus@9278
   118
main(int argc, char *argv[])
icculus@9278
   119
{
icculus@9278
   120
    int i;
icculus@9278
   121
philipp@9922
   122
    /* Enable standard application logging */
icculus@9278
   123
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
icculus@9278
   124
icculus@9278
   125
    SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
icculus@9278
   126
icculus@9278
   127
    /* Initialize test framework */
icculus@9278
   128
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
icculus@9278
   129
    if (!state) {
icculus@9278
   130
        return 1;
slouken@1895
   131
    }
icculus@9278
   132
icculus@12732
   133
    if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
icculus@12732
   134
        SDLTest_CommonQuit(state);
icculus@12732
   135
        return 1;
icculus@9278
   136
    }
icculus@9278
   137
icculus@12070
   138
    SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
icculus@12070
   139
    SDL_EventState(SDL_DROPTEXT, SDL_ENABLE);
icculus@12070
   140
brandon@10193
   141
    for (i = 0; i < state->num_windows; ++i) {
brandon@10193
   142
        SDL_Renderer *renderer = state->renderers[i];
brandon@10193
   143
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
brandon@10193
   144
        SDL_RenderClear(renderer);
brandon@10193
   145
    }
brandon@10193
   146
 
icculus@9278
   147
    /* Main render loop */
icculus@9278
   148
    done = 0;
icculus@9278
   149
#ifdef __EMSCRIPTEN__
icculus@9278
   150
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   151
#else
icculus@9278
   152
    while (!done) {
icculus@9278
   153
        loop();
icculus@9278
   154
    }
icculus@9278
   155
#endif
slouken@6677
   156
    SDL_FreeCursor(cursor);
slouken@6677
   157
slouken@1895
   158
    quit(0);
gabomdq@7663
   159
    /* keep the compiler happy ... */
slouken@7191
   160
    return(0);
slouken@1895
   161
}
slouken@1895
   162
slouken@1895
   163
/* vi: set ts=4 sw=4 expandtab: */