src/audio/qsa/SDL_qsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 14 Jul 2013 18:17:28 -0700
changeset 7523 9e9ab1dc3811
parent 7038 7f22b9ba218f
child 7719 31b5f9ff36ca
permissions -rw-r--r--
Fixed bug 1919 - Window icon disappears as soon as a renderer is created

Sebastian

Setting a window icon works just fine until a renderer is added to the window.
After adding the renderer the icon disappears.

Reproduce by:
- Take the example code from the wiki: http://wiki.libsdl.org/moin.fcg/SDL_SetWindowIcon

- Add the following two lines after SDL_FreeSurface(surface);
SDL_Delay(1000);
SDL_Renderer* ren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

-compile and run

You will see the window icon correctly at first. After the Delay the Icon will disappear.
slouken@3099
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@3099
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@3099
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@3099
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@3099
    20
*/
slouken@3099
    21
slouken@3099
    22
#include "SDL_config.h"
slouken@3099
    23
slouken@6044
    24
#if SDL_AUDIO_DRIVER_QSA
slouken@6044
    25
slouken@3099
    26
#include <errno.h>
slouken@3099
    27
#include <unistd.h>
slouken@3099
    28
#include <fcntl.h>
slouken@3099
    29
#include <signal.h>
slouken@3099
    30
#include <sys/types.h>
slouken@3099
    31
#include <sys/time.h>
slouken@3099
    32
#include <sched.h>
slouken@3099
    33
#include <sys/select.h>
slouken@3099
    34
#include <sys/neutrino.h>
slouken@3099
    35
#include <sys/asoundlib.h>
slouken@3099
    36
slouken@3099
    37
#include "SDL_timer.h"
slouken@3099
    38
#include "SDL_audio.h"
slouken@3099
    39
#include "../SDL_audiomem.h"
slouken@3099
    40
#include "../SDL_audio_c.h"
slouken@3099
    41
#include "SDL_qsa_audio.h"
slouken@3099
    42
slouken@3099
    43
/* default channel communication parameters */
slouken@3099
    44
#define DEFAULT_CPARAMS_RATE   44100
slouken@3099
    45
#define DEFAULT_CPARAMS_VOICES 1
slouken@3099
    46
slouken@3099
    47
#define DEFAULT_CPARAMS_FRAG_SIZE 4096
slouken@3099
    48
#define DEFAULT_CPARAMS_FRAGS_MIN 1
slouken@3099
    49
#define DEFAULT_CPARAMS_FRAGS_MAX 1
slouken@3099
    50
slouken@3099
    51
#define QSA_NO_WORKAROUNDS  0x00000000
slouken@3099
    52
#define QSA_MMAP_WORKAROUND 0x00000001
slouken@3099
    53
slouken@3099
    54
struct BuggyCards
slouken@3099
    55
{
slouken@3139
    56
    char *cardname;
slouken@3099
    57
    unsigned long bugtype;
slouken@3099
    58
};
slouken@3099
    59
slouken@3099
    60
#define QSA_WA_CARDS             3
slouken@3099
    61
#define QSA_MAX_CARD_NAME_LENGTH 33
slouken@3099
    62
slouken@3139
    63
struct BuggyCards buggycards[QSA_WA_CARDS] = {
slouken@3139
    64
    {"Sound Blaster Live!", QSA_MMAP_WORKAROUND},
slouken@3139
    65
    {"Vortex 8820", QSA_MMAP_WORKAROUND},
slouken@3139
    66
    {"Vortex 8830", QSA_MMAP_WORKAROUND},
slouken@3099
    67
};
slouken@3099
    68
slouken@3099
    69
/* List of found devices */
slouken@3099
    70
#define QSA_MAX_DEVICES       32
slouken@3139
    71
#define QSA_MAX_NAME_LENGTH   81+16     /* Hardcoded in QSA, can't be changed */
slouken@3099
    72
slouken@3099
    73
typedef struct _QSA_Device
slouken@3099
    74
{
slouken@3139
    75
    char name[QSA_MAX_NAME_LENGTH];     /* Long audio device name for SDL  */
slouken@3139
    76
    int cardno;
slouken@3139
    77
    int deviceno;
slouken@3099
    78
} QSA_Device;
slouken@3099
    79
slouken@3099
    80
QSA_Device qsa_playback_device[QSA_MAX_DEVICES];
slouken@3139
    81
uint32_t qsa_playback_devices;
slouken@3099
    82
slouken@3099
    83
QSA_Device qsa_capture_device[QSA_MAX_DEVICES];
slouken@3139
    84
uint32_t qsa_capture_devices;
slouken@3099
    85
icculus@7038
    86
static inline int
slouken@3139
    87
QSA_SetError(const char *fn, int status)
slouken@3099
    88
{
icculus@7038
    89
    return SDL_SetError("QSA: %s() failed: %s", fn, snd_strerror(status));
slouken@3099
    90
}
slouken@3099
    91
slouken@3099
    92
/* card names check to apply the workarounds */
slouken@3139
    93
static int
slouken@3139
    94
QSA_CheckBuggyCards(_THIS, unsigned long checkfor)
slouken@3099
    95
{
slouken@3139
    96
    char scardname[QSA_MAX_CARD_NAME_LENGTH];
slouken@3139
    97
    int it;
slouken@3099
    98
slouken@3139
    99
    if (snd_card_get_name
slouken@3139
   100
        (this->hidden->cardno, scardname, QSA_MAX_CARD_NAME_LENGTH - 1) < 0) {
slouken@3139
   101
        return 0;
slouken@3139
   102
    }
slouken@3099
   103
slouken@3139
   104
    for (it = 0; it < QSA_WA_CARDS; it++) {
slouken@3139
   105
        if (SDL_strcmp(buggycards[it].cardname, scardname) == 0) {
slouken@3139
   106
            if (buggycards[it].bugtype == checkfor) {
slouken@3139
   107
                return 1;
slouken@3139
   108
            }
slouken@3139
   109
        }
slouken@3139
   110
    }
slouken@3139
   111
slouken@3139
   112
    return 0;
slouken@3099
   113
}
slouken@3099
   114
icculus@5590
   115
/* !!! FIXME: does this need to be here? Does the SDL version not work? */
slouken@3139
   116
static void
slouken@3139
   117
QSA_ThreadInit(_THIS)
slouken@3099
   118
{
slouken@3139
   119
    struct sched_param param;
slouken@3139
   120
    int status;
slouken@3099
   121
slouken@3139
   122
    /* Increase default 10 priority to 25 to avoid jerky sound */
slouken@3139
   123
    status = SchedGet(0, 0, &param);
slouken@3139
   124
    param.sched_priority = param.sched_curpriority + 15;
slouken@3139
   125
    status = SchedSet(0, 0, SCHED_NOCHANGE, &param);
slouken@3099
   126
}
slouken@3099
   127
slouken@3099
   128
/* PCM channel parameters initialize function */
slouken@3139
   129
static void
slouken@3139
   130
QSA_InitAudioParams(snd_pcm_channel_params_t * cpars)
slouken@3099
   131
{
slouken@3139
   132
    SDL_memset(cpars, 0, sizeof(snd_pcm_channel_params_t));
slouken@3099
   133
slouken@3139
   134
    cpars->channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@3139
   135
    cpars->mode = SND_PCM_MODE_BLOCK;
slouken@3139
   136
    cpars->start_mode = SND_PCM_START_DATA;
slouken@3139
   137
    cpars->stop_mode = SND_PCM_STOP_STOP;
slouken@3139
   138
    cpars->format.format = SND_PCM_SFMT_S16_LE;
slouken@3139
   139
    cpars->format.interleave = 1;
slouken@3139
   140
    cpars->format.rate = DEFAULT_CPARAMS_RATE;
slouken@3139
   141
    cpars->format.voices = DEFAULT_CPARAMS_VOICES;
slouken@3139
   142
    cpars->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
slouken@3139
   143
    cpars->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
slouken@3139
   144
    cpars->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
slouken@3099
   145
}
slouken@3099
   146
slouken@3099
   147
/* This function waits until it is possible to write a full sound buffer */
slouken@3139
   148
static void
slouken@3139
   149
QSA_WaitDevice(_THIS)
slouken@3099
   150
{
slouken@3139
   151
    fd_set wfds;
slouken@3139
   152
    fd_set rfds;
slouken@3139
   153
    int selectret;
slouken@3139
   154
    struct timeval timeout;
slouken@3099
   155
slouken@3139
   156
    if (!this->hidden->iscapture) {
slouken@3139
   157
        FD_ZERO(&wfds);
slouken@3139
   158
        FD_SET(this->hidden->audio_fd, &wfds);
slouken@3139
   159
    } else {
slouken@3139
   160
        FD_ZERO(&rfds);
slouken@3139
   161
        FD_SET(this->hidden->audio_fd, &rfds);
slouken@3139
   162
    }
slouken@3099
   163
slouken@3139
   164
    do {
slouken@3139
   165
        /* Setup timeout for playing one fragment equal to 2 seconds          */
slouken@3139
   166
        /* If timeout occured than something wrong with hardware or driver    */
slouken@3139
   167
        /* For example, Vortex 8820 audio driver stucks on second DAC because */
slouken@3139
   168
        /* it doesn't exist !                                                 */
slouken@3139
   169
        timeout.tv_sec = 2;
slouken@3139
   170
        timeout.tv_usec = 0;
slouken@3139
   171
        this->hidden->timeout_on_wait = 0;
slouken@3099
   172
slouken@3139
   173
        if (!this->hidden->iscapture) {
slouken@3139
   174
            selectret =
slouken@3139
   175
                select(this->hidden->audio_fd + 1, NULL, &wfds, NULL,
slouken@3139
   176
                       &timeout);
slouken@3139
   177
        } else {
slouken@3139
   178
            selectret =
slouken@3139
   179
                select(this->hidden->audio_fd + 1, &rfds, NULL, NULL,
slouken@3139
   180
                       &timeout);
slouken@3139
   181
        }
slouken@3139
   182
slouken@3139
   183
        switch (selectret) {
slouken@3139
   184
        case -1:
slouken@3139
   185
            {
icculus@7038
   186
                SDL_SetError("QSA: select() failed: %s", strerror(errno));
slouken@3139
   187
                return;
slouken@3139
   188
            }
slouken@3139
   189
            break;
slouken@3139
   190
        case 0:
slouken@3139
   191
            {
icculus@7038
   192
                SDL_SetError("QSA: timeout on buffer waiting occured");
slouken@3139
   193
                this->hidden->timeout_on_wait = 1;
slouken@3139
   194
                return;
slouken@3139
   195
            }
slouken@3139
   196
            break;
slouken@3139
   197
        default:
slouken@3139
   198
            {
slouken@3139
   199
                if (!this->hidden->iscapture) {
slouken@3139
   200
                    if (FD_ISSET(this->hidden->audio_fd, &wfds)) {
slouken@3139
   201
                        return;
slouken@3099
   202
                    }
slouken@3139
   203
                } else {
slouken@3139
   204
                    if (FD_ISSET(this->hidden->audio_fd, &rfds)) {
slouken@3139
   205
                        return;
slouken@3099
   206
                    }
slouken@3139
   207
                }
slouken@3139
   208
            }
slouken@3139
   209
            break;
slouken@3139
   210
        }
slouken@3139
   211
    } while (1);
slouken@3099
   212
}
slouken@3099
   213
