2 * This file is part of Disobedience
3 * Copyright (C) 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file disobedience/rtp.c
21 * @brief RTP player support for Disobedience
24 #include "disobedience.h"
25 #include <sys/types.h>
30 #include <sys/utsname.h>
33 /** @brief Path to RTP player's control socket */
34 static char *rtp_socket
;
36 /** @brief Path to RTP player's logfile */
39 /** @brief Initialize @ref rtp_socket and @ref rtp_log if necessary */
40 static void rtp_init(void) {
42 const char *home
= getenv("HOME");
46 byte_xasprintf(&dir
, "%s/.disorder/", home
);
49 byte_xasprintf(&base
, "%s/", dir
);
51 byte_xasprintf(&base
, "%s/%s-", dir
, uts
.nodename
);
52 byte_xasprintf(&rtp_socket
, "%srtp", base
);
53 byte_xasprintf(&rtp_log
, "%srtp.log", base
);
57 /** @brief Return a connection to the RTP player's control socket */
58 static FILE *rtp_connect(void) {
59 struct sockaddr_un un
;
64 memset(&un
, 0, sizeof un
);
65 un
.sun_family
= AF_UNIX
;
66 strcpy(un
.sun_path
, rtp_socket
);
67 fd
= xsocket(PF_UNIX
, SOCK_STREAM
, 0);
68 if(connect(fd
, (const struct sockaddr
*)&un
, sizeof un
) < 0) {
69 /* Connection refused just means that the player quit without deleting its
76 /* Anything else may be a problem */
77 fpopup_msg(GTK_MESSAGE_ERROR
, "connecting to %s: %s",
78 rtp_socket
, strerror(errno
));
84 if(!(fp
= fdopen(fd
, "r+"))) {
85 fpopup_msg(GTK_MESSAGE_ERROR
, "error calling fdopen: %s", strerror(errno
));
92 /** @brief Return non-0 iff the RTP player is running */
93 int rtp_running(void) {
94 FILE *fp
= rtp_connect();
97 return 0; /* no connection -> not running */
98 fclose(fp
); /* connection -> running */
102 /** @brief Activate the RTP player if it is not running */
103 void start_rtp(void) {
108 return; /* already running */
109 /* double-fork so we don't have to wait() later */
110 if(!(pid
= xfork())) {
112 fatal(errno
, "error calling setsid");
113 if(!(pid
= xfork())) {
116 /* log errors and output somewhere reasonably sane. rtp_running()
117 * will have made sure the directory exists. */
118 if((fd
= open(rtp_log
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) < 0)
119 fatal(errno
, "creating %s", rtp_log
);
122 fatal(errno
, "dup2");
124 fatal(errno
, "close");
125 /* We don't want to hang onto whatever stdin was */
126 if((fd
= open("/dev/null", O_RDONLY
)) < 0)
127 fatal(errno
, "opening /dev/null");
129 fatal(errno
, "dup2");
131 fatal(errno
, "close");
132 /* execute the player */
133 execlp("disorder-playrtp",
134 "disorder-playrtp", "--socket", rtp_socket
, (char *)0);
135 fatal(errno
, "disorder-playrtp");
142 while(waitpid(pid
, &w
, 0) < 0 && errno
== EINTR
)
147 /** @brief Stop the RTP player if it is running */
148 void stop_rtp(void) {
149 FILE *fp
= rtp_connect();
152 return; /* already stopped */
153 fprintf(fp
, "stop\n");