test/testpower.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 07 Jul 2019 09:10:56 -0700
changeset 12928 3c4a4b1077cd
parent 12503 806492103856
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
*/
icculus@3170
    12
/* Simple test of power subsystem. */
icculus@3170
    13
icculus@3170
    14
#include <stdio.h>
icculus@3170
    15
#include "SDL.h"
icculus@3170
    16
slouken@3186
    17
static void
slouken@3186
    18
report_power(void)
icculus@3170
    19
{
icculus@3170
    20
    int seconds, percent;
icculus@3170
    21
    const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
icculus@3170
    22
    char *statestr = NULL;
icculus@3170
    23
aschiffler@7639
    24
    SDL_Log("SDL-reported power info...\n");
slouken@3186
    25
    switch (state) {
slouken@3186
    26
    case SDL_POWERSTATE_UNKNOWN:
slouken@3186
    27
        statestr = "Unknown";
slouken@3186
    28
        break;
slouken@3186
    29
    case SDL_POWERSTATE_ON_BATTERY:
slouken@3186
    30
        statestr = "On battery";
slouken@3186
    31
        break;
slouken@3186
    32
    case SDL_POWERSTATE_NO_BATTERY:
slouken@3186
    33
        statestr = "No battery";
slouken@3186
    34
        break;
slouken@3186
    35
    case SDL_POWERSTATE_CHARGING:
slouken@3186
    36
        statestr = "Charging";
slouken@3186
    37
        break;
slouken@3186
    38
    case SDL_POWERSTATE_CHARGED:
slouken@3186
    39
        statestr = "Charged";
slouken@3186
    40
        break;
slouken@3186
    41
    default:
slouken@3186
    42
        statestr = "!!API ERROR!!";
slouken@3186
    43
        break;
icculus@3170
    44
    }
icculus@3170
    45
aschiffler@7639
    46
    SDL_Log("State: %s\n", statestr);
icculus@3170
    47
icculus@3170
    48
    if (percent == -1) {
aschiffler@7639
    49
        SDL_Log("Percent left: unknown\n");
icculus@3170
    50
    } else {
aschiffler@7639
    51
        SDL_Log("Percent left: %d%%\n", percent);
icculus@3170
    52
    }
icculus@3170
    53
icculus@3170
    54
    if (seconds == -1) {
aschiffler@7639
    55
        SDL_Log("Time left: unknown\n");
icculus@3170
    56
    } else {
aschiffler@7639
    57
        SDL_Log("Time left: %d minutes, %d seconds\n", (int) (seconds / 60),
slouken@3186
    58
               (int) (seconds % 60));
icculus@3170
    59
    }
icculus@3170
    60
}
icculus@3170
    61
icculus@3170
    62
slouken@3186
    63
int
slouken@3186
    64
main(int argc, char *argv[])
icculus@3170
    65
{
aschiffler@7639
    66
    /* Enable standard application logging */
aschiffler@7639
    67
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    68
slouken@7307
    69
    if (SDL_Init(0) == -1) {
aschiffler@7639
    70
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
icculus@3170
    71
        return 1;
icculus@3170
    72
    }
icculus@3170
    73
icculus@3170
    74
    report_power();
icculus@3170
    75
icculus@3170
    76
    SDL_Quit();
icculus@3170
    77
    return 0;
icculus@3170
    78
}
icculus@3170
    79
icculus@3170
    80
/* end of testpower.c ... */