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