summaryrefslogtreecommitdiff
path: root/src/sys/oss/volume-oss.c
blob: 7771307d275338fa4e23e897c1448cf160c9bac0 (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
/*
 * Schism Tracker - a cross-platform Impulse Tracker clone
 * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
 * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
 * copyright (c) 2009 Storlek & Mrs. Brisby
 * copyright (c) 2010-2012 Storlek
 * URL: http://schismtracker.org/
 *
 * 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
 */

#include "headers.h"

#include "util.h"
#include "log.h"

#ifdef USE_OSS

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>

#include <errno.h>
#include <fcntl.h>

/* Hmm. I've found that some systems actually don't support the idea of a
 * "master" volume, so this became necessary. I suppose PCM is really the
 * more useful setting to change anyway. */
#if 0
# define SCHISM_MIXER_CONTROL SOUND_MIXER_VOLUME
#else
# define SCHISM_MIXER_CONTROL SOUND_MIXER_PCM
#endif

#define VOLUME_MAX 100

/* --------------------------------------------------------------------- */

static const char *device_file = NULL;

/* --------------------------------------------------------------------- */

static int open_mixer_device(void)
{
	const char *ptr;

	if (!device_file) {
		ptr = "/dev/sound/mixer";
		if (access(ptr, F_OK) < 0) {
			/* this had better work :) */
			ptr = "/dev/mixer";
		}
		device_file = ptr;
	}

	return open(device_file, O_RDWR);
}

/* --------------------------------------------------------------------- */

int oss_volume_get_max(void);
int oss_volume_get_max(void)
{
	return VOLUME_MAX;
}

void oss_volume_read(int *left, int *right);
void oss_volume_read(int *left, int *right)
{
	int fd;
	uint8_t volume[4];

	fd = open_mixer_device();
	if (fd < 0) {
		log_perror(device_file);
		*left = *right = 0;
		return;
	}

	if (ioctl(fd, MIXER_READ(SCHISM_MIXER_CONTROL), volume) == EOF) {
		log_perror(device_file);
		*left = *right = 0;
	} else {
		*left = volume[0];
		*right = volume[1];
	}

	close(fd);
}

void oss_volume_write(int left, int right);
void oss_volume_write(int left, int right)
{
	int fd;
	uint8_t volume[4];

	volume[0] = CLAMP(left, 0, VOLUME_MAX);
	volume[1] = CLAMP(right, 0, VOLUME_MAX);

	fd = open_mixer_device();
	if (fd < 0) {
		log_perror(device_file);
		return;
	}

	if (ioctl(fd, MIXER_WRITE(SCHISM_MIXER_CONTROL), volume) == EOF) {
		log_perror(device_file);
	}

	close(fd);
}

#endif
© All Rights Reserved