OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4AppMac.mm
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 // based on SDL implementation
18 
20 #include <GL/glew.h>
21 
22 #include "C4Include.h"
23 #include "platform/C4Window.h"
24 #include "graphics/C4Draw.h"
25 
26 #include "platform/C4App.h"
27 #import <Cocoa/Cocoa.h>
28 
29 #ifndef USE_CONSOLE
31 #import "graphics/C4DrawGLMac.h"
32 
33 bool C4AbstractApp::Copy(const std::string &text, bool fClipboard)
34 {
35  NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
36  [pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
37  NSString* string = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding];
38  if (![pasteboard setString:string forType:NSStringPboardType])
39  {
40  Log("Writing to Cocoa pasteboard failed");
41  return false;
42  }
43  return true;
44 }
45 
46 std::string C4AbstractApp::Paste(bool fClipboard)
47 {
48  if (fClipboard)
49  {
50  NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
51  const char* chars = [[pasteboard stringForType:NSStringPboardType] cStringUsingEncoding:NSUTF8StringEncoding];
52  return chars;
53  }
54  return std::string();
55 }
56 
57 bool C4AbstractApp::IsClipboardFull(bool fClipboard)
58 {
59  return [[NSPasteboard generalPasteboard] availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]];
60 }
61 
62 void C4AbstractApp::MessageDialog(const char * message)
63 {
64  NSAlert* alert = [NSAlert alertWithMessageText:@"Fatal Error"
65  defaultButton:nil
66  alternateButton:nil
67  otherButton:nil
68  informativeTextWithFormat:@"%@",
69  [NSString stringWithUTF8String:message]
70  ];
71  [alert runModal];
72 }
73 
75 {
76  [NSApp requestUserAttention:NSCriticalRequest];
77 }
78 
79 #ifdef USE_COCOA
80 
81 C4AbstractApp::C4AbstractApp(): Active(false), fQuitMsgReceived(false), MainThread(pthread_self()), fDspModeSet(false)
82 {
83 }
84 
86 
87 bool C4AbstractApp::Init(int argc, char * argv[])
88 {
89  // Set locale
90  setlocale(LC_ALL,"");
91 
92  // Custom initialization
93  return DoInit (argc, argv);
94 }
95 
96 
97 void C4AbstractApp::Clear() {}
98 
100 {
101  [NSApp terminate:[NSApp delegate]];
102 }
103 
105 {
106  // Always fail after quit message
107  if(fQuitMsgReceived)
108  return false;
109 
110  while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource);
111  NSEvent* event;
112  while ((event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSEventTrackingRunLoopMode dequeue:YES]) != nil)
113  {
114  [NSApp sendEvent:event];
115  [NSApp updateWindows];
116  }
117  return true;
118 }
119 
120 static int32_t bitDepthFromPixelEncoding(CFStringRef encoding)
121 {
122  // copy-pasta: http://gitorious.org/ogre3d/mainlinemirror/commit/722dbd024aa91a6401850788db76af89c364d6e7
123  if (CFStringCompare(encoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
124  return 32;
125  else if(CFStringCompare(encoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
126  return 16;
127  else if(CFStringCompare(encoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
128  return 8;
129  else
130  return -1; // fail
131 }
132 
133 bool C4AbstractApp::GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, int32_t *piRefreshRate, uint32_t iMonitor)
134 {
135  // No support for multiple monitors.
136  CFArrayRef array = CGDisplayCopyAllDisplayModes(iMonitor, NULL);
137  bool good_index = iIndex >= 0 && iIndex < (int32_t)CFArrayGetCount(array);
138  if (good_index)
139  {
140  CGDisplayModeRef displayMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(array, iIndex);
141  *piXRes = CGDisplayModeGetWidth(displayMode);
142  *piYRes = CGDisplayModeGetHeight(displayMode);
143  CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding(displayMode);
144  *piBitDepth = bitDepthFromPixelEncoding(pixelEncoding);
145  CFRelease(pixelEncoding);
146  }
147  CFRelease(array);
148  return good_index;
149 }
150 
152 {
153 }
154 
155 bool C4AbstractApp::SetVideoMode(int iXRes, int iYRes, unsigned int iRefreshRate, unsigned int iMonitor, bool fFullScreen)
156 {
157  fFullScreen &= !lionAndBeyond(); // Always false for Lion since then Lion's true(tm) Fullscreen is used
158  C4WindowController* controller = pWindow->objectiveCObject<C4WindowController>();
159  NSWindow* window = controller.window;
160 
161  size_t dw = CGDisplayPixelsWide(C4OpenGLView.displayID);
162  size_t dh = CGDisplayPixelsHigh(C4OpenGLView.displayID);
163  if (iXRes == -1)
164  iXRes = dw;
165  if (iYRes == -1)
166  iYRes = dh;
167  ActualFullscreenX = iXRes;
168  ActualFullscreenY = iYRes;
169  [C4OpenGLView setSurfaceBackingSizeOf:[C4OpenGLView mainContext] width:ActualFullscreenX height:ActualFullscreenY];
170  if ((window.styleMask & NSFullScreenWindowMask) == 0)
171  {
172  [window setResizeIncrements:NSMakeSize(1.0, 1.0)];
173  pWindow->SetSize(iXRes, iYRes);
174  [controller setFullscreen:fFullScreen];
175  [window setAspectRatio:[[window contentView] frame].size];
176  [window center];
177  }
178  else
179  {
180  [window toggleFullScreen:window];
181  pWindow->SetSize(dw, dh);
182  }
183  if (!fFullScreen)
184  [window makeKeyAndOrderFront:nil];
185  OnResolutionChanged(iXRes, iYRes);
186  return true;
187 }
188 
189 #endif // USE_COCOA
190 #endif // USE_CONSOLE
191 
193 {
194  id languages = [[NSUserDefaults standardUserDefaults] valueForKey:@"AppleLanguages"];
195  return languages && [[languages objectAtIndex:0] isEqualToString:@"de"];
196 }
197 
198 bool OpenURL(const char* szURL)
199 {
200  std::string command = std::string("open ") + '"' + szURL + '"';
201  std::system(command.c_str());
202  return true;
203 }
204 
205 bool EraseItemSafe(const char* szFilename)
206 {
207  NSString* filename = [NSString stringWithUTF8String: szFilename];
208  return [[NSWorkspace sharedWorkspace]
209  performFileOperation: NSWorkspaceRecycleOperation
210  source: [filename stringByDeletingLastPathComponent]
211  destination: @""
212  files: [NSArray arrayWithObject: [filename lastPathComponent]]
213  tag: 0];
214 }
215 
216 std::string C4AbstractApp::GetGameDataPath()
217 {
218  return [[[NSBundle mainBundle] resourcePath] fileSystemRepresentation];
219 }
virtual void OnResolutionChanged(unsigned int iXRes, unsigned int iYRes)=0
void FlashWindow()
Definition: C4AppMac.mm:74
void MessageDialog(const char *message)
Definition: C4AppMac.mm:62
bool EraseItemSafe(const char *szFilename)
Definition: C4AppMac.mm:205
~C4AbstractApp() override
Definition: C4AppT.cpp:37
bool SetVideoMode(int iXRes, int iYRes, unsigned int iRefreshRate, unsigned int iMonitor, bool fFullScreen)
Definition: C4AppSDL.cpp:354
bool IsGermanSystem()
Definition: C4AppMac.mm:192
bool FlushMessages()
Definition: C4AppSDL.cpp:115
void SetSize(unsigned int cx, unsigned int cy)
Definition: C4AppT.cpp:110
void setFullscreen:(BOOL fullscreen)
virtual bool DoInit(int argc, char *argv[])=0
bool lionAndBeyond()
bool OpenURL(const char *szURL)
Definition: C4AppMac.mm:198
bool fQuitMsgReceived
Definition: C4App.h:81
virtual void Quit()
Definition: C4AppSDL.cpp:110
bool GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, int32_t *piRefreshRate, uint32_t iMonitor)
Definition: C4AppSDL.cpp:335
bool Copy(const std::string &text, bool fClipboard=true)
Definition: C4AppMac.mm:33
void RestoreVideoMode()
Definition: C4AppSDL.cpp:443
bool Init(int argc, char *argv[])
Definition: C4AppSDL.cpp:87
virtual void Clear()
Definition: C4AppSDL.cpp:105
std::string Paste(bool fClipboard=true)
Definition: C4AppMac.mm:46
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
C4Window * pWindow
Definition: C4App.h:80
bool IsClipboardFull(bool fClipboard=true)
Definition: C4AppMac.mm:57