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
24 #include <Security/AuthSession.h>
25 #include <IOKit/pwr_mgt/IOPMLib.h>
30 #include "wine/server.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(macdrv
);
34 #ifndef kIOPMAssertionTypePreventUserIdleDisplaySleep
35 #define kIOPMAssertionTypePreventUserIdleDisplaySleep CFSTR("PreventUserIdleDisplaySleep")
37 #ifndef kCFCoreFoundationVersionNumber10_7
38 #define kCFCoreFoundationVersionNumber10_7 635.00
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 /**************************************************************************
69 const char* debugstr_cf(CFTypeRef t
)
74 if (!t
) return "(null)";
76 if (CFGetTypeID(t
) == CFStringGetTypeID())
79 s
= CFCopyDescription(t
);
80 ret
= CFStringGetCStringPtr(s
, kCFStringEncodingUTF8
);
81 if (ret
) ret
= debugstr_a(ret
);
84 const UniChar
* u
= CFStringGetCharactersPtr(s
);
86 ret
= debugstr_wn((const WCHAR
*)u
, CFStringGetLength(s
));
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
);
100 /***********************************************************************
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 /***********************************************************************
117 * Set up the Mac driver options.
119 static void setup_options(void)
121 char buffer
[MAX_PATH
+ 16];
122 HKEY hkey
, appkey
= 0;
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
)
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;
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
;
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
;
196 gl_surface_mode
= GL_SURFACE_IN_FRONT_OPAQUE
;
199 if (appkey
) RegCloseKey(appkey
);
200 if (hkey
) RegCloseKey(hkey
);
204 /***********************************************************************
207 static void load_strings(HINSTANCE instance
)
209 static const unsigned int ids
[] = {
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
,
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
;
227 dict
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
,
228 &kCFTypeDictionaryValueCallBacks
);
231 ERR("Failed to create localized strings dictionary\n");
235 for (i
= 0; i
< sizeof(ids
) / sizeof(ids
[0]); i
++)
238 int len
= LoadStringW(instance
, ids
[i
], (LPWSTR
)&str
, 0);
241 CFNumberRef key
= CFNumberCreate(NULL
, kCFNumberIntType
, &ids
[i
]);
242 CFStringRef value
= CFStringCreateWithCharacters(NULL
, (UniChar
*)str
, len
);
244 CFDictionarySetValue(dict
, key
, value
);
246 ERR("Failed to add string ID 0x%04x %s\n", ids
[i
], debugstr_wn(str
, len
));
249 ERR("Failed to load string ID 0x%04x\n", ids
[i
]);
252 localized_strings
= dict
;
256 /***********************************************************************
259 static BOOL
process_attach(void)
261 SessionAttributeBits attributes
;
264 status
= SessionGetInfo(callerSecuritySession
, NULL
, &attributes
);
265 if (status
!= noErr
|| !(attributes
& sessionHasGraphicAccess
))
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");
280 macdrv_clipboard_process_attach();
286 /***********************************************************************
289 static void thread_detach(void)
291 struct macdrv_thread_data
*data
= macdrv_thread_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
)
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");
320 SERVER_START_REQ(set_queue_fd
)
322 req
->handle
= wine_server_obj_handle(handle
);
323 ret
= wine_server_call(req
);
328 MESSAGE("macdrv: Can't store handle for event queue fd\n");
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");
351 if (!(data
->queue
= macdrv_create_event_queue(macdrv_handle_event
)))
353 ERR("macdrv: Can't create event queue.\n");
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
);
369 /***********************************************************************
372 BOOL WINAPI
DllMain(HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
378 case DLL_PROCESS_ATTACH
:
379 macdrv_module
= hinst
;
380 ret
= process_attach();
382 case DLL_THREAD_DETACH
:
389 /***********************************************************************
390 * SystemParametersInfo (MACDRV.@)
392 BOOL CDECL
macdrv_SystemParametersInfo( UINT action
, UINT int_param
, void *ptr_param
, UINT flags
)
396 case SPI_GETSCREENSAVEACTIVE
:
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;
408 CFNumberGetValue(count
, kCFNumberLongType
, &longCount
);
410 CFNumberGetValue(count2
, kCFNumberLongType
, &longCount2
);
412 *(BOOL
*)ptr_param
= !longCount
&& !longCount2
;
413 CFRelease(assertionStates
);
417 WARN("Could not determine screen saver state, error code %d\n", status
);
418 *(BOOL
*)ptr_param
= TRUE
;
424 case SPI_SETSCREENSAVEACTIVE
:
426 static IOPMAssertionID powerAssertion
= kIOPMNullAssertionID
;
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
;
442 assertName
= kIOPMAssertionTypeNoDisplaySleep
;
443 IOPMAssertionCreateWithName( assertName
, kIOPMAssertionLevelOn
,
444 CFSTR("Wine Process requesting no screen saver"),