CTWM
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
20
21static int need_sound_init = 1;
22static int sound_from_config = 0;
23static int sound_fd = 0;
24static int sound_state = 1;
25#define HOSTNAME_LEN 200
27
28/*
29 * Function to trim away spaces at the start and end of a string
30 */
31static char *
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 */
51static const char *magic_events[] = {
52 "Startup",
53 "Shutdown",
54};
55#define NMAGICEVENTS (sizeof(magic_events) / sizeof(*magic_events))
56
57static int
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 */
83void
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
97 if(hostname[0] == '\0') {
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 */
126void
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 */
151void
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");
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 */
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
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 */
221void
223{
224 /* Bounds */
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
251void
256
257void
259{
261}
262
263
264/*
265 * Flag that we loaded sounds from the ctwmrc
266 */
267void
272
273
274/*
275 * Toggle the sound on/off
276 */
277void
279{
280 sound_state ^= 1;
281}
282
283
284/*
285 * Re-read the sounds mapping file
286 */
287void
289{
291}
292
293/*
294 * Set the SoundHost and force the sound_fd to be re-opened.
295 */
296void
298{
300 hostname[HOSTNAME_LEN - 1] = '\0'; /* JIC */
301 if(sound_fd != 0) {
303 }
304 sound_fd = 0;
305}
306
307/*
308 * Set the sound to play for a given event
309 */
310int
311set_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 */
317 if(i < 0) {
319 }
320 if(i < 0) {
321 return -1;
322 }
323
324 /* Gotcha */
325 return set_sound_event(i, soundfile);
326}
327
328int
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) {
340 }
341
342 /* Setup new */
344 if(rp[snd] == NULL) {
345 rplay_perror("create");
346 return -1;
347 }
349 < 0) {
350 rplay_perror("rplay");
351 }
352
353 return 0;
354}
static int PlaceX
Definition add_window.c:82
size_t event_names_size(void)
Definition event_names.c:19
int event_num_by_name(const char *ename)
Definition event_names.c:45
void sound_clear_list(void)
Definition sound.c:127
void play_sound(int snd)
Definition sound.c:222
#define NMAGICEVENTS
Definition sound.c:55
RPLAY ** rp
Definition sound.c:19
#define NEVENTS
Definition sound.c:76
static int sound_from_config
Definition sound.c:22
void sound_load_list(void)
Definition sound.c:152
static int need_sound_init
Definition sound.c:21
void sound_set_from_config(void)
Definition sound.c:268
int set_sound_event_name(const char *ename, const char *soundfile)
Definition sound.c:311
static int sound_state
Definition sound.c:24
void toggle_sound(void)
Definition sound.c:278
static int sound_magic_event_name2num(const char *name)
Definition sound.c:58
void reread_sounds(void)
Definition sound.c:288
void sound_init(void)
Definition sound.c:84
static int sound_fd
Definition sound.c:23
static char hostname[200]
Definition sound.c:26
#define HOSTNAME_LEN
Definition sound.c:25
static const char * magic_events[]
Definition sound.c:51
int set_sound_event(int snd, const char *soundfile)
Definition sound.c:329
void play_startup_sound(void)
Definition sound.c:252
void play_exit_sound(void)
Definition sound.c:258
void set_sound_host(char *host)
Definition sound.c:297
static char * trim_spaces(char *str)
Definition sound.c:32