include: Add event trace flags and guid to evntrace.h.
[wine.git] / dlls / winemac.drv / macdrv_main.c
blob38352a82bb6c6bf47be0e6fc661fcd7ca3313c10
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 HMODULE macdrv_module = 0;
63 /**************************************************************************
64 * debugstr_cf
66 const char* debugstr_cf(CFTypeRef t)
68 CFStringRef s;
69 const char* ret;
71 if (!t) return "(null)";
73 if (CFGetTypeID(t) == CFStringGetTypeID())
74 s = t;
75 else
76 s = CFCopyDescription(t);
77 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
78 if (ret) ret = debugstr_a(ret);
79 if (!ret)
81 const UniChar* u = CFStringGetCharactersPtr(s);
82 if (u)
83 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
85 if (!ret)
87 UniChar buf[200];
88 int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0]));
89 CFStringGetCharacters(s, CFRangeMake(0, len), buf);
90 ret = debugstr_wn(buf, len);
92 if (s != t) CFRelease(s);
93 return ret;
97 /***********************************************************************
98 * get_config_key
100 * Get a config key from either the app-specific or the default config
102 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name,
103 char *buffer, DWORD size)
105 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
106 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
107 return ERROR_FILE_NOT_FOUND;
111 /***********************************************************************
112 * setup_options
114 * Set up the Mac driver options.
116 static void setup_options(void)
118 char buffer[MAX_PATH + 16];
119 HKEY hkey, appkey = 0;
120 DWORD len;
122 /* @@ Wine registry key: HKCU\Software\Wine\Mac Driver */
123 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Mac Driver", &hkey)) hkey = 0;
125 /* open the app-specific key */
127 len = GetModuleFileNameA(0, buffer, MAX_PATH);
128 if (len && len < MAX_PATH)
130 HKEY tmpkey;
131 char *p, *appname = buffer;
132 if ((p = strrchr(appname, '/'))) appname = p + 1;
133 if ((p = strrchr(appname, '\\'))) appname = p + 1;
134 strcat(appname, "\\Mac Driver");
135 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mac Driver */
136 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
138 if (RegOpenKeyA(tmpkey, appname, &appkey)) appkey = 0;
139 RegCloseKey(tmpkey);
143 if (!get_config_key(hkey, appkey, "WindowsFloatWhenInactive", buffer, sizeof(buffer)))
145 if (!strcmp(buffer, "none"))
146 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONE;
147 else if (!strcmp(buffer, "all"))
148 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_ALL;
149 else
150 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN;
153 if (!get_config_key(hkey, appkey, "CaptureDisplaysForFullscreen", buffer, sizeof(buffer)))
154 capture_displays_for_fullscreen = IS_OPTION_TRUE(buffer[0]);
156 if (!get_config_key(hkey, appkey, "SkipSingleBufferFlushes", buffer, sizeof(buffer)))
157 skip_single_buffer_flushes = IS_OPTION_TRUE(buffer[0]);
159 if (!get_config_key(hkey, appkey, "AllowVerticalSync", buffer, sizeof(buffer)))
160 allow_vsync = IS_OPTION_TRUE(buffer[0]);
162 if (!get_config_key(hkey, appkey, "AllowSetGamma", buffer, sizeof(buffer)))
163 allow_set_gamma = IS_OPTION_TRUE(buffer[0]);
165 if (!get_config_key(hkey, appkey, "LeftOptionIsAlt", buffer, sizeof(buffer)))
166 left_option_is_alt = IS_OPTION_TRUE(buffer[0]);
167 if (!get_config_key(hkey, appkey, "RightOptionIsAlt", buffer, sizeof(buffer)))
168 right_option_is_alt = IS_OPTION_TRUE(buffer[0]);
170 if (!get_config_key(hkey, appkey, "AllowSoftwareRendering", buffer, sizeof(buffer)))
171 allow_software_rendering = IS_OPTION_TRUE(buffer[0]);
173 /* Value name chosen to match what's used in the X11 driver. */
174 if (!get_config_key(hkey, appkey, "Decorated", buffer, sizeof(buffer)))
175 disable_window_decorations = !IS_OPTION_TRUE(buffer[0]);
177 if (!get_config_key(hkey, appkey, "AllowImmovableWindows", buffer, sizeof(buffer)))
178 allow_immovable_windows = IS_OPTION_TRUE(buffer[0]);
180 if (!get_config_key(hkey, appkey, "CursorClippingLocksWindows", buffer, sizeof(buffer)))
181 cursor_clipping_locks_windows = IS_OPTION_TRUE(buffer[0]);
183 if (!get_config_key(hkey, appkey, "UsePreciseScrolling", buffer, sizeof(buffer)))
184 use_precise_scrolling = IS_OPTION_TRUE(buffer[0]);
186 if (appkey) RegCloseKey(appkey);
187 if (hkey) RegCloseKey(hkey);
191 /***********************************************************************
192 * process_attach
194 static BOOL process_attach(void)
196 SessionAttributeBits attributes;
197 OSStatus status;
199 status = SessionGetInfo(callerSecuritySession, NULL, &attributes);
200 if (status != noErr || !(attributes & sessionHasGraphicAccess))
201 return FALSE;
203 setup_options();
205 if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
207 macdrv_err_on = ERR_ON(macdrv);
208 if (macdrv_start_cocoa_app(GetTickCount64()))
210 ERR("Failed to start Cocoa app main loop\n");
211 return FALSE;
214 macdrv_clipboard_process_attach();
216 return TRUE;
220 /***********************************************************************
221 * thread_detach
223 static void thread_detach(void)
225 struct macdrv_thread_data *data = macdrv_thread_data();
227 if (data)
229 macdrv_destroy_event_queue(data->queue);
230 if (data->keyboard_layout_uchr)
231 CFRelease(data->keyboard_layout_uchr);
232 HeapFree(GetProcessHeap(), 0, data);
233 /* clear data in case we get re-entered from user32 before the thread is truly dead */
234 TlsSetValue(thread_data_tls_index, NULL);
239 /***********************************************************************
240 * set_queue_display_fd
242 * Store the event queue fd into the message queue
244 static void set_queue_display_fd(int fd)
246 HANDLE handle;
247 int ret;
249 if (wine_server_fd_to_handle(fd, GENERIC_READ | SYNCHRONIZE, 0, &handle))
251 MESSAGE("macdrv: Can't allocate handle for event queue fd\n");
252 ExitProcess(1);
254 SERVER_START_REQ(set_queue_fd)
256 req->handle = wine_server_obj_handle(handle);
257 ret = wine_server_call(req);
259 SERVER_END_REQ;
260 if (ret)
262 MESSAGE("macdrv: Can't store handle for event queue fd\n");
263 ExitProcess(1);
265 CloseHandle(handle);
269 /***********************************************************************
270 * macdrv_init_thread_data
272 struct macdrv_thread_data *macdrv_init_thread_data(void)
274 struct macdrv_thread_data *data = macdrv_thread_data();
275 TISInputSourceRef input_source;
277 if (data) return data;
279 if (!(data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
281 ERR("could not create data\n");
282 ExitProcess(1);
285 if (!(data->queue = macdrv_create_event_queue(macdrv_handle_event)))
287 ERR("macdrv: Can't create event queue.\n");
288 ExitProcess(1);
291 macdrv_get_input_source_info(&data->keyboard_layout_uchr, &data->keyboard_type, &data->iso_keyboard, &input_source);
292 data->active_keyboard_layout = macdrv_get_hkl_from_source(input_source);
293 CFRelease(input_source);
294 macdrv_compute_keyboard_layout(data);
296 set_queue_display_fd(macdrv_get_event_queue_fd(data->queue));
297 TlsSetValue(thread_data_tls_index, data);
299 return data;
303 /***********************************************************************
304 * DllMain
306 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
308 BOOL ret = TRUE;
310 switch(reason)
312 case DLL_PROCESS_ATTACH:
313 macdrv_module = hinst;
314 ret = process_attach();
315 break;
316 case DLL_THREAD_DETACH:
317 thread_detach();
318 break;
320 return ret;
323 /***********************************************************************
324 * SystemParametersInfo (MACDRV.@)
326 BOOL CDECL macdrv_SystemParametersInfo( UINT action, UINT int_param, void *ptr_param, UINT flags )
328 switch (action)
330 case SPI_GETSCREENSAVEACTIVE:
331 if (ptr_param)
333 CFDictionaryRef assertionStates;
334 IOReturn status = IOPMCopyAssertionsStatus(&assertionStates);
335 if (status == kIOReturnSuccess)
337 CFNumberRef count = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypeNoDisplaySleep);
338 CFNumberRef count2 = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypePreventUserIdleDisplaySleep);
339 long longCount = 0, longCount2 = 0;
341 if (count)
342 CFNumberGetValue(count, kCFNumberLongType, &longCount);
343 if (count2)
344 CFNumberGetValue(count2, kCFNumberLongType, &longCount2);
346 *(BOOL *)ptr_param = !longCount && !longCount2;
347 CFRelease(assertionStates);
349 else
351 WARN("Could not determine screen saver state, error code %d\n", status);
352 *(BOOL *)ptr_param = TRUE;
354 return TRUE;
356 break;
358 case SPI_SETSCREENSAVEACTIVE:
360 static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
361 if (int_param)
363 if (powerAssertion != kIOPMNullAssertionID)
365 IOPMAssertionRelease(powerAssertion);
366 powerAssertion = kIOPMNullAssertionID;
369 else if (powerAssertion == kIOPMNullAssertionID)
371 CFStringRef assertName;
372 /*Are we running Lion or later?*/
373 if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber10_7)
374 assertName = kIOPMAssertionTypePreventUserIdleDisplaySleep;
375 else
376 assertName = kIOPMAssertionTypeNoDisplaySleep;
377 IOPMAssertionCreateWithName( assertName, kIOPMAssertionLevelOn,
378 CFSTR("Wine Process requesting no screen saver"),
379 &powerAssertion);
382 break;
384 return FALSE;