-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathlibspeechd.c
2224 lines (1906 loc) · 53.8 KB
/
libspeechd.c
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
libspeechd.c - Shared library for easy access to Speech Dispatcher functions
*
* Copyright (C) 2001, 2002, 2003, 2006, 2007, 2008 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1, or (at your option)
* any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* $Id: libspeechd.c,v 1.37 2008-12-23 09:15:32 pdm Exp $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <wchar.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <glib.h>
#include <errno.h>
#include <assert.h>
#include <netdb.h>
/*
* This is needed because speechd_types.h is in a different location in
* the source tree's include directory than it will be when it is
* installed on the user's system.
*/
#include <speechd_types.h>
#include <speechd_defines.h>
#include "libspeechd.h"
/* Comment/uncomment to switch debugging on/off */
// #define LIBSPEECHD_DEBUG 1
/* Unless there is an fatal error, it doesn't print anything */
#define SPD_FATAL(msg) { printf("Fatal error (libspeechd) [%s:%d]:"msg, __FILE__, __LINE__); fflush(stdout); exit(EXIT_FAILURE); }
/* -------------- Private functions headers ------------------------*/
#ifdef LIBSPEECHD_DEBUG
static FILE *spd_debug = NULL;
#endif
static int spd_set_priority(SPDConnection * connection, SPDPriority priority);
static char *escape_dot(const char *text);
static int isanum(char *str);
static char *get_reply(SPDConnection * connection);
static int get_err_code(char *reply);
static char *get_param_str(char *reply, int num, int *err);
static char *get_param_str_and_advance(char **reply, int *err);
static int get_param_int(char *reply, int num, int *err);
static int ret_ok(char *reply);
static void SPD_DBG(char *format, ...);
static void *spd_events_handler(void *);
static const int range_low = -100;
static const int range_high = 100;
pthread_mutex_t spd_logging_mutex = PTHREAD_MUTEX_INITIALIZER;
struct SPDConnection_threaddata {
pthread_t events_thread;
pthread_cond_t cond_reply_ready;
pthread_mutex_t mutex_reply_ready;
pthread_cond_t cond_reply_ack;
pthread_mutex_t mutex_reply_ack;
};
/*
* Added by Willie Walker - strndup was a GNU libc extensions
* that was adopted in the POSIX.1-2008 standard, but is not yet found
* on all systems.
*/
#ifndef HAVE_STRNDUP
char *strndup(const char *s, size_t n)
{
size_t nAvail;
char *p;
if (!s)
return 0;
if (strlen(s) > n)
nAvail = n + 1;
else
nAvail = strlen(s) + 1;
p = malloc(nAvail);
memcpy(p, s, nAvail);
p[nAvail - 1] = '\0';
return p;
}
#endif /* HAVE_STRNDUP */
#define SPD_REPLY_BUF_SIZE 65536
/** Read from the connection's buffer or socket until a newline is
read. Return a pointer to the beginning of the data, and the
length of the data (including newline). The returned value will
not be null-terminated, and will include the newline. Note that
the returned pointer will only be valid until the next call to
get_line.
If, after SPD_REPLY_BUF_SIZE bytes, there is no newline (or in the
event of a read error) return NULL.
Unlike getline, this does not handle embedded \0 bytes.
*/
static
char *get_line(SPDConnection * conn, int *n)
{
int bytes;
int i;
char *ret = NULL;
int search_start = conn->buf_start;
int message_prefix_len;
while (1) {
for (i = search_start; i < conn->buf_used; i++) {
if (conn->buf[i] == '\n') {
*n = i + 1 - conn->buf_start;
ret = conn->buf + conn->buf_start;
conn->buf_start = i + 1;
return ret;
}
}
if (conn->buf_start != 0) {
message_prefix_len = conn->buf_used - conn->buf_start;
memmove(conn->buf, conn->buf + conn->buf_start,
message_prefix_len);
search_start = message_prefix_len;
conn->buf_used = message_prefix_len;
conn->buf_start = 0;
}
if (conn->buf_used == SPD_REPLY_BUF_SIZE) {
SPD_FATAL
("No newline after reading SPD_REPLY_BUF_SIZE");
return NULL;
}
bytes =
read(conn->socket, conn->buf + conn->buf_used,
SPD_REPLY_BUF_SIZE - conn->buf_used);
if (bytes == -1)
return NULL;
if (bytes == 0) {
errno = ECONNRESET;
return NULL;
}
conn->buf_used += bytes;
}
}
/* --------------------- Public functions ------------------------- */
/* Determine address for the unix socket */
static char *_get_default_unix_socket_name(void)
{
GString *socket_filename;
char *h;
const char *rundir = g_get_user_runtime_dir();
socket_filename = g_string_new("");
g_string_printf(socket_filename, "%s/speech-dispatcher/speechd.sock",
rundir);
// Do not return glib string, but glibc string...
h = strdup(socket_filename->str);
g_string_free(socket_filename, 1);
return h;
}
void SPDConnectionAddress__free(SPDConnectionAddress * address)
{
if (!address)
return;
free(address->unix_socket_name);
free(address->inet_socket_host);
free(address->dbus_bus);
free(address);
}
SPDConnectionAddress *spd_get_default_address(char **error)
{
const gchar *env_address = g_getenv("SPEECHD_ADDRESS");
gchar **pa; /* parsed address */
SPDConnectionAddress *address = malloc(sizeof(SPDConnectionAddress));
address->unix_socket_name = NULL;
address->inet_socket_host = NULL;
address->dbus_bus = NULL;
if (env_address == NULL) { // Default method = unix sockets
address->method = SPD_METHOD_UNIX_SOCKET;
address->unix_socket_name = _get_default_unix_socket_name();
} else {
pa = g_strsplit(env_address, ":", 0);
assert(pa);
if (!g_strcmp0(pa[0], "unix_socket") || pa[0] == NULL) { // Unix sockets
address->method = SPD_METHOD_UNIX_SOCKET;
if (pa[1] == NULL) {
address->unix_socket_name =
_get_default_unix_socket_name();
} else {
address->unix_socket_name = strdup(pa[1]);
}
} else if (!g_strcmp0(pa[0], "inet_socket")) { // Inet sockets
address->method = SPD_METHOD_INET_SOCKET;
if (pa[1] == NULL) {
address->inet_socket_host = strdup("127.0.0.1");
address->inet_socket_port = 6560;
} else {
address->inet_socket_host = strdup(pa[1]);
if (pa[2] == NULL) {
address->inet_socket_port =
SPEECHD_DEFAULT_PORT;
} else {
address->inet_socket_port = atoi(pa[2]);
}
}
} else { // Unknown or unsupported method requested
*error =
strdup
("Unknown or unsupported communication method");
SPDConnectionAddress__free(address);
address = NULL;
}
g_strfreev(pa);
}
return address;
}
static void _init_debug(void)
{
#ifdef LIBSPEECHD_DEBUG
if (!spd_debug) {
spd_debug = fopen("/tmp/libspeechd.log", "w");
if (spd_debug == NULL)
SPD_FATAL("COULDN'T ACCESS FILE INTENDED FOR DEBUG");
SPD_DBG("Debugging started");
}
#endif /* LIBSPEECHD_DEBUG */
}
/* Opens a new Speech Dispatcher connection.
* Returns socket file descriptor of the created connection
* or -1 if no connection was opened. */
SPDConnection *spd_open(const char *client_name, const char *connection_name,
const char *user_name, SPDConnectionMode mode)
{
char *error;
int autospawn = 1;
SPDConnection *conn;
conn = spd_open2(client_name, connection_name, user_name,
mode, NULL, autospawn, &error);
if (!conn) {
_init_debug();
assert(error);
SPD_DBG("Could not connect to Speech Dispatcher: %s", error);
free(error);
}
return conn;
}
#define MAX_IP_SIZE 16+1
/* TODO: This only works in IPV4 */
static char *resolve_host(char *host_name_or_ip, int *is_localhost,
gchar ** error)
{
struct addrinfo *addr_result;
int err;
char *resolve_buffer = malloc(MAX_IP_SIZE * sizeof(char));
const char *resolved_ip = NULL;
char *ip;
*error = NULL;
struct sockaddr_in *addr_in;
if (resolve_buffer == NULL) {
*error = g_strdup("Failed to allocate memory.");
return NULL;
}
err = getaddrinfo(host_name_or_ip, 0, NULL, &addr_result);
if (err) {
*error =
g_strdup_printf("Can't resolve address %d due to error %s:",
err, gai_strerror(err));
free(resolve_buffer);
return NULL;
}
/* Take the first address returned as we are only interested in host ip */
addr_in = (struct sockaddr_in *)addr_result->ai_addr;
resolved_ip =
inet_ntop(AF_INET, &(addr_in->sin_addr.s_addr), resolve_buffer,
MAX_IP_SIZE);
if (resolved_ip == NULL) {
*error =
g_strdup_printf
("Could not convert address, due to the following error: %s",
strerror(errno));
freeaddrinfo(addr_result);
free(resolve_buffer);
return NULL;
}
if (!strncmp(resolved_ip, "127.", 4)) {
*is_localhost = 1;
/* In case of local addresses, use 127.0.0.1 which is guaranteed
to be local and the server listens on it */
free(resolve_buffer);
ip = strdup("127.0.0.1");
} else {
*is_localhost = 0;
ip = resolve_buffer;
}
freeaddrinfo(addr_result);
return ip;
}
static int
spawn_server(const SPDConnectionAddress * address, int is_localhost,
gchar ** spawn_error)
{
gchar *speechd_cmd[16];
gchar *stderr_output;
gboolean spawn_ok;
GError *gerror = NULL;
int exit_status;
int i;
const char *cmd;
if ((address->method == SPD_METHOD_INET_SOCKET) && (!is_localhost)) {
*spawn_error =
g_strdup
("Spawn failed, the given network address doesn't seem to be on localhost");
return 1;
}
cmd = getenv("SPEECHD_CMD");
if (!cmd)
cmd = SPD_SPAWN_CMD;
speechd_cmd[0] = g_strdup(cmd);
speechd_cmd[1] = g_strdup("--spawn");
speechd_cmd[2] = g_strdup("--communication-method");
if (address->method == SPD_METHOD_INET_SOCKET) {
speechd_cmd[3] = g_strdup("inet_socket");
speechd_cmd[4] = g_strdup("--port");
speechd_cmd[5] =
g_strdup_printf("%d", address->inet_socket_port);
speechd_cmd[6] = NULL;
} else if (address->method == SPD_METHOD_UNIX_SOCKET) {
speechd_cmd[3] = g_strdup("unix_socket");
speechd_cmd[4] = g_strdup("--socket-path");
speechd_cmd[5] =
g_strdup_printf("%s", address->unix_socket_name);
speechd_cmd[6] = NULL;
} else
assert(0);
spawn_ok =
g_spawn_sync(NULL, (gchar **) speechd_cmd, NULL,
G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL, NULL,
NULL, NULL, &stderr_output, &exit_status, &gerror);
for (i = 0; speechd_cmd[i] != NULL; i++)
g_free(speechd_cmd[i]);
if (!spawn_ok) {
*spawn_error =
g_strdup_printf("Autospawn failed. Spawn error %d: %s",
gerror->code, gerror->message);
return 1;
} else {
if (exit_status) {
*spawn_error =
g_strdup_printf
("Autospawn failed. Speech Dispatcher refused to start with error code, "
"stating this as a reason: %s", stderr_output);
g_free(stderr_output);
return 1;
} else {
*spawn_error = NULL;
g_free(stderr_output);
return 0;
}
}
assert(0);
}
SPDConnection *spd_open2(const char *client_name, const char *connection_name,
const char *user_name, SPDConnectionMode mode,
const SPDConnectionAddress * address, int autospawn,
char **error_result)
{
SPDConnection *connection = NULL;
SPDConnectionAddress *defaultAddress = NULL;
char *set_client_name = NULL;
char *conn_name = NULL;
char *usr_name = NULL;
int ret;
int tcp_no_delay = 1;
/* Autospawn related */
int spawn_err;
gchar *spawn_report;
char *host_ip;
int is_localhost = 1;
struct sockaddr_in address_inet;
struct sockaddr_un address_unix;
struct sockaddr *sock_address;
size_t sock_address_len;
_init_debug();
if (client_name == NULL) {
*error_result = strdup("ERROR: Client name not specified");
SPD_DBG(*error_result);
return NULL;
}
if (user_name == NULL) {
usr_name = strdup((char *)g_get_user_name());
} else
usr_name = strdup(user_name);
if (connection_name == NULL)
conn_name = strdup("main");
else
conn_name = strdup(connection_name);
if (address == NULL) {
char *err = NULL;
defaultAddress = spd_get_default_address(&err);
address = defaultAddress;
if (!address) {
assert(err);
*error_result = err;
SPD_DBG(*error_result);
goto out;
}
}
/* Connect to server using the selected method */
connection = malloc(sizeof(SPDConnection));
if (address->method == SPD_METHOD_INET_SOCKET) {
gchar *resolve_error;
host_ip =
resolve_host(address->inet_socket_host, &is_localhost,
&resolve_error);
if (host_ip == NULL) {
*error_result = strdup(resolve_error);
g_free(resolve_error);
free(connection);
connection = NULL;
goto out;
}
address_inet.sin_addr.s_addr = inet_addr(host_ip);
free(host_ip);
address_inet.sin_port = htons(address->inet_socket_port);
address_inet.sin_family = AF_INET;
connection->socket = socket(AF_INET, SOCK_STREAM, 0);
sock_address = (struct sockaddr *)&address_inet;
sock_address_len = sizeof(address_inet);
} else if (address->method == SPD_METHOD_UNIX_SOCKET) {
/* Create the unix socket */
address_unix.sun_family = AF_UNIX;
strncpy(address_unix.sun_path, address->unix_socket_name,
sizeof(address_unix.sun_path));
address_unix.sun_path[sizeof(address_unix.sun_path) - 1] = '\0';
connection->socket = socket(AF_UNIX, SOCK_STREAM, 0);
sock_address = (struct sockaddr *)&address_unix;
sock_address_len = SUN_LEN(&address_unix);
} else
SPD_FATAL("Unsupported connection method for spd_open2()");
if (connection->socket < 0) {
free(connection);
connection = NULL;
goto out;
}
connection->stream = (void*) 1;
ret = connect(connection->socket, sock_address, sock_address_len);
if (ret == -1) {
/* Suppose server might not be running, try to autospawn (autostart) it */
if (autospawn) {
spawn_err =
spawn_server(address, is_localhost, &spawn_report);
if (!spawn_err)
spawn_report =
g_strdup("Server successfully autospawned");
ret =
connect(connection->socket, sock_address,
sock_address_len);
} else {
spawn_report = g_strdup("Autospawn disabled");
}
if (ret == -1) {
if (address->method == SPD_METHOD_INET_SOCKET)
*error_result =
g_strdup_printf
("Error: Can't connect to %s on port %d using inet sockets: %s. "
"Autospawn: %s", address->inet_socket_host,
address->inet_socket_port, strerror(errno),
spawn_report);
else if (address->method == SPD_METHOD_UNIX_SOCKET)
*error_result =
g_strdup_printf
("Error: Can't connect to unix socket %s: %s. Autospawn: %s",
address->unix_socket_name, strerror(errno),
spawn_report);
else
assert(0);
SPD_DBG(*error_result);
close(connection->socket);
free(connection);
connection = NULL;
goto out;
}
g_free(spawn_report);
}
if (address->method == SPD_METHOD_INET_SOCKET)
setsockopt(connection->socket, IPPROTO_TCP, TCP_NODELAY,
&tcp_no_delay, sizeof(int));
connection->callback_begin = NULL;
connection->callback_end = NULL;
connection->callback_im = NULL;
connection->callback_pause = NULL;
connection->callback_resume = NULL;
connection->callback_cancel = NULL;
connection->mode = mode;
/* Set up buffer for the socket */
connection->buf_start = 0;
connection->buf_used = 0;
connection->buf = malloc(SPD_REPLY_BUF_SIZE);
if (!connection->buf) {
*error_result =
strdup("Out of memory allocating connection buffer");
SPD_DBG(*error_result);
close(connection->socket);
free(connection);
connection = NULL;
goto out;
}
pthread_mutex_init(&connection->ssip_mutex, NULL);
if (mode == SPD_MODE_THREADED) {
SPD_DBG
("Initializing threads, condition variables and mutexes...");
connection->td = malloc(sizeof(*connection->td));
pthread_cond_init(&connection->td->cond_reply_ready, NULL);
pthread_mutex_init(&connection->td->mutex_reply_ready, NULL);
pthread_cond_init(&connection->td->cond_reply_ack, NULL);
pthread_mutex_init(&connection->td->mutex_reply_ack, NULL);
ret =
pthread_create(&connection->td->events_thread, NULL,
spd_events_handler, connection);
if (ret != 0) {
*error_result = strdup("Thread initialization failed");
SPD_DBG(*error_result);
close(connection->socket);
free(connection->buf);
free(connection);
connection = NULL;
goto out;
}
connection->reply = NULL;
}
/* By now, the connection is created and operational */
set_client_name =
g_strdup_printf("SET SELF CLIENT_NAME \"%s:%s:%s\"", usr_name,
client_name, conn_name);
ret = spd_execute_command_wo_mutex(connection, set_client_name);
out:
free(usr_name);
free(conn_name);
free(set_client_name);
SPDConnectionAddress__free(defaultAddress);
return connection;
}
int spd_fd(SPDConnection * connection)
{
return connection->socket;
}
#define RET(r) \
{ \
pthread_mutex_unlock(&connection->ssip_mutex); \
return r; \
}
/* Get the client id of the connection */
int spd_get_client_id(SPDConnection * connection)
{
int ret;
int err;
char *reply = NULL;
spd_execute_command_with_reply(connection, "HISTORY GET CLIENT_ID", &reply);
ret = get_param_int(reply, 1, &err);
free(reply);
return ret;
}
/* Close a Speech Dispatcher connection */
void spd_close(SPDConnection * connection)
{
pthread_mutex_lock(&connection->ssip_mutex);
if (connection->mode == SPD_MODE_THREADED) {
pthread_cancel(connection->td->events_thread);
pthread_mutex_destroy(&connection->td->mutex_reply_ready);
pthread_mutex_destroy(&connection->td->mutex_reply_ack);
pthread_cond_destroy(&connection->td->cond_reply_ready);
pthread_cond_destroy(&connection->td->cond_reply_ack);
pthread_join(connection->td->events_thread, NULL);
connection->mode = SPD_MODE_SINGLE;
free(connection->td);
}
/* close the socket */
if (connection->socket >= 0) {
close(connection->socket);
connection->socket = -1;
connection->stream = NULL;
}
free(connection->buf);
pthread_mutex_unlock(&connection->ssip_mutex);
pthread_mutex_destroy(&connection->ssip_mutex);
free(connection);
}
/* Helper functions for spd_say. */
static inline int
spd_say_prepare(SPDConnection * connection, SPDPriority priority,
const char *text, char **escaped_text)
{
int ret = 0;
SPD_DBG("Text to say is: %s", text);
/* Insure that there is no escape sequence in the text */
*escaped_text = escape_dot(text);
/* Caller is now responsible for escaped_text. */
if (*escaped_text == NULL) { /* Out of memory. */
SPD_DBG("spd_say could not allocate memory.");
ret = -1;
} else {
/* Set priority */
SPD_DBG("Setting priority");
ret = spd_set_priority(connection, priority);
if (!ret) {
/* Start the data flow */
SPD_DBG("Sending SPEAK");
ret = spd_execute_command_wo_mutex(connection, "speak");
if (ret) {
SPD_DBG("Error: Can't start data flow!");
}
}
}
return ret;
}
static inline int spd_say_sending(SPDConnection * connection, const char *text)
{
int msg_id = -1;
int err = 0;
char *reply = NULL;
char *pret = NULL;
/* Send data */
SPD_DBG("Sending data");
pret = spd_send_data_wo_mutex(connection, text, SPD_NO_REPLY);
if (pret == NULL) {
SPD_DBG("Can't send data wo mutex");
} else {
/* Terminate data flow */
SPD_DBG("Terminating data flow");
err =
spd_execute_command_with_reply(connection, "\r\n.", &reply);
if (err) {
SPD_DBG("Can't terminate data flow");
} else {
msg_id = get_param_int(reply, 1, &err);
if (err < 0) {
SPD_DBG
("Can't determine SSIP message unique ID parameter.");
msg_id = -1;
}
}
}
free(reply);
free(pret);
return msg_id;
}
/* Say TEXT with priority PRIORITY.
* Returns msg_uid on success, -1 otherwise. */
int spd_say(SPDConnection * connection, SPDPriority priority, const char *text)
{
char *escaped_text = NULL;
int msg_id = -1;
int prepare_failed = 0;
if (text != NULL) {
pthread_mutex_lock(&connection->ssip_mutex);
prepare_failed =
spd_say_prepare(connection, priority, text, &escaped_text);
if (!prepare_failed)
msg_id = spd_say_sending(connection, escaped_text);
free(escaped_text);
pthread_mutex_unlock(&connection->ssip_mutex);
} else {
SPD_DBG("spd_say called with a NULL argument for <text>");
}
SPD_DBG("Returning from spd_say");
return msg_id;
}
/* The same as spd_say, accepts also formatted strings */
int
spd_sayf(SPDConnection * connection, SPDPriority priority, const char *format,
...)
{
static int ret;
va_list args;
char *buf;
if (format == NULL)
return -1;
/* Print the text to buffer */
va_start(args, format);
buf = g_strdup_vprintf(format, args);
va_end(args);
/* Send the buffer to Speech Dispatcher */
ret = spd_say(connection, priority, buf);
free(buf);
return ret;
}
int spd_stop(SPDConnection * connection)
{
return spd_execute_command(connection, "STOP SELF");
}
int spd_stop_all(SPDConnection * connection)
{
return spd_execute_command(connection, "STOP ALL");
}
int spd_stop_uid(SPDConnection * connection, int target_uid)
{
static char command[16];
sprintf(command, "STOP %d", target_uid);
return spd_execute_command(connection, command);
}
int spd_cancel(SPDConnection * connection)
{
return spd_execute_command(connection, "CANCEL SELF");
}
int spd_cancel_all(SPDConnection * connection)
{
return spd_execute_command(connection, "CANCEL ALL");
}
int spd_cancel_uid(SPDConnection * connection, int target_uid)
{
static char command[16];
sprintf(command, "CANCEL %d", target_uid);
return spd_execute_command(connection, command);
}
int spd_pause(SPDConnection * connection)
{
return spd_execute_command(connection, "PAUSE SELF");
}
int spd_pause_all(SPDConnection * connection)
{
return spd_execute_command(connection, "PAUSE ALL");
}
int spd_pause_uid(SPDConnection * connection, int target_uid)
{
char command[16];
sprintf(command, "PAUSE %d", target_uid);
return spd_execute_command(connection, command);
}
int spd_resume(SPDConnection * connection)
{
return spd_execute_command(connection, "RESUME SELF");
}
int spd_resume_all(SPDConnection * connection)
{
return spd_execute_command(connection, "RESUME ALL");
}
int spd_resume_uid(SPDConnection * connection, int target_uid)
{
static char command[16];
sprintf(command, "RESUME %d", target_uid);
return spd_execute_command(connection, command);
}
int
spd_key(SPDConnection * connection, SPDPriority priority, const char *key_name)
{
char *command_key;
int ret;
if (key_name == NULL)
return -1;
pthread_mutex_lock(&connection->ssip_mutex);
ret = spd_set_priority(connection, priority);
if (ret)
RET(-1);
command_key = g_strdup_printf("KEY %s", key_name);
ret = spd_execute_command_wo_mutex(connection, command_key);
free(command_key);
if (ret)
RET(-1);
pthread_mutex_unlock(&connection->ssip_mutex);
return 0;
}
int
spd_char(SPDConnection * connection, SPDPriority priority,
const char *character)
{
static char command[16];
int ret;
if (character == NULL)
return -1;
if (strlen(character) > 6)
return -1;
pthread_mutex_lock(&connection->ssip_mutex);
ret = spd_set_priority(connection, priority);
if (ret)
RET(-1);
if (!strcmp(character, " "))
sprintf(command, "CHAR space");
else
sprintf(command, "CHAR %s", character);
ret = spd_execute_command_wo_mutex(connection, command);
if (ret)
RET(-1);
pthread_mutex_unlock(&connection->ssip_mutex);
return 0;
}
int
spd_wchar(SPDConnection * connection, SPDPriority priority, wchar_t wcharacter)
{
static char command[16];
char character[8];
int ret;
pthread_mutex_lock(&connection->ssip_mutex);
ret = wcrtomb(character, wcharacter, NULL);
if (ret <= 0)
RET(-1);
character[ret] = '\0';
ret = spd_set_priority(connection, priority);
if (ret)
RET(-1);
assert(character != NULL);
sprintf(command, "CHAR %s", character);
ret = spd_execute_command_wo_mutex(connection, command);
if (ret)
RET(-1);
pthread_mutex_unlock(&connection->ssip_mutex);
return 0;
}
int
spd_sound_icon(SPDConnection * connection, SPDPriority priority,
const char *icon_name)
{
char *command;
int ret;
if (icon_name == NULL)
return -1;
pthread_mutex_lock(&connection->ssip_mutex);
ret = spd_set_priority(connection, priority);
if (ret)
RET(-1);
command = g_strdup_printf("SOUND_ICON %s", icon_name);
ret = spd_execute_command_wo_mutex(connection, command);
free(command);
if (ret)
RET(-1);
pthread_mutex_unlock(&connection->ssip_mutex);
return 0;
}
// Set functions for Punctuation
int spd_w_set_punctuation(SPDConnection * connection, SPDPunctuation type,
const char *who)
{
char command[32];
int ret;
if (type == SPD_PUNCT_ALL)
sprintf(command, "SET %s PUNCTUATION all", who);
if (type == SPD_PUNCT_NONE)
sprintf(command, "SET %s PUNCTUATION none", who);
if (type == SPD_PUNCT_SOME)
sprintf(command, "SET %s PUNCTUATION some", who);
if (type == SPD_PUNCT_MOST)
sprintf(command, "SET %s PUNCTUATION most", who);
ret = spd_execute_command(connection, command);
return ret;
}
int spd_set_punctuation(SPDConnection * connection, SPDPunctuation type)
{
return spd_w_set_punctuation(connection, type, SPD_SELF);
}
int spd_set_punctuation_all(SPDConnection * connection, SPDPunctuation type)
{
return spd_w_set_punctuation(connection, type, SPD_ALLCLIENTS);
}