slouken@3139
   214
static void
slouken@3139
   215
QSA_PlayDevice(_THIS)
slouken@3099
   216
{
slouken@3139
   217
    snd_pcm_channel_status_t cstatus;
slouken@3139
   218
    int written;
slouken@3139
   219
    int status;
slouken@3139
   220
    int towrite;
slouken@3139
   221
    void *pcmbuffer;
slouken@3099
   222
slouken@3139
   223
    if ((!this->enabled) || (!this->hidden)) {
slouken@3139
   224
        return;
slouken@3139
   225
    }
slouken@3099
   226
slouken@3139
   227
    towrite = this->spec.size;
slouken@3139
   228
    pcmbuffer = this->hidden->pcm_buf;
slouken@3099
   229
slouken@3139
   230
    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
slouken@3139
   231
    do {
slouken@3139
   232
        written =
slouken@3139
   233
            snd_pcm_plugin_write(this->hidden->audio_handle, pcmbuffer,
slouken@3139
   234
                                 towrite);
slouken@3139
   235
        if (written != towrite) {
slouken@3139
   236
            /* Check if samples playback got stuck somewhere in hardware or in */
slouken@3139
   237
            /* the audio device driver */
slouken@3139
   238
            if ((errno == EAGAIN) && (written == 0)) {
slouken@3139
   239
                if (this->hidden->timeout_on_wait != 0) {
icculus@7038
   240
                    SDL_SetError("QSA: buffer playback timeout");
slouken@3139
   241
                    return;
slouken@3139
   242
                }
slouken@3099
   243
            }
slouken@3099
   244
slouken@3139
   245
            /* Check for errors or conditions */
slouken@3139
   246
            if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
slouken@3139
   247
                /* Let a little CPU time go by and try to write again */
slouken@3139
   248
                SDL_Delay(1);
slouken@3099
   249
slouken@3139
   250
                /* if we wrote some data */
slouken@3139
   251
                towrite -= written;
slouken@3139
   252
                pcmbuffer += written * this->spec.channels;
slouken@3139
   253
                continue;
slouken@3139
   254
            } else {
slouken@3139
   255
                if ((errno == EINVAL) || (errno == EIO)) {
slouken@3139
   256
                    SDL_memset(&cstatus, 0, sizeof(cstatus));
slouken@3139
   257
                    if (!this->hidden->iscapture) {
slouken@3139
   258
                        cstatus.channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@3139
   259
                    } else {
slouken@3139
   260
                        cstatus.channel = SND_PCM_CHANNEL_CAPTURE;
slouken@3139
   261
                    }
slouken@3099
   262
slouken@3139
   263
                    status =
slouken@3139
   264
                        snd_pcm_plugin_status(this->hidden->audio_handle,
slouken@3139
   265
                                              &cstatus);
slouken@3139
   266
                    if (status < 0) {
slouken@3139
   267
                        QSA_SetError("snd_pcm_plugin_status", status);
slouken@3139
   268
                        return;
slouken@3139
   269
                    }
slouken@3099
   270
slouken@3139
   271
                    if ((cstatus.status == SND_PCM_STATUS_UNDERRUN) ||
slouken@3139
   272
                        (cstatus.status == SND_PCM_STATUS_READY)) {
slouken@3139
   273
                        if (!this->hidden->iscapture) {
slouken@3139
   274
                            status =
slouken@3139
   275
                                snd_pcm_plugin_prepare(this->hidden->
slouken@3139
   276
                                                       audio_handle,
slouken@3139
   277
                                                       SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   278
                        } else {
slouken@3139
   279
                            status =
slouken@3139
   280
                                snd_pcm_plugin_prepare(this->hidden->
slouken@3139
   281
                                                       audio_handle,
slouken@3139
   282
                                                       SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   283
                        }
slouken@3139
   284
                        if (status < 0) {
slouken@3139
   285
                            QSA_SetError("snd_pcm_plugin_prepare", status);
slouken@3139
   286
                            return;
slouken@3139
   287
                        }
slouken@3139
   288
                    }
slouken@3139
   289
                    continue;
slouken@3139
   290
                } else {
slouken@3139
   291
                    return;
slouken@3139
   292
                }
slouken@3099
   293
            }
slouken@3139
   294
        } else {
slouken@3139
   295
            /* we wrote all remaining data */
slouken@3139
   296
            towrite -= written;
slouken@3139
   297
            pcmbuffer += written * this->spec.channels;
slouken@3139
   298
        }
slouken@3139
   299
    } while ((towrite > 0) && (this->enabled));
slouken@3099
   300
slouken@3139
   301
    /* If we couldn't write, assume fatal error for now */
slouken@3139
   302
    if (towrite != 0) {
slouken@3139
   303
        this->enabled = 0;
slouken@3139
   304
    }
slouken@3099
   305
}
slouken@3099
   306
slouken@3139
   307
static Uint8 *
slouken@3139
   308
QSA_GetDeviceBuf(_THIS)
slouken@3099
   309
{
slouken@3139
   310
    return this->hidden->pcm_buf;
slouken@3099
   311
}
slouken@3099
   312
slouken@3139
   313
static void
slouken@3139
   314
QSA_CloseDevice(_THIS)
slouken@3099
   315
{
slouken@3139
   316
    if (this->hidden != NULL) {
slouken@3139
   317
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   318
            if (!this->hidden->iscapture) {
slouken@3139
   319
                /* Finish playing available samples */
slouken@3139
   320
                snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   321
                                     SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   322
            } else {
slouken@3139
   323
                /* Cancel unread samples during capture */
slouken@3139
   324
                snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   325
                                     SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   326
            }
slouken@3139
   327
            snd_pcm_close(this->hidden->audio_handle);
slouken@3139
   328
            this->hidden->audio_handle = NULL;
slouken@3139
   329
        }
slouken@3099
   330
slouken@3139
   331
        if (this->hidden->pcm_buf != NULL) {
slouken@3139
   332
            SDL_FreeAudioMem(this->hidden->pcm_buf);
slouken@3139
   333
            this->hidden->pcm_buf = NULL;
slouken@3139
   334
        }
slouken@3099
   335
slouken@3139
   336
        SDL_free(this->hidden);
slouken@3139
   337
        this->hidden = NULL;
slouken@3139
   338
    }
slouken@3099
   339
}
slouken@3099
   340
