File: Audio.c

package info (click to toggle)
asclassic 1.1b-29
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 2,824 kB
  • ctags: 2,018
  • sloc: ansic: 21,407; makefile: 406; sh: 275
file content (503 lines) | stat: -rw-r--r-- 10,696 bytes parent folder | download | duplicates (5)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* 
 * Copyright (C) 1994 Mark Boyns (boyns@sdsu.edu) and
 *                    Mark Scott (mscott@mcd.mot.com)
 *
 * FvwmAudio version 1.1
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* ChangeLog

 * Fixed FvwmAudio to reflect the changes made to the module protocol.

 * Szijarto Szabolcs <saby@sch.bme.hu> provided FvwmSound code that used
 $HOSTDISPLAY for the rplay host.  The code has been added to FvwmAudio.

 * Fixed bugs reported by beta testers, thanks!

 * Builtin rplay support has been added to FvwmAudio.  This support is
 enabled when FvwmAudio is compiled with HAVE_RPLAY defined and when
 FvwmAudioPlayCmd is set to builtin-rplay.  I guess it's safe to say
 that FvwmSound is now obsolete. -- Mark Boyns 5/7/94
 
 * FvwmAudio is based heavily on an Fvwm module "FvwmSound" by Mark Boyns
 and the real credit for this really goes to him for the concept.
 I just stripped out the "rplay" library dependencies to allow generic
 audio play command support.

 */

/*
 * This module is based on FvwmModuleDebugger which has the following
 * copyright:
 *
 * This module, and the entire ModuleDebugger program, and the concept for
 * interfacing this module to the Window Manager, are all original work
 * by Robert Nation
 *
 * Copyright 1994, Robert Nation. No guarantees or warantees or anything
 * are provided or implied in any way whatsoever. Use this program at your
 * own risk. Permission to use this program for any purpose is given,
 * as long as the copyright is kept intact.
 */

/*
 * afterstep includes:
 */
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include "../../configure.h"
#include "../../afterstep/module.h"
#include "../../version.h"
#include "../../lib/aftersteplib.h"     

/*
 * rplay includes:
 */
#ifdef HAVE_RPLAY
#include <rplay.h>
#endif

#define BUILTIN_STARTUP		MAX_MESSAGES
#define BUILTIN_SHUTDOWN	MAX_MESSAGES+1
#define BUILTIN_UNKNOWN		MAX_MESSAGES+2
#define MAX_BUILTIN		3

#define BUFSIZE			512

/* globals */
char	*MyName;
int	fd_width;
int	fd[2];
char	audio_play_cmd_line[BUFSIZE], audio_play_dir[BUFSIZE];
time_t	audio_delay = 0;	/* seconds */
#ifdef HAVE_RPLAY
int	rplay_fd = -1;
#endif

/* prototypes */
void	Loop(int *);
void	process_message(unsigned long,unsigned long *);
void	DeadPipe(int);
void	config(char *);
void	done(int);
int     audio_play(short); 

/* define the message table */
char	*messages[MAX_MESSAGES+MAX_BUILTIN] =
{
	"toggle_paging",
	"new_page",
	"new_desk",
	"add_window",
	"raise_window",
	"lower_window",
	"configure_window",
	"focus_change",
	"destroy_window",
	"iconify",
	"deiconify",
	"window_name",
	"icon_name",
	"res_class",
	"res_name",
	"end_windowlist",
	"icon_location",
	"map",
/* add builtins here */
	"startup",
	"shutdown",
	"unknown",
};

/* define the sound table  */
char	*sound_table[MAX_MESSAGES+MAX_BUILTIN];

#ifdef HAVE_RPLAY
/* define the rplay table */
RPLAY	*rplay_table[MAX_MESSAGES+MAX_BUILTIN];
#endif

