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