CFArray Bundle(SecIdentity identity, IEnumerable <SecCertificate> certificates) { if (identity == null) { throw new ArgumentNullException("identity"); } int i = 0; int n = 0; if (certificates != null) { foreach (var obj in certificates) { n++; } } var ptrs = new IntPtr [n + 1]; ptrs [0] = identity.Handle; foreach (var certificate in certificates) { ptrs [++i] = certificate.Handle; } return(CFArray.CreateArray(ptrs)); }
protected void SetArrayValue(NSString key, INativeObject[] values) { if (NullCheckAndRemoveKey(key, values == null)) { CFMutableDictionary.SetValue(Dictionary.Handle, key.Handle, CFArray.FromNativeObjects(values).Handle); } }
// TODO: need to setup a server that requires client-side certificates public IList <T> GetDistinguishedNames <T> () { IntPtr p; result = SSLCopyDistinguishedNames(Handle, out p); if (p == IntPtr.Zero) { return(null); // empty } if (result != SslStatus.Success) { CFObject.CFRelease(p); return(null); // error } var array = new CFArray(p, false); var list = new List <T> (); for (int i = 0; i < array.Count; i++) { // CFData -> X500DistinguishedName -> string list.Add(Convert <T> (p)); } CFObject.CFRelease(p); return(list); }
static INativeObject [] QueryAsReference(CFDictionary query, out SecStatusCode result) { if (query == null) { result = SecStatusCode.Param; return(null); } IntPtr ptr; result = SecItem.SecItemCopyMatching(query.Handle, out ptr); if (result == SecStatusCode.Success && ptr != IntPtr.Zero) { var array = CFArray.ArrayFromHandle <INativeObject> (ptr, p => { IntPtr cfType = CFType.GetTypeID(p); if (cfType == SecCertificate.GetTypeID()) { return(new SecCertificate(p, true)); } if (cfType == SecKey.GetTypeID()) { return(new SecKey(p, true)); } if (cfType == SecIdentity.GetTypeID()) { return(new SecIdentity(p, true)); } throw new Exception(String.Format("Unexpected type: 0x{0:x}", cfType)); }); return(array); } return(null); }
string[] CopyDistinguishedNames() { IntPtr arrayPtr; var result = SSLCopyDistinguishedNames(Handle, out arrayPtr); CheckStatusAndThrow(result); if (arrayPtr == IntPtr.Zero) { return(new string[0]); } using (var array = new CFArray(arrayPtr, true)) { var names = new string [array.Count]; for (int i = 0; i < array.Count; i++) { using (var data = new CFData(array[i], false)) { var buffer = new byte [(int)data.Length]; Marshal.Copy(data.Bytes, buffer, 0, buffer.Length); var dn = new X500DistinguishedName(buffer); names[i] = dn.Name; } } return(names); } }
public PMStatusCode CreatePrinterList(out string [] printerList, out int index, out PMPrinter printer) { IntPtr array, printerHandle; var code = PMSessionCreatePrinterList(handle, out array, out index, out printerHandle); if (code != PMStatusCode.Ok) { printerList = null; printer = null; return(code); } printerList = CFArray.StringArrayFromHandle(array); CFObject.CFRelease(array); if (printerHandle != IntPtr.Zero) { // Now get the printer, we do not own it, so retain. printer = new PMPrinter(printerHandle, owns: false); } else { printer = null; } return(PMStatusCode.Ok); }
public IDictionary <IOHIDElement, IOHIDValue> GetValues(IOHIDElement[] elements) { ThrowIfDisposed(); if (elements == null) { throw new ArgumentNullException("elements"); } var elementArray = CFArray.FromNativeObjects(elements); IOHIDElement[] keys = new IOHIDElement[elements.Length]; IOHIDValue[] values = new IOHIDValue[elements.Length]; CFDictionary multiple = CFDictionary.FromObjectsAndKeys(values, keys); var multipleRef = multiple.Handle; var result = IOHIDDeviceCopyValueMultiple(Handle, elementArray.Handle, ref multipleRef); IOObject.ThrowIfError(result); var dict = new Dictionary <IOHIDElement, IOHIDValue> (multiple.Count); IntPtr[] keyRefs, valueRefs; multiple.GetKeysAndValues(out keyRefs, out valueRefs); for (int i = 0; i < multiple.Count; i++) { dict.Add(new IOHIDElement(keyRefs [i], true), new IOHIDValue(valueRefs [i], true)); } CFType.Release(multiple.Handle); return(dict); }
public void CreateTest() { var handle = CFArray.Create(TestArray); using var a = Runtime.GetINativeObject <CFArray> (handle, true); VerifyArray(a); Assert.AreEqual((nint)1, CFGetRetainCount(handle), "RC"); }
public static bool MatchFontDescriptors(CTFontDescriptor[] descriptors, NSSet mandatoryAttributes, Func <CTFontDescriptorMatchingState, IntPtr, bool> progressHandler) { var ma = mandatoryAttributes == null ? IntPtr.Zero : mandatoryAttributes.Handle; // FIXME: SIGSEGV probably due to mandatoryAttributes mismatch using (var ar = CFArray.FromNativeObjects(descriptors)) { return(CTFontDescriptorMatchFontDescriptorsWithProgressHandler(ar.Handle, ma, progressHandler)); } }
public void ArrayFromHandleTest_bool_false() { var handle = CFArray.Create(TestArray); var a = CFArray.ArrayFromHandle <NSString> (handle, false); VerifyArray(a); Assert.AreEqual((nint)1, CFGetRetainCount(handle), "RC"); CFRelease(handle); }
public void ArrayFromHandleFuncTest() { var handle = CFArray.Create(TestArray); var a = CFArray.ArrayFromHandleFunc <string> (handle, (v) => CFString.FromHandle(v)); VerifyArray(a); Assert.AreEqual((nint)1, CFGetRetainCount(handle), "RC"); CFRelease(handle); }
public static string[] GetStringArray(IDictionary <NSObject, NSObject> dictionary, NSObject key) { var value = dictionary [key]; if (value == null) { return(Array.Empty <string> ()); } return(CFArray.StringArrayFromHandle(value.Handle)); }
public CTFontDescriptor[] GetMatchingFontDescriptors(NSSet?mandatoryAttributes) { var cfArrayRef = CTFontDescriptorCreateMatchingFontDescriptors(Handle, mandatoryAttributes.GetHandle()); if (cfArrayRef == IntPtr.Zero) { return(Array.Empty <CTFontDescriptor> ()); } return(CFArray.ArrayFromHandleFunc(cfArrayRef, fd => new CTFontDescriptor(cfArrayRef, false), true) !); }
public CTFontDescriptor [] GetMatchingFontDescriptors(CTFontCollectionOptions?options) { var cfArrayRef = CTFontCollectionCreateMatchingFontDescriptorsWithOptions(Handle, options.GetHandle()); if (cfArrayRef == IntPtr.Zero) { return(Array.Empty <CTFontDescriptor> ()); } return(CFArray.ArrayFromHandleFunc(cfArrayRef, fd => new CTFontDescriptor(fd, false), true) !); }
public SecStatusCode SetAnchorCertificates(SecCertificate[] array) { if (array == null) { return(SecTrustSetAnchorCertificates(handle, IntPtr.Zero)); } using (var certs = CFArray.FromNativeObjects(array)) { return(SecTrustSetAnchorCertificates(handle, certs.Handle)); } }
static IntPtr Create(CGColorSpace?colorspace, CGColor [] colors) { if (colors is null) { throw new ArgumentNullException(nameof(colors)); } using (var array = CFArray.FromNativeObjects(colors)) return(CGGradientCreateWithColors(colorspace.GetHandle(), array.Handle, null)); }
public static T[] GetNativeArray <T> (NSDictionary dictionary, NSObject key, Converter <IntPtr, T> converter) { var cfArrayRef = CFDictionary.GetValue(dictionary.Handle, key.Handle); if (cfArrayRef == IntPtr.Zero || CFArray.GetCount(cfArrayRef) == 0) { return(new T [0]); } return(NSArray.ArrayFromHandle(cfArrayRef, converter)); }
public static TextInputSource[] List(bool all = false) { var sources = new CFArray(TISCreateInputSourceList(IntPtr.Zero, all), true); var list = new TextInputSource[sources.Count]; for (var i = 0; i < sources.Count; ++i) { list[i] = new TextInputSource(sources.GetValue(i), false); } return(list); }
protected string[]? GetStringArrayValue(NSString key) { if (key is null) { throw new ArgumentNullException(nameof(key)); } var array = Dictionary.LowlevelObjectForKey(key.Handle); return(CFArray.StringArrayFromHandle(array) !); }
public CTFontCollection?WithFontDescriptors(CTFontDescriptor[]?queryDescriptors, CTFontCollectionOptions?options) { using var descriptors = queryDescriptors is null ? null : CFArray.FromNativeObjects(queryDescriptors); var h = CTFontCollectionCreateCopyWithFontDescriptors(Handle, descriptors.GetHandle(), options.GetHandle()); if (h == IntPtr.Zero) { return(null); } return(new CTFontCollection(h, true)); }
public static string [] GetAllRoleHandlersForContentType(string contentType, LSRoles roles = LSRoles.All) { if (contentType == null) { throw new ArgumentNullException(nameof(contentType)); } return(CFArray.StringArrayFromHandle( LSCopyAllRoleHandlersForContentType(new NSString(contentType).Handle, roles) )); }
public static string [] GetSelectedLanguages(MACaptionAppearanceDomain domain) { using (var langs = new CFArray(MACaptionAppearanceCopySelectedLanguages((int)domain), owns: true)) { var languages = new string [langs.Count]; for (int i = 0; i < langs.Count; i++) { languages[i] = CFString.FetchString(langs.GetValue(i)); } return(languages); } }
public static NSString [] GetPreferredCaptioningMediaCharacteristics(MACaptionAppearanceDomain domain) { using (var chars = new CFArray(MACaptionAppearanceCopyPreferredCaptioningMediaCharacteristics((int)domain), owns: true)) { NSString [] characteristics = new NSString [chars.Count]; for (int i = 0; i < chars.Count; i++) { characteristics[i] = new NSString(chars.GetValue(i)); } return(characteristics); } }
public static string [] GetAllHandlersForUrlScheme(string urlScheme) { if (urlScheme == null) { throw new ArgumentNullException(nameof(urlScheme)); } return(CFArray.StringArrayFromHandle( LSCopyAllHandlersForURLScheme(new NSString(urlScheme).Handle) )); }
public PMStatusCode TryGetPaperList(out PMPaper []?paperList) { var code = PMPrinterGetPaperList(Handle, out var m); if (code != PMStatusCode.Ok) { paperList = null; return(code); } paperList = (PMPaper [])CFArray.ArrayFromHandleFunc <PMPaper> (m, (handle) => new PMPaper(handle, false)) !; return(PMStatusCode.Ok); }
public CGGradient(CGColorSpace colorspace, CGColor [] colors) { if (colors == null) { throw new ArgumentNullException("colors"); } IntPtr csh = colorspace == null ? IntPtr.Zero : colorspace.handle; using (var array = CFArray.FromNativeObjects(colors)) handle = CGGradientCreateWithColors(csh, array.Handle, null); }
void Initialize(SafeSecCertificateHandle[] array, SecPolicy policy) { var handles = new IntPtr [array.Length]; for (int i = 0; i < array.Length; i++) { handles [i] = array [i].DangerousGetHandle(); } using (var certs = CFArray.CreateArray(handles)) { Initialize(certs.Handle, policy); } }
public CGPDFStream[] GetStreams() { using (CFArray a = new CFArray(CGPDFContentStreamGetStreams(Handle))) { var streams = new CGPDFStream [a.Count]; for (int i = 0; i < a.Count; i++) { streams [i] = new CGPDFStream(a.GetValue(i)); } return(streams); // note: CGPDFStreamRef is weird because it has no retain/release calls unlike other CGPDF* types } }
public static PMStatusCode CreatePrinterList(out PMPrinter []?printerList) { var code = PMServerCreatePrinterList(IntPtr.Zero /* ServerLocal */, out var arr); if (code != PMStatusCode.Ok) { printerList = null; return(code); } printerList = CFArray.ArrayFromHandleFunc <PMPrinter> (arr, (handle) => new PMPrinter(handle, false), true); return(PMStatusCode.Ok); }
static IntPtr Create(CGColorSpace?colorspace, CGColor [] colors, nfloat []?locations) { // colors is __nullable but would return a `nil` instance back, // which is not something we can handle nicely from a .NET constructor if (colors is null) { throw new ArgumentNullException(nameof(colors)); } using (var array = CFArray.FromNativeObjects(colors)) return(CGGradientCreateWithColors(colorspace.GetHandle(), array.Handle, locations)); }
JoystickData CreateJoystick(IntPtr sender, IntPtr device) { JoystickData joy = null; // Retrieve all elements of this device CFArrayRef element_array_ref = NativeMethods.IOHIDDeviceCopyMatchingElements(device, IntPtr.Zero, IntPtr.Zero); if (element_array_ref != IntPtr.Zero) { joy = new JoystickData(); int axes = 0; int buttons = 0; int hats = 0; CFStringRef name_ref = NativeMethods.IOHIDDeviceGetProperty(device, NativeMethods.IOHIDProductKey); string name = CF.CFStringGetCString(name_ref); Guid guid = CreateJoystickGuid(device, name); List<int> button_elements = new List<int>(); List<IOHIDElementRef> hat_elements = new List<CFAllocatorRef>(); CFArray element_array = new CFArray(element_array_ref); for (int i = 0; i < element_array.Count; i++) { IOHIDElementRef element_ref = element_array[i]; IOHIDElementType type = NativeMethods.IOHIDElementGetType(element_ref); HIDPage page = NativeMethods.IOHIDElementGetUsagePage(element_ref); int usage = NativeMethods.IOHIDElementGetUsage(element_ref); switch (page) { case HIDPage.GenericDesktop: switch ((HIDUsageGD)usage) { case HIDUsageGD.X: case HIDUsageGD.Y: case HIDUsageGD.Z: case HIDUsageGD.Rx: case HIDUsageGD.Ry: case HIDUsageGD.Rz: case HIDUsageGD.Slider: case HIDUsageGD.Dial: case HIDUsageGD.Wheel: axes++; break; case HIDUsageGD.Hatswitch: hats++; hat_elements.Add(element_ref); break; } break; case HIDPage.Simulation: switch ((HIDUsageSim)usage) { case HIDUsageSim.Rudder: case HIDUsageSim.Throttle: axes++; break; } break; case HIDPage.Button: button_elements.Add(usage); break; } } if (axes > JoystickState.MaxAxes) { Debug.Print("[Mac] JoystickAxis limit reached ({0} > {1}), please report a bug at http://www.opentk.com", axes, JoystickState.MaxAxes); axes = JoystickState.MaxAxes; } if (buttons > JoystickState.MaxButtons) { Debug.Print("[Mac] JoystickButton limit reached ({0} > {1}), please report a bug at http://www.opentk.com", buttons, JoystickState.MaxButtons); buttons = JoystickState.MaxButtons; } if (hats > JoystickState.MaxHats) { Debug.Print("[Mac] JoystickHat limit reached ({0} > {1}), please report a bug at http://www.opentk.com", hats, JoystickState.MaxHats); hats = JoystickState.MaxHats; } joy.Name = name; joy.Guid = guid; joy.State.SetIsConnected(true); joy.Capabilities = new JoystickCapabilities(axes, buttons, hats, true); // Map button elements to JoystickButtons for (int button = 0; button < button_elements.Count; button++) { joy.ElementUsageToButton.Add(button_elements[button], JoystickButton.Button0 + button); } for (int hat = 0; hat < hat_elements.Count; hat++) { joy.ElementToHat.Add(hat_elements[hat], JoystickHat.Hat0 + hat); } } CF.CFRelease(element_array_ref); return joy; }
public static CFProxy[] ExecuteProxyAutoConfigurationURL (IntPtr proxyAutoConfigURL, Uri targetURL) { CFUrl url = CFUrl.Create (targetURL.AbsoluteUri); if (url == null) return null; CFProxy[] proxies = null; var runLoop = CFRunLoop.CurrentRunLoop; // Callback that will be called after executing the configuration script CFProxyAutoConfigurationResultCallback cb = delegate (IntPtr client, IntPtr proxyList, IntPtr error) { if (proxyList != IntPtr.Zero) { var array = new CFArray (proxyList, false); proxies = new CFProxy [array.Count]; for (int i = 0; i < proxies.Length; i++) { CFDictionary dict = new CFDictionary (array[i], false); proxies[i] = new CFProxy (dict); } array.Dispose (); } runLoop.Stop (); }; var clientContext = new CFStreamClientContext (); var loopSource = CFNetworkExecuteProxyAutoConfigurationURL (proxyAutoConfigURL, url.Handle, cb, ref clientContext); // Create a private mode var mode = CFString.Create ("Mono.MacProxy"); runLoop.AddSource (loopSource, mode); runLoop.RunInMode (mode, double.MaxValue, false); runLoop.RemoveSource (loopSource, mode); return proxies; }
JoystickData CreateJoystick(IntPtr sender, IntPtr device) { JoystickData joy = null; // Retrieve all elements of this device CFArrayRef element_array_ref = NativeMethods.IOHIDDeviceCopyMatchingElements(device, IntPtr.Zero, IntPtr.Zero); if (element_array_ref != IntPtr.Zero) { joy = new JoystickData(); int axes = 0; int buttons = 0; int dpads = 0; CFStringRef name_ref = NativeMethods.IOHIDDeviceGetProperty(device, NativeMethods.IOHIDProductKey); string name = CF.CFStringGetCString(name_ref); Guid guid = CreateJoystickGuid(device, name); List<int> button_elements = new List<int>(); CFArray element_array = new CFArray(element_array_ref); for (int i = 0; i < element_array.Count; i++) { IOHIDElementRef element_ref = element_array[i]; IOHIDElementType type = NativeMethods.IOHIDElementGetType(element_ref); HIDPage page = NativeMethods.IOHIDElementGetUsagePage(element_ref); int usage = NativeMethods.IOHIDElementGetUsage(element_ref); switch (page) { case HIDPage.GenericDesktop: switch ((HIDUsageGD)usage) { case HIDUsageGD.X: case HIDUsageGD.Y: case HIDUsageGD.Z: case HIDUsageGD.Rx: case HIDUsageGD.Ry: case HIDUsageGD.Rz: case HIDUsageGD.Slider: case HIDUsageGD.Dial: case HIDUsageGD.Wheel: axes++; break; case HIDUsageGD.Hatswitch: dpads++; break; } break; case HIDPage.Simulation: switch ((HIDUsageSim)usage) { case HIDUsageSim.Rudder: case HIDUsageSim.Throttle: axes++; break; } break; case HIDPage.Button: button_elements.Add(usage); break; } } joy.Name = name; joy.Guid = guid; joy.State.SetIsConnected(true); joy.Capabilities = new JoystickCapabilities(axes, buttons, true); // Map button elements to JoystickButtons for (int button = 0; button < button_elements.Count; button++) { joy.ElementUsageToButton.Add(button_elements[button], JoystickButton.Button0 + button); } } CF.CFRelease(element_array_ref); return joy; }
static QuartzDisplayDeviceDriver() { lock (display_lock) { // To minimize the need to add static methods to OpenTK.Graphics.DisplayDevice // we only allow settings to be set through its constructor. // Thus, we save all necessary parameters in temporary variables // and construct the device when every needed detail is available. // The main DisplayDevice constructor adds the newly constructed device // to the list of available devices. const int maxDisplayCount = 20; IntPtr[] displays = new IntPtr[maxDisplayCount]; int displayCount; unsafe { fixed(IntPtr* displayPtr = displays) { CG.GetActiveDisplayList(maxDisplayCount, displayPtr, out displayCount); } } Debug.Print("CoreGraphics reported {0} displays.", displayCount); Debug.Indent(); for (int i = 0; i < displayCount; i++) { IntPtr currentDisplay = displays[i]; // according to docs, first element in the array is always the // main display. bool primary = (i == 0); if (primary) mainDisplay = currentDisplay; // gets current settings int currentWidth = CG.DisplayPixelsWide(currentDisplay); int currentHeight = CG.DisplayPixelsHigh(currentDisplay); Debug.Print("Display {0} is at {1}x{2}", i, currentWidth, currentHeight); IntPtr displayModesPtr = CG.DisplayAvailableModes(currentDisplay); CFArray displayModes = new CFArray(displayModesPtr); Debug.Print("Supports {0} display modes.", displayModes.Count); DisplayResolution opentk_dev_current_res = null; List<DisplayResolution> opentk_dev_available_res = new List<DisplayResolution>(); IntPtr currentModePtr = CG.DisplayCurrentMode(currentDisplay); CFDictionary currentMode = new CFDictionary(currentModePtr); for (int j = 0; j < displayModes.Count; j++) { CFDictionary dict = new CFDictionary(displayModes[j]); int width = (int) dict.GetNumberValue("Width"); int height = (int) dict.GetNumberValue("Height"); int bpp = (int) dict.GetNumberValue("BitsPerPixel"); double freq = dict.GetNumberValue("RefreshRate"); bool current = currentMode.Ref == dict.Ref; //if (current) Debug.Write(" * "); //else Debug.Write(" "); //Debug.Print("Mode {0} is {1}x{2}x{3} @ {4}.", j, width, height, bpp, freq); DisplayResolution thisRes = new DisplayResolution(0, 0, width, height, bpp, (float)freq); opentk_dev_available_res.Add(thisRes); if (current) opentk_dev_current_res = thisRes; } DisplayDevice opentk_dev = new DisplayDevice(opentk_dev_current_res, primary, opentk_dev_available_res); displayMap.Add(opentk_dev, currentDisplay); } Debug.Unindent(); } }
public bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution) { IntPtr display = displayMap[device]; IntPtr currentModePtr = CG.DisplayCurrentMode(display); if (storedModes.ContainsKey(display) == false) { storedModes.Add(display, currentModePtr); } IntPtr displayModesPtr = CG.DisplayAvailableModes(display); CFArray displayModes = new CFArray(displayModesPtr); for (int j = 0; j < displayModes.Count; j++) { CFDictionary dict = new CFDictionary(displayModes[j]); int width = (int)dict.GetNumberValue("Width"); int height = (int)dict.GetNumberValue("Height"); int bpp = (int)dict.GetNumberValue("BitsPerPixel"); double freq = dict.GetNumberValue("RefreshRate"); if (width == resolution.Width && height == resolution.Height && bpp == resolution.BitsPerPixel && System.Math.Abs(freq - resolution.RefreshRate) < 1e-6) { if (displaysCaptured.Contains(display) == false) { CG.DisplayCapture(display); } Debug.Print("Changing resolution to {0}x{1}x{2}@{3}.", width, height, bpp, freq); CG.DisplaySwitchToMode(display, displayModes[j]); return true; } } return false; }