2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 namespace Tizen.System
22 /// Enumeration for the available display states.
23 /// An application cannot put the device into the power off state or the suspend state.
25 /// <since_tizen> 3 </since_tizen>
26 public enum DisplayState
31 /// <since_tizen> 3 </since_tizen>
36 /// <since_tizen> 3 </since_tizen>
41 /// <since_tizen> 3 </since_tizen>
46 /// The Display class provides the properties and events to control the display status and brightness.
49 /// The Display API provides the way to get the current display brightness value,
50 /// the display state, and the total number of available displays.
51 /// It also provides the events for an application to receive the display state change events from the device.
52 /// To receive the display event, the application should register with an associated EventHandler.
55 /// http://tizen.org/privilege/display
58 /// Console.WriteLine("Display current state is: {0}", Tizen.System.Display.State);
59 /// Console.WriteLine("Total number of Displays are: {0}", Tizen.System.Display.NumberOfDisplays);
63 private readonly int _displayId;
64 private static readonly object s_lock = new object();
65 private static Lazy<IReadOnlyList<Display>> _lazyDisplays;
66 private Display(int deviceNumber)
68 _displayId = deviceNumber;
72 /// The number of available display devices.
74 /// <since_tizen> 3 </since_tizen>
75 public static int NumberOfDisplays
80 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetNumbers(out number);
81 if (res != DeviceError.None)
83 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get number of displays.");
89 /// Gets all the available displays.
90 /// The display at the index zero is always assigned to the main display.
92 /// <since_tizen> 3 </since_tizen>
93 public static IReadOnlyList<Display> Displays
97 _lazyDisplays = new Lazy<IReadOnlyList<Display>>(() => GetAllDisplayes());
98 return _lazyDisplays.Value;
103 private static IReadOnlyList<Display> GetAllDisplayes()
105 List<Display> displays = new List<Display>();
107 for (int i = 0; i < NumberOfDisplays; i++)
109 displays.Add(new Display(i));
115 /// The maximum brightness value that can be set for the specific display.
117 /// <since_tizen> 3 </since_tizen>
119 /// Display display = Display.Displays[0];
120 /// Console.WriteLine("Display MaxBrightness is: {0}", display.MaxBrightness);
122 public int MaxBrightness
127 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetMaxBrightness(_displayId, out max);
128 if (res != DeviceError.None)
130 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness.");
137 /// The brightness value of the display.
140 /// The brightness value should be less than or equal to the MaxBrightness value.
142 /// <since_tizen> 3 </since_tizen>
143 /// <exception cref="ArgumentException">When an invalid parameter value is set.</exception>
144 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
146 /// Display display = Display.Displays[0];
147 /// Console.WriteLine("Display current Brightness is: {0}", display.Brightness);
149 public int Brightness
154 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetBrightness(_displayId, out brightness);
155 if (res != DeviceError.None)
157 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness.");
163 //TODO: Check for maximum throw exception..
164 DeviceError res = (DeviceError)Interop.Device.DeviceDisplaySetBrightness(_displayId, value);
165 if (res != DeviceError.None)
167 throw DeviceExceptionFactory.CreateException(res, "unable to set brightness value");
172 /// The current device display state.
174 /// <since_tizen> 3 </since_tizen>
175 public static DisplayState State
180 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetState(out state);
181 if (res != DeviceError.None)
183 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Display state.");
185 return (DisplayState)state;
189 private static event EventHandler<DisplayStateChangedEventArgs> s_stateChanged;
191 /// StateChanged is raised when the state of the display is changed.
193 /// <since_tizen> 3 </since_tizen>
195 /// public static async Task DisplayEventHandler()
197 /// EventHandler<DisplayStateChangedEventArgs> handler = null;
198 /// handler = (object sender, DisplayStateChangedEventArgs args) =>
200 /// Console.WriteLine("Display State is: {0}", args.State);
202 /// Battery.StateChanged += handler;
203 /// await Task.Delay(20000);
206 public static event EventHandler<DisplayStateChangedEventArgs> StateChanged
212 if (s_stateChanged == null)
214 EventListenerStart();
216 s_stateChanged += value;
223 s_stateChanged -= value;
224 if (s_stateChanged == null)
232 private static Interop.Device.deviceCallback s_handler;
233 private static void EventListenerStart()
235 s_handler = (int type, IntPtr value, IntPtr data) =>
237 int val = value.ToInt32();
238 DisplayStateChangedEventArgs e = new DisplayStateChangedEventArgs()
240 State = (DisplayState)val
242 s_stateChanged?.Invoke(null, e);
245 Interop.Device.DeviceAddCallback(EventType.DisplayState, s_handler, IntPtr.Zero);
248 private static void EventListenerStop()
250 Interop.Device.DeviceRemoveCallback(EventType.DisplayState, s_handler);