wined3d: Get rid of the "render_to_fbo" field from the wined3d_swapchain structure.
[wine.git] / dlls / mountmgr.sys / diskarb.c
blobe33365f6301ab9ebb5e718e5dc9737e064135038
1 /*
2 * Devices support using the MacOS Disk Arbitration library.
4 * Copyright 2006 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
31 #endif
32 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
33 #include <DiskArbitration/DiskArbitration.h>
34 #endif
35 #if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
36 #include <SystemConfiguration/SCDynamicStoreCopyDHCPInfo.h>
37 #include <SystemConfiguration/SCNetworkConfiguration.h>
38 #endif
40 #include "mountmgr.h"
41 #define USE_WS_PREFIX
42 #include "winsock2.h"
43 #include "ws2ipdef.h"
44 #include "nldef.h"
45 #include "netioapi.h"
46 #include "dhcpcsdk.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
51 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
53 typedef struct
55 uint64_t bus;
56 uint64_t port;
57 uint64_t target;
58 uint64_t lun;
59 } dk_scsi_identify_t;
61 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
63 static void appeared_callback( DADiskRef disk, void *context )
65 CFDictionaryRef dict = DADiskCopyDescription( disk );
66 const void *ref;
67 char device[64];
68 char mount_point[PATH_MAX];
69 char model[64];
70 size_t model_len = 0;
71 GUID guid, *guid_ptr = NULL;
72 enum device_type type = DEVICE_UNKNOWN;
73 UINT pdtype = 0;
74 SCSI_ADDRESS scsi_addr;
75 UNICODE_STRING devname;
76 int fd;
78 if (!dict) return;
80 if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumeUUID") )))
82 CFUUIDBytes bytes = CFUUIDGetUUIDBytes( ref );
83 memcpy( &guid, &bytes, sizeof(guid) );
84 guid_ptr = &guid;
87 /* get device name */
88 if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) goto done;
89 strcpy( device, "/dev/r" );
90 CFStringGetCString( ref, device + 6, sizeof(device) - 6, kCFStringEncodingASCII );
92 if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumePath") )))
93 CFURLGetFileSystemRepresentation( ref, true, (UInt8 *)mount_point, sizeof(mount_point) );
94 else
95 mount_point[0] = 0;
97 if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaKind") )))
99 if (!CFStringCompare( ref, CFSTR("IOCDMedia"), 0 ))
101 type = DEVICE_CDROM;
102 pdtype = 5;
104 if (!CFStringCompare( ref, CFSTR("IODVDMedia"), 0 ) ||
105 !CFStringCompare( ref, CFSTR("IOBDMedia"), 0 ))
107 type = DEVICE_DVD;
108 pdtype = 5;
110 if (!CFStringCompare( ref, CFSTR("IOMedia"), 0 ))
111 type = DEVICE_HARDDISK;
114 if ((ref = CFDictionaryGetValue( dict, CFSTR("DADeviceVendor") )))
116 CFIndex i;
118 CFStringGetCString( ref, model, sizeof(model), kCFStringEncodingASCII );
119 model_len += CFStringGetLength( ref );
120 /* Pad to 8 characters */
121 for (i = 0; i < (CFIndex)8 - CFStringGetLength( ref ); ++i)
122 model[model_len++] = ' ';
124 if ((ref = CFDictionaryGetValue( dict, CFSTR("DADeviceModel") )))
126 CFIndex i;
128 CFStringGetCString( ref, model+model_len, sizeof(model)-model_len, kCFStringEncodingASCII );
129 model_len += CFStringGetLength( ref );
130 /* Pad to 16 characters */
131 for (i = 0; i < (CFIndex)16 - CFStringGetLength( ref ); ++i)
132 model[model_len++] = ' ';
134 if ((ref = CFDictionaryGetValue( dict, CFSTR("DADeviceRevision") )))
136 CFIndex i;
138 CFStringGetCString( ref, model+model_len, sizeof(model)-model_len, kCFStringEncodingASCII );
139 model_len += CFStringGetLength( ref );
140 /* Pad to 4 characters */
141 for (i = 0; i < (CFIndex)4 - CFStringGetLength( ref ); ++i)
142 model[model_len++] = ' ';
145 TRACE( "got mount notification for '%s' on '%s' uuid %s\n",
146 device, mount_point, wine_dbgstr_guid(guid_ptr) );
148 devname.Buffer = NULL;
149 if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaRemovable") )) && CFBooleanGetValue( ref ))
150 add_dos_device( -1, device, device, mount_point, type, guid_ptr, &devname );
151 else
152 if (guid_ptr) add_volume( device, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr, NULL );
154 if (!access( device, R_OK ) &&
155 (fd = open( device, O_RDONLY )) >= 0)
157 dk_scsi_identify_t dsi;
159 if (ioctl( fd, DKIOCSCSIIDENTIFY, &dsi ) >= 0)
161 scsi_addr.PortNumber = dsi.bus;
162 scsi_addr.PathId = dsi.port;
163 scsi_addr.TargetId = dsi.target;
164 scsi_addr.Lun = dsi.lun;
166 /* FIXME: get real controller Id for SCSI */
167 /* FIXME: get real driver name */
168 create_scsi_entry( &scsi_addr, 255, "WINE SCSI", pdtype, model, &devname );
170 close( fd );
173 done:
174 CFRelease( dict );
177 static void changed_callback( DADiskRef disk, CFArrayRef keys, void *context )
179 appeared_callback( disk, context );
182 static void disappeared_callback( DADiskRef disk, void *context )
184 CFDictionaryRef dict = DADiskCopyDescription( disk );
185 const void *ref;
186 char device[100];
188 if (!dict) return;
190 /* get device name */
191 if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) goto done;
192 strcpy( device, "/dev/r" );
193 CFStringGetCString( ref, device + 6, sizeof(device) - 6, kCFStringEncodingASCII );
195 TRACE( "got unmount notification for '%s'\n", device );
197 if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaRemovable") )) && CFBooleanGetValue( ref ))
198 remove_dos_device( -1, device );
199 else
200 remove_volume( device );
202 done:
203 CFRelease( dict );
206 static DWORD WINAPI runloop_thread( void *arg )
208 DASessionRef session = DASessionCreate( NULL );
210 if (!session) return 1;
212 DASessionScheduleWithRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
213 DARegisterDiskAppearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
214 appeared_callback, NULL );
215 DARegisterDiskDisappearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
216 disappeared_callback, NULL );
217 DARegisterDiskDescriptionChangedCallback( session, kDADiskDescriptionMatchVolumeMountable,
218 kDADiskDescriptionWatchVolumePath, changed_callback, NULL );
219 CFRunLoopRun();
220 DASessionUnscheduleFromRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
221 CFRelease( session );
222 return 0;
225 void initialize_diskarbitration(void)
227 HANDLE handle;
229 if (!(handle = CreateThread( NULL, 0, runloop_thread, NULL, 0, NULL ))) return;
230 CloseHandle( handle );
233 #else /* HAVE_DISKARBITRATION_DISKARBITRATION_H */
235 void initialize_diskarbitration(void)
237 TRACE( "Skipping, Disk Arbitration support not compiled in\n" );
240 #endif /* HAVE_DISKARBITRATION_DISKARBITRATION_H */
242 #if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
244 static UInt8 map_option( ULONG option )
246 switch (option)
248 case OPTION_SUBNET_MASK: return 1;
249 case OPTION_ROUTER_ADDRESS: return 3;
250 case OPTION_HOST_NAME: return 12;
251 case OPTION_DOMAIN_NAME: return 15;
252 case OPTION_BROADCAST_ADDRESS: return 28;
253 case OPTION_MSFT_IE_PROXY: return 252;
254 default:
255 FIXME( "unhandled option %u\n", option );
256 return 0;
260 #define IF_NAMESIZE 16
261 static BOOL map_adapter_name( const NET_LUID *luid, WCHAR *unix_name, DWORD len )
263 return !ConvertInterfaceLuidToAlias( luid, unix_name, len );
266 static CFStringRef find_service_id( const NET_LUID *adapter )
268 SCPreferencesRef prefs;
269 SCNetworkSetRef set = NULL;
270 CFArrayRef services = NULL;
271 CFStringRef id, ret = NULL;
272 WCHAR unix_name[IF_NAMESIZE];
273 CFIndex i;
275 if (!map_adapter_name( adapter, unix_name, ARRAY_SIZE(unix_name) )) return NULL;
276 if (!(prefs = SCPreferencesCreate( NULL, CFSTR("mountmgr.sys"), NULL ))) return NULL;
277 if (!(set = SCNetworkSetCopyCurrent( prefs ))) goto done;
278 if (!(services = SCNetworkSetCopyServices( set ))) goto done;
280 for (i = 0; i < CFArrayGetCount( services ); i++)
282 SCNetworkServiceRef service;
283 UniChar buf[IF_NAMESIZE] = {0};
284 CFStringRef name;
286 service = CFArrayGetValueAtIndex( services, i );
287 name = SCNetworkInterfaceGetBSDName( SCNetworkServiceGetInterface(service) );
288 if (name && CFStringGetLength( name ) < ARRAY_SIZE( buf ))
290 CFStringGetCharacters( name, CFRangeMake(0, CFStringGetLength(name)), buf );
291 if (!lstrcmpW( buf, unix_name ) && (id = SCNetworkServiceGetServiceID( service )))
293 ret = CFStringCreateCopy( NULL, id );
294 break;
299 done:
300 if (services) CFRelease( services );
301 if (set) CFRelease( set );
302 CFRelease( prefs );
303 return ret;
306 ULONG get_dhcp_request_param( const NET_LUID *adapter, struct mountmgr_dhcp_request_param *param, char *buf, ULONG offset,
307 ULONG size )
309 CFStringRef service_id = find_service_id( adapter );
310 CFDictionaryRef dict;
311 CFDataRef value;
312 DWORD ret = 0;
313 CFIndex len;
315 param->offset = 0;
316 param->size = 0;
318 if (!service_id) return 0;
319 if (!(dict = SCDynamicStoreCopyDHCPInfo( NULL, service_id )))
321 CFRelease( service_id );
322 return 0;
324 CFRelease( service_id );
325 if (!(value = DHCPInfoGetOptionData( dict, map_option(param->id) )))
327 CFRelease( dict );
328 return 0;
330 len = CFDataGetLength( value );
332 switch (param->id)
334 case OPTION_SUBNET_MASK:
335 case OPTION_ROUTER_ADDRESS:
336 case OPTION_BROADCAST_ADDRESS:
338 DWORD *ptr = (DWORD *)(buf + offset);
339 if (len == sizeof(*ptr) && size >= sizeof(*ptr))
341 CFDataGetBytes( value, CFRangeMake(0, len), (UInt8 *)ptr );
342 param->offset = offset;
343 param->size = sizeof(*ptr);
344 TRACE( "returning %08x\n", *ptr );
346 ret = sizeof(*ptr);
347 break;
349 case OPTION_HOST_NAME:
350 case OPTION_DOMAIN_NAME:
351 case OPTION_MSFT_IE_PROXY:
353 char *ptr = buf + offset;
354 if (size >= len)
356 CFDataGetBytes( value, CFRangeMake(0, len), (UInt8 *)ptr );
357 param->offset = offset;
358 param->size = len;
359 TRACE( "returning %s\n", debugstr_an(ptr, len) );
361 ret = len;
362 break;
364 default:
365 FIXME( "option %u not supported\n", param->id );
366 break;
369 CFRelease( dict );
370 return ret;
373 #elif !defined(SONAME_LIBDBUS_1)
375 ULONG get_dhcp_request_param( const NET_LUID *adapter, struct mountmgr_dhcp_request_param *param, char *buf, ULONG offset,
376 ULONG size )
378 FIXME( "support not compiled in\n" );
379 return 0;
382 #endif