blob: 2b50ecfea16eef172426bf99ac86cb5e578b8b3c [file] [log] [blame]
Pierre Ossman407a5c32011-05-26 14:48:29 +00001/* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
Pierre Ossmanb808ffd2012-09-11 11:12:56 +000019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
Pierre Ossman407a5c32011-05-26 14:48:29 +000023#include <FL/Fl.H>
24#include <FL/Fl_Window.H>
25#include <FL/x.H>
26
27#import <Cocoa/Cocoa.h>
28
Pierre Ossmana4f0f182011-06-14 13:36:57 +000029static bool captured = false;
30
Pierre Ossman3d759112012-08-27 14:40:51 +000031int cocoa_capture_display(Fl_Window *win, bool all_displays)
Pierre Ossman407a5c32011-05-26 14:48:29 +000032{
33 NSWindow *nsw;
34
35 nsw = (NSWindow*)fl_xid(win);
36
Pierre Ossmana4f0f182011-06-14 13:36:57 +000037 if (!captured) {
Pierre Ossman3d759112012-08-27 14:40:51 +000038 if (all_displays) {
39 if (CGCaptureAllDisplays() != kCGErrorSuccess)
40 return 1;
41 } else {
42 CGDirectDisplayID displays[16];
43 CGDisplayCount count;
44 int index;
45
46 if (CGGetActiveDisplayList(16, displays, &count) != kCGErrorSuccess)
47 return 1;
48
49 if (count != Fl::screen_count())
50 return 1;
51
52#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
53 index = Fl::screen_num(win->x(), win->y(), win->w(), win->h());
54#else
55 index = 0;
56#endif
57
58 if (CGDisplayCapture(displays[index]) != kCGErrorSuccess)
59 return 1;
60 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +000061
62 captured = true;
63 }
64
Pierre Ossman407a5c32011-05-26 14:48:29 +000065 if ([nsw level] == CGShieldingWindowLevel())
66 return 0;
67
Pierre Ossman407a5c32011-05-26 14:48:29 +000068 [nsw setLevel:CGShieldingWindowLevel()];
69
70 return 0;
71}
72
73void cocoa_release_display(Fl_Window *win)
74{
75 NSWindow *nsw;
76 int newlevel;
77
Pierre Ossmana4f0f182011-06-14 13:36:57 +000078 if (captured)
Pierre Ossman3d759112012-08-27 14:40:51 +000079 CGReleaseAllDisplays();
Pierre Ossmana4f0f182011-06-14 13:36:57 +000080
81 captured = false;
Pierre Ossman407a5c32011-05-26 14:48:29 +000082
83 nsw = (NSWindow*)fl_xid(win);
84
85 // Someone else has already changed the level of this window
86 if ([nsw level] != CGShieldingWindowLevel())
87 return;
88
89 // FIXME: Store the previous level somewhere so we don't have to hard
90 // code a level here.
91#ifdef HAVE_FLTK_FULLSCREEN
92 if (win->fullscreen_active() && win->contains(Fl::focus()))
93 newlevel = NSStatusWindowLevel;
94 else
95#endif
96 newlevel = NSNormalWindowLevel;
97
98 // Only change if different as the level change also moves the window
99 // to the top of that level.
100 if ([nsw level] != newlevel)
101 [nsw setLevel:newlevel];
102}