psapi/tests: Adjust margin for win10.
[wine/multimedia.git] / dlls / winemac.drv / macdrv_main.c
blob62c70df64dfd4051247522451f9c41b15b81e73f
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 int cursor_clipping_locks_windows = TRUE;
59 int use_precise_scrolling = TRUE;
60 int gl_surface_mode = GL_SURFACE_IN_FRONT_OPAQUE;
61 HMODULE macdrv_module = 0;
64 /**************************************************************************
65 * debugstr_cf
67 const char* debugstr_cf(CFTypeRef t)
69 CFStringRef s;
70 const char* ret;
72 if (!t) return "(null)";
74 if (CFGetTypeID(t) == CFStringGetTypeID())
75 s = t;
76 else
77 s = CFCopyDescription(t);
78 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
79 if (ret) ret = debugstr_a(ret);
80 if (!ret)
82 const UniChar* u = CFStringGetCharactersPtr(s);
83 if (u)
84 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
86 if (!ret)
88 UniChar buf[200];
89 int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0]));
90 CFStringGetCharacters(s, CFRangeMake(0, len), buf);
91 ret = debugstr_wn(buf, len);
93 if (s != t) CFRelease(s);
94 return ret;
98 /***********************************************************************
99 * get_config_key
101 * Get a config key from either the app-specific or the default config
103 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name,
104 char *buffer, DWORD size)
106 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
107 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
108 return ERROR_FILE_NOT_FOUND;
112 /***********************************************************************
113 * setup_options
115 * Set up the Mac driver options.
117 static void setup_options(void)
119 char buffer[MAX_PATH + 16];
120 HKEY hkey, appkey = 0;
121 DWORD len;
123 /* @@ Wine registry key: HKCU\Software\Wine\Mac Driver */
124 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Mac Driver", &hkey)) hkey = 0;
126 /* open the app-specific key */
128 len = GetModuleFileNameA(0, buffer, MAX_PATH);
129 if (len && len < MAX_PATH)
131 HKEY tmpkey;
132 char *p, *appname = buffer;
133 if ((p = strrchr(appname, '/'))) appname = p + 1;
134 if ((p = strrchr(appname, '\\'))) appname = p + 1;
135 strcat(appname, "\\Mac Driver");
136 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mac Driver */
137 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
139 if (RegOpenKeyA(tmpkey, appname, &appkey)) appkey = 0;
140 RegCloseKey(tmpkey);
144 if (!get_config_key(hkey, appkey, "WindowsFloatWhenInactive", buffer, sizeof(buffer)))
146 if (!strcmp(buffer, "none"))
147 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONE;
148 else if (!strcmp(buffer, "all"))
149 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_ALL;
150 else
151 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN;
154 if (!get_config_key(hkey, appkey, "CaptureDisplaysForFullscreen", buffer, sizeof(buffer)))
155 capture_displays_for_fullscreen = IS_OPTION_TRUE(buffer[0]);
157 if (!get_config_key(hkey, appkey, "SkipSingleBufferFlushes", buffer, sizeof(buffer)))
158 skip_single_buffer_flushes = IS_OPTION_TRUE(buffer[0]);
160 if (!get_config_key(hkey, appkey, "AllowVerticalSync", buffer, sizeof(buffer)))
161 allow_vsync = IS_OPTION_TRUE(buffer[0]);
163 if (!get_config_key(hkey, appkey, "AllowSetGamma", buffer, sizeof(buffer)))
164 allow_set_gamma = IS_OPTION_TRUE(buffer[0]);
166 if (!get_config_key(hkey, appkey, "LeftOptionIsAlt", buffer, sizeof(buffer)))
167 left_option_is_alt = IS_OPTION_TRUE(buffer[0]);
168 if (!get_config_key(hkey, appkey, "RightOptionIsAlt", buffer, sizeof(buffer)))
169 right_option_is_alt = IS_OPTION_TRUE(buffer[0]);
171 if (!get_config_key(hkey, appkey, "AllowSoftwareRendering", buffer, sizeof(buffer)))
172 allow_software_rendering = IS_OPTION_TRUE(buffer[0]);
174 /* Value name chosen to match what's used in the X11 driver. */
175 if (!get_config_key(hkey, appkey, "Decorated", buffer, sizeof(buffer)))
176 disable_window_decorations = !IS_OPTION_TRUE(buffer[0]);
178 if (!get_config_key(hkey, appkey, "AllowImmovableWindows", buffer, sizeof(buffer)))
179 allow_immovable_windows = IS_OPTION_TRUE(buffer[0]);
181 if (!get_config_key(hkey, appkey, "CursorClippingLocksWindows", buffer, sizeof(buffer)))
182 cursor_clipping_locks_windows = IS_OPTION_TRUE(buffer[0]);
184 if (!get_config_key(hkey, appkey, "UsePreciseScrolling", buffer, sizeof(buffer)))
185 use_precise_scrolling = IS_OPTION_TRUE(buffer[0]);
187 if (!get_config_key(hkey, appkey, "OpenGLSurfaceMode", buffer, sizeof(buffer)))
189 if (!strcmp(buffer, "transparent"))
190 gl_surface_mode = GL_SURFACE_IN_FRONT_TRANSPARENT;
191 else if (!strcmp(buffer, "behind"))
192 gl_surface_mode = GL_SURFACE_BEHIND;
193 else
194 gl_surface_mode = GL_SURFACE_IN_FRONT_OPAQUE;
197 if (appkey) RegCloseKey(appkey);
198 if (hkey) RegCloseKey(hkey);
202 /***********************************************************************
203 * process_attach
205 static BOOL process_attach(void)
207 SessionAttributeBits attributes;
208 OSStatus status;
210 status = SessionGetInfo(callerSecuritySession, NULL, &attributes);
211 if (status != noErr || !(attributes & sessionHasGraphicAccess))
212 return FALSE;
214 setup_options();
216 if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
218 macdrv_err_on = ERR_ON(macdrv);
219 if (macdrv_start_cocoa_app(GetTickCount64()))
221 ERR("Failed to start Cocoa app main loop\n");
222 return FALSE;
225 macdrv_clipboard_process_attach();
227 return TRUE;
231 /***********************************************************************
232 * thread_detach
234 static void thread_detach(void)
236 struct macdrv_thread_data *data = macdrv_thread_data();
238 if (data)
240 macdrv_destroy_event_queue(data->queue);
241 if (data->keyboard_layout_uchr)
242 CFRelease(data->keyboard_layout_uchr);
243 HeapFree(GetProcessHeap(), 0, data);
244 /* clear data in case we get re-entered from user32 before the thread is truly dead */
245 TlsSetValue(thread_data_tls_index, NULL);
250 /***********************************************************************
251 * set_queue_display_fd
253 * Store the event queue fd into the message queue
255 static void set_queue_display_fd(int fd)
257 HANDLE handle;
258 int ret;
260 if (wine_server_fd_to_handle(fd, GENERIC_READ | SYNCHRONIZE, 0, &handle))
262 MESSAGE("macdrv: Can't allocate handle for event queue fd\n");
263 ExitProcess(1);
265 SERVER_START_REQ(set_queue_fd)
267 req->handle = wine_server_obj_handle(handle);
268 ret = wine_server_call(req);
270 SERVER_END_REQ;
271 if (ret)
273 MESSAGE("macdrv: Can't store handle for event queue fd\n");
274 ExitProcess(1);
276 CloseHandle(handle);
280 /***********************************************************************
281 * macdrv_init_thread_data
283 struct macdrv_thread_data *macdrv_init_thread_data(void)
285 struct macdrv_thread_data *data = macdrv_thread_data();
286 TISInputSourceRef input_source;
288 if (data) return data;
290 if (!(data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
292 ERR("could not create data\n");
293 ExitProcess(1);
296 if (!(data->queue = macdrv_create_event_queue(macdrv_handle_event)))
298 ERR("macdrv: Can't create event queue.\n");
299 ExitProcess(1);
302 macdrv_get_input_source_info(&data->keyboard_layout_uchr, &data->keyboard_type, &data->iso_keyboard, &input_source);
303 data->active_keyboard_layout = macdrv_get_hkl_from_source(input_source);
304 CFRelease(input_source);
305 macdrv_compute_keyboard_layout(data);
307 set_queue_display_fd(macdrv_get_event_queue_fd(data->queue));
308 TlsSetValue(thread_data_tls_index, data);
310 return data;
314 /***********************************************************************
315 * DllMain
317 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
319 BOOL ret = TRUE;
321 switch(reason)
323 case DLL_PROCESS_ATTACH:
324 macdrv_module = hinst;
325 ret = process_attach();
326 break;
327 case DLL_THREAD_DETACH:
328 thread_detach();
329 break;
331 return ret;
334 /***********************************************************************
335 * SystemParametersInfo (MACDRV.@)
337 BOOL CDECL macdrv_SystemParametersInfo( UINT action, UINT int_param, void *ptr_param, UINT flags )
339 switch (action)
341 case SPI_GETSCREENSAVEACTIVE:
342 if (ptr_param)
344 CFDictionaryRef assertionStates;
345 IOReturn status = IOPMCopyAssertionsStatus(&assertionStates);
346 if (status == kIOReturnSuccess)
348 CFNumberRef count = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypeNoDisplaySleep);
349 CFNumberRef count2 = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypePreventUserIdleDisplaySleep);
350 long longCount = 0, longCount2 = 0;
352 if (count)
353 CFNumberGetValue(count, kCFNumberLongType, &longCount);
354 if (count2)
355 CFNumberGetValue(count2, kCFNumberLongType, &longCount2);
357 *(BOOL *)ptr_param = !longCount && !longCount2;
358 CFRelease(assertionStates);
360 else
362 WARN("Could not determine screen saver state, error code %d\n", status);
363 *(BOOL *)ptr_param = TRUE;
365 return TRUE;
367 break;
369 case SPI_SETSCREENSAVEACTIVE:
371 static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
372 if (int_param)
374 if (powerAssertion != kIOPMNullAssertionID)
376 IOPMAssertionRelease(powerAssertion);
377 powerAssertion = kIOPMNullAssertionID;
380 else if (powerAssertion == kIOPMNullAssertionID)
382 CFStringRef assertName;
383 /*Are we running Lion or later?*/
384 if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber10_7)
385 assertName = kIOPMAssertionTypePreventUserIdleDisplaySleep;
386 else
387 assertName = kIOPMAssertionTypeNoDisplaySleep;
388 IOPMAssertionCreateWithName( assertName, kIOPMAssertionLevelOn,
389 CFSTR("Wine Process requesting no screen saver"),
390 &powerAssertion);
393 break;
395 return FALSE;