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