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