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