summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-sound.c
blob: 5e8aacfddff1ecd5b9edfc11b6e347e0373d6517 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-sound.c: manage the sound playing process and other sound utilities
  
   Copyright (C) 1999, 2000 Eazel, Inc.
  
   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.
  
   Author: Andy Hertzfeld <andy@eazel.com>
*/

#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#ifdef HAVE_WAIT_H
#  include <wait.h>
#else
#  ifdef HAVE_SYS_WAIT_H
#    include <sys/wait.h>
#  endif
#endif
#include <esd.h>

#include <eel/eel-gconf-extensions.h>
#include "nautilus-sound.h"

/* Keep track of the sound playing process */
#define CURRENT_SOUND_STATE_KEY "/apps/nautilus/sound_state"

static gboolean
kill_sound_if_necessary (void)
{
	pid_t child;
	int status_result;
	pid_t sound_process;
	
	/* fetch the sound state */
	sound_process = eel_gconf_get_integer (CURRENT_SOUND_STATE_KEY);
	
	/* if there was a sound playing, kill it */
	if (sound_process > 0) {
		kill (-sound_process, SIGTERM);
 		child = waitpid (sound_process, &status_result, 0);
		return TRUE;
	}

	return FALSE;
}

/* initialize_sound is called at application start up time.  It puts the sound system
   into a quiescent state */
void
nautilus_sound_init (void)
{ 	
	if (eel_gconf_key_is_writable (CURRENT_SOUND_STATE_KEY)) {
		eel_gconf_set_integer (CURRENT_SOUND_STATE_KEY, 0);
		eel_gconf_suggest_sync ();
	}
}

/* if there is a sound registered, kill it, and register the empty sound */
void
nautilus_sound_kill_sound (void)
{
	/* if not writable, we can't do anything, definately don't
	   kill since the key is bogus */
	if (!eel_gconf_key_is_writable (CURRENT_SOUND_STATE_KEY)) {
		return;
	}

	/* if there is a sound in progress, kill it */
	if (kill_sound_if_necessary ()) {
		/* set the process state to quiescent */
		eel_gconf_set_integer (CURRENT_SOUND_STATE_KEY, 0);
		eel_gconf_suggest_sync ();
	}
}

/* register a new sound process, including kill any old one if necessary */
void
nautilus_sound_register_sound (pid_t sound_process)
{
	/* if not writable, we can't do anything, definately don't
	   kill since the key is bogus */
	if (!eel_gconf_key_is_writable (CURRENT_SOUND_STATE_KEY)) {
		return;
	}

	/* if there is a sound in progress, kill it */
	kill_sound_if_necessary ();
	
	/* record the new sound process ID */
	eel_gconf_set_integer (CURRENT_SOUND_STATE_KEY, sound_process);
	eel_gconf_suggest_sync ();
}

/* This function does two things. First it checks to see a sound is currently playing.  If it is,
 * it returns the process id of the external application playing the sound. If no sound is playing,
 * it return the value set in nautilus_sound_init() when system audio output capabilites
 * were queried.
 */
gboolean
nautilus_sound_can_play_sound (void)
{
	int sound_process, open_result;

	/* If we can't write to the key we won't be able to ever
	   stop the process ... */
	if (!eel_gconf_key_is_writable (CURRENT_SOUND_STATE_KEY)) {
		return FALSE;
	}
	
	/* first see if there's already one in progress; if so, return true */
	sound_process = eel_gconf_get_integer (CURRENT_SOUND_STATE_KEY);
	if (sound_process > 0) {
		return TRUE;
	}
		
	/* Now check and see if system has audio out capabilites */
	open_result = esd_open_sound (NULL);
	if (open_result == -1) {
 		return FALSE;
	} else {
		esd_close (open_result);
		return TRUE;
	}
}