main(int argc, char **argv)
{
	char *temp, *s;

	if ((argc != 6)&&(argc != 7))
	{
		fprintf(stderr,"%s Version %s should only be executed by afterstep!\n",
			MyName, VERSION);
		exit(1);
	}

	/* Save our program  name - for error messages */
	temp = argv[0];
	s=strrchr(argv[0], '/');
	if (s != NULL)
		temp = s + 1;

	MyName = safemalloc(strlen(temp)+2);
	strcpy(MyName,"*");
	strcat(MyName, temp);

	/* Dead pipe == AS died */
	signal (SIGPIPE, DeadPipe);  
  
	fd[0] = atoi(argv[1]);
	fd[1] = atoi(argv[2]);

	audio_play_dir[0] = '\0';
	audio_play_cmd_line[0] = '\0';

	/*
	 * Read the sound configuration.
	 */
	config(argv[3]);

	/*
	 * Play the startup sound.
	 */
	audio_play(BUILTIN_STARTUP);
	SendText(fd,"Nop",0);
	Loop(fd);
}

/***********************************************************************
 *
 *  Procedure:
 *	config - read the sound configuration file.
 *
 ***********************************************************************/
void	config(char *config_file)
{
	FILE	*fp;
	char	buf[BUFSIZE];
	char	*message;
	char	*sound;
	char	*p;
	int	i, found;
#ifdef HAVE_RPLAY
	char	host[128];
	int	volume = RPLAY_DEFAULT_VOLUME;
	int	priority = RPLAY_DEFAULT_PRIORITY;
#endif
	
	fp = fopen(config_file, "r");
	if (fp == NULL)
	{
		done(1);
	}

	/*
	 * Intialize all the sounds.
	 */
	for (i = 0; i < MAX_MESSAGES+MAX_BUILTIN; i++) {
		sound_table[i] = NULL;
#ifdef HAVE_RPLAY
		rplay_table[i] = NULL;
#endif
	}

#ifdef HAVE_RPLAY
	strcpy(host, rplay_default_host());
#endif

	while (fgets(buf, sizeof(buf), fp))
	{
		buf[strlen(buf)-1] = '\0';
		if (buf[0] != '*')
		{
			continue;
		}
		/*
		 * Search for *ASAudio.
		 */
		if (mystrncasecmp(buf, MyName, strlen(MyName)) == 0)
		{
			p = strtok(buf, " \t");
			
			if (mystrcasecmp(p, "*AudioPlayCmd") == 0) {
				p = strtok(NULL, "\n"); /* allow parameters */
				if (p && *p) {
					strcpy(audio_play_cmd_line, p);
				}
			}
			else if (mystrcasecmp(p, "*AudioDir") == 0) {
				p = strtok(NULL, " \t");
				if (p && *p) {
					strcpy(audio_play_dir, p);
				}
			}
			else if (mystrcasecmp(p, "*AudioDelay") == 0) {
				p = strtok(NULL, " \t");
				if (p && *p) {
					audio_delay = atoi(p);
				}
			}
			
#ifdef HAVE_RPLAY
			/*
			 * Check for rplay configuration options.
			 */
			else if (mystrcasecmp(p, "*AudioRplayHost") == 0)
			{
				p = strtok(NULL, " \t");
				if (p && *p)
				{
					/*
					 * Check for environment variables like $HOSTDISPLAY.
					 */
					if (*p == '$')
					{
						char	*c1, *c2;
						c2 = host;
						for (c1 = (char *)getenv(p+1); *c1 && (*c1 != ':'); c1++)
						{
							*c2++ = *c1;
						}
						*c2 = '\0';
					}
					else
					{
						strcpy(host, p);
					}
				}
			}
			else if (mystrcasecmp(p, "*AudioRplayVolume") == 0)
			{
				p = strtok(NULL, " \t");
				if (p && *p) {
					volume = atoi(p);
				}
			}
			else if (mystrcasecmp(p, "*AudioRplayPriority") == 0)
			{
				p = strtok(NULL, " \t");
				if (p && *p) {
					priority = atoi(p);
				}
			}
#endif
			/*
			 * *Audio <message_type> <audio_file>
			 */
			else  {
				message = strtok(NULL, " \t");
				sound = strtok(NULL, " \t");
				
				if (!message || !*message || !sound || !*sound)
				{
					continue;
				}
				
				found = 0;
				
				for (i = 0; !found && i < MAX_MESSAGES+MAX_BUILTIN; i++)
				{
					if (mystrcasecmp(message, messages[i]) == 0) {
#ifdef HAVE_RPLAY
						rplay_table[i] = rplay_create(RPLAY_PLAY);
						rplay_set(rplay_table[i], RPLAY_APPEND,
							RPLAY_SOUND,	sound,
							RPLAY_PRIORITY,	priority,
							RPLAY_VOLUME,	volume,
							NULL);
#endif
						sound_table[i]=safemalloc(strlen(sound)+1);
						strcpy(sound_table[i],sound);
						found++;
					}
				}
			}
		}
	}

	fclose(fp);

#ifdef HAVE_RPLAY
	/*
	 * Builtin rplay support is enabled when FvwmAudioPlayCmd == builtin-rplay.
	 */
	if (mystrcasecmp(audio_play_cmd_line, "builtin-rplay") == 0)
	{
		rplay_fd = rplay_open(host);
		if (rplay_fd < 0)
		{
			rplay_perror("rplay_open");
			done(1);
		}
	}
#endif
}

