summaryrefslogtreecommitdiff
path: root/pcsound/pcsound_linux.c
blob: 7c1abc7b8c76f83e2a0c0ab958ac9ba090d49477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// Copyright(C) 2007 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
//    PC speaker driver for Linux.
//
//-----------------------------------------------------------------------------

#include "config.h"

#ifdef HAVE_LINUX_KD_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <linux/kd.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>

#include "SDL.h"
#include "SDL_thread.h"

#include "pcsound.h"

#define CONSOLE_DEVICE "/dev/console"

static int console_handle;
static pcsound_callback_func callback;
static int sound_thread_running = 0;
static SDL_Thread *sound_thread_handle;
static int sleep_adjust = 0;

static void AdjustedSleep(unsigned int ms)
{
    unsigned int start_time;
    unsigned int end_time;
    unsigned int actual_time;

    // Adjust based on previous error to keep the tempo right

    if (sleep_adjust > ms)
    {
        sleep_adjust -= ms;
        return;
    }
    else
    {
        ms -= sleep_adjust;
    }

    // Do the sleep and record how long it takes

    start_time = SDL_GetTicks();

    SDL_Delay(ms);
    
    end_time = SDL_GetTicks();

    if (end_time > start_time)
    {
        actual_time = end_time - start_time;
    }
    else
    {
        actual_time = ms;
    }

    if (actual_time < ms)
    {
        actual_time = ms;
    }

    // Save sleep_adjust for next time

    sleep_adjust = actual_time - ms;
}

static int SoundThread(void *unused)
{
    int frequency;
    int duration;
    int cycles;
    
    while (sound_thread_running)
    {
        callback(&duration, &frequency);

        if (frequency != 0) 
        {
            cycles = PCSOUND_8253_FREQUENCY / frequency;
        }
        else
        {
            cycles = 0;
        }

        ioctl(console_handle, KIOCSOUND, cycles);

        AdjustedSleep(duration);
    }

    return 0;
}

static int PCSound_Linux_Init(pcsound_callback_func callback_func)
{
    // Try to open the console

    console_handle = open(CONSOLE_DEVICE, O_WRONLY);

    if (console_handle == -1)
    {
        // Don't have permissions for the console device?

	fprintf(stderr, "PCSound_Linux_Init: Failed to open '%s': %s\n",
			CONSOLE_DEVICE, strerror(errno));
        return 0;
    }

    if (ioctl(console_handle, KIOCSOUND, 0) < 0)
    {
        // KIOCSOUND not supported: non-PC linux?

        close(console_handle);
        return 0;
    }

    // Start a thread up to generate PC speaker output
    
    callback = callback_func;
    sound_thread_running = 1;

    sound_thread_handle = SDL_CreateThread(SoundThread, NULL);
    
    return 1;
}

static void PCSound_Linux_Shutdown(void)
{
    sound_thread_running = 0;
    SDL_WaitThread(sound_thread_handle, NULL);
    close(console_handle);
}

pcsound_driver_t pcsound_linux_driver =
{
    "Linux",
    PCSound_Linux_Init,
    PCSound_Linux_Shutdown,
};

#endif /* #ifdef HAVE_LINUX_KD_H */