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;
57 /**************************************************************************
60 const char* debugstr_cf(CFTypeRef t
)
65 if (!t
) return "(null)";
67 if (CFGetTypeID(t
) == CFStringGetTypeID())
70 s
= CFCopyDescription(t
);
71 ret
= CFStringGetCStringPtr(s
, kCFStringEncodingUTF8
);
72 if (ret
) ret
= debugstr_a(ret
);
75 const UniChar
* u
= CFStringGetCharactersPtr(s
);
77 ret
= debugstr_wn((const WCHAR
*)u
, CFStringGetLength(s
));
82 int len
= min(CFStringGetLength(s
), sizeof(buf
)/sizeof(buf
[0]));
83 CFStringGetCharacters(s
, CFRangeMake(0, len
), buf
);
84 ret
= debugstr_wn(buf
, len
);
86 if (s
!= t
) CFRelease(s
);
91 /***********************************************************************
94 static void set_app_icon(void)
96 CFArrayRef images
= create_app_icon_images();
99 macdrv_set_application_icon(images
);
105 /***********************************************************************
108 * Get a config key from either the app-specific or the default config
110 static inline DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
,
111 char *buffer
, DWORD size
)
113 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
114 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
115 return ERROR_FILE_NOT_FOUND
;
119 /***********************************************************************
122 * Set up the Mac driver options.
124 static void setup_options(void)
126 char buffer
[MAX_PATH
+ 16];
127 HKEY hkey
, appkey
= 0;
130 /* @@ Wine registry key: HKCU\Software\Wine\Mac Driver */
131 if (RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Mac Driver", &hkey
)) hkey
= 0;
133 /* open the app-specific key */
135 len
= GetModuleFileNameA(0, buffer
, MAX_PATH
);
136 if (len
&& len
< MAX_PATH
)
139 char *p
, *appname
= buffer
;
140 if ((p
= strrchr(appname
, '/'))) appname
= p
+ 1;
141 if ((p
= strrchr(appname
, '\\'))) appname
= p
+ 1;
142 strcat(appname
, "\\Mac Driver");
143 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mac Driver */
144 if (!RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
146 if (RegOpenKeyA(tmpkey
, appname
, &appkey
)) appkey
= 0;
151 if (!get_config_key(hkey
, appkey
, "WindowsFloatWhenInactive", buffer
, sizeof(buffer
)))
153 if (!strcmp(buffer
, "none"))
154 topmost_float_inactive
= TOPMOST_FLOAT_INACTIVE_NONE
;
155 else if (!strcmp(buffer
, "all"))
156 topmost_float_inactive
= TOPMOST_FLOAT_INACTIVE_ALL
;
158 topmost_float_inactive
= TOPMOST_FLOAT_INACTIVE_NONFULLSCREEN
;
161 if (!get_config_key(hkey
, appkey
, "CaptureDisplaysForFullscreen", buffer
, sizeof(buffer
)))
162 capture_displays_for_fullscreen
= IS_OPTION_TRUE(buffer
[0]);
164 if (!get_config_key(hkey
, appkey
, "SkipSingleBufferFlushes", buffer
, sizeof(buffer
)))
165 skip_single_buffer_flushes
= IS_OPTION_TRUE(buffer
[0]);
167 if (!get_config_key(hkey
, appkey
, "AllowVerticalSync", buffer
, sizeof(buffer
)))
168 allow_vsync
= IS_OPTION_TRUE(buffer
[0]);
170 if (!get_config_key(hkey
, appkey
, "AllowSetGamma", buffer
, sizeof(buffer
)))
171 allow_set_gamma
= IS_OPTION_TRUE(buffer
[0]);
173 if (!get_config_key(hkey
, appkey
, "LeftOptionIsAlt", buffer
, sizeof(buffer
)))
174 left_option_is_alt
= IS_OPTION_TRUE(buffer
[0]);
175 if (!get_config_key(hkey
, appkey
, "RightOptionIsAlt", buffer
, sizeof(buffer
)))
176 right_option_is_alt
= IS_OPTION_TRUE(buffer
[0]);
178 if (appkey
) RegCloseKey(appkey
);
179 if (hkey
) RegCloseKey(hkey
);
183 /***********************************************************************
186 static BOOL
process_attach( HINSTANCE instance
)
188 SessionAttributeBits attributes
;
191 status
= SessionGetInfo(callerSecuritySession
, NULL
, &attributes
);
192 if (status
!= noErr
|| !(attributes
& sessionHasGraphicAccess
))
197 if ((thread_data_tls_index
= TlsAlloc()) == TLS_OUT_OF_INDEXES
) return FALSE
;
199 macdrv_err_on
= ERR_ON(macdrv
);
200 if (macdrv_start_cocoa_app(GetTickCount64()))
202 ERR("Failed to start Cocoa app main loop\n");
207 macdrv_clipboard_process_attach();
208 IME_RegisterClasses( instance
);
214 /***********************************************************************
217 static void thread_detach(void)
219 struct macdrv_thread_data
*data
= macdrv_thread_data();
223 macdrv_destroy_event_queue(data
->queue
);
224 if (data
->keyboard_layout_uchr
)
225 CFRelease(data
->keyboard_layout_uchr
);
226 HeapFree(GetProcessHeap(), 0, data
);
231 /***********************************************************************
232 * set_queue_display_fd
234 * Store the event queue fd into the message queue
236 static void set_queue_display_fd(int fd
)
241 if (wine_server_fd_to_handle(fd
, GENERIC_READ
| SYNCHRONIZE
, 0, &handle
))
243 MESSAGE("macdrv: Can't allocate handle for event queue fd\n");
246 SERVER_START_REQ(set_queue_fd
)
248 req
->handle
= wine_server_obj_handle(handle
);
249 ret
= wine_server_call(req
);
254 MESSAGE("macdrv: Can't store handle for event queue fd\n");
261 /***********************************************************************
262 * macdrv_init_thread_data
264 struct macdrv_thread_data
*macdrv_init_thread_data(void)
266 struct macdrv_thread_data
*data
= macdrv_thread_data();
268 if (data
) return data
;
270 if (!(data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
))))
272 ERR("could not create data\n");
276 if (!(data
->queue
= macdrv_create_event_queue(macdrv_handle_event
)))
278 ERR("macdrv: Can't create event queue.\n");
282 data
->keyboard_layout_uchr
= macdrv_copy_keyboard_layout(&data
->keyboard_type
, &data
->iso_keyboard
);
283 macdrv_compute_keyboard_layout(data
);
285 set_queue_display_fd(macdrv_get_event_queue_fd(data
->queue
));
286 TlsSetValue(thread_data_tls_index
, data
);
292 /***********************************************************************
295 BOOL WINAPI
DllMain(HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
301 case DLL_PROCESS_ATTACH
:
302 ret
= process_attach( hinst
);
304 case DLL_THREAD_DETACH
:
311 /***********************************************************************
312 * SystemParametersInfo (MACDRV.@)
314 BOOL CDECL
macdrv_SystemParametersInfo( UINT action
, UINT int_param
, void *ptr_param
, UINT flags
)
318 case SPI_GETSCREENSAVEACTIVE
:
321 CFDictionaryRef assertionStates
;
322 IOReturn status
= IOPMCopyAssertionsStatus(&assertionStates
);
323 if (status
== kIOReturnSuccess
)
325 CFNumberRef count
= CFDictionaryGetValue(assertionStates
, kIOPMAssertionTypeNoDisplaySleep
);
326 CFNumberRef count2
= CFDictionaryGetValue(assertionStates
, kIOPMAssertionTypePreventUserIdleDisplaySleep
);
327 long longCount
= 0, longCount2
= 0;
330 CFNumberGetValue(count
, kCFNumberLongType
, &longCount
);
332 CFNumberGetValue(count2
, kCFNumberLongType
, &longCount2
);
334 *(BOOL
*)ptr_param
= !longCount
&& !longCount2
;
335 CFRelease(assertionStates
);
339 WARN("Could not determine screen saver state, error code %d\n", status
);
340 *(BOOL
*)ptr_param
= TRUE
;
346 case SPI_SETSCREENSAVEACTIVE
:
348 static IOPMAssertionID powerAssertion
= kIOPMNullAssertionID
;
351 if (powerAssertion
!= kIOPMNullAssertionID
)
353 IOPMAssertionRelease(powerAssertion
);
354 powerAssertion
= kIOPMNullAssertionID
;
357 else if (powerAssertion
== kIOPMNullAssertionID
)
359 CFStringRef assertName
;
360 /*Are we running Lion or later?*/
361 if (kCFCoreFoundationVersionNumber
>= kCFCoreFoundationVersionNumber10_7
)
362 assertName
= kIOPMAssertionTypePreventUserIdleDisplaySleep
;
364 assertName
= kIOPMAssertionTypeNoDisplaySleep
;
365 IOPMAssertionCreateWithName( assertName
, kIOPMAssertionLevelOn
,
366 CFSTR("Wine Process requesting no screen saver"),