Release 4.0.0-preview1-00301
[platform/core/csapi/tizenfx.git] / src / Tizen.System / Device / Display.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using System.Collections.Generic;
19 namespace Tizen.System
20 {
21     /// <summary>
22     /// Enumeration for the available display states.
23     /// An application cannot put the device into the power off state or the suspend state.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public enum DisplayState
27     {
28         /// <summary>
29         /// Normal state.
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         Normal = 0,
33         /// <summary>
34         /// Screen dim state.
35         /// </summary>
36         /// <since_tizen> 3 </since_tizen>
37         Dim,
38         /// <summary>
39         /// Screen off state.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         Off
43     }
44
45     /// <summary>
46     /// The Display class provides the properties and events to control the display status and brightness.
47     /// </summary>
48     /// <remarks>
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.
53     /// </remarks>
54     /// <privilege>
55     /// http://tizen.org/privilege/display
56     /// </privilege>
57     /// <code>
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);
60     /// </code>
61     public class Display
62     {
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)
67         {
68             _displayId = deviceNumber;
69         }
70
71         /// <summary>
72         /// The number of available display devices.
73         /// </summary>
74         /// <since_tizen> 3 </since_tizen>
75         public static int NumberOfDisplays
76         {
77             get
78             {
79                 int number = 0;
80                 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetNumbers(out number);
81                 if (res != DeviceError.None)
82                 {
83                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get number of displays.");
84                 }
85                 return number;
86             }
87         }
88         /// <summary>
89         /// Gets all the available displays.
90         /// The display at the index zero is always assigned to the main display.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         public static IReadOnlyList<Display> Displays
94         {
95             get
96             {
97                 _lazyDisplays = new Lazy<IReadOnlyList<Display>>(() => GetAllDisplayes());
98                 return _lazyDisplays.Value;
99             }
100         }
101
102
103         private static IReadOnlyList<Display> GetAllDisplayes()
104         {
105             List<Display> displays = new List<Display>();
106
107             for (int i = 0; i < NumberOfDisplays; i++)
108             {
109                 displays.Add(new Display(i));
110             }
111             return displays;
112
113         }
114         /// <summary>
115         /// The maximum brightness value that can be set for the specific display.
116         /// </summary>
117         /// <since_tizen> 3 </since_tizen>
118         /// <code>
119         ///     Display display = Display.Displays[0];
120         ///     Console.WriteLine("Display MaxBrightness is: {0}", display.MaxBrightness);
121         /// </code>
122         public int MaxBrightness
123         {
124             get
125             {
126                 int max = 0;
127                 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetMaxBrightness(_displayId, out max);
128                 if (res != DeviceError.None)
129                 {
130                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness.");
131                 }
132                 return max;
133             }
134         }
135
136         /// <summary>
137         /// The brightness value of the display.
138         /// </summary>
139         /// <remarks>
140         /// The brightness value should be less than or equal to the MaxBrightness value.
141         /// </remarks>
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>
145         /// <code>
146         ///     Display display = Display.Displays[0];
147         ///     Console.WriteLine("Display current Brightness is: {0}", display.Brightness);
148         /// </code>
149         public int Brightness
150         {
151             get
152             {
153                 int brightness = 0;
154                 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetBrightness(_displayId, out brightness);
155                 if (res != DeviceError.None)
156                 {
157                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness.");
158                 }
159                 return brightness;
160             }
161             set
162             {
163                 //TODO: Check for maximum throw exception..
164                 DeviceError res = (DeviceError)Interop.Device.DeviceDisplaySetBrightness(_displayId, value);
165                 if (res != DeviceError.None)
166                 {
167                     throw DeviceExceptionFactory.CreateException(res, "unable to set brightness value");
168                 }
169             }
170         }
171         /// <summary>
172         /// The current device display state.
173         /// </summary>
174         /// <since_tizen> 3 </since_tizen>
175         public static DisplayState State
176         {
177             get
178             {
179                 int state = 0;
180                 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetState(out state);
181                 if (res != DeviceError.None)
182                 {
183                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Display state.");
184                 }
185                 return (DisplayState)state;
186             }
187         }
188
189         private static event EventHandler<DisplayStateChangedEventArgs> s_stateChanged;
190         /// <summary>
191         ///  StateChanged is raised when the state of the display is changed.
192         /// </summary>
193         /// <since_tizen> 3 </since_tizen>
194         /// <code>
195         /// public static async Task DisplayEventHandler()
196         /// {
197         ///     EventHandler&lt;DisplayStateChangedEventArgs&gt; handler = null;
198         ///     handler = (object sender, DisplayStateChangedEventArgs args) =>
199         ///     {
200         ///          Console.WriteLine("Display State is: {0}", args.State);
201         ///     }
202         ///     Battery.StateChanged += handler;
203         ///     await Task.Delay(20000);
204         ///  }
205         /// </code>
206         public static event EventHandler<DisplayStateChangedEventArgs> StateChanged
207         {
208             add
209             {
210                 lock (s_lock)
211                 {
212                     if (s_stateChanged == null)
213                     {
214                         EventListenerStart();
215                     }
216                     s_stateChanged += value;
217                 }
218             }
219             remove
220             {
221                 lock (s_lock)
222                 {
223                     s_stateChanged -= value;
224                     if (s_stateChanged == null)
225                     {
226                         EventListenerStop();
227                     }
228                 }
229             }
230         }
231
232         private static Interop.Device.deviceCallback s_handler;
233         private static void EventListenerStart()
234         {
235             s_handler = (int type, IntPtr value, IntPtr data) =>
236             {
237                 int val = value.ToInt32();
238                 DisplayStateChangedEventArgs e = new DisplayStateChangedEventArgs()
239                 {
240                     State = (DisplayState)val
241                 };
242                 s_stateChanged?.Invoke(null, e);
243                 return true;
244             };
245             Interop.Device.DeviceAddCallback(EventType.DisplayState, s_handler, IntPtr.Zero);
246         }
247
248         private static void EventListenerStop()
249         {
250             Interop.Device.DeviceRemoveCallback(EventType.DisplayState, s_handler);
251         }
252     }
253 }