2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
23 #include "fullscreen.h"
30 #include "image-load.h"
32 #include "ui-fileops.h"
39 *----------------------------------------------------------------------------
40 * full screen functions
41 *----------------------------------------------------------------------------
44 #define FULL_SCREEN_HIDE_MOUSE_DELAY 3000
45 #define FULL_SCREEN_BUSY_MOUSE_DELAY 200
48 FULLSCREEN_CURSOR_HIDDEN = 1 << 0,
49 FULLSCREEN_CURSOR_NORMAL = 1 << 1,
50 FULLSCREEN_CURSOR_BUSY = 1 << 2
53 void clear_mouse_cursor(GtkWidget *widget, gint state)
55 GdkWindow *window = gtk_widget_get_window(widget);
60 display = gdk_display_get_default();
62 if (state & FULLSCREEN_CURSOR_BUSY)
66 cursor = gdk_cursor_new_for_display(display, GDK_WATCH);
67 gdk_window_set_cursor(window, cursor);
68 g_object_unref(G_OBJECT(cursor));
70 else if (state & FULLSCREEN_CURSOR_NORMAL)
72 gdk_window_set_cursor(window, nullptr);
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));
84 gboolean fullscreen_hide_mouse_cb(gpointer data)
86 auto fs = static_cast<FullScreenData *>(data);
88 if (!fs->hide_mouse_id) return FALSE;
90 fs->cursor_state &= ~FULLSCREEN_CURSOR_NORMAL;
91 if (!(fs->cursor_state & FULLSCREEN_CURSOR_BUSY)) clear_mouse_cursor(fs->window, fs->cursor_state);
93 g_source_remove(fs->hide_mouse_id);
94 fs->hide_mouse_id = 0;
98 void fullscreen_hide_mouse_disable(FullScreenData *fs)
100 if (fs->hide_mouse_id)
102 g_source_remove(fs->hide_mouse_id);
103 fs->hide_mouse_id = 0;
107 void fullscreen_hide_mouse_reset(FullScreenData *fs)
109 fullscreen_hide_mouse_disable(fs);
110 fs->hide_mouse_id = g_timeout_add(FULL_SCREEN_HIDE_MOUSE_DELAY, fullscreen_hide_mouse_cb, fs);
113 gboolean fullscreen_mouse_moved(GtkWidget *, GdkEventMotion *, gpointer data)
115 auto fs = static_cast<FullScreenData *>(data);
117 if (!(fs->cursor_state & FULLSCREEN_CURSOR_NORMAL))
119 fs->cursor_state |= FULLSCREEN_CURSOR_NORMAL;
120 if (!(fs->cursor_state & FULLSCREEN_CURSOR_BUSY)) clear_mouse_cursor(fs->window, fs->cursor_state);
122 fullscreen_hide_mouse_reset(fs);
127 void fullscreen_busy_mouse_disable(FullScreenData *fs)
129 if (fs->busy_mouse_id)
131 g_source_remove(fs->busy_mouse_id);
132 fs->busy_mouse_id = 0;
136 void fullscreen_mouse_set_busy(FullScreenData *fs, gboolean busy)
138 fullscreen_busy_mouse_disable(fs);
140 if (!!(fs->cursor_state & FULLSCREEN_CURSOR_BUSY) == (busy)) return;
144 fs->cursor_state |= FULLSCREEN_CURSOR_BUSY;
148 fs->cursor_state &= ~FULLSCREEN_CURSOR_BUSY;
151 clear_mouse_cursor(fs->window, fs->cursor_state);
154 gboolean fullscreen_mouse_set_busy_cb(gpointer data)
156 auto fs = static_cast<FullScreenData *>(data);
158 fs->busy_mouse_id = 0;
159 fullscreen_mouse_set_busy(fs, TRUE);
163 void fullscreen_mouse_set_busy_idle(FullScreenData *fs)
165 if (!fs->busy_mouse_id)
167 fs->busy_mouse_id = g_timeout_add(FULL_SCREEN_BUSY_MOUSE_DELAY,
168 fullscreen_mouse_set_busy_cb, fs);
172 void fullscreen_image_update_cb(ImageWindow *, gpointer data)
174 auto fs = static_cast<FullScreenData *>(data);
177 image_loader_get_pixbuf(fs->imd->il) != image_get_pixbuf(fs->imd))
179 fullscreen_mouse_set_busy_idle(fs);
183 void fullscreen_image_complete_cb(ImageWindow *, gboolean preload, gpointer data)
185 auto fs = static_cast<FullScreenData *>(data);
187 if (!preload) fullscreen_mouse_set_busy(fs, FALSE);
190 #define XSCREENSAVER_BINARY "xscreensaver-command"
191 #define XSCREENSAVER_COMMAND "xscreensaver-command -deactivate >&- 2>&- &"
193 void fullscreen_saver_deactivate()
195 static gboolean checked = FALSE;
196 static gboolean found = FALSE;
201 found = file_in_path(XSCREENSAVER_BINARY);
206 runcmd(XSCREENSAVER_COMMAND);
210 gboolean fullscreen_saver_block_cb(gpointer)
212 if (options->fullscreen.disable_saver)
214 fullscreen_saver_deactivate();
220 gboolean fullscreen_delete_cb(GtkWidget *, GdkEventAny *, gpointer data)
222 auto fs = static_cast<FullScreenData *>(data);
230 *----------------------------------------------------------------------------
231 * full screen preferences and utils
232 *----------------------------------------------------------------------------
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
245 std::string description;
246 GdkRectangle geometry;
249 GdkRectangle get_screen_default_geometry(GdkScreen *screen)
251 GdkRectangle geometry;
255 geometry.width = gdk_screen_get_width(screen);
256 geometry.height = gdk_screen_get_height(screen);
261 std::vector<ScreenData> fullscreen_prefs_list()
263 std::vector<ScreenData> list;
268 display = gdk_display_get_default();
269 screen = gdk_display_get_default_screen(display);
270 monitors = gdk_display_get_n_monitors(display);
272 for (gint j = -1; j < monitors; j++)
278 name = gdk_screen_make_display_name(screen);
282 rect = get_screen_default_geometry(screen);
283 subname = g_strdup(_("Full size"));
287 auto monitor = gdk_display_get_monitor(display, j);
288 gdk_monitor_get_geometry(monitor, &rect);
289 subname = g_strdup(gdk_monitor_get_model(monitor));
290 if (subname == nullptr)
292 subname = g_strdup_printf("%s %d", _("Monitor"), j + 1);
297 sd.number = 100 + j + 1;
298 sd.description = std::string(_("Screen")) + " " + name + ", " + subname;
301 DEBUG_1("Screen %d %30s %4d,%4d (%4dx%4d)",
302 sd.number, sd.description.c_str(), sd.geometry.x, sd.geometry.y, sd.geometry.width, sd.geometry.height);
313 /* screen_num is interpreted as such:
314 * -1 window manager determines size and position, fallback is (1) active monitor
315 * 0 full size of screen containing widget
316 * 1 size of monitor containing widget
317 * 100 full size of screen 1 (screen, monitor counts start at 1)
318 * 101 size of monitor 1 on screen 1
319 * 203 size of monitor 3 on screen 2
321 * dest_screen: screen to place widget [use gtk_window_set_screen()]
322 * same_region: the returned region will overlap the current location of widget.
324 GdkRectangle fullscreen_prefs_get_geometry(gint screen_num, GtkWidget *widget, GdkScreen *&dest_screen, gboolean &same_region)
326 if (screen_num >= 100)
328 std::vector<ScreenData> list = fullscreen_prefs_list();
329 auto it = std::find_if(list.cbegin(), list.cend(), [screen_num](const ScreenData &sd){ return sd.number == screen_num; });
330 if (it != list.cend())
332 GdkDisplay *display = gdk_display_get_default();
334 dest_screen = gdk_display_get_default_screen(display);
335 same_region = (!widget || !gtk_widget_get_window(widget) ||
336 (dest_screen == gtk_widget_get_screen(widget) &&
337 (it->number%100 == 0 ||
338 it->number%100 == gdk_screen_get_monitor_at_window(dest_screen, gtk_widget_get_window(widget)) + 1)));
342 else if (screen_num < 0) screen_num = 1;
344 GdkRectangle geometry;
346 if (screen_num != 1 || !widget || !gtk_widget_get_window(widget))
350 dest_screen = gtk_widget_get_screen(widget);
354 dest_screen = gdk_screen_get_default();
357 geometry = get_screen_default_geometry(dest_screen);
364 display = gtk_widget_get_display(widget);
365 monitor = gdk_display_get_monitor_at_window(display, gtk_widget_get_window(widget));
367 gdk_monitor_get_geometry(monitor, &geometry);
369 dest_screen = gtk_widget_get_screen(widget);
376 #pragma GCC diagnostic push
377 #pragma GCC diagnostic ignored "-Wunused-function"
378 gint fullscreen_prefs_find_screen_for_widget_unused(GtkWidget *widget)
384 if (!widget || !gtk_widget_get_window(widget)) return 0;
386 screen = gtk_widget_get_screen(widget);
387 monitor = gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(widget));
389 n = 100 + monitor + 1;
391 DEBUG_1("Screen appears to be %d", n);
395 #pragma GCC diagnostic pop
398 FS_MENU_COLUMN_NAME = 0,
402 #define BUTTON_ABOVE_KEY "button_above"
404 void fullscreen_prefs_selection_cb(GtkWidget *combo, gpointer data)
406 auto value = static_cast<gint *>(data);
413 store = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
414 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) return;
415 gtk_tree_model_get(store, &iter, FS_MENU_COLUMN_VALUE, value, -1);
417 button = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(combo), BUTTON_ABOVE_KEY));
420 gtk_widget_set_sensitive(button, *value != -1);
424 void fullscreen_prefs_selection_add(GtkListStore *store, const gchar *text, gint value)
428 gtk_list_store_append(store, &iter);
429 gtk_list_store_set(store, &iter, FS_MENU_COLUMN_NAME, text,
430 FS_MENU_COLUMN_VALUE, value, -1);
437 *----------------------------------------------------------------------------
438 * full screen functions
439 *----------------------------------------------------------------------------
442 FullScreenData *fullscreen_start(GtkWidget *window, ImageWindow *imd,
443 FullScreenData::StopFunc stop_func, gpointer stop_data)
447 GdkGeometry geometry;
449 if (!window || !imd) return nullptr;
451 fs = g_new0(FullScreenData, 1);
453 fs->cursor_state = FULLSCREEN_CURSOR_HIDDEN;
455 fs->normal_window = window;
456 fs->normal_imd = imd;
458 fs->stop_func = stop_func;
459 fs->stop_data = stop_data;
461 DEBUG_1("full screen requests screen %d", options->fullscreen.screen);
462 GdkRectangle rect = fullscreen_prefs_get_geometry(options->fullscreen.screen, window, screen, fs->same_region);
464 fs->window = window_new("fullscreen", nullptr, nullptr, _("Full screen"));
465 DEBUG_NAME(fs->window);
467 g_signal_connect(G_OBJECT(fs->window), "delete_event",
468 G_CALLBACK(fullscreen_delete_cb), fs);
470 /* few cosmetic details */
471 gtk_window_set_decorated(GTK_WINDOW(fs->window), FALSE);
472 gtk_container_set_border_width(GTK_CONTAINER(fs->window), 0);
474 /* keep window above others, if requested */
475 if (options->fullscreen.above)
477 gq_gtk_window_set_keep_above(GTK_WINDOW(fs->window), TRUE);
480 /* set default size and position, so the window appears where it was before */
481 gtk_window_set_default_size(GTK_WINDOW(fs->window), rect.width, rect.height);
482 gq_gtk_window_move(GTK_WINDOW(fs->window), rect.x, rect.y);
484 /* By setting USER_POS and USER_SIZE, most window managers will
485 * not request positioning of the full screen window (for example twm).
487 * In addition, setting gravity to STATIC will result in the
488 * decorations of twm to not effect the requested window position,
489 * the decorations will simply be off screen, except in multi monitor setups :-/
491 geometry.min_width = 1;
492 geometry.min_height = 1;
493 geometry.base_width = rect.width;
494 geometry.base_height = rect.height;
495 geometry.win_gravity = GDK_GRAVITY_STATIC;
496 gtk_window_set_geometry_hints(GTK_WINDOW(fs->window), fs->window, &geometry,
497 static_cast<GdkWindowHints>(GDK_HINT_WIN_GRAVITY | GDK_HINT_USER_POS | GDK_HINT_USER_SIZE));
499 gtk_widget_realize(fs->window);
501 if ((options->fullscreen.screen % 100) == 0)
504 gdkwin = gtk_widget_get_window(fs->window);
505 if (gdkwin != nullptr)
506 gdk_window_set_fullscreen_mode(gdkwin, GDK_FULLSCREEN_ON_ALL_MONITORS);
509 /* make window fullscreen -- let Gtk do it's job, don't screw it in any way */
510 gtk_window_fullscreen(GTK_WINDOW(fs->window));
512 /* move it to requested screen */
513 if (options->fullscreen.screen >= 0)
515 gtk_window_set_screen(GTK_WINDOW(fs->window), screen);
518 fs->imd = image_new(FALSE);
520 gq_gtk_container_add(GTK_WIDGET(fs->window), fs->imd->widget);
522 image_background_set_color_from_options(fs->imd, TRUE);
523 image_set_delay_flip(fs->imd, options->fullscreen.clean_flip);
524 image_auto_refresh_enable(fs->imd, fs->normal_imd->auto_refresh);
526 if (options->fullscreen.clean_flip)
528 image_set_update_func(fs->imd, fullscreen_image_update_cb, fs);
529 image_set_complete_func(fs->imd, fullscreen_image_complete_cb, fs);
532 gtk_widget_show(fs->imd->widget);
536 DEBUG_2("Original window is not visible, enabling std. fullscreen mode");
537 image_move_from_image(fs->imd, fs->normal_imd);
541 DEBUG_2("Original window is still visible, enabling presentation fullscreen mode");
542 image_copy_from_image(fs->imd, fs->normal_imd);
545 if (options->stereo.enable_fsmode)
547 image_stereo_set(fs->imd, options->stereo.fsmode);
550 gtk_widget_show(fs->window);
552 /* for hiding the mouse */
553 g_signal_connect(G_OBJECT(fs->imd->pr), "motion_notify_event",
554 G_CALLBACK(fullscreen_mouse_moved), fs);
555 clear_mouse_cursor(fs->window, fs->cursor_state);
557 /* set timer to block screen saver */
558 fs->saver_block_id = g_timeout_add(60 * 1000, fullscreen_saver_block_cb, fs);
560 /* hide normal window */
561 /** @FIXME properly restore this window on show
565 if (options->hide_window_in_fullscreen)
567 gtk_widget_hide(fs->normal_window);
569 image_change_fd(fs->normal_imd, nullptr, image_zoom_get(fs->normal_imd));
575 void fullscreen_stop(FullScreenData *fs)
579 if (fs->saver_block_id) g_source_remove(fs->saver_block_id);
581 fullscreen_hide_mouse_disable(fs);
582 fullscreen_busy_mouse_disable(fs);
583 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
587 image_move_from_image(fs->normal_imd, fs->imd);
588 if (options->hide_window_in_fullscreen)
590 gtk_widget_show(fs->normal_window);
592 if (options->stereo.enable_fsmode)
594 image_stereo_set(fs->normal_imd, options->stereo.mode);
599 if (fs->stop_func) fs->stop_func(fs, fs->stop_data);
601 gq_gtk_widget_destroy(fs->window);
603 gtk_window_present(GTK_WINDOW(fs->normal_window));
610 *----------------------------------------------------------------------------
611 * full screen preferences and utils
612 *----------------------------------------------------------------------------
615 GtkWidget *fullscreen_prefs_selection_new(const gchar *text, gint *screen_value, gboolean *)
621 GtkCellRenderer *renderer;
625 if (!screen_value) return nullptr;
627 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
629 hbox = pref_box_new(vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
630 if (text) pref_label_new(hbox, text);
632 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
633 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
634 g_object_unref(store);
636 renderer = gtk_cell_renderer_text_new();
637 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
638 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer,
639 "text", FS_MENU_COLUMN_NAME, NULL);
641 fullscreen_prefs_selection_add(store, _("Determined by Window Manager"), -1);
642 fullscreen_prefs_selection_add(store, _("Active screen"), 0);
643 if (*screen_value == 0) current = 1;
644 fullscreen_prefs_selection_add(store, _("Active monitor"), 1);
645 if (*screen_value == 1) current = 2;
648 std::vector<ScreenData> list = fullscreen_prefs_list();
649 for (const ScreenData &sd : list)
651 fullscreen_prefs_selection_add(store, sd.description.c_str(), sd.number);
652 if (*screen_value == sd.number) current = n;
657 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), current);
659 gq_gtk_box_pack_start(GTK_BOX(hbox), combo, FALSE, FALSE, 0);
660 gtk_widget_show(combo);
662 g_signal_connect(G_OBJECT(combo), "changed",
663 G_CALLBACK(fullscreen_prefs_selection_cb), screen_value);
667 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */