089507ad4ff81e5291c6d9c2f21d21c21d54d074
[geeqie.git] / src / remote.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "remote.h"
24
25 #include "cache_maint.h"
26 #include "collect.h"
27 #include "collect-io.h"
28 #include "filedata.h"
29 #include "filefilter.h"
30 #include "image.h"
31 #include "img-view.h"
32 #include "layout.h"
33 #include "layout_image.h"
34 #include "layout_util.h"
35 #include "misc.h"
36 #include "pixbuf-renderer.h"
37 #include "slideshow.h"
38 #include "ui_fileops.h"
39 #include "rcfile.h"
40
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <signal.h>
44 #include <errno.h>
45
46 #include "glua.h"
47
48 #define SERVER_MAX_CLIENTS 8
49
50 #define REMOTE_SERVER_BACKLOG 4
51
52
53 #ifndef UNIX_PATH_MAX
54 #define UNIX_PATH_MAX 108
55 #endif
56
57
58 static RemoteConnection *remote_client_open(const gchar *path);
59 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
60 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
61
62 static LayoutWindow *lw_id = NULL; /* points to the window set by the --id option */
63
64 typedef struct _RemoteClient RemoteClient;
65 struct _RemoteClient {
66         gint fd;
67         guint channel_id; /* event source id */
68         RemoteConnection *rc;
69 };
70
71 typedef struct _RemoteData RemoteData;
72 struct _RemoteData {
73         CollectionData *command_collection;
74 };
75
76
77 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
78 {
79         RemoteClient *client = data;
80         RemoteConnection *rc;
81         GIOStatus status = G_IO_STATUS_NORMAL;
82
83         lw_id = NULL;
84         rc = client->rc;
85
86         if (condition & G_IO_IN)
87                 {
88                 gchar *buffer = NULL;
89                 GError *error = NULL;
90                 gsize termpos;
91
92                 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
93                         {
94                         if (buffer)
95                                 {
96                                 buffer[termpos] = '\0';
97
98                                 if (strlen(buffer) > 0)
99                                         {
100                                         if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
101                                         g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
102                                         g_io_channel_flush(source, NULL);
103                                         }
104                                 g_free(buffer);
105
106                                 buffer = NULL;
107                                 }
108                         }
109
110                 if (error)
111                         {
112                         log_printf("error reading socket: %s\n", error->message);
113                         g_error_free(error);
114                         }
115                 }
116
117         if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
118                 {
119                 rc->clients = g_list_remove(rc->clients, client);
120
121                 DEBUG_1("HUP detected, closing client.");
122                 DEBUG_1("client count %d", g_list_length(rc->clients));
123
124                 g_source_remove(client->channel_id);
125                 close(client->fd);
126                 g_free(client);
127                 }
128
129         return TRUE;
130 }
131
132 static void remote_server_client_add(RemoteConnection *rc, gint fd)
133 {
134         RemoteClient *client;
135         GIOChannel *channel;
136
137         if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
138                 {
139                 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
140                 close(fd);
141                 return;
142                 }
143
144         client = g_new0(RemoteClient, 1);
145         client->rc = rc;
146         client->fd = fd;
147
148         channel = g_io_channel_unix_new(fd);
149         client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
150                                                  remote_server_client_cb, client, NULL);
151         g_io_channel_unref(channel);
152
153         rc->clients = g_list_append(rc->clients, client);
154         DEBUG_1("client count %d", g_list_length(rc->clients));
155 }
156
157 static void remote_server_clients_close(RemoteConnection *rc)
158 {
159         while (rc->clients)
160                 {
161                 RemoteClient *client = rc->clients->data;
162
163                 rc->clients = g_list_remove(rc->clients, client);
164
165                 g_source_remove(client->channel_id);
166                 close(client->fd);
167                 g_free(client);
168                 }
169 }
170
171 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
172 {
173         RemoteConnection *rc = data;
174         gint fd;
175         guint alen;
176
177         fd = accept(rc->fd, NULL, &alen);
178         if (fd == -1)
179                 {
180                 log_printf("error accepting socket: %s\n", strerror(errno));
181                 return TRUE;
182                 }
183
184         remote_server_client_add(rc, fd);
185
186         return TRUE;
187 }
188
189 gboolean remote_server_exists(const gchar *path)
190 {
191         RemoteConnection *rc;
192
193         /* verify server up */
194         rc = remote_client_open(path);
195         remote_close(rc);
196
197         if (rc) return TRUE;
198
199         /* unable to connect, remove socket file to free up address */
200         unlink(path);
201         return FALSE;
202 }
203
204 static RemoteConnection *remote_server_open(const gchar *path)
205 {
206         RemoteConnection *rc;
207         struct sockaddr_un addr;
208         gint sun_path_len;
209         gint fd;
210         GIOChannel *channel;
211
212         if (remote_server_exists(path))
213                 {
214                 log_printf("Address already in use: %s\n", path);
215                 return NULL;
216                 }
217
218         fd = socket(PF_UNIX, SOCK_STREAM, 0);
219         if (fd == -1) return NULL;
220
221         addr.sun_family = AF_UNIX;
222         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
223         strncpy(addr.sun_path, path, sun_path_len);
224         if (bind(fd, &addr, sizeof(addr)) == -1 ||
225             listen(fd, REMOTE_SERVER_BACKLOG) == -1)
226                 {
227                 log_printf("error subscribing to socket: %s\n", strerror(errno));
228                 close(fd);
229                 return NULL;
230                 }
231
232         rc = g_new0(RemoteConnection, 1);
233
234         rc->server = TRUE;
235         rc->fd = fd;
236         rc->path = g_strdup(path);
237
238         channel = g_io_channel_unix_new(rc->fd);
239         g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
240
241         rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
242                                              remote_server_read_cb, rc, NULL);
243         g_io_channel_unref(channel);
244
245         return rc;
246 }
247
248 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
249 {
250         if (!rc || !rc->server) return;
251
252         rc->read_func = func;
253         rc->read_data = data;
254 }
255
256
257 static RemoteConnection *remote_client_open(const gchar *path)
258 {
259         RemoteConnection *rc;
260         struct stat st;
261         struct sockaddr_un addr;
262         gint sun_path_len;
263         gint fd;
264
265         if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
266
267         fd = socket(PF_UNIX, SOCK_STREAM, 0);
268         if (fd == -1) return NULL;
269
270         addr.sun_family = AF_UNIX;
271         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
272         strncpy(addr.sun_path, path, sun_path_len);
273         if (connect(fd, &addr, sizeof(addr)) == -1)
274                 {
275                 DEBUG_1("error connecting to socket: %s", strerror(errno));
276                 close(fd);
277                 return NULL;
278                 }
279
280         rc = g_new0(RemoteConnection, 1);
281         rc->server = FALSE;
282         rc->fd = fd;
283         rc->path = g_strdup(path);
284
285         return rc;
286 }
287
288 static sig_atomic_t sigpipe_occurred = FALSE;
289
290 static void sighandler_sigpipe(gint sig)
291 {
292         sigpipe_occurred = TRUE;
293 }
294
295 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
296 {
297         struct sigaction new_action, old_action;
298         gboolean ret = FALSE;
299         GError *error = NULL;
300         GIOChannel *channel;
301
302         if (!rc || rc->server) return FALSE;
303         if (!text) return TRUE;
304
305         sigpipe_occurred = FALSE;
306
307         new_action.sa_handler = sighandler_sigpipe;
308         sigemptyset(&new_action.sa_mask);
309         new_action.sa_flags = 0;
310
311         /* setup our signal handler */
312         sigaction(SIGPIPE, &new_action, &old_action);
313
314         channel = g_io_channel_unix_new(rc->fd);
315
316         g_io_channel_write_chars(channel, text, -1, NULL, &error);
317         g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
318         g_io_channel_flush(channel, &error);
319
320         if (error)
321                 {
322                 log_printf("error reading socket: %s\n", error->message);
323                 g_error_free(error);
324                 ret = FALSE;;
325                 }
326         else
327                 {
328                 ret = TRUE;
329                 }
330
331         if (ret)
332                 {
333                 gchar *buffer = NULL;
334                 gsize termpos;
335                 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
336                         {
337                         if (buffer)
338                                 {
339                                 if (buffer[0] == '\n') /* empty line finishes the command */
340                                         {
341                                         g_free(buffer);
342                                         fflush(stdout);
343                                         break;
344                                         }
345                                 buffer[termpos] = '\0';
346                                 printf("%s\n", buffer);
347                                 g_free(buffer);
348                                 buffer = NULL;
349                                 }
350                         }
351
352                 if (error)
353                         {
354                         log_printf("error reading socket: %s\n", error->message);
355                         g_error_free(error);
356                         ret = FALSE;
357                         }
358                 }
359
360
361         /* restore the original signal handler */
362         sigaction(SIGPIPE, &old_action, NULL);
363         g_io_channel_unref(channel);
364         return ret;
365 }
366
367 void remote_close(RemoteConnection *rc)
368 {
369         if (!rc) return;
370
371         if (rc->server)
372                 {
373                 remote_server_clients_close(rc);
374
375                 g_source_remove(rc->channel_id);
376                 unlink(rc->path);
377                 }
378
379         if (rc->read_data)
380                 g_free(rc->read_data);
381
382         close(rc->fd);
383
384         g_free(rc->path);
385         g_free(rc);
386 }
387
388 /*
389  *-----------------------------------------------------------------------------
390  * remote functions
391  *-----------------------------------------------------------------------------
392  */
393
394 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
395 {
396         layout_image_next(lw_id);
397 }
398
399 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
400 {
401         LayoutWindow *lw = NULL;
402
403         if (!layout_valid(&lw)) return;
404
405         lw_id = layout_menu_new_window(NULL, lw);
406 }
407
408 static gboolean gr_close_window_cb()
409 {
410         if (!layout_valid(&lw_id)) return FALSE;
411
412         layout_menu_close_cb(NULL, lw_id);
413
414         return FALSE;
415 }
416
417 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
418 {
419         g_idle_add(gr_close_window_cb, NULL);
420 }
421
422 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
423 {
424         layout_image_prev(lw_id);
425 }
426
427 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
428 {
429         layout_image_first(lw_id);
430 }
431
432 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
433 {
434         layout_image_last(lw_id);
435 }
436
437 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
438 {
439         layout_image_full_screen_toggle(lw_id);
440 }
441
442 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
443 {
444         layout_image_full_screen_start(lw_id);
445 }
446
447 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
448 {
449         layout_image_full_screen_stop(lw_id);
450 }
451
452 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
453 {
454         lw_id = layout_find_by_layout_id(text);
455         if (!lw_id)
456                 {
457                 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
458                 }
459         layout_valid(&lw_id);
460 }
461
462 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
463 {
464         GList *list;
465         FileData *dir_fd = file_data_new_dir(text);
466         list = filelist_recursive(dir_fd);
467         file_data_unref(dir_fd);
468         if (!list) return;
469 //printf("length: %d\n", g_list_length(list));
470         layout_image_slideshow_stop(lw_id);
471         layout_image_slideshow_start_from_list(lw_id, list);
472 }
473
474 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
475 {
476         if (!g_strcmp0(text, "clear"))
477                 cache_maintain_home_remote(FALSE, TRUE);
478         else if (!g_strcmp0(text, "clean"))
479                 cache_maintain_home_remote(FALSE, FALSE);
480 }
481
482 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
483 {
484         if (!g_strcmp0(text, "clear"))
485                 cache_manager_standard_process_remote(TRUE);
486         else if (!g_strcmp0(text, "clean"))
487                 cache_manager_standard_process_remote(FALSE);
488 }
489
490 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
491 {
492         cache_maintain_home_remote(TRUE, FALSE);
493 }
494
495 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
496 {
497         cache_manager_render_remote(text, FALSE, FALSE);
498 }
499
500 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
501 {
502         cache_manager_render_remote(text, TRUE, FALSE);
503 }
504
505 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
506 {
507         if(options->thumbnails.spec_standard)
508                 cache_manager_render_remote(text, FALSE, TRUE);
509 }
510
511 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
512 {
513         if(options->thumbnails.spec_standard)
514                 cache_manager_render_remote(text, TRUE, TRUE);
515 }
516
517 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
518 {
519         layout_image_slideshow_toggle(lw_id);
520 }
521
522 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
523 {
524         layout_image_slideshow_start(lw_id);
525 }
526
527 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
528 {
529         layout_image_slideshow_stop(lw_id);
530 }
531
532 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
533 {
534         gdouble t1, t2, t3, n;
535         gint res;
536
537         res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
538         if (res == 3)
539                 {
540                 n = (t1 * 3600) + (t2 * 60) + t3;
541                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
542                                 t1 >= 24 || t2 >= 60 || t3 >= 60)
543                         {
544                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
545                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
546                         return;
547                         }
548                 }
549         else if (res == 2)
550                 {
551                 n = t1 * 60 + t2;
552                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
553                                 t1 >= 60 || t2 >= 60)
554                         {
555                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
556                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
557                         return;
558                         }
559                 }
560         else if (res == 1)
561                 {
562                 n = t1;
563                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
564                         {
565                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
566                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
567                         return;
568                         }
569                 }
570         else
571                 {
572                 n = 0;
573                 }
574
575         options->slideshow.delay = (gint)(n * 10.0 + 0.01);
576 }
577
578 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
579 {
580         gboolean popped;
581         gboolean hidden;
582
583         if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
584                 {
585                 layout_tools_float_set(lw_id, popped, FALSE);
586                 }
587 }
588
589 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
590 {
591         gboolean popped;
592         gboolean hidden;
593
594         if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
595                 {
596                 layout_tools_float_set(lw_id, popped, TRUE);
597                 }
598 }
599
600 static gboolean gr_quit_idle_cb(gpointer data)
601 {
602         exit_program();
603
604         return FALSE;
605 }
606
607 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
608 {
609         /* schedule exit when idle, if done from within a
610          * remote handler remote_close will crash
611          */
612         g_idle_add(gr_quit_idle_cb, NULL);
613 }
614
615 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
616 {
617         gchar *filename = expand_tilde(text);
618
619         if (isfile(filename))
620                 {
621                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
622                         {
623                         collection_window_new(filename);
624                         }
625                 else
626                         {
627                         layout_set_path(lw_id, filename);
628                         }
629                 }
630         else if (isdir(filename))
631                 {
632                 layout_set_path(lw_id, filename);
633                 }
634         else
635                 {
636                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
637                 layout_set_path(lw_id, homedir());
638                 }
639
640         g_free(filename);
641 }
642
643 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
644 {
645         gr_file_load_no_raise(text, channel, data);
646
647         gr_raise(text, channel, data);
648 }
649
650 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
651 {
652         gchar *pixel_info;
653         gint x_pixel, y_pixel;
654         gint width, height;
655         gint r_mouse, g_mouse, b_mouse;
656         PixbufRenderer *pr;
657         LayoutWindow *lw = NULL;
658
659         if (!layout_valid(&lw_id)) return;
660
661         pr = (PixbufRenderer*)lw_id->image->pr;
662
663         if (pr)
664                 {
665                 pixbuf_renderer_get_image_size(pr, &width, &height);
666                 if (width < 1 || height < 1) return;
667
668                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
669
670                 if (x_pixel >= 0 && y_pixel >= 0)
671                         {
672                         pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
673                                                          &r_mouse, &g_mouse, &b_mouse);
674
675                         pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
676                                                  x_pixel, y_pixel,
677                                                  r_mouse, g_mouse, b_mouse);
678
679                         g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
680                         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
681
682                         g_free(pixel_info);
683                         }
684                 else
685                         {
686                         return;
687                         }
688                 }
689         else
690                 {
691                 return;
692                 }
693 }
694
695 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
696 {
697         gchar *rectangle_info;
698         PixbufRenderer *pr;
699         LayoutWindow *lw = NULL;
700         gint x1, y1, x2, y2;
701
702         if (!options->draw_rectangle) return;
703         if (!layout_valid(&lw_id)) return;
704
705         pr = (PixbufRenderer*)lw_id->image->pr;
706
707         if (pr)
708                 {
709                 image_get_rectangle(&x1, &y1, &x2, &y2);
710                 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
711                                         (x2 > x1) ? x2 - x1 : x1 - x2,
712                                         (y2 > y1) ? y2 - y1 : y1 - y2,
713                                         (x2 > x1) ? x1 : x2,
714                                         (y2 > y1) ? y1 : y2);
715
716                 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
717                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
718
719                 g_free(rectangle_info);
720                 }
721 }
722
723 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
724 {
725         gchar *render_intent;
726
727         switch (options->color_profile.render_intent)
728                 {
729                 case 0:
730                         render_intent = g_strdup("Perceptual");
731                         break;
732                 case 1:
733                         render_intent = g_strdup("Relative Colorimetric");
734                         break;
735                 case 2:
736                         render_intent = g_strdup("Saturation");
737                         break;
738                 case 3:
739                         render_intent = g_strdup("Absolute Colorimetric");
740                         break;
741                 default:
742                         render_intent = g_strdup("none");
743                         break;
744                 }
745
746         g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
747         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
748
749         g_free(render_intent);
750 }
751
752 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
753 {
754         GList *list = NULL;
755         FileFormatClass class;
756         FileData *dir_fd;
757         FileData *fd;
758         GString *out_string = g_string_new(NULL);
759         GList *work;
760
761         if (strcmp(text, "") == 0)
762                 {
763                 if (layout_valid(&lw_id))
764                         {
765                         dir_fd = file_data_new_dir(lw_id->dir_fd->path);
766                         }
767                 else
768                         {
769                         return;
770                         }
771                 }
772         else
773                 {
774                 if (isdir(text))
775                         {
776                         dir_fd = file_data_new_dir(text);
777                         }
778                 else
779                         {
780                         return;
781                         }
782                 }
783
784         if (recurse)
785                 {
786                 list = filelist_recursive(dir_fd);
787                 }
788         else
789                 {
790                 filelist_read(dir_fd, &list, NULL);
791                 }
792
793         work = list;
794         while (work)
795                 {
796                 fd = work->data;
797                 g_string_append_printf(out_string, "%s", fd->path);
798                 class = filter_file_get_class(fd->path);
799
800                 switch (class)
801                         {
802                         case FORMAT_CLASS_IMAGE:
803                                 out_string = g_string_append(out_string, "    Class: Image");
804                                 break;
805                         case FORMAT_CLASS_RAWIMAGE:
806                                 out_string = g_string_append(out_string, "    Class: RAW image");
807                                 break;
808                         case FORMAT_CLASS_META:
809                                 out_string = g_string_append(out_string, "    Class: Metadata");
810                                 break;
811                         case FORMAT_CLASS_VIDEO:
812                                 out_string = g_string_append(out_string, "    Class: Video");
813                                 break;
814                         case FORMAT_CLASS_COLLECTION:
815                                 out_string = g_string_append(out_string, "    Class: Collection");
816                                 break;
817                         case FORMAT_CLASS_PDF:
818                                 out_string = g_string_append(out_string, "    Class: PDF");
819                                 break;
820                         case FORMAT_CLASS_UNKNOWN:
821                                 out_string = g_string_append(out_string, "    Class: Unknown");
822                                 break;
823                         default:
824                                 out_string = g_string_append(out_string, "    Class: Unknown");
825                                 break;
826                         }
827                 out_string = g_string_append(out_string, "\n");
828                 work = work->next;
829                 }
830
831         g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
832         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
833
834         g_string_free(out_string, TRUE);
835         filelist_free(list);
836         file_data_unref(dir_fd);
837 }
838
839 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
840 {
841         GString *contents = g_string_new(NULL);
842
843         if (is_collection(text))
844                 {
845                 collection_contents(text, &contents);
846                 }
847         else
848                 {
849                 return;
850                 }
851
852         g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
853         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
854
855         g_string_free(contents, TRUE);
856 }
857
858 static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer data)
859 {
860
861         GList *collection_list = NULL;
862         GList *work;
863         GString *out_string = g_string_new(NULL);
864
865         collect_manager_list(&collection_list, NULL, NULL);
866
867         work = collection_list;
868         while (work)
869                 {
870                 const gchar *collection_name = work->data;
871                 out_string = g_string_append(out_string, g_strdup(collection_name));
872                 out_string = g_string_append(out_string, "\n");
873
874                 work = work->next;
875                 }
876
877         g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
878         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
879
880         string_list_free(collection_list);
881         g_string_free(out_string, TRUE);
882 }
883
884
885 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
886 {
887         get_filelist(text, channel, FALSE);
888 }
889
890 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer data)
891 {
892         get_filelist(text, channel, TRUE);
893 }
894
895 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
896 {
897         gchar *out_string;
898         gchar *collection_name = NULL;
899
900         if (!layout_valid(&lw_id)) return;
901
902         if (image_get_path(lw_id->image))
903                 {
904                 if (lw_id->image->collection && lw_id->image->collection->name)
905                         {
906                         collection_name = remove_extension_from_path(lw_id->image->collection->name);
907                         out_string = g_strconcat(image_get_path(lw_id->image), "    Collection: ", collection_name, NULL);
908                         }
909                 else
910                         {
911                         out_string = g_strconcat(image_get_path(lw_id->image), NULL);
912                         }
913
914                 g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
915                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
916
917                 g_free(collection_name);
918                 g_free(out_string);
919                 }
920 }
921
922 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
923 {
924         gchar *filename = expand_tilde(text);
925
926         if (isfile(filename))
927                 {
928                 load_config_from_file(filename, FALSE);
929                 }
930         else
931                 {
932                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
933                 layout_set_path(NULL, homedir());
934                 }
935
936         g_free(filename);
937 }
938
939 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
940 {
941         gchar *filename = expand_tilde(text);
942         FileData *fd = file_data_new_group(filename);
943
944         GList *work;
945         if (fd->parent) fd = fd->parent;
946
947         g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
948         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
949
950         work = fd->sidecar_files;
951
952         while (work)
953                 {
954                 fd = work->data;
955                 work = work->next;
956                 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
957                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
958                 }
959         g_free(filename);
960 }
961
962 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
963 {
964         gchar *filename = expand_tilde(text);
965         FileData *fd = file_data_new_group(filename);
966
967         if (fd->change && fd->change->dest)
968                 {
969                 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
970                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
971                 }
972         g_free(filename);
973 }
974
975 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
976 {
977         gchar *filename = expand_tilde(text);
978
979         view_window_new(file_data_new_group(filename));
980         g_free(filename);
981 }
982
983 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
984 {
985         RemoteData *remote_data = data;
986
987         if (remote_data->command_collection)
988                 {
989                 collection_unref(remote_data->command_collection);
990                 remote_data->command_collection = NULL;
991                 }
992 }
993
994 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
995 {
996         RemoteData *remote_data = data;
997         gboolean new = TRUE;
998
999         if (!remote_data->command_collection)
1000                 {
1001                 CollectionData *cd;
1002
1003                 cd = collection_new("");
1004
1005                 g_free(cd->path);
1006                 cd->path = NULL;
1007                 g_free(cd->name);
1008                 cd->name = g_strdup(_("Command line"));
1009
1010                 remote_data->command_collection = cd;
1011                 }
1012         else
1013                 {
1014                 new = (!collection_get_first(remote_data->command_collection));
1015                 }
1016
1017         if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
1018                 {
1019                 layout_image_set_collection(NULL, remote_data->command_collection,
1020                                             collection_get_first(remote_data->command_collection));
1021                 }
1022 }
1023
1024 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
1025 {
1026         LayoutWindow *lw = NULL;
1027
1028         if (layout_valid(&lw_id))
1029                 {
1030                 gtk_window_present(GTK_WINDOW(lw_id->window));
1031                 }
1032 }
1033
1034 #ifdef HAVE_LUA
1035 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
1036 {
1037         gchar *result = NULL;
1038         gchar **lua_command;
1039
1040         lua_command = g_strsplit(text, ",", 2);
1041
1042         if (lua_command[0] && lua_command[1])
1043                 {
1044                 FileData *fd = file_data_new_group(lua_command[0]);
1045                 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1046                 if (result)
1047                         {
1048                         g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1049                         }
1050                 else
1051                         {
1052                         g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1053                         }
1054                 }
1055         else
1056                 {
1057                 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1058                 }
1059
1060         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
1061
1062         g_strfreev(lua_command);
1063         g_free(result);
1064 }
1065 #endif
1066
1067 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1068 struct _RemoteCommandEntry {
1069         gchar *opt_s;
1070         gchar *opt_l;
1071         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1072         gboolean needs_extra;
1073         gboolean prefer_command_line;
1074         gchar *parameter;
1075         gchar *description;
1076 };
1077
1078 static RemoteCommandEntry remote_commands[] = {
1079         /* short, long                  callback,               extra, prefer, parameter, description */
1080         { "-n", "--next",               gr_image_next,          FALSE, FALSE, NULL, N_("next image") },
1081         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, NULL, N_("previous image") },
1082         { NULL, "--first",              gr_image_first,         FALSE, FALSE, NULL, N_("first image") },
1083         { NULL, "--last",               gr_image_last,          FALSE, FALSE, NULL, N_("last image") },
1084         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  NULL, N_("toggle full screen") },
1085         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, NULL, N_("start full screen") },
1086         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, NULL, N_("stop full screen") },
1087         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  NULL, N_("toggle slide show") },
1088         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, NULL, N_("start slide show") },
1089         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, NULL, N_("stop slide show") },
1090         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1091         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1092         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  NULL, N_("show tools") },
1093         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  NULL, N_("hide tools") },
1094         { "-q", "--quit",               gr_quit,                FALSE, FALSE, NULL, N_("quit") },
1095         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>"), N_("load configuration from FILE") },
1096         { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1097         { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE") },
1098         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1099         { NULL, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1100         { NULL, "--tell",               gr_file_tell,           FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1101         { NULL, "--pixel-info",         gr_pixel_info,          FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1102         { NULL, "--get-rectangle",      gr_rectangle,           FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1103         { NULL, "--get-render-intent",  gr_render_intent,       FALSE, FALSE, NULL, N_("get render intent") },
1104         { NULL, "--get-filelist:",      gr_filelist,            TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1105         { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1106         { NULL, "--get-collection:",    gr_collection,          TRUE,  FALSE, N_("<COLLECTION>"), N_("get collection content") },
1107         { NULL, "--get-collection-list", gr_collection_list,    FALSE, FALSE, NULL, N_("get collection list") },
1108         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
1109         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL, N_("clear command line collection list") },
1110         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1111         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1112         { NULL, "--id:",                gr_lw_id,               TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1113         { NULL, "--new-window",         gr_new_window,          FALSE, FALSE, NULL, N_("new window") },
1114         { NULL, "--close-window",       gr_close_window,        FALSE, FALSE, NULL, N_("close window") },
1115         { "-ct:", "--cache-thumbs:",    gr_cache_thumb,         TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1116         { "-cs:", "--cache-shared:",    gr_cache_shared,        TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1117         { "-cm","--cache-metadata",      gr_cache_metadata,               FALSE, FALSE, NULL, N_("    clean the metadata cache") },
1118         { "-cr:", "--cache-render:",    gr_cache_render,        TRUE, FALSE, N_("<folder>  "), N_(" render thumbnails") },
1119         { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1120         { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1121         { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1122 #ifdef HAVE_LUA
1123         { NULL, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1124 #endif
1125         { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1126 };
1127
1128 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1129 {
1130         gboolean match = FALSE;
1131         gint i;
1132
1133         i = 0;
1134         while (!match && remote_commands[i].func != NULL)
1135                 {
1136                 if (remote_commands[i].needs_extra)
1137                         {
1138                         if (remote_commands[i].opt_s &&
1139                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1140                                 {
1141                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1142                                 return &remote_commands[i];
1143                                 }
1144                         else if (remote_commands[i].opt_l &&
1145                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1146                                 {
1147                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1148                                 return &remote_commands[i];
1149                                 }
1150                         }
1151                 else
1152                         {
1153                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1154                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1155                                 {
1156                                 if (offset) *offset = text;
1157                                 return &remote_commands[i];
1158                                 }
1159                         }
1160
1161                 i++;
1162                 }
1163
1164         return NULL;
1165 }
1166
1167 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1168 {
1169         RemoteCommandEntry *entry;
1170         const gchar *offset;
1171
1172         entry = remote_command_find(text, &offset);
1173         if (entry && entry->func)
1174                 {
1175                 entry->func(offset, channel, data);
1176                 }
1177         else
1178                 {
1179                 log_printf("unknown remote command:%s\n", text);
1180                 }
1181 }
1182
1183 void remote_help(void)
1184 {
1185         gint i;
1186         gchar *s_opt_param;
1187         gchar *l_opt_param;
1188
1189         print_term(FALSE, _("Remote command list:\n"));
1190
1191         i = 0;
1192         while (remote_commands[i].func != NULL)
1193                 {
1194                 if (remote_commands[i].description)
1195                         {
1196                         s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
1197                         l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1198                         printf_term(FALSE, "  %-11s%-1s %-30s%-s\n",
1199                                     (remote_commands[i].opt_s) ? s_opt_param : "",
1200                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
1201                                     (remote_commands[i].opt_l) ? l_opt_param : "",
1202                                     _(remote_commands[i].description));
1203                         g_free(s_opt_param);
1204                         g_free(l_opt_param);
1205                         }
1206                 i++;
1207                 }
1208         printf_term(FALSE, N_("\n  All other command line parameters are used as plain files if they exists.\n"));
1209 }
1210
1211 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1212 {
1213         gint i;
1214
1215         i = 1;
1216         while (i < argc)
1217                 {
1218                 RemoteCommandEntry *entry;
1219
1220                 entry = remote_command_find(argv[i], NULL);
1221                 if (entry)
1222                         {
1223                         list = g_list_append(list, argv[i]);
1224                         }
1225                 else if (errors && !isfile(argv[i]))
1226                         {
1227                         *errors = g_list_append(*errors, argv[i]);
1228                         }
1229                 i++;
1230                 }
1231
1232         return list;
1233 }
1234
1235 /**
1236  * \param arg_exec Binary (argv0)
1237  * \param remote_list Evaluated and recognized remote commands
1238  * \param path The current path
1239  * \param cmd_list List of all non collections in Path
1240  * \param collection_list List of all collections in argv
1241  */
1242 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1243                     GList *cmd_list, GList *collection_list)
1244 {
1245         RemoteConnection *rc;
1246         gboolean started = FALSE;
1247         gchar *buf;
1248
1249         buf = g_build_filename(get_rc_dir(), ".command", NULL);
1250         rc = remote_client_open(buf);
1251         if (!rc)
1252                 {
1253                 GString *command;
1254                 GList *work;
1255                 gint retry_count = 12;
1256                 gboolean blank = FALSE;
1257
1258                 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1259
1260                 command = g_string_new(arg_exec);
1261
1262                 work = remote_list;
1263                 while (work)
1264                         {
1265                         gchar *text;
1266                         RemoteCommandEntry *entry;
1267
1268                         text = work->data;
1269                         work = work->next;
1270
1271                         entry = remote_command_find(text, NULL);
1272                         if (entry)
1273                                 {
1274                                 if (entry->prefer_command_line)
1275                                         {
1276                                         remote_list = g_list_remove(remote_list, text);
1277                                         g_string_append(command, " ");
1278                                         g_string_append(command, text);
1279                                         }
1280                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1281                                         {
1282                                         blank = TRUE;
1283                                         }
1284                                 }
1285                         }
1286
1287                 if (blank || cmd_list || path) g_string_append(command, " --blank");
1288                 if (get_debug_level()) g_string_append(command, " --debug");
1289
1290                 g_string_append(command, " &");
1291                 runcmd(command->str);
1292                 g_string_free(command, TRUE);
1293
1294                 while (!rc && retry_count > 0)
1295                         {
1296                         usleep((retry_count > 10) ? 500000 : 1000000);
1297                         rc = remote_client_open(buf);
1298                         if (!rc) print_term(FALSE, ".");
1299                         retry_count--;
1300                         }
1301
1302                 print_term(FALSE, "\n");
1303
1304                 started = TRUE;
1305                 }
1306         g_free(buf);
1307
1308         if (rc)
1309                 {
1310                 GList *work;
1311                 const gchar *prefix;
1312                 gboolean use_path = TRUE;
1313                 gboolean sent = FALSE;
1314
1315                 work = remote_list;
1316                 while (work)
1317                         {
1318                         gchar *text;
1319                         RemoteCommandEntry *entry;
1320
1321                         text = work->data;
1322                         work = work->next;
1323
1324                         entry = remote_command_find(text, NULL);
1325                         if (entry &&
1326                             entry->opt_l &&
1327                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1328
1329                         remote_client_send(rc, text);
1330
1331                         sent = TRUE;
1332                         }
1333
1334                 if (cmd_list && cmd_list->next)
1335                         {
1336                         prefix = "--list-add:";
1337                         remote_client_send(rc, "--list-clear");
1338                         }
1339                 else
1340                         {
1341                         prefix = "file:";
1342                         }
1343
1344                 work = cmd_list;
1345                 while (work)
1346                         {
1347                         FileData *fd;
1348                         gchar *text;
1349
1350                         fd = work->data;
1351                         work = work->next;
1352
1353                         text = g_strconcat(prefix, fd->path, NULL);
1354                         remote_client_send(rc, text);
1355                         g_free(text);
1356
1357                         sent = TRUE;
1358                         }
1359
1360                 if (path && !cmd_list && use_path)
1361                         {
1362                         gchar *text;
1363
1364                         text = g_strdup_printf("file:%s", path);
1365                         remote_client_send(rc, text);
1366                         g_free(text);
1367
1368                         sent = TRUE;
1369                         }
1370
1371                 work = collection_list;
1372                 while (work)
1373                         {
1374                         const gchar *name;
1375                         gchar *text;
1376
1377                         name = work->data;
1378                         work = work->next;
1379
1380                         text = g_strdup_printf("file:%s", name);
1381                         remote_client_send(rc, text);
1382                         g_free(text);
1383
1384                         sent = TRUE;
1385                         }
1386
1387                 if (!started && !sent)
1388                         {
1389                         remote_client_send(rc, "raise");
1390                         }
1391                 }
1392         else
1393                 {
1394                 print_term(TRUE, _("Remote not available\n"));
1395                 }
1396
1397         _exit(0);
1398 }
1399
1400 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1401 {
1402         RemoteConnection *remote_connection = remote_server_open(path);
1403         RemoteData *remote_data = g_new(RemoteData, 1);
1404
1405         remote_data->command_collection = command_collection;
1406
1407         remote_server_subscribe(remote_connection, remote_cb, remote_data);
1408         return remote_connection;
1409 }
1410 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */