1 /*
2  *  Emi Clock $B!=(B $B%5%&%s%I=hM}(B
3  */
4 
5 /*
6  *  Copyright (c) 1994, 1995, 1997, 1999 Masayuki Koba
7  *
8  *  $BK\%=%U%H%&%'%"$N%=!<%9$d%P%$%J%j$r:FG[I[$9$k>l9g$O!"<!$N>r7o$r=e<i$7$F(B
9  *  $B2<$5$$!#(B
10  *
11  *  1. $BK\%=%U%H%&%'%"$rF~<j$7$?J}$K$O!"(BX11$BHG(B Emi Clock $B$N;HMQ8"$H!"Bh;0<T(B
12  *     $B$X$N:FG[I[8"$,G'$a$i$l$^$9!#$?$@$7!":FG[I[$K4X$7$F$O!"F~<j;~$N%*%j(B
13  *     $B%8%J%k$N$^$^2~JQ$;$:$K9T$&$3$H$,>r7o$G$9!#(B
14  *  2. $BK\%=%U%H%&%'%"$N0lIt$^$?$OA4It$rCx:n8"<T$KL5CG$G2~JQ$7$FG[I[$9$k$3(B
15  *     $B$H$O$G$-$^$;$s!#(B
16  *  3. $BK\%=%U%H%&%'%"$N0lIt$^$?$OA4It$rCx:n8"<T$KL5CG$GFs<!MxMQ$9$k$3$H$O(B
17  *     $B$G$-$^$;$s!#(B
18  *  4. $BK\%=%U%H%&%'%"$r%7%9%F%`$K%P%s%I%k$7$?$j!"%7%9%F%`$NDI2C%Q%C%1!<%8(B
19  *     $B$H$7$FBh;0<T$K:FG[I[$7$?$j$9$k>l9g$O!";vA0$KCx:n8"<T$K5v2D$,I,MW$G(B
20  *     $B$9!#(B
21  *  5. $BK\%=%U%H%&%'%"$r>&MQ$K;HMQ$9$k>l9g(B($B6bA,E*Mx1W$rF@$k>l9g(B)$B$O!";vA0$K(B
22  *     $BCx:n8"<T$K5v2D$,I,MW$G$9!#$3$N>l9g!"4pK\E*$KM-=~$H$J$j$^$9!#(B
23  *  6. $BK\%=%U%H%&%'%"$rMxMQ$9$k$3$H$K$h$C$FH/@8$7$?$$$+$J$kB;32$b!"Cx:n8"(B
24  *     $B<T$OIi$o$J$$$b$N$H$7$^$9!#$3$l$K9g0U$G$-$J$$>l9g$O!";HMQ8"$,$J$$$b(B
25  *     $B$N$H$7$^$9!#(B
26  *  7. $B!H(BEmi Clock$B!I$N>&I8$*$h$SK\%=%U%H%&%'%"$N2hA|$d%G%6%$%s$K4X$9$kCx:n(B
27  *     $B8"$O!"(BMotosoft $B$3$H!HK\(B $B=SLi!I;a$,M-$7$^$9!#(B
28  *  8. $B!H(BEmi Clock$B!I$N>&I8$*$h$S2hA|$d%G%6%$%s$O!"(BX11$BHG(B Emi Clock $B3+H/$N$?(B
29  *     $B$a!"(BMotosoft $B$h$j!H8E>l(B $B@59T!I$X8D?ME*$K%i%$%;%s%96!M?$5$l$F$^$9!#(B
30  *     $BBh;0<T$XFs<!%i%$%;%s%96!M?$9$k$3$H$OG'$a$i$l$F$*$j$^$;$s!#(B
31  *  9. Motosoft $B$H8E>l$KL5CG$G!"K\%=%U%H%&%'%"$N2hA|%G!<%?$rFs<!MxMQ$9$k$3(B
32  *     $B$H$r6X;_$7$^$9!#(B
33  * 10. $B$3$3$K5-=R$7$?0J30$N8"Mx$K$D$$$F$O!"F|K\9q$NCx:n8"K!$K$h$k$b$N$H$7(B
34  *     $B$^$9!#(B
35  */
36 
37 
38 /* $B#X%D!<%k%-%C%H%W%m%0%i%_%s%0$KI,MW$J%X%C%@!<(B */
39 #include <X11/Intrinsic.h>		/* $B%$%s%H%j%s%7%C%/$NDj5A(B */
40 
41 /* kernel $B4X78$N%X%C%@!<(B */
42 #include <sys/param.h>			/* $B%7%9%F%`%Q%i%a!<%?(B */
43 
44 /* $B#C8@8l%i%$%V%i%j(B */
45 #include <stdio.h>			/* $BI8=`F~=PNO%i%$%V%i%j(B */
46 
47 /* Emi Clock $BFH<+$N%X%C%@!<(B */
48 #include "config.h"			/* $B%3%s%Q%$%k4D6-$NDj5A(B */
49 #include "include/system.h"		/* $B%7%9%F%`4D6-$N:9J,5[<}(B */
50 #include "include/const.h"		/* $BDj?tDj5A(B */
51 #include "include/types.h"		/* $BFC<l$J7?$NDj5A(B */
52 #include "include/public.h"		/* $BHFMQ4X?t!?30ItDj5A%G!<%?$N@k8@(B */
53 #include "include/util.h"		/* $B%^%/%mDj5A(B */
54 #include "include/sound.h"		/* $B%5%&%s%I4D6-$N@_Dj(B */
55 
56 /* RCS ID */
57 rcsId(soundId, "$Id: sound.c,v 1.2 1999/09/02 14:33:49 koba Exp $")
58 
59 /* $B%0%m!<%P%k(B */
60 static char		*hourlyChimeFileName = NULL;
61 static char		*halfHourChimeFileName = NULL;
62 static char		*alarmFileName = NULL;
63 static SoundCacheBuffer	hourlyChimeCache;
64 static SoundCacheBuffer	halfHourChimeCache;
65 static Boolean		isHourlyChimeCached = False;
66 static Boolean		isHalfHourChimeCached = False;
67 
68 
69 /*
70  *  $B#O#S@lMQ%5%&%s%I=hM}$r=q$$$?%U%!%$%k$NFI$_9~$_(B
71  */
72 #ifdef	USE_SOUND
73 
74 #ifndef	USE_NETAUDIO
75 
76 /* NEWS-OS */
77 #ifdef	__sony_news
78 #include "sys/NEWS-OS/audio.c"
79 #endif	/* __sony_news */
80 
81 /* IRIX */
82 #ifdef	sgi
83 #include "sys/IRIX/audio.c"
84 #endif	/* sgi */
85 
86 /* HP-UX */
87 #ifdef	__hpux
88 #include "sys/HP-UX/audio.c"
89 #endif	/* __hpux */
90 
91 /* SunOS */
92 #ifdef	sun
93 #include "sys/SunOS/audio.c"
94 #endif	/* sun */
95 
96 /* BSDI BSD/OS */
97 #ifdef	__bsdi__
98 #include "sys/BSD-OS/audio.c"
99 #endif	/* __bsdi__ */
100 
101 /* Linux */
102 #ifdef	linux
103 #include "sys/Linux/audio.c"
104 #endif	/* linux */
105 
106 /* FreeBSD */
107 #ifdef	__FreeBSD__
108 #include "sys/FreeBSD/audio.c"
109 #endif	/* FreeBSD */
110 
111 #else	/* USE_NETAUDIO */
112 
113 /* netaudio (NCDaudio) */
114 #include "sys/netaudio/audio.c"
115 
116 #endif	/* !USE_NETAUDIO */
117 
118 #endif	/* USE_SOUND */
119 
120 
121 /* $B%m!<%+%k4X?t$N%W%m%H%?%$%W@k8@(B */
122 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
123 static int PlaySound __P((char *));
124 static Boolean IsSoundCacheAvailable __P((void));
125 static int CacheSound __P((char *, SoundCacheBuffer *));
126 static int PlayCacheSound __P((SoundCacheBuffer *));
127 static char *FullPathSoundFileName __P((char *));
128 #endif
129 static void FreeCacheSound __P((SoundCacheBuffer *));
130 
131 
132 /*
133  *  $B%5%&%s%I4D6-$N=i4|2=(B
134  */
135 void
SoundEnvInit()136 SoundEnvInit()
137 {
138 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
139     S_SoundEnvInit();
140 #endif
141 }
142 
143 
144 /*
145  *  $B%5%&%s%I%G%P%$%9$NB8:_H=Dj(B
146  */
147 Boolean
IsSoundAvailable()148 IsSoundAvailable()
149 {
150 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
151     return(S_IsSoundAvailable());
152 #else
153     return(False);
154 #endif
155 }
156 
157 
158 /*
159  *  $B5/F02;!V$_$e$C!*!W$N:F@8(B
160  */
161 void
PlayStartupSound()162 PlayStartupSound()
163 {
164 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
165     char	*fullPathFileName;
166 
167     fullPathFileName = FullPathSoundFileName(GetStartupSoundFileName());
168     PlaySound(fullPathFileName);
169     XtFree(fullPathFileName);
170 #endif
171 }
172 
173 
174 /*
175  *  $B!H5/F02;!V$_$e$C!*!W!I$N%5%&%s%I%U%!%$%kL><hF@(B
176  */
177 char *
GetStartupSoundFileName()178 GetStartupSoundFileName()
179 {
180 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
181     return(STARTUP_SOUND);
182 #else
183     return("");
184 #endif
185 }
186 
187 
188 /*
189  *  0$BJ,%A%c%$%`%5%&%s%I$N5-21(B
190  */
191 void
StoreHourlyChimeFileName(filename)192 StoreHourlyChimeFileName(filename)
193     char	*filename;
194 {
195     if (hourlyChimeFileName != NULL) {
196 	XtFree(hourlyChimeFileName);
197     }
198     hourlyChimeFileName = (char *)XtMalloc(strlen(filename) + 1);
199     strcpy(hourlyChimeFileName, filename);
200 
201 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
202     if (IsSoundCacheAvailable()) {
203 	char	*fullPathFileName;
204 
205 	if (isHourlyChimeCached) {
206 	    FreeCacheSound(&hourlyChimeCache);
207 	}
208 
209 	fullPathFileName = FullPathSoundFileName(hourlyChimeFileName);
210 
211 	if (CacheSound(fullPathFileName, &hourlyChimeCache)
212 							== SOUND_NO_ERROR) {
213 	    isHourlyChimeCached = True;
214 	} else {
215 	    isHourlyChimeCached = False;
216 	}
217 
218 	XtFree(fullPathFileName);
219     }
220 #endif
221 }
222 
223 
224 /*
225  *  0$BJ,%A%c%$%`%5%&%s%I$N%U%!%$%kL><hF@(B
226  */
227 char *
GetHourlyChimeFileName()228 GetHourlyChimeFileName()
229 {
230     return(hourlyChimeFileName);
231 }
232 
233 
234 /*
235  *  0$BJ,%A%c%$%`%5%&%s%I$N:F@8(B
236  */
237 void
PlayHourlyChime()238 PlayHourlyChime()
239 {
240 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
241     char	*fullPathFileName;
242 
243     fullPathFileName = FullPathSoundFileName(hourlyChimeFileName);
244 
245     if (hourlyChimeFileName != NULL) {
246 	if (!isHourlyChimeCached) {
247 	    PlaySound(fullPathFileName);
248 	} else {
249 	    if (PlayCacheSound(&hourlyChimeCache) != SOUND_NO_ERROR) {
250 		/* $B%-%c%C%7%e2=$5$l$?%5%&%s%I$r@8@.$G$-$J$+$C$?(B */
251 		PlaySound(fullPathFileName);
252 	    }
253 	}
254     }
255 
256     XtFree(fullPathFileName);
257 #endif
258 }
259 
260 
261 /*
262  *  30$BJ,%A%c%$%`%5%&%s%I$N5-21(B
263  */
264 void
StoreHalfHourChimeFileName(filename)265 StoreHalfHourChimeFileName(filename)
266     char	*filename;
267 {
268     if (halfHourChimeFileName != NULL) {
269 	XtFree(halfHourChimeFileName);
270     }
271     halfHourChimeFileName = (char *)XtMalloc(strlen(filename) + 1);
272     strcpy(halfHourChimeFileName, filename);
273 
274 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
275     if (IsSoundCacheAvailable()) {
276 	char	*fullPathFileName;
277 
278 	if (isHalfHourChimeCached) {
279 	    FreeCacheSound(&halfHourChimeCache);
280 	}
281 
282 	fullPathFileName = FullPathSoundFileName(halfHourChimeFileName);
283 
284 	if (CacheSound(fullPathFileName, &halfHourChimeCache)
285 							== SOUND_NO_ERROR) {
286 	    isHalfHourChimeCached = True;
287 	} else {
288 	    isHalfHourChimeCached = False;
289 	}
290 
291 	XtFree(fullPathFileName);
292     }
293 #endif
294 }
295 
296 
297 /*
298  *  30$BJ,%A%c%$%`%5%&%s%I$N%U%!%$%kL><hF@(B
299  */
300 char *
GetHalfHourChimeFileName()301 GetHalfHourChimeFileName()
302 {
303     return(halfHourChimeFileName);
304 }
305 
306 
307 /*
308  *  30$BJ,%A%c%$%`%5%&%s%I$N:F@8(B
309  */
310 void
PlayHalfHourChime()311 PlayHalfHourChime()
312 {
313 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
314     char	*fullPathFileName;
315 
316     fullPathFileName = FullPathSoundFileName(halfHourChimeFileName);
317 
318     if (halfHourChimeFileName != NULL) {
319 	if (!isHalfHourChimeCached) {
320 	    PlaySound(fullPathFileName);
321 	} else {
322 	    if (PlayCacheSound(&halfHourChimeCache) != SOUND_NO_ERROR) {
323 		/* $B%-%c%C%7%e2=$5$l$?%5%&%s%I$r@8@.$G$-$J$+$C$?(B */
324 		PlaySound(fullPathFileName);
325 	    }
326 	}
327     }
328 
329     XtFree(fullPathFileName);
330 #endif
331 }
332 
333 
334 /*
335  *  $B%"%i!<%`%5%&%s%I$N5-21(B
336  */
337 void
StoreAlarmFileName(filename)338 StoreAlarmFileName(filename)
339     char	*filename;
340 {
341     if (alarmFileName != NULL) {
342 	XtFree(alarmFileName);
343     }
344     alarmFileName = (char *)XtMalloc(strlen(filename) + 1);
345     strcpy(alarmFileName, filename);
346 }
347 
348 
349 /*
350  *  $B%"%i!<%`%5%&%s%I$N%U%!%$%kL><hF@(B
351  */
352 char *
GetAlarmFileName()353 GetAlarmFileName()
354 {
355     return(alarmFileName);
356 }
357 
358 
359 /*
360  *  $B%"%i!<%`%5%&%s%I$N:F@8(B
361  */
362 void
PlayAlarm()363 PlayAlarm()
364 {
365 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
366     char	*fullPathFileName;
367 
368     fullPathFileName = FullPathSoundFileName(alarmFileName);
369     PlaySound(fullPathFileName);
370     XtFree(fullPathFileName);
371 #endif
372 }
373 
374 
375 /*
376  *  $B%-%c%C%7%e2=$5$l$?%A%c%$%`$N3+J|(B
377  */
378 void
ClearChimeCache()379 ClearChimeCache()
380 {
381     if (isHourlyChimeCached) {
382     	FreeCacheSound(&hourlyChimeCache);
383     }
384 
385     if (isHalfHourChimeCached) {
386 	FreeCacheSound(&halfHourChimeCache);
387     }
388 }
389 
390 
391 /*
392  *  $B%5%&%s%I:F@8(B
393  */
394 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
395 static int
PlaySound(filename)396 PlaySound(filename)
397     char	*filename;
398 {
399     return(S_PlaySound(filename));
400 }
401 #endif
402 
403 
404 /*
405  *  $B%5%&%s%I$N%-%c%C%7%e2=2DG=!?IT2DG=H=Dj(B
406  */
407 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
408 static Boolean
IsSoundCacheAvailable()409 IsSoundCacheAvailable()
410 {
411     if (!app_data.isNoSoundCache) {
412 	return(S_IsSoundCacheAvailable());
413     }
414 
415     return(False);
416 }
417 #endif
418 
419 
420 /*
421  *  $B%5%&%s%I$N%-%c%C%7%e2=(B
422  */
423 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
424 static int
CacheSound(filename,cacheBufferPtr)425 CacheSound(filename, cacheBufferPtr)
426     char		*filename;
427     SoundCacheBuffer	*cacheBufferPtr;
428 {
429     return(S_CacheSound(filename, cacheBufferPtr));
430 }
431 #endif
432 
433 
434 /*
435  *  $B%-%c%C%7%e2=$5$l$?%5%&%s%I$N:F@8(B
436  */
437 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
438 static int
PlayCacheSound(cacheBufferPtr)439 PlayCacheSound(cacheBufferPtr)
440     SoundCacheBuffer	*cacheBufferPtr;
441 {
442     return(S_PlayCacheSound(cacheBufferPtr));
443 }
444 #endif
445 
446 
447 /*
448  *  $B%-%c%C%7%e2=$5$l$?%5%&%s%I$N3+J|(B
449  */
450 static void
FreeCacheSound(cacheBufferPtr)451 FreeCacheSound(cacheBufferPtr)
452     SoundCacheBuffer	*cacheBufferPtr;
453 {
454 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
455     S_FreeCacheSound(cacheBufferPtr);
456 #endif
457 }
458 
459 
460 /*
461  *  $B%5%&%s%I4D6-$N8e;OKv(B
462  */
463 void
SoundEnvDispose()464 SoundEnvDispose()
465 {
466     if (hourlyChimeFileName != NULL) {
467 	XtFree(hourlyChimeFileName);
468     }
469 
470     if (halfHourChimeFileName != NULL) {
471 	XtFree(halfHourChimeFileName);
472     }
473 
474     if (alarmFileName != NULL) {
475 	XtFree(alarmFileName);
476     }
477 
478 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
479     S_SoundEnvDispose();
480 #endif
481 }
482 
483 
484 /*
485  *  $B%5%&%s%I%U%!%$%kL>$N%U%k%Q%9<hF@(B
486  */
487 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
488 static char *
FullPathSoundFileName(filename)489 FullPathSoundFileName(filename)
490     char	*filename;
491 {
492     char	*fullPathFileName;
493 
494     fullPathFileName = (char *)XtMalloc(sizeof(char) *
495 			(strlen(SOUND_DIRECTORY) + strlen(filename) + 2));
496     sprintf(fullPathFileName, "%s/%s", SOUND_DIRECTORY, filename);
497     return(fullPathFileName);
498 }
499 #endif
500 
501 
502 /*
503  *  $B%5%&%s%I%U%!%$%k$NB8:_H=Dj(B
504  */
505 Boolean
IsSoundFileExist(filename)506 IsSoundFileExist(filename)
507     char	*filename;
508 {
509 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
510     char	*fullPathFileName;
511     Boolean	result;
512 
513     fullPathFileName = FullPathSoundFileName(filename);
514     result = IsFileExist(fullPathFileName);
515     XtFree(fullPathFileName);
516 
517     return(result);
518 #else
519     return(False);
520 #endif
521 }
522 
523 
524 /*
525  *  $B%5%&%s%I%U%!%$%k$N%F%9%H:F@8(B
526  */
527 int
SoundTest(filename)528 SoundTest(filename)
529     char	*filename;
530 {
531 #if defined(USE_SOUND) && defined(HAS_SOUND_CODE)
532     char	*fullPathFileName;
533     int		status;
534 
535     fullPathFileName = FullPathSoundFileName(filename);
536     status = PlaySound(fullPathFileName);
537     XtFree(fullPathFileName);
538 
539     return(status);
540 #else
541     return(SOUND_NOT_AVAILABLE);
542 #endif
543 }
544