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