Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osx.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "precompiled.h"
24 
25 #include "lib/lib.h"
26 #include "lib/sysdep/sysdep.h"
27 #include "lib/sysdep/gfx.h"
28 #include "lib/utf8.h"
29 #include "osx_bundle.h"
30 #include "osx_pasteboard.h"
31 
32 #include <ApplicationServices/ApplicationServices.h>
33 #include <AvailabilityMacros.h> // MAC_OS_X_VERSION_MIN_REQUIRED
34 #include <CoreFoundation/CoreFoundation.h>
35 #include <mach-o/dyld.h> // _NSGetExecutablePath
36 
37 
38 Status sys_clipboard_set(const wchar_t* text)
39 {
40  Status ret = INFO::OK;
41 
42  std::string str = utf8_from_wstring(text);
43  bool ok = osx_SendStringToPasteboard(str);
44  if (!ok)
45  ret = ERR::FAIL;
46  return ret;
47 }
48 
50 {
51  wchar_t* ret = NULL;
52  std::string str;
53  bool ok = osx_GetStringFromPasteboard(str);
54  if (ok)
55  {
56  // TODO: this is yucky, why are we passing around wchar_t*?
57  std::wstring wstr = wstring_from_utf8(str);
58  size_t len = wcslen(wstr.c_str());
59  ret = (wchar_t*)malloc((len+1)*sizeof(wchar_t));
60  std::copy(wstr.c_str(), wstr.c_str()+len, ret);
61  ret[len] = 0;
62  }
63 
64  return ret;
65 }
66 
67 Status sys_clipboard_free(wchar_t* copy)
68 {
69  free(copy);
70  return INFO::OK;
71 }
72 
73 
74 namespace gfx {
75 
76 Status GetVideoMode(int* xres, int* yres, int* bpp, int* freq)
77 {
78 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
79  CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);
80 #else
81  CFDictionaryRef currentMode = CGDisplayCurrentMode(kCGDirectMainDisplay);
82 #endif
83 
84  if(xres)
85  *xres = (int)CGDisplayPixelsWide(kCGDirectMainDisplay);
86 
87  if(yres)
88  *yres = (int)CGDisplayPixelsHigh(kCGDirectMainDisplay);
89 
90  if(bpp)
91  {
92 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
93  // CGDisplayBitsPerPixel was deprecated in OS X 10.6
94  CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding(currentMode);
95  if (CFStringCompare(pixelEncoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
96  *bpp = 32;
97  else if (CFStringCompare(pixelEncoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
98  *bpp = 16;
99  else if (CFStringCompare(pixelEncoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
100  *bpp = 8;
101  else // error
102  *bpp = 0;
103 
104  // We're responsible for this
105  CFRelease(pixelEncoding);
106 #else
107  CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(currentMode, kCGDisplayBitsPerPixel);
108  CFNumberGetValue(num, kCFNumberIntType, bpp);
109 #endif
110  }
111 
112  if(freq)
113  {
114 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
115  *freq = (int)CGDisplayModeGetRefreshRate(currentMode);
116 #else
117  CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(currentMode, kCGDisplayRefreshRate);
118  CFNumberGetValue(num, kCFNumberIntType, freq);
119 #endif
120  }
121 
122 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
123  // We're responsible for this
124  CGDisplayModeRelease(currentMode);
125 #endif
126 
127  return INFO::OK;
128 }
129 
130 Status GetMonitorSize(int* xres, int* yres, int* bpp, int* freq)
131 {
132  // TODO Implement
133  return ERR::NOT_SUPPORTED; // NOWARN
134 }
135 
136 } // namespace gfx
137 
138 
140 {
141  OsPath path;
142 
143  // On OS X we might be a bundle, return the bundle path as the executable name,
144  // i.e. /path/to/0ad.app instead of /path/to/0ad.app/Contents/MacOS/pyrogenesis
145  if (osx_IsAppBundleValid())
146  {
147  path = osx_GetBundlePath();
148  ENSURE(!path.empty());
149  }
150  else
151  {
152  char temp[PATH_MAX];
153  u32 size = PATH_MAX;
154  if (_NSGetExecutablePath(temp, &size) == 0)
155  {
156  char name[PATH_MAX];
157  realpath(temp, name);
158  path = OsPath(name);
159  }
160  }
161 
162  return path;
163 }
C++ interface to Cocoa implementation for pasteboards.
const Status OK
Definition: status.h:386
Status sys_clipboard_set(const wchar_t *text)
Definition: android.cpp:30
bool osx_IsAppBundleValid()
Check if app is running in a valid bundle.
Definition: osx_bundle.mm:40
std::string utf8_from_wstring(const std::wstring &src, Status *err)
opposite of wstring_from_utf8
Definition: utf8.cpp:208
LIB_API Status GetVideoMode(int *xres, int *yres, int *bpp, int *freq)
(useful for choosing a new video mode)
Definition: android.cpp:47
Status sys_clipboard_free(wchar_t *copy)
Definition: android.cpp:40
C++ interface to Cocoa implementation for getting bundle information.
const Status NOT_SUPPORTED
Definition: status.h:429
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
bool osx_GetStringFromPasteboard(std::string &out)
Get a string from the pasteboard.
Definition: path.h:75
Path OsPath
Definition: os_path.h:31
i64 Status
Error handling system.
Definition: status.h:171
LIB_API Status GetMonitorSize(int &width_mm, int &height_mm)
(useful for determining aspect ratio)
Definition: wgfx.cpp:197
#define PATH_MAX
Definition: wposix_types.h:101
wchar_t * sys_clipboard_get()
Definition: android.cpp:35
std::wstring wstring_from_utf8(const std::string &src, Status *err)
convert UTF-8 to a wide string (UTF-16 or UCS-4, depending on the platform&#39;s wchar_t).
Definition: utf8.cpp:225
bool empty() const
Definition: path.h:118
#define u32
Definition: types.h:41
unsigned short wchar_t
Definition: wgl.h:78
OsPath sys_ExecutablePathname()
Definition: bsd.cpp:33
bool osx_SendStringToPasteboard(const std::string &string)
Store a string on the pasteboard.
const Status FAIL
Definition: status.h:406
std::string osx_GetBundlePath()
Get the system path to the bundle itself.
Definition: osx_bundle.mm:53