test/testsensor.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.
     1 /*
     2   Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple test of the SDL sensor code */
    14 
    15 #include "SDL.h"
    16 
    17 static const char *GetSensorTypeString(SDL_SensorType type)
    18 {
    19     static char unknown_type[64];
    20 
    21     switch (type)
    22     {
    23     case SDL_SENSOR_INVALID:
    24         return "SDL_SENSOR_INVALID";
    25     case SDL_SENSOR_UNKNOWN:
    26         return "SDL_SENSOR_UNKNOWN";
    27     case SDL_SENSOR_ACCEL:
    28         return "SDL_SENSOR_ACCEL";
    29     case SDL_SENSOR_GYRO:
    30         return "SDL_SENSOR_GYRO";
    31     default:
    32         SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type);
    33         return unknown_type;
    34     }
    35 }
    36 
    37 static void HandleSensorEvent(SDL_SensorEvent *event)
    38 {
    39     SDL_Sensor *sensor = SDL_SensorFromInstanceID(event->which);
    40     if (!sensor) {
    41         SDL_Log("Couldn't get sensor for sensor event\n");
    42         return;
    43     }
    44 
    45     switch (SDL_SensorGetType(sensor)) {
    46     case SDL_SENSOR_ACCEL:
    47         SDL_Log("Accelerometer update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
    48         break;
    49     case SDL_SENSOR_GYRO:
    50         SDL_Log("Gyro update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
    51         break;
    52     default:
    53         SDL_Log("Sensor update for sensor type %s\n", GetSensorTypeString(SDL_SensorGetType(sensor)));
    54         break;
    55     }
    56 }
    57 
    58 int
    59 main(int argc, char **argv)
    60 {
    61     int i;
    62     int num_sensors, num_opened;
    63 
    64     /* Load the SDL library */
    65     if (SDL_Init(SDL_INIT_SENSOR) < 0) {
    66         SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
    67         return (1);
    68     }
    69 
    70     num_sensors = SDL_NumSensors();
    71     num_opened = 0;
    72 
    73     SDL_Log("There are %d sensors available\n", num_sensors);
    74     for (i = 0; i < num_sensors; ++i) {
    75         SDL_Log("Sensor %d: %s, type %s, platform type %d\n",
    76             SDL_SensorGetDeviceInstanceID(i),
    77             SDL_SensorGetDeviceName(i),
    78             GetSensorTypeString(SDL_SensorGetDeviceType(i)),
    79             SDL_SensorGetDeviceNonPortableType(i));
    80 
    81         if (SDL_SensorGetDeviceType(i) != SDL_SENSOR_UNKNOWN) {
    82             SDL_Sensor *sensor = SDL_SensorOpen(i);
    83             if (sensor == NULL) {
    84                 SDL_Log("Couldn't open sensor %d: %s\n", SDL_SensorGetDeviceInstanceID(i), SDL_GetError());
    85             } else {
    86                 ++num_opened;
    87             }
    88         }
    89     }
    90     SDL_Log("Opened %d sensors\n", num_opened);
    91 
    92     if (num_opened > 0) {
    93         SDL_bool done = SDL_FALSE;
    94         SDL_Event event;
    95 
    96         SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
    97         while (!done) {
    98             while (SDL_PollEvent(&event) > 0) {
    99                 switch (event.type) {
   100                 case SDL_SENSORUPDATE:
   101                     HandleSensorEvent(&event.sensor);
   102                     break;
   103                 case SDL_MOUSEBUTTONUP:
   104                 case SDL_KEYUP:
   105                 case SDL_QUIT:
   106                     done = SDL_TRUE;
   107                     break;
   108                 default:
   109                     break;
   110                 }
   111             }
   112         }
   113     }
   114 
   115     SDL_Quit();
   116     return (0);
   117 }