CTWM
Toggle main menu visibility
Loading...
Searching...
No Matches
/usr/src/RPM/BUILD/ctwm-4.1.0/sound.c
Go to the documentation of this file.
1
/*
2
* These routines were extracted from the sound hack for olvwm3.3 by
3
* Andrew "Ender" Scherpbier (turtle@sciences.sdsu.edu, Andrew@SDSU.Edu)
4
* and modified by J.E. Sacco (jsacco @ssl.com) for tvtwm and twm. They
5
* were then slightly adapted for ctwm by Mark Boyns (boyns@sdsu.edu),
6
* and have since been reworked more.
7
*/
8
9
#include "
ctwm.h
"
10
11
#include <rplay.h>
12
#include <string.h>
13
#include <stdio.h>
14
#include <ctype.h>
15
16
#include "
event_names.h
"
17
#include "
sound.h
"
18
19
RPLAY **
rp
= NULL;
20
21
static
int
need_sound_init
= 1;
22
static
int
sound_from_config
= 0;
23
static
int
sound_fd
= 0;
24
static
int
sound_state
= 1;
25
#define HOSTNAME_LEN 200
26
static
char
hostname
[
HOSTNAME_LEN
];
27
28
/*
29
* Function to trim away spaces at the start and end of a string
30
*/
31
static
char
*
32
trim_spaces
(
char
*str)
33
{
34
if
(str != NULL) {
35
char
*p = str + strlen(str);
36
while
(*str !=
'\0'
&& *str !=
'\r'
&& *str !=
'\n'
&& isspace(*str)) {
37
str++;
38
}
39
/* Assume all line end characters are at the end */
40
while
(p > str && isspace(p[-1])) {
41
p--;
42
}
43
*p =
'\0'
;
44
}
45
return
str;
46
}
47
48
/*
49
* Define stuff related to "magic" names.
50
*/
51
static
const
char
*
magic_events
[] = {
52
"Startup"
,
53
"Shutdown"
,
54
};
55
#define NMAGICEVENTS (sizeof(magic_events) / sizeof(*magic_events))
56
57
static
int
58
sound_magic_event_name2num
(
const
char
*name)
59
{
60
int
i;
61
62
for
(i = 0 ; i <
NMAGICEVENTS
; i++) {
63
if
(strcasecmp(name,
magic_events
[i]) == 0) {
64
/* We number these off the far end of the non-magic events */
65
return
event_names_size
() + i;
66
}
67
}
68
69
return
-1;
70
}
71
72
73
/*
74
* Now we know how many events we need to store up info for
75
*/
76
#define NEVENTS (event_names_size() + NMAGICEVENTS)
77
78
79
80
/*
81
* Initialize the subsystem and its necessary bits
82
*/
83
void
84
sound_init
(
void
)
85
{
86
if
(!
need_sound_init
) {
87
return
;
88
}
89
90
/* Can't happen */
91
if
(
sound_fd
!= 0) {
92
fprintf(stderr,
"BUG: sound_fd not set but sound inited.\n"
);
93
exit(1);
94
}
95
96
need_sound_init
= 0;
97
if
(
hostname
[0] ==
'\0'
) {
98
strncpy(
hostname
, rplay_default_host(),
HOSTNAME_LEN
- 1);
99
hostname
[
HOSTNAME_LEN
- 1] =
'\0'
;
/* JIC */
100
}
101
102
if
((
sound_fd
= rplay_open(
hostname
)) < 0) {
103
rplay_perror(
"create"
);
104
}
105
106
/*
107
* Init rp if necessary
108
*/
109
if
(
rp
== NULL) {
110
if
((
rp
= calloc(
NEVENTS
,
sizeof
(RPLAY *))) == NULL) {
111
perror(
"calloc() rplay control"
);
112
exit(1);
113
/*
114
* Should maybe just bomb out of sound stuff, but there's
115
* currently no provision for that. If malloc fails, we're
116
* pretty screwed anyway, so it's not much loss to just die.
117
*/
118
}
119
}
120
}
121
122
123
/*
124
* Clear out any set sounds
125
*/
126
void
127
sound_clear_list
(
void
)
128
{
129
int
i;
130
131
/* JIC */
132
if
(
rp
== NULL) {
133
return
;
134
}
135
136
/*
137
* Destroy any old sounds
138
*/
139
for
(i = 0; i <
NEVENTS
; i++) {
140
if
(
rp
[i] != NULL) {
141
rplay_destroy(
rp
[i]);
142
}
143
rp
[i] = NULL;
144
}
145
}
146
147
148
/*
149
* [Re]load the sounds
150
*/
151
void
152
sound_load_list
(
void
)
153
{
154
FILE *fl;
155
char
*home;
156
char
*soundfile;
157
char
buffer[100];
158
159
/* Guard; shouldn't be possible */
160
if
(
rp
== NULL) {
161
fprintf(stderr,
"Tried to load sounds before subsystem inited.\n"
);
162
exit(1);
163
}
164
165
/* Find the .ctwm-sounds file */
166
if
((home = getenv(
"HOME"
)) == NULL) {
167
home =
""
;
168
}
169
if
(asprintf(&soundfile,
"%s/.ctwm-sounds"
, home) < 0) {
170
perror(
"Failed building path to sound file"
);
171
return
;
172
}
173
fl = fopen(soundfile,
"r"
);
174
free(soundfile);
175
176
/*
177
* If it was found, but we already have sound set from the config
178
* file, complain on stderr and then return.
179
*/
180
if
(fl != NULL &&
sound_from_config
) {
181
fprintf(stderr,
"RplaySounds set in ctwmrc, not reading "
182
"~/.ctwm-sounds.\n"
);
183
fclose(fl);
184
return
;
185
}
186
187
/* Clear out the old list, whether we have new or not */
188
sound_clear_list
();
189
190
/* If there wasn't a .ctwm-sounds file, we're done now */
191
if
(fl == NULL) {
192
return
;
193
}
194
195
/* Now go ahead and parse it in */
196
while
(fgets(buffer, 100, fl) != NULL) {
197
char
*ename, *sndfile;
198
199
ename =
trim_spaces
(strtok(buffer,
": \t"
));
200
if
(ename == NULL || *ename ==
'#'
) {
201
continue
;
202
}
203
204
sndfile =
trim_spaces
(strtok(NULL,
"\r\n"
));
205
if
(sndfile == NULL || *sndfile ==
'#'
) {
206
continue
;
207
}
208
209
if
(
set_sound_event_name
(ename, sndfile) != 0) {
210
fprintf(stderr,
"Error adding sound for %s; maybe event "
211
"name is invalid?\n"
, ename);
212
}
213
}
214
fclose(fl);
215
}
216
217
218
/*
219
* Play sound
220
*/
221
void
222
play_sound
(
int
snd)
223
{
224
/* Bounds */
225
if
(snd < 0 || snd >=
NEVENTS
) {
226
return
;
227
}
228
229
/* Playing enabled */
230
if
(
sound_state
== 0) {
231
return
;
232
}
233
234
/* Better already be initted */
235
if
(
need_sound_init
) {
236
fprintf(stderr,
"BUG: play_sound() Sound should be initted already.\n"
);
237
return
;
238
}
239
240
/* Skip if this isn't a sound we have set */
241
if
(
rp
[snd] == NULL) {
242
return
;
243
}
244
245
/* And if all else fails, play it */
246
if
(rplay(
sound_fd
,
rp
[snd]) < 0) {
247
rplay_perror(
"rplay"
);
248
}
249
}
250
251
void
252
play_startup_sound
(
void
)
253
{
254
play_sound
(
sound_magic_event_name2num
(
"Startup"
));
255
}
256
257
void
258
play_exit_sound
(
void
)
259
{
260
play_sound
(
sound_magic_event_name2num
(
"Shutdown"
));
261
}
262
263
264
/*
265
* Flag that we loaded sounds from the ctwmrc
266
*/
267
void
268
sound_set_from_config
(
void
)
269
{
270
sound_from_config
= 1;
271
}
272
273
274
/*
275
* Toggle the sound on/off
276
*/
277
void
278
toggle_sound
(
void
)
279
{
280
sound_state
^= 1;
281
}
282
283
284
/*
285
* Re-read the sounds mapping file
286
*/
287
void
288
reread_sounds
(
void
)
289
{
290
sound_load_list
();
291
}
292
293
/*
294
* Set the SoundHost and force the sound_fd to be re-opened.
295
*/
296
void
297
set_sound_host
(
char
*host)
298
{
299
strncpy(
hostname
, host,
HOSTNAME_LEN
- 1);
300
hostname
[
HOSTNAME_LEN
- 1] =
'\0'
;
/* JIC */
301
if
(
sound_fd
!= 0) {
302
rplay_close(
sound_fd
);
303
}
304
sound_fd
= 0;
305
}
306
307
/*
308
* Set the sound to play for a given event
309
*/
310
int
311
set_sound_event_name
(
const
char
*ename,
const
char
*soundfile)
312
{
313
int
i;
314
315
/* Find the index we'll use in rp[] for it */
316
i =
sound_magic_event_name2num
(ename);
317
if
(i < 0) {
318
i =
event_num_by_name
(ename);
319
}
320
if
(i < 0) {
321
return
-1;
322
}
323
324
/* Gotcha */
325
return
set_sound_event
(i, soundfile);
326
}
327
328
int
329
set_sound_event
(
int
snd,
const
char
*soundfile)
330
{
331
/* This shouldn't get called before things are initialized */
332
if
(
rp
== NULL) {
333
fprintf(stderr,
"%s(): internal error: called before initialized.\n"
, __func__);
334
exit(1);
335
}
336
337
/* Cleanup old if necessary */
338
if
(
rp
[snd] != NULL) {
339
rplay_destroy(
rp
[snd]);
340
}
341
342
/* Setup new */
343
rp
[snd] = rplay_create(RPLAY_PLAY);
344
if
(
rp
[snd] == NULL) {
345
rplay_perror(
"create"
);
346
return
-1;
347
}
348
if
(rplay_set(
rp
[snd], RPLAY_INSERT, 0, RPLAY_SOUND, soundfile, NULL)
349
< 0) {
350
rplay_perror(
"rplay"
);
351
}
352
353
return
0;
354
}
ctwm.h
event_names_size
size_t event_names_size(void)
Definition
event_names.c:19
event_num_by_name
int event_num_by_name(const char *ename)
Definition
event_names.c:45
event_names.h
sound_clear_list
void sound_clear_list(void)
Definition
sound.c:127
play_sound
void play_sound(int snd)
Definition
sound.c:222
NMAGICEVENTS
#define NMAGICEVENTS
Definition
sound.c:55
rp
RPLAY ** rp
Definition
sound.c:19
NEVENTS
#define NEVENTS
Definition
sound.c:76
sound_from_config
static int sound_from_config
Definition
sound.c:22
sound_load_list
void sound_load_list(void)
Definition
sound.c:152
need_sound_init
static int need_sound_init
Definition
sound.c:21
sound_set_from_config
void sound_set_from_config(void)
Definition
sound.c:268
set_sound_event_name
int set_sound_event_name(const char *ename, const char *soundfile)
Definition
sound.c:311
sound_state
static int sound_state
Definition
sound.c:24
toggle_sound
void toggle_sound(void)
Definition
sound.c:278
sound_magic_event_name2num
static int sound_magic_event_name2num(const char *name)
Definition
sound.c:58
reread_sounds
void reread_sounds(void)
Definition
sound.c:288
sound_init
void sound_init(void)
Definition
sound.c:84
sound_fd
static int sound_fd
Definition
sound.c:23
hostname
static char hostname[200]
Definition
sound.c:26
HOSTNAME_LEN
#define HOSTNAME_LEN
Definition
sound.c:25
magic_events
static const char * magic_events[]
Definition
sound.c:51
set_sound_event
int set_sound_event(int snd, const char *soundfile)
Definition
sound.c:329
play_startup_sound
void play_startup_sound(void)
Definition
sound.c:252
play_exit_sound
void play_exit_sound(void)
Definition
sound.c:258
set_sound_host
void set_sound_host(char *host)
Definition
sound.c:297
trim_spaces
static char * trim_spaces(char *str)
Definition
sound.c:32
sound.h
sound.c
Generated by
1.18.0