winex11: Keep the result string in the IME UI window procedure.
[wine.git] / dlls / winemac.drv / macdrv_main.c
blob491ab06ac34e15ae5b9c316a8a3658da69dca2e5
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 return TRUE;
290 /***********************************************************************
291 * ThreadDetach (MACDRV.@)
293 void CDECL macdrv_ThreadDetach(void)
295 struct macdrv_thread_data *data = macdrv_thread_data();
297 if (data)
299 macdrv_destroy_event_queue(data->queue);
300 if (data->keyboard_layout_uchr)
301 CFRelease(data->keyboard_layout_uchr);
302 HeapFree(GetProcessHeap(), 0, data);
303 /* clear data in case we get re-entered from user32 before the thread is truly dead */
304 TlsSetValue(thread_data_tls_index, NULL);
309 /***********************************************************************
310 * set_queue_display_fd
312 * Store the event queue fd into the message queue
314 static void set_queue_display_fd(int fd)
316 HANDLE handle;
317 int ret;
319 if (wine_server_fd_to_handle(fd, GENERIC_READ | SYNCHRONIZE, 0, &handle))
321 MESSAGE("macdrv: Can't allocate handle for event queue fd\n");
322 ExitProcess(1);
324 SERVER_START_REQ(set_queue_fd)
326 req->handle = wine_server_obj_handle(handle);
327 ret = wine_server_call(req);
329 SERVER_END_REQ;
330 if (ret)
332 MESSAGE("macdrv: Can't store handle for event queue fd\n");
333 ExitProcess(1);
335 CloseHandle(handle);
339 /***********************************************************************
340 * macdrv_init_thread_data
342 struct macdrv_thread_data *macdrv_init_thread_data(void)
344 struct macdrv_thread_data *data = macdrv_thread_data();
345 TISInputSourceRef input_source;
347 if (data) return data;
349 if (!(data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
351 ERR("could not create data\n");
352 ExitProcess(1);
355 if (!(data->queue = macdrv_create_event_queue(macdrv_handle_event)))
357 ERR("macdrv: Can't create event queue.\n");
358 ExitProcess(1);
361 macdrv_get_input_source_info(&data->keyboard_layout_uchr, &data->keyboard_type, &data->iso_keyboard, &input_source);
362 data->active_keyboard_layout = macdrv_get_hkl_from_source(input_source);
363 CFRelease(input_source);
364 macdrv_compute_keyboard_layout(data);
366 set_queue_display_fd(macdrv_get_event_queue_fd(data->queue));
367 TlsSetValue(thread_data_tls_index, data);
369 return data;
373 /***********************************************************************
374 * DllMain
376 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
378 BOOL ret = TRUE;
380 switch(reason)
382 case DLL_PROCESS_ATTACH:
383 DisableThreadLibraryCalls( hinst );
384 macdrv_module = hinst;
385 ret = process_attach();
386 break;
388 return ret;
391 /***********************************************************************
392 * SystemParametersInfo (MACDRV.@)
394 BOOL CDECL macdrv_SystemParametersInfo( UINT action, UINT int_param, void *ptr_param, UINT flags )
396 switch (action)
398 case SPI_GETSCREENSAVEACTIVE:
399 if (ptr_param)
401 CFDictionaryRef assertionStates;
402 IOReturn status = IOPMCopyAssertionsStatus(&assertionStates);
403 if (status == kIOReturnSuccess)
405 CFNumberRef count = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypeNoDisplaySleep);
406 CFNumberRef count2 = CFDictionaryGetValue(assertionStates, kIOPMAssertionTypePreventUserIdleDisplaySleep);
407 long longCount = 0, longCount2 = 0;
409 if (count)
410 CFNumberGetValue(count, kCFNumberLongType, &longCount);
411 if (count2)
412 CFNumberGetValue(count2, kCFNumberLongType, &longCount2);
414 *(BOOL *)ptr_param = !longCount && !longCount2;
415 CFRelease(assertionStates);
417 else
419 WARN("Could not determine screen saver state, error code %d\n", status);
420 *(BOOL *)ptr_param = TRUE;
422 return TRUE;
424 break;
426 case SPI_SETSCREENSAVEACTIVE:
428 static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
429 if (int_param)
431 if (powerAssertion != kIOPMNullAssertionID)
433 IOPMAssertionRelease(powerAssertion);
434 powerAssertion = kIOPMNullAssertionID;
437 else if (powerAssertion == kIOPMNullAssertionID)
439 CFStringRef assertName;
440 /*Are we running Lion or later?*/
441 if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber10_7)
442 assertName = kIOPMAssertionTypePreventUserIdleDisplaySleep;
443 else
444 assertName = kIOPMAssertionTypeNoDisplaySleep;
445 IOPMAssertionCreateWithName( assertName, kIOPMAssertionLevelOn,
446 CFSTR("Wine Process requesting no screen saver"),
447 &powerAssertion);
450 break;
452 return FALSE;