Merge remote-tracking branch 'origin/toer'
[geeqie.git] / src / fullscreen.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2010 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "fullscreen.h"
16
17 #include "image.h"
18 #include "misc.h"
19 #include "ui_fileops.h"
20 #include "ui_menu.h"
21 #include "ui_misc.h"
22 #include "window.h"
23 #include "image-load.h"
24
25 enum {
26         FULLSCREEN_CURSOR_HIDDEN = 1 << 0,
27         FULLSCREEN_CURSOR_NORMAL = 1 << 1,
28         FULLSCREEN_CURSOR_BUSY   = 1 << 2
29 };
30
31
32 /*
33  *----------------------------------------------------------------------------
34  * full screen functions
35  *----------------------------------------------------------------------------
36  */
37
38 static void clear_mouse_cursor(GtkWidget *widget, gint state)
39 {
40         if (!widget->window) return;
41
42         if (state & FULLSCREEN_CURSOR_BUSY)
43                 {
44                 GdkCursor *cursor;
45
46                 cursor = gdk_cursor_new(GDK_WATCH);
47                 gdk_window_set_cursor(widget->window, cursor);
48                 gdk_cursor_unref(cursor);
49                 }
50         else if (state & FULLSCREEN_CURSOR_NORMAL)
51                 {
52                 gdk_window_set_cursor(widget->window, NULL);
53                 }
54         else
55                 {
56                 GdkCursor *cursor;
57                 GdkPixmap *p;
58
59                 p = gdk_bitmap_create_from_data(widget->window, "\0\0\0", 1, 1);
60
61                 cursor = gdk_cursor_new_from_pixmap(p, p,
62                                                     &widget->style->fg[GTK_STATE_ACTIVE],
63                                                     &widget->style->bg[GTK_STATE_ACTIVE],
64                                                     0, 0);
65
66                 gdk_window_set_cursor(widget->window, cursor);
67
68                 gdk_cursor_unref(cursor);
69                 g_object_unref(p);
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         gboolean same;
223         gint x, y;
224         gint w, h;
225         GdkGeometry geometry;
226
227         if (!window || !imd) return NULL;
228
229         fs = g_new0(FullScreenData, 1);
230
231         fs->cursor_state = FULLSCREEN_CURSOR_HIDDEN;
232
233         fs->normal_window = window;
234         fs->normal_imd = imd;
235
236         fs->stop_func = stop_func;
237         fs->stop_data = stop_data;
238
239         DEBUG_1("full screen requests screen %d", options->fullscreen.screen);
240         fullscreen_prefs_get_geometry(options->fullscreen.screen, window, &x, &y, &w, &h,
241                                       &screen, &same);
242
243         fs->window = window_new(GTK_WINDOW_TOPLEVEL, "fullscreen", NULL, NULL, _("Full screen"));
244
245         /* this requests no decorations, if you still have them complain to the window manager author(s) */
246         gtk_window_set_decorated(GTK_WINDOW(fs->window), FALSE);
247
248         if (options->fullscreen.screen < 0)
249                 {
250                 /* If we want control of the window size and position this is not what we want.
251                  * Geeqie needs control of which monitor(s) to use for full screen.
252                  */
253                 gtk_window_fullscreen(GTK_WINDOW(fs->window));
254                 }
255         else if (options->fullscreen.above)
256                 {
257                 /* request to be above other windows */
258                 gtk_window_set_keep_above(GTK_WINDOW(fs->window), TRUE);
259                 }
260
261         gtk_window_set_resizable(GTK_WINDOW(fs->window), FALSE);
262
263         gtk_window_set_screen(GTK_WINDOW(fs->window), screen);
264         gtk_container_set_border_width(GTK_CONTAINER(fs->window), 0);
265         g_signal_connect(G_OBJECT(fs->window), "delete_event",
266                          G_CALLBACK(fullscreen_delete_cb), fs);
267
268         geometry.min_width = w;
269         geometry.min_height = h;
270         geometry.max_width = w;
271         geometry.max_height = h;
272         geometry.base_width = w;
273         geometry.base_height = h;
274         geometry.win_gravity = GDK_GRAVITY_STATIC;
275         /* By setting USER_POS and USER_SIZE, most window managers will
276          * not request positioning of the full screen window (for example twm).
277          *
278          * In addition, setting gravity to STATIC will result in the
279          * decorations of twm to not effect the requested window position,
280          * the decorations will simply be off screen, except in multi monitor setups :-/
281          */
282         gtk_window_set_geometry_hints(GTK_WINDOW(fs->window), fs->window, &geometry,
283                                       GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE | GDK_HINT_BASE_SIZE |
284                                       GDK_HINT_WIN_GRAVITY |
285                                       GDK_HINT_USER_POS);
286
287         gtk_window_set_default_size(GTK_WINDOW(fs->window), w, h);
288         gtk_window_move(GTK_WINDOW(fs->window), x, y);
289
290         fs->imd = image_new(FALSE);
291
292         gtk_container_add(GTK_CONTAINER(fs->window), fs->imd->widget);
293
294         image_background_set_color_from_options(fs->imd, TRUE);
295         image_set_delay_flip(fs->imd, options->fullscreen.clean_flip);
296         image_auto_refresh_enable(fs->imd, fs->normal_imd->auto_refresh);
297         
298         if (options->fullscreen.clean_flip)
299                 {
300                 image_set_update_func(fs->imd, fullscreen_image_update_cb, fs);
301                 image_set_complete_func(fs->imd, fullscreen_image_complete_cb, fs);
302                 }
303
304         gtk_widget_show(fs->imd->widget);
305
306         image_change_from_image(fs->imd, fs->normal_imd);
307
308         if (options->stereo.enable_fsmode) {
309                 image_stereo_set(fs->imd, options->stereo.fsmode);
310         }
311
312         gtk_widget_show(fs->window);
313
314         /* for hiding the mouse */
315         g_signal_connect(G_OBJECT(fs->imd->pr), "motion_notify_event",
316                            G_CALLBACK(fullscreen_mouse_moved), fs);
317         clear_mouse_cursor(fs->window, fs->cursor_state);
318
319         /* set timer to block screen saver */
320         fs->saver_block_id = g_timeout_add(60 * 1000, fullscreen_saver_block_cb, fs);
321
322         /* hide normal window
323          * FIXME: properly restore this window on show
324          */
325 #ifdef HIDE_WINDOW_IN_FULLSCREEN
326         gtk_widget_hide(fs->normal_window);
327 #endif
328         image_change_fd(fs->normal_imd, NULL, image_zoom_get(fs->normal_imd));
329
330         return fs;
331 }
332
333 void fullscreen_stop(FullScreenData *fs)
334 {
335         if (!fs) return;
336
337         if (fs->saver_block_id) g_source_remove(fs->saver_block_id);
338
339         fullscreen_hide_mouse_disable(fs);
340         fullscreen_busy_mouse_disable(fs);
341         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
342
343         image_change_from_image(fs->normal_imd, fs->imd);
344
345         if (options->stereo.enable_fsmode) {
346                 image_stereo_set(fs->normal_imd, options->stereo.mode);
347         }
348
349 #ifdef HIDE_WINDOW_IN_FULLSCREEN
350         gtk_widget_show(fs->normal_window);
351 #endif
352         if (fs->stop_func) fs->stop_func(fs, fs->stop_data);
353
354         gtk_widget_destroy(fs->window);
355
356         g_free(fs);
357 }
358
359
360 /*
361  *----------------------------------------------------------------------------
362  * full screen preferences and utils
363  *----------------------------------------------------------------------------
364  */
365
366 GList *fullscreen_prefs_list(void)
367 {
368         GList *list = NULL;
369         GdkDisplay *display;
370         gint number;
371         gint i;
372
373         display = gdk_display_get_default();
374         number = gdk_display_get_n_screens(display);
375
376         for (i = 0; i < number; i++)
377                 {
378                 GdkScreen *screen;
379                 gint monitors;
380                 gint j;
381
382                 screen = gdk_display_get_screen(display, i);
383                 monitors = gdk_screen_get_n_monitors(screen);
384
385                 for (j = -1; j < monitors; j++)
386                         {
387                         ScreenData *sd;
388                         GdkRectangle rect;
389                         gchar *name;
390                         gchar *subname;
391
392                         name = gdk_screen_make_display_name(screen);
393
394                         if (j < 0)
395                                 {
396                                 rect.x = 0;
397                                 rect.y = 0;
398                                 rect.width = gdk_screen_get_width(screen);
399                                 rect.height = gdk_screen_get_height(screen);
400                                 subname = g_strdup(_("Full size"));
401                                 }
402                         else
403                                 {
404                                 gdk_screen_get_monitor_geometry(screen, j, &rect);
405                                 subname = g_strdup_printf("%s %d", _("Monitor"), j + 1);
406                                 }
407
408                         sd = g_new0(ScreenData, 1);
409                         sd->number = (i+1) * 100 + j + 1;
410                         sd->description = g_strdup_printf("%s %s, %s", _("Screen"), name, subname);
411                         sd->x = rect.x;
412                         sd->y = rect.y;
413                         sd->width = rect.width;
414                         sd->height = rect.height;
415
416                         DEBUG_1("Screen %d %30s %4d,%4d (%4dx%4d)",
417                                           sd->number, sd->description, sd->x, sd->y, sd->width, sd->height);
418
419                         list = g_list_append(list, sd);
420
421                         g_free(name);
422                         g_free(subname);
423                         }
424                 }
425
426         return list;
427 }
428
429 void fullscreen_prefs_list_free(GList *list)
430 {
431         GList *work;
432
433         work = list;
434         while (work)
435                 {
436                 ScreenData *sd = work->data;
437                 work = work->next;
438
439                 g_free(sd->description);
440                 g_free(sd);
441                 }
442
443         g_list_free(list);
444 }
445
446 ScreenData *fullscreen_prefs_list_find(GList *list, gint screen)
447 {
448         GList *work;
449
450         work = list;
451         while (work)
452                 {
453                 ScreenData *sd = work->data;
454                 work = work->next;
455
456                 if (sd->number == screen) return sd;
457                 }
458
459         return NULL;
460 }
461
462 /* screen is interpreted as such:
463  *  -1  window manager determines size and position, fallback is (1) active monitor
464  *   0  full size of screen containing widget
465  *   1  size of monitor containing widget
466  * 100  full size of screen 1 (screen, monitor counts start at 1)
467  * 101  size of monitor 1 on screen 1
468  * 203  size of monitor 3 on screen 2
469  * returns:
470  * dest_screen: screen to place widget [use gtk_window_set_screen()]
471  * same_region: the returned region will overlap the current location of widget.
472  */
473 void fullscreen_prefs_get_geometry(gint screen, GtkWidget *widget, gint *x, gint *y, gint *width, gint *height,
474                                    GdkScreen **dest_screen, gboolean *same_region)
475 {
476         GList *list;
477         ScreenData *sd;
478
479         list = fullscreen_prefs_list();
480         if (screen >= 100)
481                 {
482                 sd = fullscreen_prefs_list_find(list, screen);
483                 }
484         else
485                 {
486                 sd = NULL;
487                 if (screen < 0) screen = 1;
488                 }
489
490         if (sd)
491                 {
492                 GdkDisplay *display;
493                 GdkScreen *screen;
494                 gint n;
495
496                 display = gdk_display_get_default();
497                 n = sd->number / 100 - 1;
498                 if (n >= 0 && n < gdk_display_get_n_screens(display))
499                         {
500                         screen = gdk_display_get_screen(display, n);
501                         }
502                 else
503                         {
504                         screen = gdk_display_get_default_screen(display);
505                         }
506
507                 if (x) *x = sd->x;
508                 if (y) *y = sd->y;
509                 if (width) *width = sd->width;
510                 if (height) *height = sd->height;
511
512                 if (dest_screen) *dest_screen = screen;
513                 if (same_region) *same_region = (!widget || !widget->window ||
514                                         (screen == gtk_widget_get_screen(widget) &&
515                                         (sd->number%100 == 0 ||
516                                          sd->number%100 == gdk_screen_get_monitor_at_window(screen, widget->window)+1)));
517
518                 }
519         else if (screen != 1 || !widget || !widget->window)
520                 {
521                 GdkScreen *screen;
522
523                 if (widget)
524                         {
525                         screen = gtk_widget_get_screen(widget);
526                         }
527                 else
528                         {
529                         screen = gdk_screen_get_default();
530                         }
531
532                 if (x) *x = 0;
533                 if (y) *y = 0;
534                 if (width) *width = gdk_screen_get_width(screen);
535                 if (height) *height = gdk_screen_get_height(screen);
536
537                 if (dest_screen) *dest_screen = screen;
538                 if (same_region) *same_region = TRUE;
539                 }
540         else
541                 {
542                 GdkScreen *screen;
543                 gint monitor;
544                 GdkRectangle rect;
545
546                 screen = gtk_widget_get_screen(widget);
547                 monitor = gdk_screen_get_monitor_at_window(screen, widget->window);
548
549                 gdk_screen_get_monitor_geometry(screen, monitor, &rect);
550
551                 if (x) *x = rect.x;
552                 if (y) *y = rect.y;
553                 if (width) *width = rect.width;
554                 if (height) *height = rect.height;
555
556                 if (dest_screen) *dest_screen = screen;
557                 if (same_region) *same_region = TRUE;
558                 }
559
560         fullscreen_prefs_list_free(list);
561 }
562
563 gint fullscreen_prefs_find_screen_for_widget(GtkWidget *widget)
564 {
565         GdkScreen *screen;
566         gint monitor;
567         gint n;
568
569         if (!widget || !widget->window) return 0;
570
571         screen = gtk_widget_get_screen(widget);
572         monitor = gdk_screen_get_monitor_at_window(screen, widget->window);
573
574         n = (gdk_screen_get_number(screen)+1) * 100 + monitor + 1;
575
576         DEBUG_1("Screen appears to be %d", n);
577
578         return n;
579 }
580
581 enum {
582         FS_MENU_COLUMN_NAME = 0,
583         FS_MENU_COLUMN_VALUE
584 };
585
586 #define BUTTON_ABOVE_KEY  "button_above"
587
588 static void fullscreen_prefs_selection_cb(GtkWidget *combo, gpointer data)
589 {
590         gint *value = data;
591         GtkTreeModel *store;
592         GtkTreeIter iter;
593         GtkWidget *button;
594
595         if (!value) return;
596
597         store = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
598         if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) return;
599         gtk_tree_model_get(store, &iter, FS_MENU_COLUMN_VALUE, value, -1);
600
601         button = g_object_get_data(G_OBJECT(combo), BUTTON_ABOVE_KEY);
602         if (button)
603                 {
604                 gtk_widget_set_sensitive(button, *value != -1);
605                 }
606 }
607
608 static void fullscreen_prefs_selection_add(GtkListStore *store, const gchar *text, gint value)
609 {
610         GtkTreeIter iter;
611
612         gtk_list_store_append(store, &iter);
613         gtk_list_store_set(store, &iter, FS_MENU_COLUMN_NAME, text,
614                                          FS_MENU_COLUMN_VALUE, value, -1);
615 }
616
617 GtkWidget *fullscreen_prefs_selection_new(const gchar *text, gint *screen_value, gboolean *above_value)
618 {
619         GtkWidget *vbox;
620         GtkWidget *hbox;
621         GtkWidget *combo;
622         GtkListStore *store;
623         GtkCellRenderer *renderer;
624         GtkWidget *button = NULL;
625         GList *list;
626         GList *work;
627         gint current = 0;
628         gint n;
629
630         if (!screen_value) return NULL;
631
632         vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
633         hbox = pref_box_new(vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
634         if (text) pref_label_new(hbox, text);
635
636         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
637         combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
638         g_object_unref(store);
639
640         renderer = gtk_cell_renderer_text_new();
641         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
642         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer,
643                                        "text", FS_MENU_COLUMN_NAME, NULL);
644
645         if (above_value)
646                 {
647                 button = pref_checkbox_new_int(vbox, _("Stay above other windows"),
648                                                *above_value, above_value);
649                 gtk_widget_set_sensitive(button, *screen_value != -1);
650
651                 g_object_set_data(G_OBJECT(combo), BUTTON_ABOVE_KEY, button);
652                 }
653
654         fullscreen_prefs_selection_add(store, _("Determined by Window Manager"), -1);
655         fullscreen_prefs_selection_add(store, _("Active screen"), 0);
656         if (*screen_value == 0) current = 1;
657         fullscreen_prefs_selection_add(store, _("Active monitor"), 1);
658         if (*screen_value == 1) current = 2;
659
660         n = 3;
661         list = fullscreen_prefs_list();
662         work = list;
663         while (work)
664                 {
665                 ScreenData *sd = work->data;
666
667                 fullscreen_prefs_selection_add(store, sd->description, sd->number);
668                 if (*screen_value == sd->number) current = n;
669
670                 work = work->next;
671                 n++;
672                 }
673         fullscreen_prefs_list_free(list);
674
675         gtk_combo_box_set_active(GTK_COMBO_BOX(combo), current);
676
677         gtk_box_pack_start(GTK_BOX(hbox), combo, FALSE, FALSE, 0);
678         gtk_widget_show(combo);
679
680         g_signal_connect(G_OBJECT(combo), "changed",
681                          G_CALLBACK(fullscreen_prefs_selection_cb), screen_value);
682
683         return vbox;
684 }
685 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */