blob: 6872b23074fdeb346f6bf1cd4e9fce7bb104d72c [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
19#include <FL/Fl.H>
20#include <FL/Fl_Window.H>
21#include <FL/x.H>
22
23#import <Cocoa/Cocoa.h>
24
Pierre Ossmana4f0f182011-06-14 13:36:57 +000025static bool captured = false;
26
Pierre Ossman3d759112012-08-27 14:40:51 +000027int cocoa_capture_display(Fl_Window *win, bool all_displays)
Pierre Ossman407a5c32011-05-26 14:48:29 +000028{
29 NSWindow *nsw;
30
31 nsw = (NSWindow*)fl_xid(win);
32
Pierre Ossmana4f0f182011-06-14 13:36:57 +000033 if (!captured) {
Pierre Ossman3d759112012-08-27 14:40:51 +000034 if (all_displays) {
35 if (CGCaptureAllDisplays() != kCGErrorSuccess)
36 return 1;
37 } else {
38 CGDirectDisplayID displays[16];
39 CGDisplayCount count;
40 int index;
41
42 if (CGGetActiveDisplayList(16, displays, &count) != kCGErrorSuccess)
43 return 1;
44
45 if (count != Fl::screen_count())
46 return 1;
47
48#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
49 index = Fl::screen_num(win->x(), win->y(), win->w(), win->h());
50#else
51 index = 0;
52#endif
53
54 if (CGDisplayCapture(displays[index]) != kCGErrorSuccess)
55 return 1;
56 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +000057
58 captured = true;
59 }
60
Pierre Ossman407a5c32011-05-26 14:48:29 +000061 if ([nsw level] == CGShieldingWindowLevel())
62 return 0;
63
Pierre Ossman407a5c32011-05-26 14:48:29 +000064 [nsw setLevel:CGShieldingWindowLevel()];
65
66 return 0;
67}
68
69void cocoa_release_display(Fl_Window *win)
70{
71 NSWindow *nsw;
72 int newlevel;
73
Pierre Ossmana4f0f182011-06-14 13:36:57 +000074 if (captured)
Pierre Ossman3d759112012-08-27 14:40:51 +000075 CGReleaseAllDisplays();
Pierre Ossmana4f0f182011-06-14 13:36:57 +000076
77 captured = false;
Pierre Ossman407a5c32011-05-26 14:48:29 +000078
79 nsw = (NSWindow*)fl_xid(win);
80
81 // Someone else has already changed the level of this window
82 if ([nsw level] != CGShieldingWindowLevel())
83 return;
84
85 // FIXME: Store the previous level somewhere so we don't have to hard
86 // code a level here.
87#ifdef HAVE_FLTK_FULLSCREEN
88 if (win->fullscreen_active() && win->contains(Fl::focus()))
89 newlevel = NSStatusWindowLevel;
90 else
91#endif
92 newlevel = NSNormalWindowLevel;
93
94 // Only change if different as the level change also moves the window
95 // to the top of that level.
96 if ([nsw level] != newlevel)
97 [nsw setLevel:newlevel];
98}