Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 1 | /* 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 Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 25 | static bool captured = false; |
| 26 | |
Pierre Ossman | 3d75911 | 2012-08-27 14:40:51 +0000 | [diff] [blame] | 27 | int cocoa_capture_display(Fl_Window *win, bool all_displays) |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 28 | { |
| 29 | NSWindow *nsw; |
| 30 | |
| 31 | nsw = (NSWindow*)fl_xid(win); |
| 32 | |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 33 | if (!captured) { |
Pierre Ossman | 3d75911 | 2012-08-27 14:40:51 +0000 | [diff] [blame] | 34 | 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 Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 57 | |
| 58 | captured = true; |
| 59 | } |
| 60 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 61 | if ([nsw level] == CGShieldingWindowLevel()) |
| 62 | return 0; |
| 63 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 64 | [nsw setLevel:CGShieldingWindowLevel()]; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void cocoa_release_display(Fl_Window *win) |
| 70 | { |
| 71 | NSWindow *nsw; |
| 72 | int newlevel; |
| 73 | |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 74 | if (captured) |
Pierre Ossman | 3d75911 | 2012-08-27 14:40:51 +0000 | [diff] [blame] | 75 | CGReleaseAllDisplays(); |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 76 | |
| 77 | captured = false; |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 78 | |
| 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 | } |