<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Display;
using Windows.Devices.Enumeration;
using Windows.Foundation;

namespace Monitorian.Supplement
{
	/// <summary>
	/// A wrapper class of <see cref="Windows.Devices.Display.DisplayMonitor"/>
	/// </summary>
	/// <remarks>
	/// <see cref="Windows.Devices.Display.DisplayMonitor"/> is only available
	/// on Windows 10 (version 10.0.17134.0) or newer.
	/// </remarks>
	public class DisplayInformation
	{
		#region Type

		/// <summary>
		/// Display monitor information
		/// </summary>
		public class DisplayItem
		{
			/// <summary>
			/// Device ID (Not device interface ID)
			/// </summary>
			public string DeviceInstanceId { get; }

			/// <summary>
			/// Display name
			/// </summary>
			public string DisplayName { get; }

			/// <summary>
			/// Whether the display is connected internally
			/// </summary>
			public bool IsInternal { get; }

			/// <summary>
			/// Connection description
			/// </summary>
			public string ConnectionDescription { get; }

			/// <summary>
			/// Physical size (diagonal) in inches
			/// </summary>
			public float PhysicalSize { get; }

			internal DisplayItem(
				string deviceInstanceId,
				string displayName,
				bool isInternal,
				string connectionDescription = null,
				float physicalSize = 0F)
			{
				this.DeviceInstanceId = deviceInstanceId;
				this.DisplayName = displayName;
				this.IsInternal = isInternal;
				this.ConnectionDescription = connectionDescription;
				this.PhysicalSize = physicalSize;
			}
		}

		#endregion

		// System error code: 0x80070057 = 0x57 = ERROR_INVALID_PARAMETER
		// Error message: The parameter is incorrect.
		private const uint ERROR_INVALID_PARAMETER = 0x80070057;

		// System error code: 0x8007001F = 0x1F = ERROR_GEN_FAILURE
		// Error message: A device attached to the system is not functioning.
		private const uint ERROR_GEN_FAILURE = 0x8007001F;

		/// <summary>
		/// Gets display monitor information.
		/// </summary>
		/// <returns>Array of display monitor information</returns>
		public static async Task<DisplayItem[]> GetDisplayMonitorsAsync()
		{
			const string deviceInstanceIdKey = "System.Devices.DeviceInstanceId";

			try
			{
				var devices = await DeviceInformation.FindAllAsync(DisplayMonitor.GetDeviceSelector(), new[] { deviceInstanceIdKey });
				if (devices?.Any() is true)
				{
					var items = new List<DisplayItem>(devices.Count);
					foreach (var device in devices)
					{
						if (!device.Properties.TryGetValue(deviceInstanceIdKey, out object value))
							continue;

						var deviceInstanceId = value as string;
						if (string.IsNullOrWhiteSpace(deviceInstanceId))
							continue;

						var displayMonitor = await DisplayMonitor.FromInterfaceIdAsync(device.Id);
						//var displayMonitor = await DisplayMonitor.FromIdAsync(deviceInstanceId);
						if (displayMonitor is null)
							continue;

						//Debug.WriteLine($"DeviceInstanceId: {deviceInstanceId}");
						//Debug.WriteLine($"DisplayName: {displayMonitor.DisplayName}");
						//Debug.WriteLine($"ConnectionKind: {displayMonitor.ConnectionKind}");
						//Debug.WriteLine($"PhysicalConnector: {displayMonitor.PhysicalConnector}");
						//Debug.WriteLine($"PhysicalSize: {GetDiagonal(displayMonitor.PhysicalSizeInInches):F1}");

						items.Add(new DisplayItem(
							deviceInstanceId: deviceInstanceId,
							displayName: displayMonitor.DisplayName,
							isInternal: (displayMonitor.ConnectionKind == DisplayMonitorConnectionKind.Internal),
							connectionDescription: GetConnectionDescription(displayMonitor.ConnectionKind, displayMonitor.PhysicalConnector),
							physicalSize: GetDiagonal(displayMonitor.PhysicalSizeInInches)));
					}
					return items.ToArray();
				}
			}
			catch (ArgumentException ax) when ((uint)ax.HResult == ERROR_INVALID_PARAMETER)
			{
			}
			catch (Exception ex) when ((uint)ex.HResult == ERROR_GEN_FAILURE)
			{
			}
			return Array.Empty<DisplayItem>();<commit_msg>Modify handling of exception from DisplayMonitor<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Display;
using Windows.Devices.Enumeration;
using Windows.Foundation;

namespace Monitorian.Supplement
{
	/// <summary>
	/// A wrapper class of <see cref="Windows.Devices.Display.DisplayMonitor"/>
	/// </summary>
	/// <remarks>
	/// <see cref="Windows.Devices.Display.DisplayMonitor"/> is only available
	/// on Windows 10 (version 10.0.17134.0) or newer.
	/// </remarks>
	public class DisplayInformation
	{
		#region Type

		/// <summary>
		/// Display monitor information
		/// </summary>
		public class DisplayItem
		{
			/// <summary>
			/// Device ID (Not device interface ID)
			/// </summary>
			public string DeviceInstanceId { get; }

			/// <summary>
			/// Display name
			/// </summary>
			public string DisplayName { get; }

			/// <summary>
			/// Whether the display is connected internally
			/// </summary>
			public bool IsInternal { get; }

			/// <summary>
			/// Connection description
			/// </summary>
			public string ConnectionDescription { get; }

			/// <summary>
			/// Physical size (diagonal) in inches
			/// </summary>
			public float PhysicalSize { get; }

			internal DisplayItem(
				string deviceInstanceId,
				string displayName,
				bool isInternal,
				string connectionDescription = null,
				float physicalSize = 0F)
			{
				this.DeviceInstanceId = deviceInstanceId;
				this.DisplayName = displayName;
				this.IsInternal = isInternal;
				this.ConnectionDescription = connectionDescription;
				this.PhysicalSize = physicalSize;
			}
		}

		#endregion

		// System error code: 0x80070057 = 0x57 = ERROR_INVALID_PARAMETER
		// Error message: The parameter is incorrect.
		private const uint ERROR_INVALID_PARAMETER = 0x80070057;

		// System error code: 0x8007001F = 0x1F = ERROR_GEN_FAILURE
		// Error message: A device attached to the system is not functioning.
		private const uint ERROR_GEN_FAILURE = 0x8007001F;

		// System error code: 0x8002802B
		// Error message: Element not found.
		private const uint TYPE_E_ELEMENTNOTFOUND = 0x8002802B;

		/// <summary>
		/// Gets display monitor information.
		/// </summary>
		/// <returns>Array of display monitor information</returns>
		public static async Task<DisplayItem[]> GetDisplayMonitorsAsync()
		{
			const string deviceInstanceIdKey = "System.Devices.DeviceInstanceId";

			try
			{
				var devices = await DeviceInformation.FindAllAsync(DisplayMonitor.GetDeviceSelector(), new[] { deviceInstanceIdKey });
				if (devices?.Any() is true)
				{
					var items = new List<DisplayItem>(devices.Count);
					foreach (var device in devices)
					{
						if (!device.Properties.TryGetValue(deviceInstanceIdKey, out object value))
							continue;

						var deviceInstanceId = value as string;
						if (string.IsNullOrWhiteSpace(deviceInstanceId))
							continue;

						var displayMonitor = await DisplayMonitor.FromInterfaceIdAsync(device.Id);
						//var displayMonitor = await DisplayMonitor.FromIdAsync(deviceInstanceId);
						if (displayMonitor is null)
							continue;

						//Debug.WriteLine($"DeviceInstanceId: {deviceInstanceId}");
						//Debug.WriteLine($"DisplayName: {displayMonitor.DisplayName}");
						//Debug.WriteLine($"ConnectionKind: {displayMonitor.ConnectionKind}");
						//Debug.WriteLine($"PhysicalConnector: {displayMonitor.PhysicalConnector}");
						//Debug.WriteLine($"PhysicalSize: {GetDiagonal(displayMonitor.PhysicalSizeInInches):F1}");

						items.Add(new DisplayItem(
							deviceInstanceId: deviceInstanceId,
							displayName: displayMonitor.DisplayName,
							isInternal: (displayMonitor.ConnectionKind == DisplayMonitorConnectionKind.Internal),
							connectionDescription: GetConnectionDescription(displayMonitor.ConnectionKind, displayMonitor.PhysicalConnector),
							physicalSize: GetDiagonal(displayMonitor.PhysicalSizeInInches)));
					}
					return items.ToArray();
				}
			}
			catch (ArgumentException ax) when ((uint)ax.HResult is ERROR_INVALID_PARAMETER)
			{
			}
			catch (Exception ex) when ((uint)ex.HResult is ERROR_GEN_FAILURE or TYPE_E_ELEMENTNOTFOUND)
			{
			}
			return Array.Empty<DisplayItem>();
{'fraction_non_alphanumeric': 0.112535953978907, 'fraction_numerical': 0.014381591562799617, 'mean_word_length': 3.469737546866631, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5622456', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2304, 'n_words': 572}