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