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