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