winemac: Implement WGL_EXT_swap_control.
[wine] / dlls / winemac.drv / cocoa_display.m
1 /*
2  * MACDRV Cocoa display settings
3  *
4  * Copyright 2011, 2012 Ken Thomases for CodeWeavers Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #import <AppKit/AppKit.h>
22 #include "macdrv_cocoa.h"
23
24
25 /***********************************************************************
26  *              convert_display_rect
27  *
28  * Converts an NSRect in Cocoa's y-goes-up-from-bottom coordinate system
29  * to a CGRect in y-goes-down-from-top coordinates.
30  */
31 static inline void convert_display_rect(CGRect* out_rect, NSRect in_rect,
32                                         NSRect primary_frame)
33 {
34     *out_rect = NSRectToCGRect(in_rect);
35     out_rect->origin.y = NSMaxY(primary_frame) - NSMaxY(in_rect);
36 }
37
38
39 /***********************************************************************
40  *              macdrv_get_displays
41  *
42  * Returns information about the displays.
43  *
44  * Returns 0 on success and *displays contains a newly-allocated array
45  * of macdrv_display structures and *count contains the number of
46  * elements in that array.  The first element of the array is the
47  * primary display.  When the caller is done with the array, it should
48  * use macdrv_free_displays() to deallocate it.
49  *
50  * Returns non-zero on failure and *displays and *count are unchanged.
51  */
52 int macdrv_get_displays(struct macdrv_display** displays, int* count)
53 {
54     int ret = -1;
55     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
56
57     NSArray* screens = [NSScreen screens];
58     if (screens)
59     {
60         NSUInteger num_screens = [screens count];
61         struct macdrv_display* disps = malloc(num_screens * sizeof(disps[0]));
62
63         if (disps)
64         {
65             NSRect primary_frame;
66
67             NSUInteger i;
68             for (i = 0; i < num_screens; i++)
69             {
70                 NSScreen* screen = [screens objectAtIndex:i];
71                 NSRect frame = [screen frame];
72                 NSRect visible_frame = [screen visibleFrame];
73
74                 if (i == 0)
75                     primary_frame = frame;
76
77                 disps[i].displayID = [[[screen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue];
78                 convert_display_rect(&disps[i].frame, frame, primary_frame);
79                 convert_display_rect(&disps[i].work_frame, visible_frame,
80                                      primary_frame);
81             }
82
83             *displays = disps;
84             *count = num_screens;
85             ret = 0;
86         }
87     }
88
89     [pool release];
90     return ret;
91 }
92
93
94 /***********************************************************************
95  *              macdrv_free_displays
96  *
97  * Deallocates an array of macdrv_display structures previously returned
98  * from macdrv_get_displays().
99  */
100 void macdrv_free_displays(struct macdrv_display* displays)
101 {
102     free(displays);
103 }