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