File: espeakup.c

package info (click to toggle)
espeakup 1%3A0.71-19
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 400 kB
  • sloc: ansic: 601; sh: 192; makefile: 81
file content (156 lines) | stat: -rw-r--r-- 3,297 bytes parent folder | download | duplicates (3)
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
/*
 *  espeakup - interface which allows speakup to use espeak
 *
 *  Copyright (C) 2008 William Hubbs
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#include "espeakup.h"

/* program version */
const char *Version = "0.71";

/* path to our pid file */
const char *pidPath = "/var/run/espeakup.pid";

/* default voice settings */
const int defaultFrequency = 5;
const int defaultPitch = 5;
const int defaultRate = 5;
const int defaultVolume = 5;

char *defaultVoice = NULL;
int debug = 0;

int espeakup_is_running(void)
{
	int rc;
	FILE *pidFile;
	pid_t pid;

	rc = 0;
	pidFile = fopen(pidPath, "r");
	if (pidFile) {
		fscanf(pidFile, "%d", &pid);
		fclose(pidFile);
		if (!kill(pid, 0) || errno != ESRCH)
			rc = 1;
	}
	return rc;
}

int create_pid_file(void)
{
	FILE *pidFile;

	pidFile = fopen(pidPath, "w");
	if (!pidFile)
		return -1;

	fprintf(pidFile, "%d\n", getpid());
	fclose(pidFile);
	return 0;
}

void espeakup_sighandler(int sig)
{
	if (debug)
		printf("Caught signal %i\n", sig);

	/* clear the queue */
	queue_clear();

	/* shut down espeak and close the softsynth */
	espeak_Terminate();
	close_softsynth();

	if (!debug)
		unlink(pidPath);
	exit(0);
}

int main(int argc, char **argv)
{
	pthread_t queue_thread_id;
	struct synth_t s = {
		.voice = "",
	};

	/* process command line options */
	process_cli(argc, argv);

	/* Is the espeakup daemon running? */
	if (espeakup_is_running()) {
		printf("Espeakup is already running!\n");
		return 1;
	}

	/* open the softsynth. */
	if (! open_softsynth()) {
		perror("Unable to open the softsynth device");
		return 3;
	}

	/* register signal handler */
	signal(SIGINT, espeakup_sighandler);
	signal(SIGTERM, espeakup_sighandler);

	if (!debug) {
		/* become a daemon */
		daemon(0, 1);
	}

	/* initialize espeak */
	espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, NULL, 0);

	/* Setup initial voice parameters */
	if (defaultVoice) {
		set_voice(&s, defaultVoice);
		free(defaultVoice);
		defaultVoice = NULL;
	}
	set_frequency(&s, defaultFrequency, ADJ_SET);
	set_pitch(&s, defaultPitch, ADJ_SET);
	set_rate(&s, defaultRate, ADJ_SET);
	set_volume(&s, defaultVolume, ADJ_SET);
	espeak_SetParameter(espeakCAPITALS, 0, 0);

	/* Spawn our queue-processing thread. */
	int err = pthread_create(&queue_thread_id, NULL, &queue_runner, &s);
	if (err != 0) {
		return 4;
	}

	if (!debug) {
		/* We are now ready, write our pid file. */
		if (create_pid_file() < 0) {
			perror("Unable to create pid file");
			return 2;
		}
	}

	/* run the main loop */
	main_loop(&s);

	return 0;
}