gdi32: Don't call GetObjectW() unless necessary.
[wine.git] / dlls / winemac.drv / macdrv_main.c
blob2ce78d1caa2f2ff8273d69655b0eca483fe52c5a
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 int retina_enabled = FALSE;
62 HMODULE macdrv_module = 0;
64 CFDictionaryRef localized_strings;
67 /**************************************************************************
68 * debugstr_cf
70 const char* debugstr_cf(CFTypeRef t)
72 CFStringRef s;
73 const char* ret;
75 if (!t) return "(null)";
77 if (CFGetTypeID(t) == CFStringGetTypeID())
78 s = t;
79 else
80 s = CFCopyDescription(t);
81 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
82 if (ret) ret = debugstr_a(ret);
83 if (!ret)
85 const UniChar* u = CFStringGetCharactersPtr(s);
86 if (u)
87 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
89 if (!ret)
91 UniChar buf[200];
92 int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0]));
93 CFStringGetCharacters(s, CFRangeMake(0, len), buf);
94 ret = debugstr_wn(buf, len);
96 if (s != t) CFRelease(s);
97 return ret;
101 /***********************************************************************
102 * get_config_key
104 * Get a config key from either the app-specific or the default config
106 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name,
107 char *buffer, DWORD size)
109 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
110 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
111 return ERROR_FILE_NOT_FOUND;
115 /***********************************************************************
116 * setup_options
118 * Set up the Mac driver options.
120 static void setup_options(void)
122 char buffer[MAX_PATH + 16];
123 HKEY hkey, appkey = 0;
124 DWORD len;
126 /* @@ Wine registry key: HKCU\Software\Wine\Mac Driver */
127 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Mac Driver", &hkey)) hkey = 0;
129 /* open the app-specific key */
131 len = GetModuleFileNameA(0, buffer, MAX_PATH);
132 if (len && len < MAX_PATH)
134 HKEY tmpkey;
135 char *p, *appname = buffer;
136 if ((p = strrchr(appname, '/'))) appname = p + 1;
137 if ((p = strrchr(appname, '\\'))) appname = p + 1;
138 strcat(appname, "\\Mac Driver");
139 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mac Driver */
140 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
142 if (RegOpenKeyA(tmpkey, appname, &appkey)) appkey = 0;
143 RegCloseKey(tmpkey);
147 if (!get_config_key(hkey, appkey, "WindowsFloatWhenInactive", buffer, sizeof(buffer)))
149 if (!strcmp(buffer, "none"))
150 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONE;
151 else if (!strcmp(buffer, "all"))
152 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_ALL;
153 else
154 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN;
157 if (!get_config_key(hkey, appkey, "CaptureDisplaysForFullscreen", buffer, sizeof(buffer)))
158 capture_displays_for_fullscreen = IS_OPTION_TRUE(buffer[0]);
160 if (!get_config_key(hkey, appkey, "SkipSingleBufferFlushes", buffer, sizeof(buffer)))
161 skip_single_buffer_flushes = IS_OPTION_TRUE(buffer[0]);
163 if (!get_config_key(hkey, appkey, "AllowVerticalSync", buffer, sizeof(buffer)))
164 allow_vsync = IS_OPTION_TRUE(buffer[0]);
166 if (!get_config_key(hkey, appkey, "AllowSetGamma", buffer, sizeof(buffer)))
167 allow_set_gamma = IS_OPTION_TRUE(buffer[0]);
169 if (!get_config_key(hkey, appkey, "LeftOptionIsAlt", buffer, sizeof(buffer)))
170 left_option_is_alt = IS_OPTION_TRUE(buffer[0]);
171 if (!get_config_key(hkey, appkey, "RightOptionIsAlt", buffer, sizeof(buffer)))
172 right_option_is_alt = IS_OPTION_TRUE(buffer[0]);
174 if (!get_config_key(hkey, appkey, "AllowSoftwareRendering", buffer, sizeof(buffer)))
175 allow_software_rendering = IS_OPTION_TRUE(buffer[0]);
177 /* Value name chosen to match what's used in the X11 driver. */
178 if (!get_config_key(hkey, appkey, "Decorated", buffer, sizeof(buffer)))
179 disable_window_decorations = !IS_OPTION_TRUE(buffer[0]);
181 if (!get_config_key(hkey, appkey, "AllowImmovableWindows", buffer, sizeof(buffer)))
182 allow_immovable_windows = IS_OPTION_TRUE(buffer[0]);
184 if (!get_config_key(hkey, appkey, "CursorClippingLocksWindows", buffer, sizeof(buffer)))
185 cursor_clipping_locks_windows = IS_OPTION_TRUE(buffer[0]);
187 if (!get_config_key(hkey, appkey, "UsePreciseScrolling", buffer, sizeof(buffer)))
188 use_precise_scrolling = IS_OPTION_TRUE(buffer[0]);
190 if (!get_config_key(hkey, appkey, "OpenGLSurfaceMode", buffer, sizeof(buffer)))
192 if (!strcmp(buffer, "transparent"))
193 gl_surface_mode = GL_SURFACE_IN_FRONT_TRANSPARENT;
194 else if (!strcmp(buffer, "behind"))
195 gl_surface_mode = GL_SURFACE_BEHIND;
196 else
197 gl_surface_mode = GL_SURFACE_IN_FRONT_OPAQUE;
200 /* Don't use appkey. The DPI and monitor sizes should be consistent for all
201 processes in the prefix. */
202 if (!get_config_key(hkey, NULL, "RetinaMode", buffer, sizeof(buffer)))
203 retina_enabled = IS_OPTION_TRUE(buffer[0]);
205 if (appkey) RegCloseKey(appkey);
206 if (hkey) RegCloseKey(hkey);
210 /***********************************************************************
211 * load_strings
213 static void load_strings(HINSTANCE instance)
215 static const unsigned int ids[] = {
216 STRING_MENU_WINE,
217 STRING_MENU_ITEM_HIDE_APPNAME,
218 STRING_MENU_ITEM_HIDE,
219 STRING_MENU_ITEM_HIDE_OTHERS,
220 STRING_MENU_ITEM_SHOW_ALL,
221 STRING_MENU_ITEM_QUIT_APPNAME,
222 STRING_MENU_ITEM_QUIT,
224 STRING_MENU_WINDOW,
225 STRING_MENU_ITEM_MINIMIZE,
226 STRING_MENU_ITEM_ZOOM,
227 STRING_MENU_ITEM_ENTER_FULL_SCREEN,
228 STRING_MENU_ITEM_BRING_ALL_TO_FRONT,
230 CFMutableDictionaryRef dict;
231 int i;
233 dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,
234 &kCFTypeDictionaryValueCallBacks);
235 if (!dict)
237 ERR("Failed to create localized strings dictionary\n");
238 return;
241 for (i = 0; i < sizeof(ids) / sizeof(ids[0]); i++)
243 LPCWSTR str;
244 int len = LoadStringW(instance, ids[i], (LPWSTR)&str, 0);
245 if (str && len)
247 CFNumberRef key = CFNumberCreate(NULL, kCFNumberIntType, &ids[i]);
248 CFStringRef value = CFStringCreateWithCharacters(NULL, (UniChar*)str, len);
249 if (key && value)
250 CFDictionarySetValue(dict, key, value);
251 else
252 ERR("Failed to add string ID 0x%04x %s\n", ids[i], debugstr_wn(str, len));
254 else
255 ERR("Failed to load string ID 0x%04x\n", ids[i]);
258 localized_strings = dict;
262 /***********************************************************************
263 * process_attach
265 static BOOL process_attach(void)
267 SessionAttributeBits attributes;
268 OSStatus status;
270 status = SessionGetInfo(callerSecuritySession, NULL, &attributes);
271 if (status != noErr || !(attributes & sessionHasGraphicAccess))
272 return FALSE;
274 setup_options();
275 load_strings(macdrv_module);
277 if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
279 macdrv_err_on = ERR_ON(macdrv);
280 if (macdrv_start_cocoa_app(GetTickCount64()))
282 ERR("Failed to start Cocoa app main loop\n");
283 return FALSE;
286 macdrv_clipboard_process_attach();
288 return TRUE;
292 /***********************************************************************
293 * thread_detach
295 static void thread_detach(void)
297 struct macdrv_thread_data *data = macdrv_thread_data();
299 if (data)
301 macdrv_destroy_event_queue(data->queue);
302 if (data->keyboard_layout_uchr)
303 CFRelease(data->keyboard_layout_uchr);
304 HeapFree(GetProcessHeap(), 0, data);
305 /* clear data in case we get re-entered from user32 before the thread is truly dead */
306 TlsSetValue(thread_data_tls_index, NULL);
311 /***********************************************************************
312 * set_queue_display_fd
314 * Store the event queue fd into the message queue
316 static void set_queue_display_fd(int fd)
318 HANDLE handle;
319 int ret;
321 if (wine_server_fd_to_handle(fd, GENERIC_READ | SYNCHRONIZE, 0, &handle))
323 MESSAGE("macdrv: Can't allocate handle for event queue fd\n");
324 ExitProcess(1);
326 SERVER_START_REQ(set_queue_fd)
328 req->handle = wine_server_obj_handle(handle);
329 ret = wine_server_call(req);
331 SERVER_END_REQ;
332 if (ret)
334 MESSAGE("macdrv: Can't store handle for event queue fd\n");
335 ExitProcess(1);
337 CloseHandle(handle);
341 /***********************************************************************
342 * macdrv_init_thread_data
344 struct macdrv_thread_data *macdrv_init_thread_data(void)
346 struct macdrv_thread_data *data = macdrv_thread_data();
347 TISInputSourceRef input_source;
349 if (data) return data;
351 if (!(data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
353 ERR("could not create data\n");
354 ExitProcess(1);
357 if (!(data->queue = macdrv_create_event_queue(macdrv_handle_event)))
359 ERR("macdrv: Can't create event queue.\n");
360 ExitProcess(1);
363 macdrv_get_input_source_info(&data->keyboard_layout_uchr, &data->keyboard_type, &data->iso_keyboard, &input_source);
364 data->active_keyboard_layout = macdrv_get_hkl_from_source(input_source);
365 CFRelease(input_source);
366 macdrv_compute_keyboard_layout(data);
368 set_queue_display_fd(macdrv_get_event_queue_fd(data->queue));
369 TlsSetValue(thread_data_tls_index, data);
371 return data;
375 /***********************************************************************
376 * DllMain
378 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
380 BOOL ret = TRUE;
382 switch(reason)
384 case DLL_PROCESS_ATTACH:
385 macdrv_module = hinst;
386 ret = process_attach();
387 break;
388 case DLL_THREAD_DETACH:
389 thread_detach();
390 break;
392 return ret;
395 /***********************************************************************
396 * SystemParametersInfo (MACDRV.@)
398 BOOL CDECL macdrv_SystemParametersInfo( UINT action, UINT int_param, void *ptr_param, UINT flags )
400 switch (action)
402 case SPI_GETSCREENSAVEACTIVE:
403 if (ptr_param)
405 CFDictionaryRef assertionStates;
406 IOReturn status = IOPMCopyAssertionsStatus(&assertionStates);
407 if (status == kIOReturnSuccess)
409 CFNumberRef count = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypeNoDisplaySleep);
410 CFNumberRef count2 = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypePreventUserIdleDisplaySleep);
411 long longCount = 0, longCount2 = 0;
413 if (count)
414 CFNumberGetValue(count, kCFNumberLongType, &longCount);
415 if (count2)
416 CFNumberGetValue(count2, kCFNumberLongType, &longCount2);
418 *(BOOL *)ptr_param = !longCount && !longCount2;
419 CFRelease(assertionStates);
421 else
423 WARN("Could not determine screen saver state, error code %d\n", status);
424 *(BOOL *)ptr_param = TRUE;
426 return TRUE;
428 break;
430 case SPI_SETSCREENSAVEACTIVE:
432 static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
433 if (int_param)
435 if (powerAssertion != kIOPMNullAssertionID)
437 IOPMAssertionRelease(powerAssertion);
438 powerAssertion = kIOPMNullAssertionID;
441 else if (powerAssertion == kIOPMNullAssertionID)
443 CFStringRef assertName;
444 /*Are we running Lion or later?*/
445 if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber10_7)
446 assertName = kIOPMAssertionTypePreventUserIdleDisplaySleep;
447 else
448 assertName = kIOPMAssertionTypeNoDisplaySleep;
449 IOPMAssertionCreateWithName( assertName, kIOPMAssertionLevelOn,
450 CFSTR("Wine Process requesting no screen saver"),
451 &powerAssertion);
454 break;
456 return FALSE;