makefiles: Generate the common rules for programs from configure.
[wine.git] / dlls / winemac.drv / macdrv_main.c
blobd27ce94ca47029c6670c9e8ba319b5fd8e23ac2f
1 /*
2 * MACDRV initialization code
4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2000 Alexandre Julliard
6 * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <Security/AuthSession.h>
25 #include <IOKit/pwr_mgt/IOPMLib.h>
27 #include "macdrv.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "wine/server.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(macdrv);
34 #ifndef kIOPMAssertionTypePreventUserIdleDisplaySleep
35 #define kIOPMAssertionTypePreventUserIdleDisplaySleep CFSTR("PreventUserIdleDisplaySleep")
36 #endif
37 #ifndef kCFCoreFoundationVersionNumber10_7
38 #define kCFCoreFoundationVersionNumber10_7 635.00
39 #endif
41 #define IS_OPTION_TRUE(ch) \
42 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
44 C_ASSERT(NUM_EVENT_TYPES <= sizeof(macdrv_event_mask) * 8);
46 DWORD thread_data_tls_index = TLS_OUT_OF_INDEXES;
48 int topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN;
49 int capture_displays_for_fullscreen = 0;
50 BOOL skip_single_buffer_flushes = FALSE;
51 BOOL allow_vsync = TRUE;
52 BOOL allow_set_gamma = TRUE;
53 int left_option_is_alt = 0;
54 int right_option_is_alt = 0;
57 /**************************************************************************
58 * debugstr_cf
60 const char* debugstr_cf(CFTypeRef t)
62 CFStringRef s;
63 const char* ret;
65 if (!t) return "(null)";
67 if (CFGetTypeID(t) == CFStringGetTypeID())
68 s = t;
69 else
70 s = CFCopyDescription(t);
71 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
72 if (ret) ret = debugstr_a(ret);
73 if (!ret)
75 const UniChar* u = CFStringGetCharactersPtr(s);
76 if (u)
77 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
79 if (!ret)
81 UniChar buf[200];
82 int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0]));
83 CFStringGetCharacters(s, CFRangeMake(0, len), buf);
84 ret = debugstr_wn(buf, len);
86 if (s != t) CFRelease(s);
87 return ret;
91 /***********************************************************************
92 * set_app_icon
94 static void set_app_icon(void)
96 CFArrayRef images = create_app_icon_images();
97 if (images)
99 macdrv_set_application_icon(images);
100 CFRelease(images);
105 /***********************************************************************
106 * get_config_key
108 * Get a config key from either the app-specific or the default config
110 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name,
111 char *buffer, DWORD size)
113 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
114 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
115 return ERROR_FILE_NOT_FOUND;
119 /***********************************************************************
120 * setup_options
122 * Set up the Mac driver options.
124 static void setup_options(void)
126 char buffer[MAX_PATH + 16];
127 HKEY hkey, appkey = 0;
128 DWORD len;
130 /* @@ Wine registry key: HKCU\Software\Wine\Mac Driver */
131 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Mac Driver", &hkey)) hkey = 0;
133 /* open the app-specific key */
135 len = GetModuleFileNameA(0, buffer, MAX_PATH);
136 if (len && len < MAX_PATH)
138 HKEY tmpkey;
139 char *p, *appname = buffer;
140 if ((p = strrchr(appname, '/'))) appname = p + 1;
141 if ((p = strrchr(appname, '\\'))) appname = p + 1;
142 strcat(appname, "\\Mac Driver");
143 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mac Driver */
144 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
146 if (RegOpenKeyA(tmpkey, appname, &appkey)) appkey = 0;
147 RegCloseKey(tmpkey);
151 if (!get_config_key(hkey, appkey, "WindowsFloatWhenInactive", buffer, sizeof(buffer)))
153 if (!strcmp(buffer, "none"))
154 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONE;
155 else if (!strcmp(buffer, "all"))
156 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_ALL;
157 else
158 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN;
161 if (!get_config_key(hkey, appkey, "CaptureDisplaysForFullscreen", buffer, sizeof(buffer)))
162 capture_displays_for_fullscreen = IS_OPTION_TRUE(buffer[0]);
164 if (!get_config_key(hkey, appkey, "SkipSingleBufferFlushes", buffer, sizeof(buffer)))
165 skip_single_buffer_flushes = IS_OPTION_TRUE(buffer[0]);
167 if (!get_config_key(hkey, appkey, "AllowVerticalSync", buffer, sizeof(buffer)))
168 allow_vsync = IS_OPTION_TRUE(buffer[0]);
170 if (!get_config_key(hkey, appkey, "AllowSetGamma", buffer, sizeof(buffer)))
171 allow_set_gamma = IS_OPTION_TRUE(buffer[0]);
173 if (!get_config_key(hkey, appkey, "LeftOptionIsAlt", buffer, sizeof(buffer)))
174 left_option_is_alt = IS_OPTION_TRUE(buffer[0]);
175 if (!get_config_key(hkey, appkey, "RightOptionIsAlt", buffer, sizeof(buffer)))
176 right_option_is_alt = IS_OPTION_TRUE(buffer[0]);
178 if (appkey) RegCloseKey(appkey);
179 if (hkey) RegCloseKey(hkey);
183 /***********************************************************************
184 * process_attach
186 static BOOL process_attach( HINSTANCE instance )
188 SessionAttributeBits attributes;
189 OSStatus status;
191 status = SessionGetInfo(callerSecuritySession, NULL, &attributes);
192 if (status != noErr || !(attributes & sessionHasGraphicAccess))
193 return FALSE;
195 setup_options();
197 if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
199 macdrv_err_on = ERR_ON(macdrv);
200 if (macdrv_start_cocoa_app(GetTickCount64()))
202 ERR("Failed to start Cocoa app main loop\n");
203 return FALSE;
206 set_app_icon();
207 macdrv_clipboard_process_attach();
208 IME_RegisterClasses( instance );
210 return TRUE;
214 /***********************************************************************
215 * thread_detach
217 static void thread_detach(void)
219 struct macdrv_thread_data *data = macdrv_thread_data();
221 if (data)
223 macdrv_destroy_event_queue(data->queue);
224 if (data->keyboard_layout_uchr)
225 CFRelease(data->keyboard_layout_uchr);
226 HeapFree(GetProcessHeap(), 0, data);
231 /***********************************************************************
232 * set_queue_display_fd
234 * Store the event queue fd into the message queue
236 static void set_queue_display_fd(int fd)
238 HANDLE handle;
239 int ret;
241 if (wine_server_fd_to_handle(fd, GENERIC_READ | SYNCHRONIZE, 0, &handle))
243 MESSAGE("macdrv: Can't allocate handle for event queue fd\n");
244 ExitProcess(1);
246 SERVER_START_REQ(set_queue_fd)
248 req->handle = wine_server_obj_handle(handle);
249 ret = wine_server_call(req);
251 SERVER_END_REQ;
252 if (ret)
254 MESSAGE("macdrv: Can't store handle for event queue fd\n");
255 ExitProcess(1);
257 CloseHandle(handle);
261 /***********************************************************************
262 * macdrv_init_thread_data
264 struct macdrv_thread_data *macdrv_init_thread_data(void)
266 struct macdrv_thread_data *data = macdrv_thread_data();
268 if (data) return data;
270 if (!(data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
272 ERR("could not create data\n");
273 ExitProcess(1);
276 if (!(data->queue = macdrv_create_event_queue(macdrv_handle_event)))
278 ERR("macdrv: Can't create event queue.\n");
279 ExitProcess(1);
282 data->keyboard_layout_uchr = macdrv_copy_keyboard_layout(&data->keyboard_type, &data->iso_keyboard);
283 macdrv_compute_keyboard_layout(data);
285 set_queue_display_fd(macdrv_get_event_queue_fd(data->queue));
286 TlsSetValue(thread_data_tls_index, data);
288 return data;
292 /***********************************************************************
293 * DllMain
295 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
297 BOOL ret = TRUE;
299 switch(reason)
301 case DLL_PROCESS_ATTACH:
302 ret = process_attach( hinst );
303 break;
304 case DLL_THREAD_DETACH:
305 thread_detach();
306 break;
308 return ret;
311 /***********************************************************************
312 * SystemParametersInfo (MACDRV.@)
314 BOOL CDECL macdrv_SystemParametersInfo( UINT action, UINT int_param, void *ptr_param, UINT flags )
316 switch (action)
318 case SPI_GETSCREENSAVEACTIVE:
319 if (ptr_param)
321 CFDictionaryRef assertionStates;
322 IOReturn status = IOPMCopyAssertionsStatus(&assertionStates);
323 if (status == kIOReturnSuccess)
325 CFNumberRef count = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypeNoDisplaySleep);
326 CFNumberRef count2 = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypePreventUserIdleDisplaySleep);
327 long longCount = 0, longCount2 = 0;
329 if (count)
330 CFNumberGetValue(count, kCFNumberLongType, &longCount);
331 if (count2)
332 CFNumberGetValue(count2, kCFNumberLongType, &longCount2);
334 *(BOOL *)ptr_param = !longCount && !longCount2;
335 CFRelease(assertionStates);
337 else
339 WARN("Could not determine screen saver state, error code %d\n", status);
340 *(BOOL *)ptr_param = TRUE;
342 return TRUE;
344 break;
346 case SPI_SETSCREENSAVEACTIVE:
348 static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
349 if (int_param)
351 if (powerAssertion != kIOPMNullAssertionID)
353 IOPMAssertionRelease(powerAssertion);
354 powerAssertion = kIOPMNullAssertionID;
357 else if (powerAssertion == kIOPMNullAssertionID)
359 CFStringRef assertName;
360 /*Are we running Lion or later?*/
361 if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber10_7)
362 assertName = kIOPMAssertionTypePreventUserIdleDisplaySleep;
363 else
364 assertName = kIOPMAssertionTypeNoDisplaySleep;
365 IOPMAssertionCreateWithName( assertName, kIOPMAssertionLevelOn,
366 CFSTR("Wine Process requesting no screen saver"),
367 &powerAssertion);
370 break;
372 return FALSE;