Mercurial > geeqie
annotate src/remote.c @ 1554:a96a20c7feb1
improved remote protocol to allow bidirectional communication
author | nadvornik |
---|---|
date | Mon, 20 Apr 2009 19:44:50 +0000 |
parents | 24a12aa0cb54 |
children | 3840882253fe |
rev | line source |
---|---|
9 | 1 /* |
196 | 2 * Geeqie |
9 | 3 * (C) 2004 John Ellis |
1284 | 4 * Copyright (C) 2008 - 2009 The Geeqie Team |
9 | 5 * |
6 * Author: John Ellis | |
7 * | |
8 * This software is released under the GNU General Public License (GNU GPL). | |
9 * Please read the included file COPYING for more information. | |
10 * This software comes with no warranty of any kind, use at your own risk! | |
11 */ | |
12 | |
13 | |
281 | 14 #include "main.h" |
9 | 15 #include "remote.h" |
16 | |
649 | 17 #include "collect.h" |
18 #include "filedata.h" | |
19 #include "img-view.h" | |
20 #include "layout.h" | |
21 #include "layout_image.h" | |
1022
9962b24b6b43
Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
1000
diff
changeset
|
22 #include "misc.h" |
649 | 23 #include "slideshow.h" |
24 #include "ui_fileops.h" | |
1467 | 25 #include "rcfile.h" |
9 | 26 |
27 #include <sys/socket.h> | |
28 #include <sys/un.h> | |
29 #include <signal.h> | |
30 #include <errno.h> | |
31 | |
32 | |
33 #define SERVER_MAX_CLIENTS 8 | |
34 | |
35 #define REMOTE_SERVER_BACKLOG 4 | |
36 | |
37 | |
38 #ifndef UNIX_PATH_MAX | |
39 #define UNIX_PATH_MAX 108 | |
40 #endif | |
41 | |
42 | |
649 | 43 static RemoteConnection *remote_client_open(const gchar *path); |
44 static gint remote_client_send(RemoteConnection *rc, const gchar *text); | |
45 | |
46 | |
9 | 47 typedef struct _RemoteClient RemoteClient; |
48 struct _RemoteClient { | |
49 gint fd; | |
1523 | 50 guint channel_id; /* event source id */ |
9 | 51 RemoteConnection *rc; |
52 }; | |
53 | |
649 | 54 typedef struct _RemoteData RemoteData; |
55 struct _RemoteData { | |
56 CollectionData *command_collection; | |
57 }; | |
58 | |
9 | 59 |
60 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data) | |
61 { | |
62 RemoteClient *client = data; | |
63 RemoteConnection *rc; | |
1299 | 64 GIOStatus status = G_IO_STATUS_NORMAL; |
9 | 65 |
66 rc = client->rc; | |
67 | |
68 if (condition & G_IO_IN) | |
69 { | |
70 gchar *buffer = NULL; | |
71 GError *error = NULL; | |
1300 | 72 gsize termpos; |
9 | 73 |
1299 | 74 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL) |
9 | 75 { |
76 if (buffer) | |
77 { | |
78 buffer[termpos] = '\0'; | |
79 | |
80 if (strlen(buffer) > 0) | |
81 { | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
82 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
83 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */ |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
84 g_io_channel_flush(source, NULL); |
9 | 85 } |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
86 g_free(buffer); |
9 | 87 |
88 buffer = NULL; | |
89 } | |
90 } | |
91 | |
92 if (error) | |
93 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
94 log_printf("error reading socket: %s\n", error->message); |
9 | 95 g_error_free(error); |
96 } | |
97 } | |
98 | |
1299 | 99 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR) |
9 | 100 { |
101 rc->clients = g_list_remove(rc->clients, client); | |
102 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
103 DEBUG_1("HUP detected, closing client."); |
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
104 DEBUG_1("client count %d", g_list_length(rc->clients)); |
495 | 105 |
9 | 106 g_source_remove(client->channel_id); |
107 close(client->fd); | |
108 g_free(client); | |
109 } | |
110 | |
111 return TRUE; | |
112 } | |
113 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
114 static void remote_server_client_add(RemoteConnection *rc, gint fd) |
9 | 115 { |
116 RemoteClient *client; | |
117 GIOChannel *channel; | |
118 | |
119 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS) | |
120 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
121 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS); |
9 | 122 close(fd); |
123 return; | |
124 } | |
125 | |
126 client = g_new0(RemoteClient, 1); | |
127 client->rc = rc; | |
128 client->fd = fd; | |
129 | |
130 channel = g_io_channel_unix_new(fd); | |
131 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP, | |
132 remote_server_client_cb, client, NULL); | |
133 g_io_channel_unref(channel); | |
134 | |
135 rc->clients = g_list_append(rc->clients, client); | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
136 DEBUG_1("client count %d", g_list_length(rc->clients)); |
9 | 137 } |
138 | |
139 static void remote_server_clients_close(RemoteConnection *rc) | |
140 { | |
141 while (rc->clients) | |
142 { | |
143 RemoteClient *client = rc->clients->data; | |
144 | |
145 rc->clients = g_list_remove(rc->clients, client); | |
146 | |
147 g_source_remove(client->channel_id); | |
148 close(client->fd); | |
149 g_free(client); | |
150 } | |
151 } | |
152 | |
153 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data) | |
154 { | |
155 RemoteConnection *rc = data; | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
156 gint fd; |
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
157 guint alen; |
9 | 158 |
159 fd = accept(rc->fd, NULL, &alen); | |
160 if (fd == -1) | |
161 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
162 log_printf("error accepting socket: %s\n", strerror(errno)); |
9 | 163 return TRUE; |
164 } | |
165 | |
166 remote_server_client_add(rc, fd); | |
167 | |
168 return TRUE; | |
169 } | |
170 | |
1444 | 171 static gboolean remote_server_exists(const gchar *path) |
9 | 172 { |
173 RemoteConnection *rc; | |
174 | |
175 /* verify server up */ | |
176 rc = remote_client_open(path); | |
177 remote_close(rc); | |
178 | |
179 if (rc) return TRUE; | |
180 | |
181 /* unable to connect, remove socket file to free up address */ | |
182 unlink(path); | |
183 return FALSE; | |
184 } | |
185 | |
649 | 186 static RemoteConnection *remote_server_open(const gchar *path) |
9 | 187 { |
188 RemoteConnection *rc; | |
189 struct sockaddr_un addr; | |
190 gint sun_path_len; | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
191 gint fd; |
9 | 192 GIOChannel *channel; |
193 | |
194 if (remote_server_exists(path)) | |
195 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
196 log_printf("Address already in use: %s\n", path); |
9 | 197 return NULL; |
198 } | |
199 | |
200 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
201 if (fd == -1) return NULL; | |
202 | |
203 addr.sun_family = AF_UNIX; | |
204 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX); | |
205 strncpy(addr.sun_path, path, sun_path_len); | |
206 if (bind(fd, &addr, sizeof(addr)) == -1 || | |
207 listen(fd, REMOTE_SERVER_BACKLOG) == -1) | |
208 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
209 log_printf("error subscribing to socket: %s\n", strerror(errno)); |
9 | 210 close(fd); |
211 return NULL; | |
212 } | |
213 | |
214 rc = g_new0(RemoteConnection, 1); | |
1367
fe4da037be21
When g_new0() is used, drop redundant initializations to NULL, FALSE or 0, second pass.
zas_
parents:
1307
diff
changeset
|
215 |
9 | 216 rc->server = TRUE; |
217 rc->fd = fd; | |
218 rc->path = g_strdup(path); | |
219 | |
220 channel = g_io_channel_unix_new(rc->fd); | |
1291
50ae02a4a675
replaced bar_info with an universal bar, restored the original
nadvornik
parents:
1284
diff
changeset
|
221 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL); |
50ae02a4a675
replaced bar_info with an universal bar, restored the original
nadvornik
parents:
1284
diff
changeset
|
222 |
9 | 223 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN, |
224 remote_server_read_cb, rc, NULL); | |
225 g_io_channel_unref(channel); | |
226 | |
227 return rc; | |
228 } | |
229 | |
649 | 230 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data) |
9 | 231 { |
232 if (!rc || !rc->server) return; | |
233 | |
234 rc->read_func = func; | |
235 rc->read_data = data; | |
236 } | |
237 | |
238 | |
649 | 239 static RemoteConnection *remote_client_open(const gchar *path) |
9 | 240 { |
241 RemoteConnection *rc; | |
242 struct stat st; | |
243 struct sockaddr_un addr; | |
244 gint sun_path_len; | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
245 gint fd; |
9 | 246 |
247 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL; | |
248 | |
249 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
250 if (fd == -1) return NULL; | |
251 | |
252 addr.sun_family = AF_UNIX; | |
253 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX); | |
254 strncpy(addr.sun_path, path, sun_path_len); | |
255 if (connect(fd, &addr, sizeof(addr)) == -1) | |
442 | 256 { |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
257 DEBUG_1("error connecting to socket: %s", strerror(errno)); |
442 | 258 close(fd); |
259 return NULL; | |
260 } | |
9 | 261 |
262 rc = g_new0(RemoteConnection, 1); | |
263 rc->server = FALSE; | |
264 rc->fd = fd; | |
265 rc->path = g_strdup(path); | |
266 | |
267 return rc; | |
268 } | |
269 | |
270 static sig_atomic_t sigpipe_occured = FALSE; | |
271 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
272 static void sighandler_sigpipe(gint sig) |
9 | 273 { |
274 sigpipe_occured = TRUE; | |
275 } | |
276 | |
1444 | 277 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text) |
9 | 278 { |
279 struct sigaction new_action, old_action; | |
1437 | 280 gboolean ret = FALSE; |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
281 GError *error = NULL; |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
282 GIOChannel *channel; |
9 | 283 |
284 if (!rc || rc->server) return FALSE; | |
285 if (!text) return TRUE; | |
286 | |
287 sigpipe_occured = FALSE; | |
288 | |
289 new_action.sa_handler = sighandler_sigpipe; | |
512
f9bf33be53ff
Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents:
507
diff
changeset
|
290 sigemptyset(&new_action.sa_mask); |
9 | 291 new_action.sa_flags = 0; |
292 | |
293 /* setup our signal handler */ | |
512
f9bf33be53ff
Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents:
507
diff
changeset
|
294 sigaction(SIGPIPE, &new_action, &old_action); |
9 | 295 |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
296 channel = g_io_channel_unix_new(rc->fd); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
297 |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
298 g_io_channel_write_chars(channel, text, -1, NULL, &error); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
299 g_io_channel_write_chars(channel, "\n", -1, NULL, &error); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
300 g_io_channel_flush(channel, &error); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
301 |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
302 if (error) |
9 | 303 { |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
304 log_printf("error reading socket: %s\n", error->message); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
305 g_error_free(error); |
9 | 306 ret = FALSE;; |
307 } | |
308 else | |
309 { | |
310 ret = TRUE; | |
311 } | |
312 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
313 if (ret) |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
314 { |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
315 gchar *buffer = NULL; |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
316 gsize termpos; |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
317 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL) |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
318 { |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
319 if (buffer) |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
320 { |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
321 if (buffer[0] == '\n') /* empty line finishes the command */ |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
322 { |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
323 g_free(buffer); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
324 break; |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
325 } |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
326 buffer[termpos] = '\0'; |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
327 printf("%s\n", buffer); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
328 g_free(buffer); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
329 buffer = NULL; |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
330 } |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
331 } |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
332 |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
333 if (error) |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
334 { |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
335 log_printf("error reading socket: %s\n", error->message); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
336 g_error_free(error); |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
337 ret = FALSE; |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
338 } |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
339 } |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
340 |
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
341 |
9 | 342 /* restore the original signal handler */ |
512
f9bf33be53ff
Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents:
507
diff
changeset
|
343 sigaction(SIGPIPE, &old_action, NULL); |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
344 g_io_channel_unref(channel); |
9 | 345 return ret; |
346 } | |
347 | |
348 void remote_close(RemoteConnection *rc) | |
349 { | |
350 if (!rc) return; | |
351 | |
352 if (rc->server) | |
353 { | |
354 remote_server_clients_close(rc); | |
355 | |
356 g_source_remove(rc->channel_id); | |
357 unlink(rc->path); | |
358 } | |
359 | |
649 | 360 if (rc->read_data) |
361 g_free(rc->read_data); | |
362 | |
9 | 363 close(rc->fd); |
364 | |
365 g_free(rc->path); | |
366 g_free(rc); | |
367 } | |
649 | 368 |
369 /* | |
370 *----------------------------------------------------------------------------- | |
371 * remote functions | |
372 *----------------------------------------------------------------------------- | |
373 */ | |
374 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
375 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 376 { |
377 layout_image_next(NULL); | |
378 } | |
379 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
380 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 381 { |
382 layout_image_prev(NULL); | |
383 } | |
384 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
385 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 386 { |
387 layout_image_first(NULL); | |
388 } | |
389 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
390 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 391 { |
392 layout_image_last(NULL); | |
393 } | |
394 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
395 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 396 { |
397 layout_image_full_screen_toggle(NULL); | |
398 } | |
399 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
400 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 401 { |
402 layout_image_full_screen_start(NULL); | |
403 } | |
404 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
405 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 406 { |
407 layout_image_full_screen_stop(NULL); | |
408 } | |
409 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
410 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 411 { |
412 GList *list; | |
783 | 413 FileData *dir_fd = file_data_new_simple(text); |
414 list = filelist_recursive(dir_fd); | |
415 file_data_unref(dir_fd); | |
649 | 416 if (!list) return; |
417 //printf("length: %d\n", g_list_length(list)); | |
418 layout_image_slideshow_stop(NULL); | |
419 layout_image_slideshow_start_from_list(NULL, list); | |
420 } | |
421 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
422 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 423 { |
424 layout_image_slideshow_toggle(NULL); | |
425 } | |
426 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
427 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 428 { |
429 layout_image_slideshow_start(NULL); | |
430 } | |
431 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
432 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 433 { |
434 layout_image_slideshow_stop(NULL); | |
435 } | |
436 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
437 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 438 { |
439 gdouble n; | |
440 | |
1307 | 441 n = g_ascii_strtod(text, NULL); |
649 | 442 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS) |
443 { | |
444 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n", | |
445 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS); | |
446 return; | |
447 } | |
448 options->slideshow.delay = (gint)(n * 10.0 + 0.01); | |
449 } | |
450 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
451 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 452 { |
1416 | 453 gboolean popped; |
454 gboolean hidden; | |
649 | 455 |
456 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden) | |
457 { | |
458 layout_tools_float_set(NULL, popped, FALSE); | |
459 } | |
460 } | |
461 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
462 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 463 { |
1416 | 464 gboolean popped; |
465 gboolean hidden; | |
649 | 466 |
467 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden) | |
468 { | |
469 layout_tools_float_set(NULL, popped, TRUE); | |
470 } | |
471 } | |
472 | |
1444 | 473 static gboolean gr_quit_idle_cb(gpointer data) |
649 | 474 { |
475 exit_program(); | |
476 | |
477 return FALSE; | |
478 } | |
479 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
480 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 481 { |
482 /* schedule exit when idle, if done from within a | |
483 * remote handler remote_close will crash | |
484 */ | |
485 g_idle_add(gr_quit_idle_cb, NULL); | |
486 } | |
487 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
488 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 489 { |
653 | 490 gchar *filename = expand_tilde(text); |
491 | |
492 if (isfile(filename)) | |
649 | 493 { |
781
2d2cca2bceb0
Replace hardcoded collection filename extension by a macro (GQ_COLLECTION_EXT).
zas_
parents:
713
diff
changeset
|
494 if (file_extension_match(filename, GQ_COLLECTION_EXT)) |
649 | 495 { |
653 | 496 collection_window_new(filename); |
649 | 497 } |
498 else | |
499 { | |
653 | 500 layout_set_path(NULL, filename); |
649 | 501 } |
502 } | |
653 | 503 else if (isdir(filename)) |
649 | 504 { |
653 | 505 layout_set_path(NULL, filename); |
649 | 506 } |
507 else | |
508 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
509 log_printf("remote sent filename that does not exist:\"%s\"\n", filename); |
649 | 510 } |
653 | 511 |
512 g_free(filename); | |
649 | 513 } |
514 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
515 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data) |
1467 | 516 { |
517 gchar *filename = expand_tilde(text); | |
518 | |
519 if (isfile(filename)) | |
520 { | |
1484 | 521 load_config_from_file(filename, FALSE); |
1467 | 522 } |
523 else | |
524 { | |
525 log_printf("remote sent filename that does not exist:\"%s\"\n", filename); | |
526 } | |
527 | |
528 g_free(filename); | |
529 } | |
530 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
531 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 532 { |
653 | 533 gchar *filename = expand_tilde(text); |
534 | |
535 view_window_new(file_data_new_simple(filename)); | |
536 g_free(filename); | |
649 | 537 } |
538 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
539 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 540 { |
541 RemoteData *remote_data = data; | |
542 | |
543 if (remote_data->command_collection) | |
544 { | |
545 collection_unref(remote_data->command_collection); | |
546 remote_data->command_collection = NULL; | |
547 } | |
995 | 548 } |
649 | 549 |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
550 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 551 { |
552 RemoteData *remote_data = data; | |
1437 | 553 gboolean new = TRUE; |
649 | 554 |
555 if (!remote_data->command_collection) | |
556 { | |
557 CollectionData *cd; | |
558 | |
559 cd = collection_new(""); | |
560 | |
561 g_free(cd->path); | |
562 cd->path = NULL; | |
563 g_free(cd->name); | |
564 cd->name = g_strdup(_("Command line")); | |
565 | |
566 remote_data->command_collection = cd; | |
567 } | |
568 else | |
569 { | |
570 new = (!collection_get_first(remote_data->command_collection)); | |
571 } | |
572 | |
573 if (collection_add(remote_data->command_collection, file_data_new_simple(text), FALSE) && new) | |
574 { | |
575 layout_image_set_collection(NULL, remote_data->command_collection, | |
576 collection_get_first(remote_data->command_collection)); | |
577 } | |
578 } | |
579 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
580 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data) |
649 | 581 { |
582 LayoutWindow *lw = NULL; | |
583 | |
584 if (layout_valid(&lw)) | |
585 { | |
586 gtk_window_present(GTK_WINDOW(lw->window)); | |
587 } | |
588 } | |
589 | |
590 typedef struct _RemoteCommandEntry RemoteCommandEntry; | |
591 struct _RemoteCommandEntry { | |
592 gchar *opt_s; | |
593 gchar *opt_l; | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
594 void (*func)(const gchar *text, GIOChannel *channel, gpointer data); |
1444 | 595 gboolean needs_extra; |
596 gboolean prefer_command_line; | |
649 | 597 gchar *description; |
598 }; | |
599 | |
600 static RemoteCommandEntry remote_commands[] = { | |
601 /* short, long callback, extra, prefer,description */ | |
602 { "-n", "--next", gr_image_next, FALSE, FALSE, N_("next image") }, | |
603 { "-b", "--back", gr_image_prev, FALSE, FALSE, N_("previous image") }, | |
604 { NULL, "--first", gr_image_first, FALSE, FALSE, N_("first image") }, | |
605 { NULL, "--last", gr_image_last, FALSE, FALSE, N_("last image") }, | |
606 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, N_("toggle full screen") }, | |
607 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, N_("start full screen") }, | |
608 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, N_("stop full screen") }, | |
609 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, N_("toggle slide show") }, | |
610 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, N_("start slide show") }, | |
611 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, N_("stop slide show") }, | |
1475 | 612 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("start recursive slide show") }, |
649 | 613 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("set slide show delay in seconds") }, |
614 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, N_("show tools") }, | |
615 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, N_("hide tools") }, | |
616 { "-q", "--quit", gr_quit, FALSE, FALSE, N_("quit") }, | |
1475 | 617 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("load config file") }, |
649 | 618 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("open file") }, |
619 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("open file in new window") }, | |
620 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL }, | |
621 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, NULL }, | |
622 { NULL, "raise", gr_raise, FALSE, FALSE, NULL }, | |
623 { NULL, NULL, NULL, FALSE, FALSE, NULL } | |
624 }; | |
625 | |
626 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset) | |
627 { | |
1437 | 628 gboolean match = FALSE; |
649 | 629 gint i; |
630 | |
631 i = 0; | |
632 while (!match && remote_commands[i].func != NULL) | |
633 { | |
634 if (remote_commands[i].needs_extra) | |
635 { | |
636 if (remote_commands[i].opt_s && | |
637 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0) | |
638 { | |
639 if (offset) *offset = text + strlen(remote_commands[i].opt_s); | |
640 return &remote_commands[i]; | |
641 } | |
642 else if (remote_commands[i].opt_l && | |
643 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0) | |
644 { | |
645 if (offset) *offset = text + strlen(remote_commands[i].opt_l); | |
646 return &remote_commands[i]; | |
647 } | |
648 } | |
649 else | |
650 { | |
651 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) || | |
652 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0)) | |
653 { | |
654 if (offset) *offset = text; | |
655 return &remote_commands[i]; | |
656 } | |
657 } | |
658 | |
659 i++; | |
660 } | |
661 | |
662 return NULL; | |
663 } | |
664 | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
665 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data) |
649 | 666 { |
667 RemoteCommandEntry *entry; | |
668 const gchar *offset; | |
669 | |
670 entry = remote_command_find(text, &offset); | |
671 if (entry && entry->func) | |
672 { | |
1554
a96a20c7feb1
improved remote protocol to allow bidirectional communication
nadvornik
parents:
1523
diff
changeset
|
673 entry->func(offset, channel, data); |
649 | 674 } |
675 else | |
676 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
677 log_printf("unknown remote command:%s\n", text); |
649 | 678 } |
679 } | |
680 | |
681 void remote_help(void) | |
682 { | |
683 gint i; | |
684 | |
685 print_term(_("Remote command list:\n")); | |
686 | |
687 i = 0; | |
688 while (remote_commands[i].func != NULL) | |
689 { | |
690 if (remote_commands[i].description) | |
691 { | |
692 printf_term(" %-3s%s %-20s %s\n", | |
693 (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "", | |
694 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ", | |
695 (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "", | |
696 _(remote_commands[i].description)); | |
697 } | |
698 i++; | |
699 } | |
700 } | |
701 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
702 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors) |
649 | 703 { |
704 gint i; | |
705 | |
706 i = 1; | |
707 while (i < argc) | |
708 { | |
709 RemoteCommandEntry *entry; | |
710 | |
711 entry = remote_command_find(argv[i], NULL); | |
712 if (entry) | |
713 { | |
714 list = g_list_append(list, argv[i]); | |
715 } | |
652
9bcfd6d7a902
Display a message when invalid remote options are used.
zas_
parents:
649
diff
changeset
|
716 else if (errors) |
9bcfd6d7a902
Display a message when invalid remote options are used.
zas_
parents:
649
diff
changeset
|
717 { |
9bcfd6d7a902
Display a message when invalid remote options are used.
zas_
parents:
649
diff
changeset
|
718 *errors = g_list_append(*errors, argv[i]); |
9bcfd6d7a902
Display a message when invalid remote options are used.
zas_
parents:
649
diff
changeset
|
719 } |
649 | 720 i++; |
721 } | |
722 | |
723 return list; | |
724 } | |
725 | |
726 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path, | |
727 GList *cmd_list, GList *collection_list) | |
728 { | |
729 RemoteConnection *rc; | |
1437 | 730 gboolean started = FALSE; |
649 | 731 gchar *buf; |
732 | |
1145
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1144
diff
changeset
|
733 buf = g_build_filename(get_rc_dir(), ".command", NULL); |
649 | 734 rc = remote_client_open(buf); |
735 if (!rc) | |
736 { | |
737 GString *command; | |
738 GList *work; | |
739 gint retry_count = 12; | |
1437 | 740 gboolean blank = FALSE; |
649 | 741 |
742 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME); | |
743 | |
744 command = g_string_new(arg_exec); | |
745 | |
746 work = remote_list; | |
747 while (work) | |
748 { | |
749 gchar *text; | |
750 RemoteCommandEntry *entry; | |
751 | |
752 text = work->data; | |
753 work = work->next; | |
754 | |
755 entry = remote_command_find(text, NULL); | |
756 if (entry) | |
757 { | |
758 if (entry->prefer_command_line) | |
759 { | |
760 remote_list = g_list_remove(remote_list, text); | |
761 g_string_append(command, " "); | |
762 g_string_append(command, text); | |
763 } | |
764 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0) | |
765 { | |
766 blank = TRUE; | |
767 } | |
768 } | |
769 } | |
770 | |
771 if (blank || cmd_list || path) g_string_append(command, " --blank"); | |
772 if (get_debug_level()) g_string_append(command, " --debug"); | |
773 | |
774 g_string_append(command, " &"); | |
1144
5fe3b8b3a612
Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents:
1055
diff
changeset
|
775 runcmd(command->str); |
649 | 776 g_string_free(command, TRUE); |
777 | |
778 while (!rc && retry_count > 0) | |
779 { | |
780 usleep((retry_count > 10) ? 500000 : 1000000); | |
781 rc = remote_client_open(buf); | |
782 if (!rc) print_term("."); | |
783 retry_count--; | |
784 } | |
785 | |
786 print_term("\n"); | |
787 | |
788 started = TRUE; | |
789 } | |
790 g_free(buf); | |
791 | |
792 if (rc) | |
793 { | |
794 GList *work; | |
795 const gchar *prefix; | |
1437 | 796 gboolean use_path = TRUE; |
797 gboolean sent = FALSE; | |
649 | 798 |
799 work = remote_list; | |
800 while (work) | |
801 { | |
802 gchar *text; | |
803 RemoteCommandEntry *entry; | |
804 | |
805 text = work->data; | |
806 work = work->next; | |
807 | |
808 entry = remote_command_find(text, NULL); | |
809 if (entry && | |
810 entry->opt_l && | |
811 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE; | |
812 | |
813 remote_client_send(rc, text); | |
814 | |
815 sent = TRUE; | |
816 } | |
817 | |
818 if (cmd_list && cmd_list->next) | |
819 { | |
820 prefix = "--list-add:"; | |
821 remote_client_send(rc, "--list-clear"); | |
822 } | |
823 else | |
824 { | |
825 prefix = "file:"; | |
826 } | |
827 | |
828 work = cmd_list; | |
829 while (work) | |
830 { | |
831 FileData *fd; | |
832 gchar *text; | |
833 | |
834 fd = work->data; | |
835 work = work->next; | |
836 | |
837 text = g_strconcat(prefix, fd->path, NULL); | |
838 remote_client_send(rc, text); | |
839 g_free(text); | |
840 | |
841 sent = TRUE; | |
842 } | |
843 | |
844 if (path && !cmd_list && use_path) | |
845 { | |
846 gchar *text; | |
847 | |
848 text = g_strdup_printf("file:%s", path); | |
849 remote_client_send(rc, text); | |
850 g_free(text); | |
851 | |
852 sent = TRUE; | |
853 } | |
854 | |
855 work = collection_list; | |
856 while (work) | |
857 { | |
858 const gchar *name; | |
859 gchar *text; | |
860 | |
861 name = work->data; | |
862 work = work->next; | |
863 | |
864 text = g_strdup_printf("file:%s", name); | |
865 remote_client_send(rc, text); | |
866 g_free(text); | |
867 | |
868 sent = TRUE; | |
869 } | |
870 | |
871 if (!started && !sent) | |
872 { | |
873 remote_client_send(rc, "raise"); | |
874 } | |
875 } | |
876 else | |
877 { | |
878 print_term(_("Remote not available\n")); | |
879 } | |
880 | |
881 _exit(0); | |
882 } | |
883 | |
884 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection) | |
885 { | |
886 RemoteConnection *remote_connection = remote_server_open(path); | |
887 RemoteData *remote_data = g_new(RemoteData, 1); | |
888 | |
889 remote_data->command_collection = command_collection; | |
890 | |
891 remote_server_subscribe(remote_connection, remote_cb, remote_data); | |
892 return remote_connection; | |
893 } | |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1022
diff
changeset
|
894 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |