gmpc version 0.18.102
[gmpc.git] / remote / main.c
blobca920f8a544c959da5c7d934de838c62c71f5a6a
1 /* Gnome Music Player (GMPC)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <gtk/gtk.h>
24 #include <config.h>
25 #include "bacon-message-connection.h"
27 int main ( int argc, char **argv )
29 BaconMessageConnection *bacon_connection = NULL;
33 GError *error = NULL;
34 GOptionContext *context;
35 /* version */
36 gboolean version;
37 /* quit gmpc */
38 gboolean quit=FALSE;
39 /* play */
40 gboolean play=FALSE;
41 gboolean pause=FALSE;
42 gboolean prev=FALSE;
43 gboolean next=FALSE;
44 gboolean stop=FALSE;
45 gboolean toggle_view = FALSE;
46 gboolean hide_view = FALSE;
47 gboolean show_view = FALSE;
48 gboolean spawn = FALSE;
49 gchar *stream = NULL;
51 GOptionEntry entries[] = {
53 "version", 0, 0, G_OPTION_ARG_NONE, &version,
54 "Print the version number and exit", NULL
57 "quit", 'q', 0, G_OPTION_ARG_NONE, &quit,
58 "Quit the running gmpc", NULL
61 "play", 'c', 0, G_OPTION_ARG_NONE, &play,
62 "Give the running gmpc the play command", NULL
65 "pause", 'v', 0, G_OPTION_ARG_NONE, &pause,
66 "Give the running gmpc the pause command", NULL
69 "next", 'b', 0, G_OPTION_ARG_NONE, &next,
70 "Give the running gmpc the next command", NULL
73 "prev", 'z', 0, G_OPTION_ARG_NONE, &prev,
74 "Give the running gmpc the prev command", NULL
77 "stop", 'x', 0, G_OPTION_ARG_NONE, &stop,
78 "Give the running gmpc the stop command", NULL
82 "stream", 's', 0, G_OPTION_ARG_STRING, &stream,
83 "Give the running gmpc a stream to play", NULL
87 "toggle-view", 't', 0, G_OPTION_ARG_NONE, &toggle_view,
88 "Give the running gmpc the command to toggle the window visibility", NULL
92 "hide-view", 'h', 0, G_OPTION_ARG_NONE, &hide_view,
93 "Give the running gmpc the command to hide the window.", NULL
96 "show-view", 'k', 0, G_OPTION_ARG_NONE, &show_view,
97 "Give the running gmpc the command to show the window.", NULL
98 },
100 "spawn", 's', 0, G_OPTION_ARG_NONE, &spawn,
101 "Spawn gmpc if not running", NULL
103 {NULL}
106 gtk_init(&argc, &argv);
107 /*Create the commandline option parser */
108 context = g_option_context_new("GMPC remote program");
109 g_option_context_add_main_entries(context, entries, NULL);
111 /*Time to parse the options */
112 g_option_context_parse(context, &argc, &argv, &error);
113 g_option_context_free(context);
114 if(error){
115 g_error("Failed to parse command line options: '%s'", error->message);
118 bacon_connection = bacon_message_connection_new("gmpc");
119 while(bacon_connection)
121 if (!bacon_message_connection_get_is_server (bacon_connection))
123 if(play || pause)
125 printf("send play\n");
126 bacon_message_connection_send(bacon_connection, "PLAY");
128 if(prev)
130 printf("send prev\n");
131 bacon_message_connection_send(bacon_connection, "PREV");
133 if(next)
135 printf("send next\n");
136 bacon_message_connection_send(bacon_connection, "NEXT");
138 if(stop)
140 printf("send stop\n");
141 bacon_message_connection_send(bacon_connection, "STOP");
143 if(toggle_view)
145 printf("send toggle view\n");
146 bacon_message_connection_send(bacon_connection, "TOGGLE_VIEW");
148 if(hide_view)
150 printf("send hide view\n");
151 bacon_message_connection_send(bacon_connection, "HIDE_VIEW");
153 if(show_view)
155 printf("send show view\n");
156 bacon_message_connection_send(bacon_connection, "SHOW_VIEW");
158 if(stream)
160 gchar *str = g_strdup_printf("STREAM %s", stream);
161 printf("Send stream: %s\n", stream);
162 bacon_message_connection_send(bacon_connection, str);
163 g_free(str);
166 if(quit)
168 printf("send quit\n");
169 bacon_message_connection_send(bacon_connection, "QUIT");
171 bacon_message_connection_free (bacon_connection);
172 return EXIT_SUCCESS;
174 else if(spawn){
175 int count = 10;
176 printf("starting gmpc\n");
178 bacon_message_connection_free (bacon_connection);
179 bacon_connection = NULL;
181 g_spawn_command_line_async("gmpc",&error);
182 if(error){
183 g_error("Failed to spawn gmpc: '%s'", error->message);
186 g_usleep(300000);
187 printf("waiting for gmpc to come up\n");
188 if(bacon_connection)
189 bacon_message_connection_free (bacon_connection);
190 bacon_connection = bacon_message_connection_new("gmpc");
191 }while(bacon_message_connection_get_is_server (bacon_connection) && (--count > 0));
192 if(count == 0){
193 g_error("Failed to get gmpc to respond");
195 /* Send connect */
196 bacon_message_connection_send(bacon_connection, "CONNECT");
197 g_usleep(300000);
198 }else{
199 printf("GMPC is not running\n");
200 bacon_message_connection_free (bacon_connection);
201 return EXIT_FAILURE;
204 g_error("Failed to create IPC connection\n");
205 return EXIT_FAILURE;