[Device] Implementation of system.device module including error handling.
[platform/core/csapi/system.git] / Tizen.System / Device / Display.cs
1 using System;
2 using System.Collections.Generic;
3
4 namespace Tizen.System
5 {
6     /// <summary>
7     /// Enumeration for the available display states.
8     /// An application cannot put the device into the power off state or the suspend state.
9     /// </summary>
10     public enum DisplayState
11     {
12         /// <summary>
13         /// Normal state
14         /// </summary>
15         Normal = 0,
16         /// <summary>
17         /// Screen dim state
18         /// </summary>
19         Dim,
20         /// <summary>
21         /// Screen off state
22         /// </summary>
23         Off
24     }
25
26     /// <summary>
27     /// The Display class provides properties and methods to control the display status and brightness.
28     /// </summary>
29     public class Display
30     {
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)
35         {
36             _displayId = deviceNumber;
37         }
38
39         /// <summary>
40         /// The number of display devices.
41         /// </summary>
42         public static int NumberOfDisplays
43         {
44             get
45             {
46                 int number = 0;
47                 DeviceError res = (DeviceError)Interop.Device.DeviceDisplayGetNumbers(out number);
48                 if (res != DeviceError.None)
49                 {
50                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get number of displays.");
51                 }
52                 return number;
53             }
54         }
55
56         /// <summary>
57         /// The maximum brightness value that can be set.
58         /// </summary>
59         public int MaxBrightness
60         {
61             get
62             {
63                 int max = 0;
64                 DeviceError res = (DeviceError) Interop.Device.DeviceDisplayGetMaxBrightness(_displayId, out max);
65                 if (res != DeviceError.None)
66                 {
67                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness.");
68                 }
69                 return max;
70             }
71         }
72
73         /// <summary>
74         /// The display brightness value.
75         /// </summary>
76         public int Brightness
77         {
78             get
79             {
80                 int brightness = 0;
81                 DeviceError res = (DeviceError) Interop.Device.DeviceDisplayGetBrightness(_displayId, out brightness);
82                 if (res != DeviceError.None)
83                 {
84                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness.");
85                 }
86                 return brightness;
87             }
88             set
89             {
90                 //TODO: Check for maximum throw exception..
91                 DeviceError res = (DeviceError) Interop.Device.DeviceDisplaySetBrightness(_displayId, value);
92                 if (res != DeviceError.None)
93                 {
94                     throw DeviceExceptionFactory.CreateException(res, "unable to set brightness value");
95                 }
96             }
97         }
98         /// <summary>
99         /// Get the Display instance for the given display index.
100         /// </summary>
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)
104         {
105             //TODO: throw an exception for invalid display Id.
106             if(deviceNumber < 0 || deviceNumber >= NumberOfDisplays)
107             {
108                 throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "invalid display number");
109             }
110             if (!s_displays.ContainsKey(deviceNumber))
111             {
112                 s_displays.Add(deviceNumber, new Display(deviceNumber));
113             }
114             return s_displays[deviceNumber];
115         }
116         /// <summary>
117         /// The current display state.
118         /// </summary>
119         public static DisplayState State
120         {
121             get
122             {
123                 int state = 0;
124                 DeviceError res = (DeviceError) Interop.Device.DeviceDisplayGetState(out state);
125                 if (res != DeviceError.None)
126                 {
127                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Display state.");
128                 }
129                 return (DisplayState)state;
130             }
131         }
132
133         private static event EventHandler<DisplayStateChangedEventArgs> s_stateChanged;
134         /// <summary>
135         /// StateChanged is raised when the state of the display is changed
136         /// </summary>
137         /// <param name="sender"></param>
138         /// <param name="e">An DisplayStateChangedEventArgs object that contains the changed state</param>
139
140         public static event EventHandler<DisplayStateChangedEventArgs> StateChanged
141         {
142             add
143             {
144                 lock (s_lock)
145                 {
146                     if (s_stateChanged == null)
147                     {
148                         EventListenerStart();
149                     }
150                     s_stateChanged += value;
151                 }
152             }
153             remove
154             {
155                 lock (s_lock)
156                 {
157                     s_stateChanged -= value;
158                     if (s_stateChanged == null)
159                     {
160                         EventListenerStop();
161                     }
162                 }
163             }
164         }
165
166         private static Interop.Device.deviceCallback s_handler;
167         private static void EventListenerStart()
168         {
169             s_handler = (int type, IntPtr value, IntPtr data) =>
170             {
171                 int val = value.ToInt32();
172                 DisplayStateChangedEventArgs e = new DisplayStateChangedEventArgs()
173                 {
174                     State = (DisplayState)val
175                 };
176                 s_stateChanged?.Invoke(null, e);
177                 return true;
178             };
179             Interop.Device.DeviceAddCallback(EventType.DisplayState, s_handler, IntPtr.Zero);
180         }
181
182         private static void EventListenerStop()
183         {
184             Interop.Device.DeviceRemoveCallback(EventType.DisplayState, s_handler);
185         }
186     }
187 }