Trim trailing white spaces.
[geeqie.git] / src / fullscreen.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
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
14 #include "main.h"
15 #include "fullscreen.h"
16
17 #include "image.h"
18 #include "misc.h"
19 #include "ui_fileops.h"
20 #include "ui_menu.h"
21 #include "ui_misc.h"
22 #include "window.h"
23 #include "image-load.h"
24
25 enum {
26         FULLSCREEN_CURSOR_HIDDEN = 1 << 0,
27         FULLSCREEN_CURSOR_NORMAL = 1 << 1,
28         FULLSCREEN_CURSOR_BUSY   = 1 << 2
29 };
30
31
32 /*
33  *----------------------------------------------------------------------------
34  * full screen functions
35  *----------------------------------------------------------------------------
36  */
37
38 static void clear_mouse_cursor(GtkWidget *widget, gint state)
39 {
40         GdkWindow *window = gtk_widget_get_window(widget);
41         if (!window) return;
42
43         if (state & FULLSCREEN_CURSOR_BUSY)
44                 {
45                 GdkCursor *cursor;
46
47                 cursor = gdk_cursor_new(GDK_WATCH);
48                 gdk_window_set_cursor(window, cursor);
49                 gdk_cursor_unref(cursor);
50                 }
51         else if (state & FULLSCREEN_CURSOR_NORMAL)
52                 {
53                 gdk_window_set_cursor(window, NULL);
54                 }
55         else
56                 {
57                 GdkCursor *cursor;
58                 
59                 cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
60                 gdk_window_set_cursor(window, cursor);
61                 gdk_cursor_unref(cursor);
62                 }
63 }
64
65 static gboolean fullscreen_hide_mouse_cb(gpointer data)
66 {
67         FullScreenData *fs = data;
68
69         if (!fs->hide_mouse_id) return FALSE;
70
71         fs->cursor_state &= ~FULLSCREEN_CURSOR_NORMAL;
72         if (!(fs->cursor_state & FULLSCREEN_CURSOR_BUSY)) clear_mouse_cursor(fs->window, fs->cursor_state);
73
74         g_source_remove(fs->hide_mouse_id);
75         fs->hide_mouse_id = 0;
76         return FALSE;
77 }
78
79 static void fullscreen_hide_mouse_disable(FullScreenData *fs)
80 {
81         if (fs->hide_mouse_id)
82                 {
83                 g_source_remove(fs->hide_mouse_id);
84                 fs->hide_mouse_id = 0;
85                 }
86 }
87
88 static void fullscreen_hide_mouse_reset(FullScreenData *fs)
89 {
90         fullscreen_hide_mouse_disable(fs);
91         fs->hide_mouse_id = g_timeout_add(FULL_SCREEN_HIDE_MOUSE_DELAY, fullscreen_hide_mouse_cb, fs);
92 }
93
94 static gboolean fullscreen_mouse_moved(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
95 {
96         FullScreenData *fs = data;
97
98         if (!(fs->cursor_state & FULLSCREEN_CURSOR_NORMAL))
99                 {
100                 fs->cursor_state |= FULLSCREEN_CURSOR_NORMAL;
101                 if (!(fs->cursor_state & FULLSCREEN_CURSOR_BUSY)) clear_mouse_cursor(fs->window, fs->cursor_state);
102                 }
103         fullscreen_hide_mouse_reset(fs);
104
105         return FALSE;
106 }
107
108 static void fullscreen_busy_mouse_disable(FullScreenData *fs)
109 {
110         if (fs->busy_mouse_id)
111                 {
112                 g_source_remove(fs->busy_mouse_id);
113                 fs->busy_mouse_id = 0;
114                 }
115 }
116
117 static void fullscreen_mouse_set_busy(FullScreenData *fs, gboolean busy)
118 {
119         fullscreen_busy_mouse_disable(fs);
120
121         if (!!(fs->cursor_state & FULLSCREEN_CURSOR_BUSY) == (busy)) return;
122
123         if (busy)
124                 {
125                 fs->cursor_state |= FULLSCREEN_CURSOR_BUSY;
126                 }
127         else
128                 {
129                 fs->cursor_state &= ~FULLSCREEN_CURSOR_BUSY;
130                 }
131
132         clear_mouse_cursor(fs->window, fs->cursor_state);
133 }
134
135 static gboolean fullscreen_mouse_set_busy_cb(gpointer data)
136 {
137         FullScreenData *fs = data;
138
139         fs->busy_mouse_id = 0;
140         fullscreen_mouse_set_busy(fs, TRUE);
141         return FALSE;
142 }
143
144 static void fullscreen_mouse_set_busy_idle(FullScreenData *fs)
145 {
146         if (!fs->busy_mouse_id)
147                 {
148                 fs->busy_mouse_id = g_timeout_add(FULL_SCREEN_BUSY_MOUSE_DELAY,
149                                                   fullscreen_mouse_set_busy_cb, fs);
150                 }
151 }
152
153 static void fullscreen_image_update_cb(ImageWindow *imd, gpointer data)
154 {
155         FullScreenData *fs = data;
156
157         if (fs->imd->il &&
158             image_loader_get_pixbuf(fs->imd->il) != image_get_pixbuf(fs->imd))
159                 {
160                 fullscreen_mouse_set_busy_idle(fs);
161                 }
162 }
163
164 static void fullscreen_image_complete_cb(ImageWindow *imd, gboolean preload, gpointer data)
165 {
166         FullScreenData *fs = data;
167
168         if (!preload) fullscreen_mouse_set_busy(fs, FALSE);
169 }
170
171 #define XSCREENSAVER_BINARY     "xscreensaver-command"
172 #define XSCREENSAVER_COMMAND    "xscreensaver-command -deactivate >&- 2>&- &"
173
174 static void fullscreen_saver_deactivate(void)
175 {
176         static gboolean checked = FALSE;
177         static gboolean found = FALSE;
178
179         if (!checked)
180                 {
181                 checked = TRUE;
182                 found = file_in_path(XSCREENSAVER_BINARY);
183                 }
184
185         if (found)
186                 {
187                 runcmd(XSCREENSAVER_COMMAND);
188                 }
189 }
190
191 static gboolean fullscreen_saver_block_cb(gpointer data)
192 {
193         if (options->fullscreen.disable_saver)
194                 {
195                 fullscreen_saver_deactivate();
196                 }
197
198         return TRUE;
199 }
200
201 static gboolean fullscreen_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
202 {
203         FullScreenData *fs = data;
204
205         fullscreen_stop(fs);
206         return TRUE;
207 }
208
209 FullScreenData *fullscreen_start(GtkWidget *window, ImageWindow *imd,
210                                  void (*stop_func)(FullScreenData *, gpointer), gpointer stop_data)
211 {
212         FullScreenData *fs;
213         GdkScreen *screen;
214         gint x, y;
215         gint w, h;
216         GdkGeometry geometry;
217         GdkWindow *gdkwin;
218
219         if (!window || !imd) return NULL;
220
221         fs = g_new0(FullScreenData, 1);
222
223         fs->cursor_state = FULLSCREEN_CURSOR_HIDDEN;
224
225         fs->normal_window = window;
226         fs->normal_imd = imd;
227
228         fs->stop_func = stop_func;
229         fs->stop_data = stop_data;
230
231         DEBUG_1("full screen requests screen %d", options->fullscreen.screen);
232         fullscreen_prefs_get_geometry(options->fullscreen.screen, window, &x, &y, &w, &h,
233                                       &screen, &fs->same_region);
234
235         fs->window = window_new(GTK_WINDOW_TOPLEVEL, "fullscreen", NULL, NULL, _("Full screen"));
236
237         /* this requests no decorations, if you still have them complain to the window manager author(s) */
238         gtk_window_set_decorated(GTK_WINDOW(fs->window), FALSE);
239
240         if (options->fullscreen.screen < 0)
241                 {
242                 /* If we want control of the window size and position this is not what we want.
243                  * Geeqie needs control of which monitor(s) to use for full screen.
244                  */
245                 gtk_window_fullscreen(GTK_WINDOW(fs->window));
246                 }
247         else
248                 {
249                 gtk_window_set_screen(GTK_WINDOW(fs->window), screen);
250                 if (options->fullscreen.above)
251                         gtk_window_set_keep_above(GTK_WINDOW(fs->window), TRUE);
252                 }
253
254         gtk_window_set_resizable(GTK_WINDOW(fs->window), FALSE);
255         gtk_container_set_border_width(GTK_CONTAINER(fs->window), 0);
256         g_signal_connect(G_OBJECT(fs->window), "delete_event",
257                          G_CALLBACK(fullscreen_delete_cb), fs);
258
259         geometry.min_width = w;
260         geometry.min_height = h;
261         geometry.max_width = w;
262         geometry.max_height = h;
263         geometry.base_width = w;
264         geometry.base_height = h;
265         geometry.win_gravity = GDK_GRAVITY_STATIC;
266         /* By setting USER_POS and USER_SIZE, most window managers will
267          * not request positioning of the full screen window (for example twm).
268          *
269          * In addition, setting gravity to STATIC will result in the
270          * decorations of twm to not effect the requested window position,
271          * the decorations will simply be off screen, except in multi monitor setups :-/
272          */
273         gtk_window_set_geometry_hints(GTK_WINDOW(fs->window), fs->window, &geometry,
274                                       GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE | GDK_HINT_BASE_SIZE |
275                                       GDK_HINT_WIN_GRAVITY |
276                                       GDK_HINT_USER_POS);
277
278         gtk_window_set_default_size(GTK_WINDOW(fs->window), w, h);
279         gtk_window_move(GTK_WINDOW(fs->window), x, y);
280
281         gtk_widget_realize(fs->window);
282         gdkwin = gtk_widget_get_window(fs->window);
283         if (gdkwin != NULL)
284                 gdk_window_set_override_redirect(gdkwin, TRUE);
285
286         fs->imd = image_new(FALSE);
287
288         gtk_container_add(GTK_CONTAINER(fs->window), fs->imd->widget);
289
290         image_background_set_color_from_options(fs->imd, TRUE);
291         image_set_delay_flip(fs->imd, options->fullscreen.clean_flip);
292         image_auto_refresh_enable(fs->imd, fs->normal_imd->auto_refresh);
293         
294         if (options->fullscreen.clean_flip)
295                 {
296                 image_set_update_func(fs->imd, fullscreen_image_update_cb, fs);
297                 image_set_complete_func(fs->imd, fullscreen_image_complete_cb, fs);
298                 }
299
300         gtk_widget_show(fs->imd->widget);
301
302         if (fs->same_region)
303                 {
304                 DEBUG_0("Original vindow is not visible, enabling std. fullscreen mode");
305                 image_move_from_image(fs->imd, fs->normal_imd);
306                 }
307         else
308                 {
309                 DEBUG_0("Original vindow is still visible, enabling presentation fullscreen mode");
310                 image_copy_from_image(fs->imd, fs->normal_imd);
311                 }
312
313         if (options->stereo.enable_fsmode) {
314                 image_stereo_set(fs->imd, options->stereo.fsmode);
315         }
316
317         gtk_widget_show(fs->window);
318
319         /* for hiding the mouse */
320         g_signal_connect(G_OBJECT(fs->imd->pr), "motion_notify_event",
321                            G_CALLBACK(fullscreen_mouse_moved), fs);
322         clear_mouse_cursor(fs->window, fs->cursor_state);
323
324         /* set timer to block screen saver */
325         fs->saver_block_id = g_timeout_add(60 * 1000, fullscreen_saver_block_cb, fs);
326
327         /* hide normal window
328          * FIXME: properly restore this window on show
329          */
330         if (fs->same_region)
331                 {
332 #ifdef HIDE_WINDOW_IN_FULLSCREEN
333                 gtk_widget_hide(fs->normal_window);
334 #endif
335                 image_change_fd(fs->normal_imd, NULL, image_zoom_get(fs->normal_imd));
336                 }
337
338         return fs;
339 }
340
341 void fullscreen_stop(FullScreenData *fs)
342 {
343         if (!fs) return;
344
345         if (fs->saver_block_id) g_source_remove(fs->saver_block_id);
346
347         fullscreen_hide_mouse_disable(fs);
348         fullscreen_busy_mouse_disable(fs);
349         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
350
351         if (fs->same_region)
352                 {
353                 image_move_from_image(fs->normal_imd, fs->imd);
354 #ifdef HIDE_WINDOW_IN_FULLSCREEN
355                 gtk_widget_show(fs->normal_window);
356 #endif
357                 if (options->stereo.enable_fsmode)
358                         {
359                         image_stereo_set(fs->normal_imd, options->stereo.mode);
360                         }
361                 }
362
363
364         if (fs->stop_func) fs->stop_func(fs, fs->stop_data);
365
366         gtk_widget_destroy(fs->window);
367
368         g_free(fs);
369 }
370
371
372 /*
373  *----------------------------------------------------------------------------
374  * full screen preferences and utils
375  *----------------------------------------------------------------------------
376  */
377
378 GList *fullscreen_prefs_list(void)
379 {
380         GList *list = NULL;
381         GdkDisplay *display;
382         gint number;
383         gint i;
384
385         display = gdk_display_get_default();
386         number = gdk_display_get_n_screens(display);
387
388         for (i = 0; i < number; i++)
389                 {
390                 GdkScreen *screen;
391                 gint monitors;
392                 gint j;
393
394                 screen = gdk_display_get_screen(display, i);
395                 monitors = gdk_screen_get_n_monitors(screen);
396
397                 for (j = -1; j < monitors; j++)
398                         {
399                         ScreenData *sd;
400                         GdkRectangle rect;
401                         gchar *name;
402                         gchar *subname;
403
404                         name = gdk_screen_make_display_name(screen);
405
406                         if (j < 0)
407                                 {
408                                 rect.x = 0;
409                                 rect.y = 0;
410                                 rect.width = gdk_screen_get_width(screen);
411                                 rect.height = gdk_screen_get_height(screen);
412                                 subname = g_strdup(_("Full size"));
413                                 }
414                         else
415                                 {
416                                 gdk_screen_get_monitor_geometry(screen, j, &rect);
417                                 subname = gdk_screen_get_monitor_plug_name(screen, j);
418                                 if (subname == NULL)
419                                         {
420                                         subname = g_strdup_printf("%s %d", _("Monitor"), j + 1);
421                                         }
422                                 }
423
424                         sd = g_new0(ScreenData, 1);
425                         sd->number = (i+1) * 100 + j + 1;
426                         sd->description = g_strdup_printf("%s %s, %s", _("Screen"), name, subname);
427                         sd->x = rect.x;
428                         sd->y = rect.y;
429                         sd->width = rect.width;
430                         sd->height = rect.height;
431
432                         DEBUG_1("Screen %d %30s %4d,%4d (%4dx%4d)",
433                                           sd->number, sd->description, sd->x, sd->y, sd->width, sd->height);
434
435                         list = g_list_append(list, sd);
436
437                         g_free(name);
438                         g_free(subname);
439                         }
440                 }
441
442         return list;
443 }
444
445 void fullscreen_prefs_list_free(GList *list)
446 {
447         GList *work;
448
449         work = list;
450         while (work)
451                 {
452                 ScreenData *sd = work->data;
453                 work = work->next;
454
455                 g_free(sd->description);
456                 g_free(sd);
457                 }
458
459         g_list_free(list);
460 }
461
462 ScreenData *fullscreen_prefs_list_find(GList *list, gint screen)
463 {
464         GList *work;
465
466         work = list;
467         while (work)
468                 {
469                 ScreenData *sd = work->data;
470                 work = work->next;
471
472                 if (sd->number == screen) return sd;
473                 }
474
475         return NULL;
476 }
477
478 /* screen is interpreted as such:
479  *  -1  window manager determines size and position, fallback is (1) active monitor
480  *   0  full size of screen containing widget
481  *   1  size of monitor containing widget
482  * 100  full size of screen 1 (screen, monitor counts start at 1)
483  * 101  size of monitor 1 on screen 1
484  * 203  size of monitor 3 on screen 2
485  * returns:
486  * dest_screen: screen to place widget [use gtk_window_set_screen()]
487  * same_region: the returned region will overlap the current location of widget.
488  */
489 void fullscreen_prefs_get_geometry(gint screen, GtkWidget *widget, gint *x, gint *y, gint *width, gint *height,
490                                    GdkScreen **dest_screen, gboolean *same_region)
491 {
492         GList *list;
493         ScreenData *sd;
494
495         list = fullscreen_prefs_list();
496         if (screen >= 100)
497                 {
498                 sd = fullscreen_prefs_list_find(list, screen);
499                 }
500         else
501                 {
502                 sd = NULL;
503                 if (screen < 0) screen = 1;
504                 }
505
506         if (sd)
507                 {
508                 GdkDisplay *display;
509                 GdkScreen *screen;
510                 gint n;
511
512                 display = gdk_display_get_default();
513                 n = sd->number / 100 - 1;
514                 if (n >= 0 && n < gdk_display_get_n_screens(display))
515                         {
516                         screen = gdk_display_get_screen(display, n);
517                         }
518                 else
519                         {
520                         screen = gdk_display_get_default_screen(display);
521                         }
522
523                 if (x) *x = sd->x;
524                 if (y) *y = sd->y;
525                 if (width) *width = sd->width;
526                 if (height) *height = sd->height;
527
528                 if (dest_screen) *dest_screen = screen;
529                 if (same_region) *same_region = (!widget || !gtk_widget_get_window(widget) ||
530                                         (screen == gtk_widget_get_screen(widget) &&
531                                         (sd->number%100 == 0 ||
532                                          sd->number%100 == gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(widget))+1)));
533
534                 }
535         else if (screen != 1 || !widget || !gtk_widget_get_window(widget))
536                 {
537                 GdkScreen *screen;
538
539                 if (widget)
540                         {
541                         screen = gtk_widget_get_screen(widget);
542                         }
543                 else
544                         {
545                         screen = gdk_screen_get_default();
546                         }
547
548                 if (x) *x = 0;
549                 if (y) *y = 0;
550                 if (width) *width = gdk_screen_get_width(screen);
551                 if (height) *height = gdk_screen_get_height(screen);
552
553                 if (dest_screen) *dest_screen = screen;
554                 if (same_region) *same_region = TRUE;
555                 }
556         else
557                 {
558                 GdkScreen *screen;
559                 gint monitor;
560                 GdkRectangle rect;
561
562                 screen = gtk_widget_get_screen(widget);
563                 monitor = gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(widget));
564
565                 gdk_screen_get_monitor_geometry(screen, monitor, &rect);
566
567                 if (x) *x = rect.x;
568                 if (y) *y = rect.y;
569                 if (width) *width = rect.width;
570                 if (height) *height = rect.height;
571
572                 if (dest_screen) *dest_screen = screen;
573                 if (same_region) *same_region = TRUE;
574                 }
575
576         fullscreen_prefs_list_free(list);
577 }
578
579 gint fullscreen_prefs_find_screen_for_widget(GtkWidget *widget)
580 {
581         GdkScreen *screen;
582         gint monitor;
583         gint n;
584
585         if (!widget || !gtk_widget_get_window(widget)) return 0;
586
587         screen = gtk_widget_get_screen(widget);
588         monitor = gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(widget));
589
590         n = (gdk_screen_get_number(screen)+1) * 100 + monitor + 1;
591
592         DEBUG_1("Screen appears to be %d", n);
593
594         return n;
595 }
596
597 enum {
598         FS_MENU_COLUMN_NAME = 0,
599         FS_MENU_COLUMN_VALUE
600 };
601
602 #define BUTTON_ABOVE_KEY  "button_above"
603
604 static void fullscreen_prefs_selection_cb(GtkWidget *combo, gpointer data)
605 {
606         gint *value = data;
607         GtkTreeModel *store;
608         GtkTreeIter iter;
609         GtkWidget *button;
610
611         if (!value) return;
612
613         store = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
614         if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) return;
615         gtk_tree_model_get(store, &iter, FS_MENU_COLUMN_VALUE, value, -1);
616
617         button = g_object_get_data(G_OBJECT(combo), BUTTON_ABOVE_KEY);
618         if (button)
619                 {
620                 gtk_widget_set_sensitive(button, *value != -1);
621                 }
622 }
623
624 static void fullscreen_prefs_selection_add(GtkListStore *store, const gchar *text, gint value)
625 {
626         GtkTreeIter iter;
627
628         gtk_list_store_append(store, &iter);
629         gtk_list_store_set(store, &iter, FS_MENU_COLUMN_NAME, text,
630                                          FS_MENU_COLUMN_VALUE, value, -1);
631 }
632
633 GtkWidget *fullscreen_prefs_selection_new(const gchar *text, gint *screen_value, gboolean *above_value)
634 {
635         GtkWidget *vbox;
636         GtkWidget *hbox;
637         GtkWidget *combo;
638         GtkListStore *store;
639         GtkCellRenderer *renderer;
640         GtkWidget *button = NULL;
641         GList *list;
642         GList *work;
643         gint current = 0;
644         gint n;
645
646         if (!screen_value) return NULL;
647
648         vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
649         hbox = pref_box_new(vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
650         if (text) pref_label_new(hbox, text);
651
652         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
653         combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
654         g_object_unref(store);
655
656         renderer = gtk_cell_renderer_text_new();
657         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
658         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer,
659                                        "text", FS_MENU_COLUMN_NAME, NULL);
660
661         if (above_value)
662                 {
663                 button = pref_checkbox_new_int(vbox, _("Stay above other windows"),
664                                                *above_value, above_value);
665                 gtk_widget_set_sensitive(button, *screen_value != -1);
666
667                 g_object_set_data(G_OBJECT(combo), BUTTON_ABOVE_KEY, button);
668                 }
669
670         fullscreen_prefs_selection_add(store, _("Determined by Window Manager"), -1);
671         fullscreen_prefs_selection_add(store, _("Active screen"), 0);
672         if (*screen_value == 0) current = 1;
673         fullscreen_prefs_selection_add(store, _("Active monitor"), 1);
674         if (*screen_value == 1) current = 2;
675
676         n = 3;
677         list = fullscreen_prefs_list();
678         work = list;
679         while (work)
680                 {
681                 ScreenData *sd = work->data;
682
683                 fullscreen_prefs_selection_add(store, sd->description, sd->number);
684                 if (*screen_value == sd->number) current = n;
685
686                 work = work->next;
687                 n++;
688                 }
689         fullscreen_prefs_list_free(list);
690
691         gtk_combo_box_set_active(GTK_COMBO_BOX(combo), current);
692
693         gtk_box_pack_start(GTK_BOX(hbox), combo, FALSE, FALSE, 0);
694         gtk_widget_show(combo);
695
696         g_signal_connect(G_OBJECT(combo), "changed",
697                          G_CALLBACK(fullscreen_prefs_selection_cb), screen_value);
698
699         return vbox;
700 }
701 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */