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