oleaut32/tests: Use common wine_dbgstr_guid implementation from test.h.
[wine.git] / dlls / winemac.drv / macdrv_main.c
blob1aab785b1fa1bda0b9a46d74de9d0693d65bf82a
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;
55 BOOL allow_software_rendering = FALSE;
56 BOOL disable_window_decorations = FALSE;
57 int allow_immovable_windows = TRUE;
58 HMODULE macdrv_module = 0;
61 /**************************************************************************
62 * debugstr_cf
64 const char* debugstr_cf(CFTypeRef t)
66 CFStringRef s;
67 const char* ret;
69 if (!t) return "(null)";
71 if (CFGetTypeID(t) == CFStringGetTypeID())
72 s = t;
73 else
74 s = CFCopyDescription(t);
75 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
76 if (ret) ret = debugstr_a(ret);
77 if (!ret)
79 const UniChar* u = CFStringGetCharactersPtr(s);
80 if (u)
81 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
83 if (!ret)
85 UniChar buf[200];
86 int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0]));
87 CFStringGetCharacters(s, CFRangeMake(0, len), buf);
88 ret = debugstr_wn(buf, len);
90 if (s != t) CFRelease(s);
91 return ret;
95 /***********************************************************************
96 * get_config_key
98 * Get a config key from either the app-specific or the default config
100 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name,
101 char *buffer, DWORD size)
103 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
104 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
105 return ERROR_FILE_NOT_FOUND;
109 /***********************************************************************
110 * setup_options
112 * Set up the Mac driver options.
114 static void setup_options(void)
116 char buffer[MAX_PATH + 16];
117 HKEY hkey, appkey = 0;
118 DWORD len;
120 /* @@ Wine registry key: HKCU\Software\Wine\Mac Driver */
121 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Mac Driver", &hkey)) hkey = 0;
123 /* open the app-specific key */
125 len = GetModuleFileNameA(0, buffer, MAX_PATH);
126 if (len && len < MAX_PATH)
128 HKEY tmpkey;
129 char *p, *appname = buffer;
130 if ((p = strrchr(appname, '/'))) appname = p + 1;
131 if ((p = strrchr(appname, '\\'))) appname = p + 1;
132 strcat(appname, "\\Mac Driver");
133 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mac Driver */
134 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
136 if (RegOpenKeyA(tmpkey, appname, &appkey)) appkey = 0;
137 RegCloseKey(tmpkey);
141 if (!get_config_key(hkey, appkey, "WindowsFloatWhenInactive", buffer, sizeof(buffer)))
143 if (!strcmp(buffer, "none"))
144 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONE;
145 else if (!strcmp(buffer, "all"))
146 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_ALL;
147 else
148 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN;
151 if (!get_config_key(hkey, appkey, "CaptureDisplaysForFullscreen", buffer, sizeof(buffer)))
152 capture_displays_for_fullscreen = IS_OPTION_TRUE(buffer[0]);
154 if (!get_config_key(hkey, appkey, "SkipSingleBufferFlushes", buffer, sizeof(buffer)))
155 skip_single_buffer_flushes = IS_OPTION_TRUE(buffer[0]);
157 if (!get_config_key(hkey, appkey, "AllowVerticalSync", buffer, sizeof(buffer)))
158 allow_vsync = IS_OPTION_TRUE(buffer[0]);
160 if (!get_config_key(hkey, appkey, "AllowSetGamma", buffer, sizeof(buffer)))
161 allow_set_gamma = IS_OPTION_TRUE(buffer[0]);
163 if (!get_config_key(hkey, appkey, "LeftOptionIsAlt", buffer, sizeof(buffer)))
164 left_option_is_alt = IS_OPTION_TRUE(buffer[0]);
165 if (!get_config_key(hkey, appkey, "RightOptionIsAlt", buffer, sizeof(buffer)))
166 right_option_is_alt = IS_OPTION_TRUE(buffer[0]);
168 if (!get_config_key(hkey, appkey, "AllowSoftwareRendering", buffer, sizeof(buffer)))
169 allow_software_rendering = IS_OPTION_TRUE(buffer[0]);
171 /* Value name chosen to match what's used in the X11 driver. */
172 if (!get_config_key(hkey, appkey, "Decorated", buffer, sizeof(buffer)))
173 disable_window_decorations = !IS_OPTION_TRUE(buffer[0]);
175 if (!get_config_key(hkey, appkey, "AllowImmovableWindows", buffer, sizeof(buffer)))
176 allow_immovable_windows = IS_OPTION_TRUE(buffer[0]);
178 if (appkey) RegCloseKey(appkey);
179 if (hkey) RegCloseKey(hkey);
183 /***********************************************************************
184 * process_attach
186 static BOOL process_attach(void)
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 macdrv_clipboard_process_attach();
208 return TRUE;
212 /***********************************************************************
213 * thread_detach
215 static void thread_detach(void)
217 struct macdrv_thread_data *data = macdrv_thread_data();
219 if (data)
221 macdrv_destroy_event_queue(data->queue);
222 if (data->keyboard_layout_uchr)
223 CFRelease(data->keyboard_layout_uchr);
224 HeapFree(GetProcessHeap(), 0, data);
225 /* clear data in case we get re-entered from user32 before the thread is truly dead */
226 TlsSetValue(thread_data_tls_index, NULL);
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();
267 TISInputSourceRef input_source;
269 if (data) return data;
271 if (!(data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
273 ERR("could not create data\n");
274 ExitProcess(1);
277 if (!(data->queue = macdrv_create_event_queue(macdrv_handle_event)))
279 ERR("macdrv: Can't create event queue.\n");
280 ExitProcess(1);
283 macdrv_get_input_source_info(&data->keyboard_layout_uchr, &data->keyboard_type, &data->iso_keyboard, &input_source);
284 data->active_keyboard_layout = macdrv_get_hkl_from_source(input_source);
285 CFRelease(input_source);
286 macdrv_compute_keyboard_layout(data);
288 set_queue_display_fd(macdrv_get_event_queue_fd(data->queue));
289 TlsSetValue(thread_data_tls_index, data);
291 return data;
295 /***********************************************************************
296 * DllMain
298 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
300 BOOL ret = TRUE;
302 switch(reason)
304 case DLL_PROCESS_ATTACH:
305 macdrv_module = hinst;
306 ret = process_attach();
307 break;
308 case DLL_THREAD_DETACH:
309 thread_detach();
310 break;
312 return ret;
315 /***********************************************************************
316 * SystemParametersInfo (MACDRV.@)
318 BOOL CDECL macdrv_SystemParametersInfo( UINT action, UINT int_param, void *ptr_param, UINT flags )
320 switch (action)
322 case SPI_GETSCREENSAVEACTIVE:
323 if (ptr_param)
325 CFDictionaryRef assertionStates;
326 IOReturn status = IOPMCopyAssertionsStatus(&assertionStates);
327 if (status == kIOReturnSuccess)
329 CFNumberRef count = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypeNoDisplaySleep);
330 CFNumberRef count2 = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypePreventUserIdleDisplaySleep);
331 long longCount = 0, longCount2 = 0;
333 if (count)
334 CFNumberGetValue(count, kCFNumberLongType, &longCount);
335 if (count2)
336 CFNumberGetValue(count2, kCFNumberLongType, &longCount2);
338 *(BOOL *)ptr_param = !longCount && !longCount2;
339 CFRelease(assertionStates);
341 else
343 WARN("Could not determine screen saver state, error code %d\n", status);
344 *(BOOL *)ptr_param = TRUE;
346 return TRUE;
348 break;
350 case SPI_SETSCREENSAVEACTIVE:
352 static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
353 if (int_param)
355 if (powerAssertion != kIOPMNullAssertionID)
357 IOPMAssertionRelease(powerAssertion);
358 powerAssertion = kIOPMNullAssertionID;
361 else if (powerAssertion == kIOPMNullAssertionID)
363 CFStringRef assertName;
364 /*Are we running Lion or later?*/
365 if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber10_7)
366 assertName = kIOPMAssertionTypePreventUserIdleDisplaySleep;
367 else
368 assertName = kIOPMAssertionTypeNoDisplaySleep;
369 IOPMAssertionCreateWithName( assertName, kIOPMAssertionLevelOn,
370 CFSTR("Wine Process requesting no screen saver"),
371 &powerAssertion);
374 break;
376 return FALSE;