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