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