2 using System.Collections.Generic;
7 /// Enumeration for the available display states.
8 /// An application cannot put the device into the power off state or the suspend state.
10 public enum DisplayState
27 /// The Display class provides properties and methods to control the display status and brightness.
31 private readonly int _displayId;
32 private static readonly Dictionary<int, Display> s_displays = new Dictionary<int, Display>();
33 private static readonly object s_lock = new object();
34 private Display(int deviceNumber)
36 _displayId = deviceNumber;
40 /// The number of display devices.
42 public static int NumberOfDisplays
47 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetNumbers(out number);
48 if (res != DeviceError.None)
50 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get number of displays.");
57 /// The maximum brightness value that can be set.
59 public int MaxBrightness
64 DeviceError res = (DeviceError) Interop.Device.DeviceDisplayGetMaxBrightness(_displayId, out max);
65 if (res != DeviceError.None)
67 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness.");
74 /// The display brightness value.
81 DeviceError res = (DeviceError) Interop.Device.DeviceDisplayGetBrightness(_displayId, out brightness);
82 if (res != DeviceError.None)
84 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness.");
90 //TODO: Check for maximum throw exception..
91 DeviceError res = (DeviceError) Interop.Device.DeviceDisplaySetBrightness(_displayId, value);
92 if (res != DeviceError.None)
94 throw DeviceExceptionFactory.CreateException(res, "unable to set brightness value");
99 /// Get the Display instance for the given display index.
101 /// <param name="deviceNumber"> The index of the display.
102 /// It can be greater than or equal to 0 and less than the number of display devices </param>
103 public static Display GetDisplay(int deviceNumber)
105 //TODO: throw an exception for invalid display Id.
106 if(deviceNumber < 0 || deviceNumber >= NumberOfDisplays)
108 throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "invalid display number");
110 if (!s_displays.ContainsKey(deviceNumber))
112 s_displays.Add(deviceNumber, new Display(deviceNumber));
114 return s_displays[deviceNumber];
117 /// The current display state.
119 public static DisplayState State
124 DeviceError res = (DeviceError) Interop.Device.DeviceDisplayGetState(out state);
125 if (res != DeviceError.None)
127 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Display state.");
129 return (DisplayState)state;
133 private static event EventHandler<DisplayStateChangedEventArgs> s_stateChanged;
135 /// StateChanged is raised when the state of the display is changed
137 /// <param name="sender"></param>
138 /// <param name="e">An DisplayStateChangedEventArgs object that contains the changed state</param>
140 public static event EventHandler<DisplayStateChangedEventArgs> StateChanged
146 if (s_stateChanged == null)
148 EventListenerStart();
150 s_stateChanged += value;
157 s_stateChanged -= value;
158 if (s_stateChanged == null)
166 private static Interop.Device.deviceCallback s_handler;
167 private static void EventListenerStart()
169 s_handler = (int type, IntPtr value, IntPtr data) =>
171 int val = value.ToInt32();
172 DisplayStateChangedEventArgs e = new DisplayStateChangedEventArgs()
174 State = (DisplayState)val
176 s_stateChanged?.Invoke(null, e);
179 Interop.Device.DeviceAddCallback(EventType.DisplayState, s_handler, IntPtr.Zero);
182 private static void EventListenerStop()
184 Interop.Device.DeviceRemoveCallback(EventType.DisplayState, s_handler);