makefiles: Generate dependencies for static libraries.
[wine.git] / dlls / winemac.drv / macdrv_main.c
blob5af0f411409b9630e9ed1c38fdb8bdc29a62d175
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;
63 CFDictionaryRef localized_strings;
66 /**************************************************************************
67 * debugstr_cf
69 const char* debugstr_cf(CFTypeRef t)
71 CFStringRef s;
72 const char* ret;
74 if (!t) return "(null)";
76 if (CFGetTypeID(t) == CFStringGetTypeID())
77 s = t;
78 else
79 s = CFCopyDescription(t);
80 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
81 if (ret) ret = debugstr_a(ret);
82 if (!ret)
84 const UniChar* u = CFStringGetCharactersPtr(s);
85 if (u)
86 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
88 if (!ret)
90 UniChar buf[200];
91 int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0]));
92 CFStringGetCharacters(s, CFRangeMake(0, len), buf);
93 ret = debugstr_wn(buf, len);
95 if (s != t) CFRelease(s);
96 return ret;
100 /***********************************************************************
101 * get_config_key
103 * Get a config key from either the app-specific or the default config
105 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name,
106 char *buffer, DWORD size)
108 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
109 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (LPBYTE)buffer, &size)) return 0;
110 return ERROR_FILE_NOT_FOUND;
114 /***********************************************************************
115 * setup_options
117 * Set up the Mac driver options.
119 static void setup_options(void)
121 char buffer[MAX_PATH + 16];
122 HKEY hkey, appkey = 0;
123 DWORD len;
125 /* @@ Wine registry key: HKCU\Software\Wine\Mac Driver */
126 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Mac Driver", &hkey)) hkey = 0;
128 /* open the app-specific key */
130 len = GetModuleFileNameA(0, buffer, MAX_PATH);
131 if (len && len < MAX_PATH)
133 HKEY tmpkey;
134 char *p, *appname = buffer;
135 if ((p = strrchr(appname, '/'))) appname = p + 1;
136 if ((p = strrchr(appname, '\\'))) appname = p + 1;
137 strcat(appname, "\\Mac Driver");
138 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mac Driver */
139 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
141 if (RegOpenKeyA(tmpkey, appname, &appkey)) appkey = 0;
142 RegCloseKey(tmpkey);
146 if (!get_config_key(hkey, appkey, "WindowsFloatWhenInactive", buffer, sizeof(buffer)))
148 if (!strcmp(buffer, "none"))
149 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONE;
150 else if (!strcmp(buffer, "all"))
151 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_ALL;
152 else
153 topmost_float_inactive = TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN;
156 if (!get_config_key(hkey, appkey, "CaptureDisplaysForFullscreen", buffer, sizeof(buffer)))
157 capture_displays_for_fullscreen = IS_OPTION_TRUE(buffer[0]);
159 if (!get_config_key(hkey, appkey, "SkipSingleBufferFlushes", buffer, sizeof(buffer)))
160 skip_single_buffer_flushes = IS_OPTION_TRUE(buffer[0]);
162 if (!get_config_key(hkey, appkey, "AllowVerticalSync", buffer, sizeof(buffer)))
163 allow_vsync = IS_OPTION_TRUE(buffer[0]);
165 if (!get_config_key(hkey, appkey, "AllowSetGamma", buffer, sizeof(buffer)))
166 allow_set_gamma = IS_OPTION_TRUE(buffer[0]);
168 if (!get_config_key(hkey, appkey, "LeftOptionIsAlt", buffer, sizeof(buffer)))
169 left_option_is_alt = IS_OPTION_TRUE(buffer[0]);
170 if (!get_config_key(hkey, appkey, "RightOptionIsAlt", buffer, sizeof(buffer)))
171 right_option_is_alt = IS_OPTION_TRUE(buffer[0]);
173 if (!get_config_key(hkey, appkey, "AllowSoftwareRendering", buffer, sizeof(buffer)))
174 allow_software_rendering = IS_OPTION_TRUE(buffer[0]);
176 /* Value name chosen to match what's used in the X11 driver. */
177 if (!get_config_key(hkey, appkey, "Decorated", buffer, sizeof(buffer)))
178 disable_window_decorations = !IS_OPTION_TRUE(buffer[0]);
180 if (!get_config_key(hkey, appkey, "AllowImmovableWindows", buffer, sizeof(buffer)))
181 allow_immovable_windows = IS_OPTION_TRUE(buffer[0]);
183 if (!get_config_key(hkey, appkey, "CursorClippingLocksWindows", buffer, sizeof(buffer)))
184 cursor_clipping_locks_windows = IS_OPTION_TRUE(buffer[0]);
186 if (!get_config_key(hkey, appkey, "UsePreciseScrolling", buffer, sizeof(buffer)))
187 use_precise_scrolling = IS_OPTION_TRUE(buffer[0]);
189 if (!get_config_key(hkey, appkey, "OpenGLSurfaceMode", buffer, sizeof(buffer)))
191 if (!strcmp(buffer, "transparent"))
192 gl_surface_mode = GL_SURFACE_IN_FRONT_TRANSPARENT;
193 else if (!strcmp(buffer, "behind"))
194 gl_surface_mode = GL_SURFACE_BEHIND;
195 else
196 gl_surface_mode = GL_SURFACE_IN_FRONT_OPAQUE;
199 if (appkey) RegCloseKey(appkey);
200 if (hkey) RegCloseKey(hkey);
204 /***********************************************************************
205 * load_strings
207 static void load_strings(HINSTANCE instance)
209 static const unsigned int ids[] = {
210 STRING_MENU_WINE,
211 STRING_MENU_ITEM_HIDE_APPNAME,
212 STRING_MENU_ITEM_HIDE,
213 STRING_MENU_ITEM_HIDE_OTHERS,
214 STRING_MENU_ITEM_SHOW_ALL,
215 STRING_MENU_ITEM_QUIT_APPNAME,
216 STRING_MENU_ITEM_QUIT,
218 STRING_MENU_WINDOW,
219 STRING_MENU_ITEM_MINIMIZE,
220 STRING_MENU_ITEM_ZOOM,
221 STRING_MENU_ITEM_ENTER_FULL_SCREEN,
222 STRING_MENU_ITEM_BRING_ALL_TO_FRONT,
224 CFMutableDictionaryRef dict;
225 int i;
227 dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,
228 &kCFTypeDictionaryValueCallBacks);
229 if (!dict)
231 ERR("Failed to create localized strings dictionary\n");
232 return;
235 for (i = 0; i < sizeof(ids) / sizeof(ids[0]); i++)
237 LPCWSTR str;
238 int len = LoadStringW(instance, ids[i], (LPWSTR)&str, 0);
239 if (str && len)
241 CFNumberRef key = CFNumberCreate(NULL, kCFNumberIntType, &ids[i]);
242 CFStringRef value = CFStringCreateWithCharacters(NULL, (UniChar*)str, len);
243 if (key && value)
244 CFDictionarySetValue(dict, key, value);
245 else
246 ERR("Failed to add string ID 0x%04x %s\n", ids[i], debugstr_wn(str, len));
248 else
249 ERR("Failed to load string ID 0x%04x\n", ids[i]);
252 localized_strings = dict;
256 /***********************************************************************
257 * process_attach
259 static BOOL process_attach(void)
261 SessionAttributeBits attributes;
262 OSStatus status;
264 status = SessionGetInfo(callerSecuritySession, NULL, &attributes);
265 if (status != noErr || !(attributes & sessionHasGraphicAccess))
266 return FALSE;
268 setup_options();
269 load_strings(macdrv_module);
271 if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
273 macdrv_err_on = ERR_ON(macdrv);
274 if (macdrv_start_cocoa_app(GetTickCount64()))
276 ERR("Failed to start Cocoa app main loop\n");
277 return FALSE;
280 macdrv_clipboard_process_attach();
282 return TRUE;
286 /***********************************************************************
287 * thread_detach
289 static void thread_detach(void)
291 struct macdrv_thread_data *data = macdrv_thread_data();
293 if (data)
295 macdrv_destroy_event_queue(data->queue);
296 if (data->keyboard_layout_uchr)
297 CFRelease(data->keyboard_layout_uchr);
298 HeapFree(GetProcessHeap(), 0, data);
299 /* clear data in case we get re-entered from user32 before the thread is truly dead */
300 TlsSetValue(thread_data_tls_index, NULL);
305 /***********************************************************************
306 * set_queue_display_fd
308 * Store the event queue fd into the message queue
310 static void set_queue_display_fd(int fd)
312 HANDLE handle;
313 int ret;
315 if (wine_server_fd_to_handle(fd, GENERIC_READ | SYNCHRONIZE, 0, &handle))
317 MESSAGE("macdrv: Can't allocate handle for event queue fd\n");
318 ExitProcess(1);
320 SERVER_START_REQ(set_queue_fd)
322 req->handle = wine_server_obj_handle(handle);
323 ret = wine_server_call(req);
325 SERVER_END_REQ;
326 if (ret)
328 MESSAGE("macdrv: Can't store handle for event queue fd\n");
329 ExitProcess(1);
331 CloseHandle(handle);
335 /***********************************************************************
336 * macdrv_init_thread_data
338 struct macdrv_thread_data *macdrv_init_thread_data(void)
340 struct macdrv_thread_data *data = macdrv_thread_data();
341 TISInputSourceRef input_source;
343 if (data) return data;
345 if (!(data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
347 ERR("could not create data\n");
348 ExitProcess(1);
351 if (!(data->queue = macdrv_create_event_queue(macdrv_handle_event)))
353 ERR("macdrv: Can't create event queue.\n");
354 ExitProcess(1);
357 macdrv_get_input_source_info(&data->keyboard_layout_uchr, &data->keyboard_type, &data->iso_keyboard, &input_source);
358 data->active_keyboard_layout = macdrv_get_hkl_from_source(input_source);
359 CFRelease(input_source);
360 macdrv_compute_keyboard_layout(data);
362 set_queue_display_fd(macdrv_get_event_queue_fd(data->queue));
363 TlsSetValue(thread_data_tls_index, data);
365 return data;
369 /***********************************************************************
370 * DllMain
372 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
374 BOOL ret = TRUE;
376 switch(reason)
378 case DLL_PROCESS_ATTACH:
379 macdrv_module = hinst;
380 ret = process_attach();
381 break;
382 case DLL_THREAD_DETACH:
383 thread_detach();
384 break;
386 return ret;
389 /***********************************************************************
390 * SystemParametersInfo (MACDRV.@)
392 BOOL CDECL macdrv_SystemParametersInfo( UINT action, UINT int_param, void *ptr_param, UINT flags )
394 switch (action)
396 case SPI_GETSCREENSAVEACTIVE:
397 if (ptr_param)
399 CFDictionaryRef assertionStates;
400 IOReturn status = IOPMCopyAssertionsStatus(&assertionStates);
401 if (status == kIOReturnSuccess)
403 CFNumberRef count = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypeNoDisplaySleep);
404 CFNumberRef count2 = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypePreventUserIdleDisplaySleep);
405 long longCount = 0, longCount2 = 0;
407 if (count)
408 CFNumberGetValue(count, kCFNumberLongType, &longCount);
409 if (count2)
410 CFNumberGetValue(count2, kCFNumberLongType, &longCount2);
412 *(BOOL *)ptr_param = !longCount && !longCount2;
413 CFRelease(assertionStates);
415 else
417 WARN("Could not determine screen saver state, error code %d\n", status);
418 *(BOOL *)ptr_param = TRUE;
420 return TRUE;
422 break;
424 case SPI_SETSCREENSAVEACTIVE:
426 static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
427 if (int_param)
429 if (powerAssertion != kIOPMNullAssertionID)
431 IOPMAssertionRelease(powerAssertion);
432 powerAssertion = kIOPMNullAssertionID;
435 else if (powerAssertion == kIOPMNullAssertionID)
437 CFStringRef assertName;
438 /*Are we running Lion or later?*/
439 if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber10_7)
440 assertName = kIOPMAssertionTypePreventUserIdleDisplaySleep;
441 else
442 assertName = kIOPMAssertionTypeNoDisplaySleep;
443 IOPMAssertionCreateWithName( assertName, kIOPMAssertionLevelOn,
444 CFSTR("Wine Process requesting no screen saver"),
445 &powerAssertion);
448 break;
450 return FALSE;