comparison src/main.c @ 649:3097880d7d95

Move all remote stuff from main.c to remote.[ch].
author zas_
date Tue, 13 May 2008 08:53:26 +0000
parents e34c1002e553
children 9bcfd6d7a902
comparison
equal deleted inserted replaced
648:e34c1002e553 649:3097880d7d95
46 46
47 #include <math.h> 47 #include <math.h>
48 48
49 49
50 static RemoteConnection *remote_connection = NULL; 50 static RemoteConnection *remote_connection = NULL;
51 static CollectionData *command_collection = NULL;
52 51
53 52
54 /* 53 /*
55 *----------------------------------------------------------------------------- 54 *-----------------------------------------------------------------------------
56 * misc (public) 55 * misc (public)
126 *x = *x * delta; 125 *x = *x * delta;
127 *y = *y * delta; 126 *y = *y * delta;
128 } 127 }
129 128
130 129
131 /*
132 *-----------------------------------------------------------------------------
133 * remote functions
134 *-----------------------------------------------------------------------------
135 */
136
137 static void gr_image_next(const gchar *text, gpointer data)
138 {
139 layout_image_next(NULL);
140 }
141
142 static void gr_image_prev(const gchar *text, gpointer data)
143 {
144 layout_image_prev(NULL);
145 }
146
147 static void gr_image_first(const gchar *text, gpointer data)
148 {
149 layout_image_first(NULL);
150 }
151
152 static void gr_image_last(const gchar *text, gpointer data)
153 {
154 layout_image_last(NULL);
155 }
156
157 static void gr_fullscreen_toggle(const gchar *text, gpointer data)
158 {
159 layout_image_full_screen_toggle(NULL);
160 }
161
162 static void gr_fullscreen_start(const gchar *text, gpointer data)
163 {
164 layout_image_full_screen_start(NULL);
165 }
166
167 static void gr_fullscreen_stop(const gchar *text, gpointer data)
168 {
169 layout_image_full_screen_stop(NULL);
170 }
171
172 static void gr_slideshow_start_rec(const gchar *text, gpointer data)
173 {
174 GList *list;
175
176 list = filelist_recursive(text);
177 if (!list) return;
178 //printf("length: %d\n", g_list_length(list));
179 layout_image_slideshow_stop(NULL);
180 layout_image_slideshow_start_from_list(NULL, list);
181 }
182
183 static void gr_slideshow_toggle(const gchar *text, gpointer data)
184 {
185 layout_image_slideshow_toggle(NULL);
186 }
187
188 static void gr_slideshow_start(const gchar *text, gpointer data)
189 {
190 layout_image_slideshow_start(NULL);
191 }
192
193 static void gr_slideshow_stop(const gchar *text, gpointer data)
194 {
195 layout_image_slideshow_stop(NULL);
196 }
197
198 static void gr_slideshow_delay(const gchar *text, gpointer data)
199 {
200 gdouble n;
201
202 n = strtod(text, NULL);
203 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
204 {
205 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
206 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
207 return;
208 }
209 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
210 }
211
212 static void gr_tools_show(const gchar *text, gpointer data)
213 {
214 gint popped;
215 gint hidden;
216
217 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
218 {
219 layout_tools_float_set(NULL, popped, FALSE);
220 }
221 }
222
223 static void gr_tools_hide(const gchar *text, gpointer data)
224 {
225 gint popped;
226 gint hidden;
227
228 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
229 {
230 layout_tools_float_set(NULL, popped, TRUE);
231 }
232 }
233
234 static gint gr_quit_idle_cb(gpointer data)
235 {
236 exit_program();
237
238 return FALSE;
239 }
240
241 static void gr_quit(const gchar *text, gpointer data)
242 {
243 /* schedule exit when idle, if done from within a
244 * remote handler remote_close will crash
245 */
246 g_idle_add(gr_quit_idle_cb, NULL);
247 }
248
249 static void gr_file_load(const gchar *text, gpointer data)
250 {
251 if (isfile(text))
252 {
253 if (file_extension_match(text, ".gqv"))
254 {
255 collection_window_new(text);
256 }
257 else
258 {
259 layout_set_path(NULL, text);
260 }
261 }
262 else if (isdir(text))
263 {
264 layout_set_path(NULL, text);
265 }
266 else
267 {
268 printf("remote sent filename that does not exist:\"%s\"\n", text);
269 }
270 }
271
272 static void gr_file_view(const gchar *text, gpointer data)
273 {
274 view_window_new(file_data_new_simple(text));
275 }
276
277 static void gr_list_clear(const gchar *text, gpointer data)
278 {
279 if (command_collection) collection_unref(command_collection);
280 command_collection = NULL;
281 }
282
283 static void gr_list_add(const gchar *text, gpointer data)
284 {
285 gint new = TRUE;
286
287 if (!command_collection)
288 {
289 CollectionData *cd;
290
291 cd = collection_new("");
292
293 g_free(cd->path);
294 cd->path = NULL;
295 g_free(cd->name);
296 cd->name = g_strdup(_("Command line"));
297
298 command_collection = cd;
299 }
300 else
301 {
302 new = (!collection_get_first(command_collection));
303 }
304
305 if (collection_add(command_collection, file_data_new_simple(text), FALSE) && new)
306 {
307 layout_image_set_collection(NULL, command_collection,
308 collection_get_first(command_collection));
309 }
310 }
311
312 static void gr_raise(const gchar *text, gpointer data)
313 {
314 LayoutWindow *lw = NULL;
315
316 if (layout_valid(&lw))
317 {
318 gtk_window_present(GTK_WINDOW(lw->window));
319 }
320 }
321
322 typedef struct _RemoteCommandEntry RemoteCommandEntry;
323 struct _RemoteCommandEntry {
324 gchar *opt_s;
325 gchar *opt_l;
326 void (*func)(const gchar *text, gpointer data);
327 gint needs_extra;
328 gint prefer_command_line;
329 gchar *description;
330 };
331
332 static RemoteCommandEntry remote_commands[] = {
333 /* short, long callback, extra, prefer,description */
334 { "-n", "--next", gr_image_next, FALSE, FALSE, N_("next image") },
335 { "-b", "--back", gr_image_prev, FALSE, FALSE, N_("previous image") },
336 { NULL, "--first", gr_image_first, FALSE, FALSE, N_("first image") },
337 { NULL, "--last", gr_image_last, FALSE, FALSE, N_("last image") },
338 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, N_("toggle full screen") },
339 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, N_("start full screen") },
340 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, N_("stop full screen") },
341 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, N_("toggle slide show") },
342 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, N_("start slide show") },
343 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, N_("stop slide show") },
344 { "-sr","--slideshow-recurse", gr_slideshow_start_rec, TRUE, FALSE, N_("start recursive slide show") },
345 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("set slide show delay in seconds") },
346 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, N_("show tools") },
347 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, N_("hide tools") },
348 { "-q", "--quit", gr_quit, FALSE, FALSE, N_("quit") },
349 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("open file") },
350 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("open file in new window") },
351 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL },
352 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, NULL },
353 { NULL, "raise", gr_raise, FALSE, FALSE, NULL },
354 { NULL, NULL, NULL, FALSE, FALSE, NULL }
355 };
356
357 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
358 {
359 gint match = FALSE;
360 gint i;
361
362 i = 0;
363 while (!match && remote_commands[i].func != NULL)
364 {
365 if (remote_commands[i].needs_extra)
366 {
367 if (remote_commands[i].opt_s &&
368 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
369 {
370 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
371 return &remote_commands[i];
372 }
373 else if (remote_commands[i].opt_l &&
374 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
375 {
376 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
377 return &remote_commands[i];
378 }
379 }
380 else
381 {
382 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
383 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
384 {
385 if (offset) *offset = text;
386 return &remote_commands[i];
387 }
388 }
389
390 i++;
391 }
392
393 return NULL;
394 }
395
396 static void remote_cb(RemoteConnection *rc, const gchar *text, gpointer data)
397 {
398 RemoteCommandEntry *entry;
399 const gchar *offset;
400
401 entry = remote_command_find(text, &offset);
402 if (entry && entry->func)
403 {
404 entry->func(offset, data);
405 }
406 else
407 {
408 printf("unknown remote command:%s\n", text);
409 }
410 }
411
412 static void remote_help(void)
413 {
414 gint i;
415
416 print_term(_("Remote command list:\n"));
417
418 i = 0;
419 while (remote_commands[i].func != NULL)
420 {
421 if (remote_commands[i].description)
422 {
423 printf_term(" %-3s%s %-20s %s\n",
424 (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
425 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
426 (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
427 _(remote_commands[i].description));
428 }
429 i++;
430 }
431 }
432
433 static GList *remote_build_list(GList *list, int argc, char *argv[])
434 {
435 gint i;
436
437 i = 1;
438 while (i < argc)
439 {
440 RemoteCommandEntry *entry;
441
442 entry = remote_command_find(argv[i], NULL);
443 if (entry)
444 {
445 list = g_list_append(list, argv[i]);
446 }
447 i++;
448 }
449
450 return list;
451 }
452
453 static void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
454 GList *cmd_list, GList *collection_list)
455 {
456 RemoteConnection *rc;
457 gint started = FALSE;
458 gchar *buf;
459
460 buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL);
461 rc = remote_client_open(buf);
462 if (!rc)
463 {
464 GString *command;
465 GList *work;
466 gint retry_count = 12;
467 gint blank = FALSE;
468
469 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
470
471 command = g_string_new(arg_exec);
472
473 work = remote_list;
474 while (work)
475 {
476 gchar *text;
477 RemoteCommandEntry *entry;
478
479 text = work->data;
480 work = work->next;
481
482 entry = remote_command_find(text, NULL);
483 if (entry)
484 {
485 if (entry->prefer_command_line)
486 {
487 remote_list = g_list_remove(remote_list, text);
488 g_string_append(command, " ");
489 g_string_append(command, text);
490 }
491 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
492 {
493 blank = TRUE;
494 }
495 }
496 }
497
498 if (blank || cmd_list || path) g_string_append(command, " --blank");
499 if (get_debug_level()) g_string_append(command, " --debug");
500
501 g_string_append(command, " &");
502 system(command->str);
503 g_string_free(command, TRUE);
504
505 while (!rc && retry_count > 0)
506 {
507 usleep((retry_count > 10) ? 500000 : 1000000);
508 rc = remote_client_open(buf);
509 if (!rc) print_term(".");
510 retry_count--;
511 }
512
513 print_term("\n");
514
515 started = TRUE;
516 }
517 g_free(buf);
518
519 if (rc)
520 {
521 GList *work;
522 const gchar *prefix;
523 gint use_path = TRUE;
524 gint sent = FALSE;
525
526 work = remote_list;
527 while (work)
528 {
529 gchar *text;
530 RemoteCommandEntry *entry;
531
532 text = work->data;
533 work = work->next;
534
535 entry = remote_command_find(text, NULL);
536 if (entry &&
537 entry->opt_l &&
538 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
539
540 remote_client_send(rc, text);
541
542 sent = TRUE;
543 }
544
545 if (cmd_list && cmd_list->next)
546 {
547 prefix = "--list-add:";
548 remote_client_send(rc, "--list-clear");
549 }
550 else
551 {
552 prefix = "file:";
553 }
554
555 work = cmd_list;
556 while (work)
557 {
558 FileData *fd;
559 gchar *text;
560
561 fd = work->data;
562 work = work->next;
563
564 text = g_strconcat(prefix, fd->path, NULL);
565 remote_client_send(rc, text);
566 g_free(text);
567
568 sent = TRUE;
569 }
570
571 if (path && !cmd_list && use_path)
572 {
573 gchar *text;
574
575 text = g_strdup_printf("file:%s", path);
576 remote_client_send(rc, text);
577 g_free(text);
578
579 sent = TRUE;
580 }
581
582 work = collection_list;
583 while (work)
584 {
585 const gchar *name;
586 gchar *text;
587
588 name = work->data;
589 work = work->next;
590
591 text = g_strdup_printf("file:%s", name);
592 remote_client_send(rc, text);
593 g_free(text);
594
595 sent = TRUE;
596 }
597
598 if (!started && !sent)
599 {
600 remote_client_send(rc, "raise");
601 }
602 }
603 else
604 {
605 print_term(_("Remote not available\n"));
606 }
607
608 _exit(0);
609 }
610 130
611 /* 131 /*
612 *----------------------------------------------------------------------------- 132 *-----------------------------------------------------------------------------
613 * command line parser (private) hehe, who needs popt anyway? 133 * command line parser (private) hehe, who needs popt anyway?
614 *----------------------------------------------------------------------------- 134 *-----------------------------------------------------------------------------
1124 GList *collection_list = NULL; 644 GList *collection_list = NULL;
1125 CollectionData *first_collection = NULL; 645 CollectionData *first_collection = NULL;
1126 gchar *geometry = NULL; 646 gchar *geometry = NULL;
1127 gchar *buf; 647 gchar *buf;
1128 gchar *bufl; 648 gchar *bufl;
649 CollectionData *cd = NULL;
1129 650
1130 /* init execution time counter (debug only) */ 651 /* init execution time counter (debug only) */
1131 init_exec_time(); 652 init_exec_time();
1132 653
1133 /* setup locale, i18n */ 654 /* setup locale, i18n */
1225 } 746 }
1226 747
1227 if (cmd_list || 748 if (cmd_list ||
1228 (startup_command_line_collection && collection_list)) 749 (startup_command_line_collection && collection_list))
1229 { 750 {
1230 CollectionData *cd;
1231 GList *work; 751 GList *work;
1232 752
1233 if (startup_command_line_collection) 753 if (startup_command_line_collection)
1234 { 754 {
1235 CollectWindow *cw; 755 CollectWindow *cw;
1238 cd = cw->cd; 758 cd = cw->cd;
1239 } 759 }
1240 else 760 else
1241 { 761 {
1242 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */ 762 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
1243 command_collection = cd;
1244 } 763 }
1245 764
1246 g_free(cd->path); 765 g_free(cd->path);
1247 cd->path = NULL; 766 cd->path = NULL;
1248 g_free(cd->name); 767 g_free(cd->name);
1301 820
1302 if (startup_full_screen) layout_image_full_screen_start(lw); 821 if (startup_full_screen) layout_image_full_screen_start(lw);
1303 if (startup_in_slideshow) layout_image_slideshow_start(lw); 822 if (startup_in_slideshow) layout_image_slideshow_start(lw);
1304 823
1305 buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL); 824 buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL);
1306 remote_connection = remote_server_open(buf); 825 remote_connection = remote_server_init(buf, cd);
1307 remote_server_subscribe(remote_connection, remote_cb, NULL);
1308 g_free(buf); 826 g_free(buf);
1309 827
1310 gtk_main(); 828 gtk_main();
1311 return 0; 829 return 0;
1312 } 830 }