slouken@3139
   341
static int
slouken@3139
   342
QSA_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@3099
   343
{
slouken@3139
   344
    int status = 0;
slouken@3139
   345
    int format = 0;
slouken@3139
   346
    SDL_AudioFormat test_format = 0;
slouken@3139
   347
    int found = 0;
slouken@3139
   348
    snd_pcm_channel_setup_t csetup;
slouken@3139
   349
    snd_pcm_channel_params_t cparams;
slouken@3099
   350
slouken@3139
   351
    /* Initialize all variables that we clean on shutdown */
slouken@3139
   352
    this->hidden =
slouken@3139
   353
        (struct SDL_PrivateAudioData *) SDL_calloc(1,
slouken@3139
   354
                                                   (sizeof
slouken@3139
   355
                                                    (struct
slouken@3139
   356
                                                     SDL_PrivateAudioData)));
slouken@3139
   357
    if (this->hidden == NULL) {
icculus@7038
   358
        return SDL_OutOfMemory();
slouken@3139
   359
    }
slouken@3139
   360
    SDL_memset(this->hidden, 0, sizeof(struct SDL_PrivateAudioData));
slouken@3099
   361
slouken@3139
   362
    /* Initialize channel transfer parameters to default */
slouken@3139
   363
    QSA_InitAudioParams(&cparams);
slouken@3099
   364
slouken@3139
   365
    /* Initialize channel direction: capture or playback */
slouken@3139
   366
    this->hidden->iscapture = iscapture;
slouken@3099
   367
slouken@3139
   368
    /* Find deviceid and cardid by device name for playback */
slouken@3139
   369
    if ((!this->hidden->iscapture) && (devname != NULL)) {
slouken@3139
   370
        uint32_t device;
slouken@3139
   371
        int32_t status;
slouken@3099
   372
slouken@3139
   373
        /* Search in the playback devices */
slouken@3139
   374
        device = 0;
slouken@3139
   375
        do {
slouken@3139
   376
            status = SDL_strcmp(qsa_playback_device[device].name, devname);
slouken@3139
   377
            if (status == 0) {
slouken@3139
   378
                /* Found requested device */
slouken@3139
   379
                this->hidden->deviceno = qsa_playback_device[device].deviceno;
slouken@3139
   380
                this->hidden->cardno = qsa_playback_device[device].cardno;
slouken@3139
   381
                break;
slouken@3139
   382
            }
slouken@3139
   383
            device++;
slouken@3139
   384
            if (device >= qsa_playback_devices) {
slouken@3139
   385
                QSA_CloseDevice(this);
icculus@7038
   386
                return SDL_SetError("No such playback device");
slouken@3139
   387
            }
slouken@3139
   388
        } while (1);
slouken@3139
   389
    }
slouken@3099
   390
slouken@3139
   391
    /* Find deviceid and cardid by device name for capture */
slouken@3139
   392
    if ((this->hidden->iscapture) && (devname != NULL)) {
slouken@3139
   393
        /* Search in the capture devices */
slouken@3139
   394
        uint32_t device;
slouken@3139
   395
        int32_t status;
slouken@3099
   396
slouken@3139
   397
        /* Searching in the playback devices */
slouken@3139
   398
        device = 0;
slouken@3139
   399
        do {
slouken@3139
   400
            status = SDL_strcmp(qsa_capture_device[device].name, devname);
slouken@3139
   401
            if (status == 0) {
slouken@3139
   402
                /* Found requested device */
slouken@3139
   403
                this->hidden->deviceno = qsa_capture_device[device].deviceno;
slouken@3139
   404
                this->hidden->cardno = qsa_capture_device[device].cardno;
slouken@3139
   405
                break;
slouken@3139
   406
            }
slouken@3139
   407
            device++;
slouken@3139
   408
            if (device >= qsa_capture_devices) {
slouken@3139
   409
                QSA_CloseDevice(this);
icculus@7038
   410
                return SDL_SetError("No such capture device");
slouken@3139
   411
            }
slouken@3139
   412
        } while (1);
slouken@3139
   413
    }
slouken@3099
   414
slouken@3139
   415
    /* Check if SDL requested default audio device */
slouken@3139
   416
    if (devname == NULL) {
slouken@3139
   417
        /* Open system default audio device */
slouken@3139
   418
        if (!this->hidden->iscapture) {
slouken@3139
   419
            status = snd_pcm_open_preferred(&this->hidden->audio_handle,
slouken@3139
   420
                                            &this->hidden->cardno,
slouken@3139
   421
                                            &this->hidden->deviceno,
slouken@3139
   422
                                            SND_PCM_OPEN_PLAYBACK);
slouken@3139
   423
        } else {
slouken@3139
   424
            status = snd_pcm_open_preferred(&this->hidden->audio_handle,
slouken@3139
   425
                                            &this->hidden->cardno,
slouken@3139
   426
                                            &this->hidden->deviceno,
slouken@3139
   427
                                            SND_PCM_OPEN_CAPTURE);
slouken@3139
   428
        }
slouken@3139
   429
    } else {
slouken@3139
   430
        /* Open requested audio device */
slouken@3139
   431
        if (!this->hidden->iscapture) {
slouken@3139
   432
            status =
slouken@3139
   433
                snd_pcm_open(&this->hidden->audio_handle,
slouken@3139
   434
                             this->hidden->cardno, this->hidden->deviceno,
slouken@3139
   435
                             SND_PCM_OPEN_PLAYBACK);
slouken@3139
   436
        } else {
slouken@3139
   437
            status =
slouken@3139
   438
                snd_pcm_open(&this->hidden->audio_handle,
slouken@3139
   439
                             this->hidden->cardno, this->hidden->deviceno,
slouken@3139
   440
                             SND_PCM_OPEN_CAPTURE);
slouken@3139
   441
        }
slouken@3139
   442
    }
slouken@3099
   443
slouken@3139
   444
    /* Check if requested device is opened */
slouken@3139
   445
    if (status < 0) {
slouken@3139
   446
        this->hidden->audio_handle = NULL;
slouken@3139
   447
        QSA_CloseDevice(this);
icculus@7038
   448
        return QSA_SetError("snd_pcm_open", status);
slouken@3139
   449
    }
slouken@3099
   450
slouken@3139
   451
    if (!QSA_CheckBuggyCards(this, QSA_MMAP_WORKAROUND)) {
slouken@3139
   452
        /* Disable QSA MMAP plugin for buggy audio drivers */
slouken@3139
   453
        status =
slouken@3139
   454
            snd_pcm_plugin_set_disable(this->hidden->audio_handle,
slouken@3139
   455
                                       PLUGIN_DISABLE_MMAP);
slouken@3139
   456
        if (status < 0) {
slouken@3139
   457
            QSA_CloseDevice(this);
icculus@7038
   458
            return QSA_SetError("snd_pcm_plugin_set_disable", status);
slouken@3139
   459
        }
slouken@3139
   460
    }
slouken@3099
   461
slouken@3139
   462
    /* Try for a closest match on audio format */
slouken@3139
   463
    format = 0;
slouken@3139
   464
    /* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */
slouken@3139
   465
    found = 0;
slouken@3099
   466
slouken@3139
   467
    for (test_format = SDL_FirstAudioFormat(this->spec.format); !found;) {
slouken@3139
   468
        /* if match found set format to equivalent QSA format */
slouken@3139
   469
        switch (test_format) {
slouken@3139
   470
        case AUDIO_U8:
slouken@3139
   471
            {
slouken@3139
   472
                format = SND_PCM_SFMT_U8;
slouken@3139
   473
                found = 1;
slouken@3139
   474
            }
slouken@3139
   475
            break;
slouken@3139
   476
        case AUDIO_S8:
slouken@3139
   477
            {
slouken@3139
   478
                format = SND_PCM_SFMT_S8;
slouken@3139
   479
                found = 1;
slouken@3139
   480
            }
slouken@3139
   481
            break;
slouken@3139
   482
        case AUDIO_S16LSB:
slouken@3139
   483
            {
slouken@3139
   484
                format = SND_PCM_SFMT_S16_LE;
slouken@3139
   485
                found = 1;
slouken@3139
   486
            }
slouken@3139
   487
            break;
slouken@3139
   488
        case AUDIO_S16MSB:
slouken@3139
   489
            {
slouken@3139
   490
                format = SND_PCM_SFMT_S16_BE;
slouken@3139
   491
                found = 1;
slouken@3139
   492
            }
slouken@3139
   493
            break;
slouken@3139
   494
        case AUDIO_U16LSB:
slouken@3139
   495
            {
slouken@3139
   496
                format = SND_PCM_SFMT_U16_LE;
slouken@3139
   497
                found = 1;
slouken@3139
   498
            }
slouken@3139
   499
            break;
slouken@3139
   500
        case AUDIO_U16MSB:
slouken@3139
   501
            {
slouken@3139
   502
                format = SND_PCM_SFMT_U16_BE;
slouken@3139
   503
                found = 1;
slouken@3139
   504
            }
slouken@3139
   505
            break;
slouken@3139
   506
        case AUDIO_S32LSB:
slouken@3139
   507
            {
slouken@3139
   508
                format = SND_PCM_SFMT_S32_LE;
slouken@3139
   509
                found = 1;
slouken@3139
   510
            }
slouken@3139
   511
            break;
slouken@3139
   512
        case AUDIO_S32MSB:
slouken@3139
   513
            {
slouken@3139
   514
                format = SND_PCM_SFMT_S32_BE;
slouken@3139
   515
                found = 1;
slouken@3139
   516
            }
slouken@3139
   517
            break;
slouken@3139
   518
        case AUDIO_F32LSB:
slouken@3139
   519
            {
slouken@3139
   520
                format = SND_PCM_SFMT_FLOAT_LE;
slouken@3139
   521
                found = 1;
slouken@3139
   522
            }
slouken@3139
   523
            break;
slouken@3139
   524
        case AUDIO_F32MSB:
slouken@3139
   525
            {
slouken@3139
   526
                format = SND_PCM_SFMT_FLOAT_BE;
slouken@3139
   527
                found = 1;
slouken@3139
   528
            }
slouken@3139
   529
            break;
slouken@3139
   530
        default:
slouken@3139
   531
            {
slouken@3139
   532
                break;
slouken@3139
   533
            }
slouken@3139
   534
        }
slouken@3099
   535
slouken@3139
   536
        if (!found) {
slouken@3139
   537
            test_format = SDL_NextAudioFormat();
slouken@3139
   538
        }
slouken@3139
   539
    }
slouken@3099
   540
slouken@3139
   541
    /* assumes test_format not 0 on success */
slouken@3139
   542
    if (test_format == 0) {
slouken@3139
   543
        QSA_CloseDevice(this);
icculus@7038
   544
        return SDL_SetError("QSA: Couldn't find any hardware audio formats");
slouken@3139
   545
    }
slouken@3099
   546
slouken@3139
   547
    this->spec.format = test_format;
slouken@3099
   548
slouken@3139
   549
    /* Set the audio format */
slouken@3139
   550
    cparams.format.format = format;
slouken@3099
   551
slouken@3139
   552
    /* Set mono/stereo/4ch/6ch/8ch audio */
slouken@3139
   553
    cparams.format.voices = this->spec.channels;
slouken@3099
   554
slouken@3139
   555
    /* Set rate */
slouken@3139
   556
    cparams.format.rate = this->spec.freq;
slouken@3099
   557
slouken@3139
   558
    /* Setup the transfer parameters according to cparams */
slouken@3139
   559
    status = snd_pcm_plugin_params(this->hidden->audio_handle, &cparams);
slouken@3139
   560
    if (status < 0) {
slouken@3139
   561
        QSA_CloseDevice(this);
icculus@7038
   562
        return QSA_SetError("snd_pcm_channel_params", status);
slouken@3139
   563
    }
slouken@3099
   564
slouken@3139
   565
    /* Make sure channel is setup right one last time */
slouken@6352
   566
    SDL_memset(&csetup, 0, sizeof(csetup));
slouken@3139
   567
    if (!this->hidden->iscapture) {
slouken@3139
   568
        csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@3139
   569
    } else {
slouken@3139
   570
        csetup.channel = SND_PCM_CHANNEL_CAPTURE;
slouken@3139
   571
    }
slouken@3099
   572
slouken@3139
   573
    /* Setup an audio channel */
slouken@3139
   574
    if (snd_pcm_plugin_setup(this->hidden->audio_handle, &csetup) < 0) {
slouken@3139
   575
        QSA_CloseDevice(this);
icculus@7038
   576
        return SDL_SetError("QSA: Unable to setup channel");
slouken@3139
   577
    }
slouken@3099
   578
slouken@3139
   579
    /* Calculate the final parameters for this audio specification */
slouken@3139
   580
    SDL_CalculateAudioSpec(&this->spec);
slouken@3099
   581
slouken@3139
   582
    this->hidden->pcm_len = this->spec.size;
slouken@3099
   583
slouken@3139
   584
    if (this->hidden->pcm_len == 0) {
slouken@3139
   585
        this->hidden->pcm_len =
slouken@3139
   586
            csetup.buf.block.frag_size * this->spec.channels *
slouken@3139
   587
            (snd_pcm_format_width(format) / 8);
slouken@3139
   588
    }
slouken@3099
   589
slouken@3139
   590
    /*
slouken@3139
   591
     * Allocate memory to the audio buffer and initialize with silence
slouken@3139
   592
     *  (Note that buffer size must be a multiple of fragment size, so find
slouken@3139
   593
     *  closest multiple)
slouken@3139
   594
     */
slouken@3139
   595
    this->hidden->pcm_buf =
slouken@3139
   596
        (Uint8 *) SDL_AllocAudioMem(this->hidden->pcm_len);
slouken@3139
   597
    if (this->hidden->pcm_buf == NULL) {
slouken@3139
   598
        QSA_CloseDevice(this);
icculus@7038
   599
        return SDL_OutOfMemory();
slouken@3139
   600
    }
slouken@3139
   601
    SDL_memset(this->hidden->pcm_buf, this->spec.silence,
slouken@3139
   602
               this->hidden->pcm_len);
slouken@3099
   603
slouken@3139
   604
    /* get the file descriptor */
slouken@3139
   605
    if (!this->hidden->iscapture) {
slouken@3139
   606
        this->hidden->audio_fd =
slouken@3139
   607
            snd_pcm_file_descriptor(this->hidden->audio_handle,
slouken@3139
   608
                                    SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   609
    } else {
slouken@3139
   610
        this->hidden->audio_fd =
slouken@3139
   611
            snd_pcm_file_descriptor(this->hidden->audio_handle,
slouken@3139
   612
                                    SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   613
    }
slouken@3099
   614
slouken@3139
   615
    if (this->hidden->audio_fd < 0) {
slouken@3139
   616
        QSA_CloseDevice(this);
icculus@7038
   617
        return QSA_SetError("snd_pcm_file_descriptor", status);
slouken@3139
   618
    }
slouken@3099
   619
slouken@3139
   620
    /* Prepare an audio channel */
slouken@3139
   621
    if (!this->hidden->iscapture) {
slouken@3139
   622
        /* Prepare audio playback */
slouken@3139
   623
        status =
slouken@3139
   624
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
slouken@3139
   625
                                   SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   626
    } else {
slouken@3139
   627
        /* Prepare audio capture */
slouken@3139
   628
        status =
slouken@3139
   629
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
slouken@3139
   630
                                   SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   631
    }
slouken@3099
   632
slouken@3139
   633
    if (status < 0) {
slouken@3139
   634
        QSA_CloseDevice(this);
icculus@7038
   635
        return QSA_SetError("snd_pcm_plugin_prepare", status);
slouken@3139
   636
    }
slouken@3099
   637
slouken@3139
   638
    /* We're really ready to rock and roll. :-) */
icculus@7038
   639
    return 0;
slouken@3099
   640
}
slouken@3099
   641
icculus@5593
   642
static void
icculus@5593
   643
QSA_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
slouken@3099
   644
{
slouken@3139
   645
    uint32_t it;
slouken@3139
   646
    uint32_t cards;
slouken@3139
   647
    uint32_t devices;
slouken@3139
   648
    int32_t status;
slouken@3139
   649
slouken@3139
   650
    /* Detect amount of available devices       */
slouken@3139
   651
    /* this value can be changed in the runtime */
slouken@3139
   652
    cards = snd_cards();
slouken@3099
   653
slouken@3139
   654
    /* If io-audio manager is not running we will get 0 as number */
slouken@3139
   655
    /* of available audio devices                                 */
slouken@3139
   656
    if (cards == 0) {
slouken@3139
   657
        /* We have no any available audio devices */
icculus@5593
   658
        return;
slouken@3139
   659
    }
slouken@3099
   660
slouken@3139
   661
    /* Find requested devices by type */
slouken@3139
   662
    if (!iscapture) {
slouken@3139
   663
        /* Playback devices enumeration requested */
slouken@3139
   664
        for (it = 0; it < cards; it++) {
slouken@3139
   665
            devices = 0;
slouken@3139
   666
            do {
slouken@3139
   667
                status =
slouken@3139
   668
                    snd_card_get_longname(it,
slouken@3139
   669
                                          qsa_playback_device
slouken@3139
   670
                                          [qsa_playback_devices].name,
slouken@3139
   671
                                          QSA_MAX_NAME_LENGTH);
slouken@3139
   672
                if (status == EOK) {
slouken@3139
   673
                    snd_pcm_t *handle;
slouken@3099
   674
slouken@3139
   675
                    /* Add device number to device name */
slouken@3139
   676
                    sprintf(qsa_playback_device[qsa_playback_devices].name +
slouken@3139
   677
                            SDL_strlen(qsa_playback_device
slouken@3139
   678
                                       [qsa_playback_devices].name), " d%d",
slouken@3139
   679
                            devices);
slouken@3099
   680
slouken@3139
   681
                    /* Store associated card number id */
slouken@3139
   682
                    qsa_playback_device[qsa_playback_devices].cardno = it;
slouken@3099
   683
slouken@3139
   684
                    /* Check if this device id could play anything */
slouken@3139
   685
                    status =
slouken@3139
   686
                        snd_pcm_open(&handle, it, devices,
slouken@3139
   687
                                     SND_PCM_OPEN_PLAYBACK);
slouken@3139
   688
                    if (status == EOK) {
slouken@3139
   689
                        qsa_playback_device[qsa_playback_devices].deviceno =
slouken@3139
   690
                            devices;
slouken@3139
   691
                        status = snd_pcm_close(handle);
slouken@3139
   692
                        if (status == EOK) {
icculus@5593
   693
                            addfn(qsa_playback_device[qsa_playback_devices].name);
slouken@3139
   694
                            qsa_playback_devices++;
slouken@3139
   695
                        }
slouken@3139
   696
                    } else {
slouken@3139
   697
                        /* Check if we got end of devices list */
slouken@3139
   698
                        if (status == -ENOENT) {
slouken@3139
   699
                            break;
slouken@3139
   700
                        }
slouken@3139
   701
                    }
slouken@3139
   702
                } else {
slouken@3139
   703
                    break;
slouken@3139
   704
                }
slouken@3139
   705
slouken@3139
   706
                /* Check if we reached maximum devices count */
slouken@3139
   707
                if (qsa_playback_devices >= QSA_MAX_DEVICES) {
slouken@3139
   708
                    break;
slouken@3139
   709
                }
slouken@3139
   710
                devices++;
slouken@3139
   711
            } while (1);
slouken@3099
   712
slouken@3099
   713
            /* Check if we reached maximum devices count */
slouken@3139
   714
            if (qsa_playback_devices >= QSA_MAX_DEVICES) {
slouken@3139
   715
                break;
slouken@3099
   716
            }
slouken@3139
   717
        }
slouken@3139
   718
    } else {
slouken@3139
   719
        /* Capture devices enumeration requested */
slouken@3139
   720
        for (it = 0; it < cards; it++) {
slouken@3139
   721
            devices = 0;
slouken@3139
   722
            do {
slouken@3139
   723
                status =
slouken@3139
   724
                    snd_card_get_longname(it,
slouken@3139
   725
                                          qsa_capture_device
slouken@3139
   726
                                          [qsa_capture_devices].name,
slouken@3139
   727
                                          QSA_MAX_NAME_LENGTH);
slouken@3139
   728
                if (status == EOK) {
slouken@3139
   729
                    snd_pcm_t *handle;
slouken@3099
   730
slouken@3139
   731
                    /* Add device number to device name */
slouken@3139
   732
                    sprintf(qsa_capture_device[qsa_capture_devices].name +
slouken@3139
   733
                            SDL_strlen(qsa_capture_device
slouken@3139
   734
                                       [qsa_capture_devices].name), " d%d",
slouken@3139
   735
                            devices);
slouken@3099
   736
slouken@3139
   737
                    /* Store associated card number id */
slouken@3139
   738
                    qsa_capture_device[qsa_capture_devices].cardno = it;
slouken@3099
   739
slouken@3139
   740
                    /* Check if this device id could play anything */
slouken@3139
   741
                    status =
slouken@3139
   742
                        snd_pcm_open(&handle, it, devices,
slouken@3139
   743
                                     SND_PCM_OPEN_CAPTURE);
slouken@3139
   744
                    if (status == EOK) {
slouken@3139
   745
                        qsa_capture_device[qsa_capture_devices].deviceno =
slouken@3139
   746
                            devices;
slouken@3139
   747
                        status = snd_pcm_close(handle);
slouken@3139
   748
                        if (status == EOK) {
icculus@5593
   749
                            addfn(qsa_capture_device[qsa_capture_devices].name);
slouken@3139
   750
                            qsa_capture_devices++;
slouken@3139
   751
                        }
slouken@3139
   752
                    } else {
slouken@3139
   753
                        /* Check if we got end of devices list */
slouken@3139
   754
                        if (status == -ENOENT) {
slouken@3139
   755
                            break;
slouken@3139
   756
                        }
slouken@3139
   757
                    }
slouken@3099
   758
slouken@3139
   759
                    /* Check if we reached maximum devices count */
slouken@3139
   760
                    if (qsa_capture_devices >= QSA_MAX_DEVICES) {
slouken@3139
   761
                        break;
slouken@3139
   762
                    }
slouken@3139
   763
                } else {
slouken@3139
   764
                    break;
slouken@3139
   765
                }
slouken@3139
   766
                devices++;
slouken@3139
   767
            } while (1);
slouken@3099
   768
slouken@3139
   769
            /* Check if we reached maximum devices count */
slouken@3139
   770
            if (qsa_capture_devices >= QSA_MAX_DEVICES) {
slouken@3139
   771
                break;
slouken@3139
   772
            }
slouken@3139
   773
        }
slouken@3139
   774
    }
slouken@3099
   775
}
slouken@3099
   776
icculus@5584
   777
static void
slouken@3139
   778
QSA_WaitDone(_THIS)
slouken@3099
   779
{
slouken@3139
   780
    if (!this->hidden->iscapture) {
slouken@3139
   781
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   782
            /* Wait till last fragment is played and stop channel */
slouken@3139
   783
            snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   784
                                 SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   785
        }
slouken@3139
   786
    } else {
slouken@3139
   787
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   788
            /* Discard all unread data and stop channel */
slouken@3139
   789
            snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   790
                                 SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   791
        }
slouken@3139
   792
    }
slouken@3139
   793
}
slouken@3139
   794
icculus@5584
   795
static void
slouken@3139
   796
QSA_Deinitialize(void)
slouken@3139
   797
{
slouken@3139
   798
    /* Clear devices array on shutdown */
slouken@3139
   799
    SDL_memset(qsa_playback_device, 0x00,
slouken@3139
   800
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   801
    SDL_memset(qsa_capture_device, 0x00,
slouken@3139
   802
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   803
    qsa_playback_devices = 0;
slouken@3139
   804
    qsa_capture_devices = 0;
slouken@3099
   805
}
slouken@3099
   806
slouken@3139
   807
static int
slouken@3139
   808
QSA_Init(SDL_AudioDriverImpl * impl)
slouken@3099
   809
{
slouken@3139
   810
    snd_pcm_t *handle = NULL;
slouken@3139
   811
    int32_t status = 0;
slouken@3139
   812
slouken@3139
   813
    /* Clear devices array */
slouken@3139
   814
    SDL_memset(qsa_playback_device, 0x00,
slouken@3139
   815
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   816
    SDL_memset(qsa_capture_device, 0x00,
slouken@3139
   817
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   818
    qsa_playback_devices = 0;
slouken@3139
   819
    qsa_capture_devices = 0;
slouken@3139
   820
slouken@3139
   821
    /* Set function pointers                                     */
slouken@3139
   822
    /* DeviceLock and DeviceUnlock functions are used default,   */
slouken@3139
   823
    /* provided by SDL, which uses pthread_mutex for lock/unlock */
slouken@3139
   824
    impl->DetectDevices = QSA_DetectDevices;
slouken@3139
   825
    impl->OpenDevice = QSA_OpenDevice;
slouken@3139
   826
    impl->ThreadInit = QSA_ThreadInit;
slouken@3139
   827
    impl->WaitDevice = QSA_WaitDevice;
slouken@3139
   828
    impl->PlayDevice = QSA_PlayDevice;
slouken@3139
   829
    impl->GetDeviceBuf = QSA_GetDeviceBuf;
slouken@3139
   830
    impl->CloseDevice = QSA_CloseDevice;
slouken@3139
   831
    impl->WaitDone = QSA_WaitDone;
slouken@3139
   832
    impl->Deinitialize = QSA_Deinitialize;
slouken@3139
   833
    impl->LockDevice = NULL;
slouken@3139
   834
    impl->UnlockDevice = NULL;
slouken@3139
   835
slouken@3139
   836
    impl->OnlyHasDefaultOutputDevice = 0;
slouken@3139
   837
    impl->ProvidesOwnCallbackThread = 0;
slouken@3139
   838
    impl->SkipMixerLock = 0;
slouken@3139
   839
    impl->HasCaptureSupport = 1;
slouken@3139
   840
    impl->OnlyHasDefaultOutputDevice = 0;
slouken@3139
   841
    impl->OnlyHasDefaultInputDevice = 0;
slouken@3139
   842
slouken@3139
   843
    /* Check if io-audio manager is running or not */
slouken@3139
   844
    status = snd_cards();
slouken@3139
   845
    if (status == 0) {
slouken@3139
   846
        /* if no, return immediately */
slouken@3139
   847
        return 1;
slouken@3139
   848
    }
slouken@3139
   849
icculus@3699
   850
    return 1;   /* this audio target is available. */
slouken@3099
   851
}
slouken@3099
   852
slouken@3139
   853
AudioBootStrap QSAAUDIO_bootstrap = {
icculus@5594
   854
    "qsa", "QNX QSA Audio", QSA_Init, 0
slouken@3099
   855
};
slouken@3099
   856
slouken@6044
   857
#endif /* SDL_AUDIO_DRIVER_QSA */
slouken@6044
   858
slouken@3099
   859
/* vi: set ts=4 sw=4 expandtab: */