/***********************************************************************
 *
 *  Procedure:
 *	Loop - wait for data to process
 *
 ***********************************************************************/
void Loop(int *fd)
{
	unsigned long	header[3], *body;
	char		*cbody;
	int		body_length,count,count2=0, total;
	time_t 		now, last_time = 0;
	unsigned long	code;
	
	while (1)
	{
		if ((count = read(fd[1],header,3*sizeof(unsigned long))) > 0)
		{
			/*
			 * Ignore messages that occur during the delay
			 * period.
			 */
			now = time(0);
			if (now < (last_time + audio_delay))
			{
				continue;
			}
			last_time = now;
			
			if(header[0] == START_FLAG)
			{
				body_length = header[2]-3;
				body = (unsigned long *)
					safemalloc(body_length * sizeof(unsigned long));
				cbody = (char *)body;
				total = 0;
				while(total < body_length*sizeof(unsigned long))
				{
					if((count2=read(fd[1],&cbody[total],
						body_length*sizeof(unsigned long)-total)) >0)
						total += count2;
					else if(count2 < 0)
						DeadPipe(0);
				}

				/*
				 * code will equal the number of shifts in the
				 * base-2 header[1] number.  Could use log here
				 * but this should be fast enough.
				 */
				code = -1;
				while (header[1])
				{
					code++;
					header[1] >>= 1;
				}
				
				/*
				 * Play the sound.
				 */
				if (code >= 0 && code < MAX_MESSAGES)
				{
					audio_play(code);
				}
				else
				{
					audio_play(BUILTIN_UNKNOWN);
				}
				
				free(body);
			}
		}
		if(count <= 0)
			DeadPipe(1);
	}
}


/***********************************************************************
 *
 *  Procedure:
 *	SIGPIPE handler - SIGPIPE means afterstep is dying
 *
 ***********************************************************************/
void DeadPipe(int nonsense)
{
	done(0);
}

/***********************************************************************
 *
 *  Procedure:
 *	done - common exit point for FvwmAudio.
 *
 ***********************************************************************/
void	done(int n)
{
	audio_play(BUILTIN_SHUTDOWN);
	exit(n);
}

/***********************************************************************
 *
 * Procedure:
 *
 *    audio_play - actually plays sound from lookup table
 *
 **********************************************************************/
int audio_play(short sound) 
{
	static char buf[BUFSIZE];

#ifdef HAVE_RPLAY
	if (rplay_fd != -1)
	{
		if (rplay_table[sound])
		{
			if (rplay(rplay_fd, rplay_table[sound]) < 0)
			{
				rplay_perror("rplay");
			}
		}
		return 0;
	}
#endif

	if (sound_table[sound])
	{
		memset(buf,NULL,BUFSIZE);

		/*
		 * Don't use audio_play_dir if it's NULL or if the sound file
		 * is an absolute pathname.
		 */
		if (audio_play_dir[0] == '\0' || sound_table[sound][0] == '/')
		{
			sprintf(buf,"%s %s", audio_play_cmd_line, sound_table[sound]);
		}
		else
		{
			sprintf(buf,"%s %s/%s", audio_play_cmd_line, audio_play_dir,
				sound_table[sound]);
		}
		return system(buf);
	}  

	return 1;
}