Release 4.0.0-preview1-00194
[platform/core/csapi/tizenfx.git] / src / ElmSharp.Wearable / ElmSharp.Wearable / RotaryEventManager.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Runtime.InteropServices;
4 using System.Text;
5
6 namespace ElmSharp.Wearable
7 {
8     /// <summary>
9     /// RotaryEventManager serve functions for global Rotary event like Galaxy Gear.
10     /// </summary>
11     public static class RotaryEventManager
12     {
13         static Dictionary<RotaryEventHandler, Interop.Eext.Eext_Rotary_Handler_Cb> s_rotaryEventHandlers = new Dictionary<RotaryEventHandler, Interop.Eext.Eext_Rotary_Handler_Cb>();
14
15         /// <summary>
16         /// Rotated will triggered when rotatable device like Galaxy Gear Bezel is rotated.
17         /// </summary>
18         public static event RotaryEventHandler Rotated
19         {
20             add
21             {
22                 if (s_rotaryEventHandlers.ContainsKey(value)) return;
23
24                 Interop.Eext.Eext_Rotary_Handler_Cb cb = (data, infoPtr) =>
25                 {
26                     var info = Interop.Eext.FromIntPtr(infoPtr);
27                     value.Invoke(new RotaryEventArgs
28                     {
29                         IsClockwise = info.Direction == Interop.Eext.Eext_Rotary_Event_Direction.Clockwise,
30                         Timestamp = info.TimeStamp
31                     });
32                     return true;
33                 };
34                 Interop.Eext.eext_rotary_event_handler_add(cb, IntPtr.Zero);
35                 s_rotaryEventHandlers[value] = cb;
36             }
37
38             remove
39             {
40                 Interop.Eext.Eext_Rotary_Handler_Cb cb;
41                 if (s_rotaryEventHandlers.TryGetValue(value, out cb))
42                 {
43                     Interop.Eext.eext_rotary_event_handler_del(cb);
44                     s_rotaryEventHandlers.Remove(value);
45                 }
46             }
47         }
48     }
49
50
51     /// <summary>
52     /// RotaryEventManager serve extension functions for Rotary event to EvasObject on device like Galaxy Gear.
53     /// </summary>
54     public static class EvasObjectExtensions
55     {
56         static Dictionary<EvasObject, RotaryEventHandler> s_rotaryObjectEventHandlers = new Dictionary<EvasObject, RotaryEventHandler>();
57         static Dictionary<EvasObject, Interop.Eext.Eext_Rotary_Event_Cb> s_rotaryObjectEventMap = new Dictionary<EvasObject, Interop.Eext.Eext_Rotary_Event_Cb>();
58
59         /// <summary>
60         /// Add a handler for Rotary event on specific EvasObject.
61         /// </summary>
62         /// <param name="obj">Target EvasObject</param>
63         /// <param name="handler">Event handler for Rotary event</param>
64         public static void AddRotaryEventHandler(this EvasObject obj, RotaryEventHandler handler)
65         {
66             EnableRotaryEventHandler(obj);
67
68             if (s_rotaryObjectEventHandlers.ContainsKey(obj))
69             {
70                 s_rotaryObjectEventHandlers[obj] += handler;
71             }
72             else
73             {
74                 s_rotaryObjectEventHandlers[obj] = handler;
75             }
76         }
77
78         /// <summary>
79         /// Remove a handler on specific EvasObject for Rotary event.
80         /// </summary>
81         /// <param name="obj">Target EvasObject</param>
82         /// <param name="handler">Event handler for Rotary event</param>
83         public static void RemoveRotaryEventHandler(this EvasObject obj, RotaryEventHandler handler)
84         {
85             if (s_rotaryObjectEventHandlers.ContainsKey(obj))
86             {
87                 s_rotaryObjectEventHandlers[obj] -= handler;
88                 if (s_rotaryObjectEventHandlers[obj] == null)
89                 {
90                     DisableRotaryEventHandler(obj, false);
91                     s_rotaryObjectEventHandlers.Remove(obj);
92                 }
93             }
94         }
95
96         /// <summary>
97         /// Activate this object can take Rotary event.
98         /// </summary>
99         /// <param name="obj">Target object</param>
100         public static void Activate(this EvasObject obj)
101         {
102             Interop.Eext.eext_rotary_object_event_activated_set(obj, true);
103         }
104
105         /// <summary>
106         /// Deactivate this object is blocked from Rotary event.
107         /// </summary>
108         /// <param name="obj">Target object</param>
109         public static void Deactivate(this EvasObject obj)
110         {
111             Interop.Eext.eext_rotary_object_event_activated_set(obj, false);
112         }
113
114         static void EnableRotaryEventHandler(EvasObject obj)
115         {
116             if (!s_rotaryObjectEventMap.ContainsKey(obj))
117             {
118                 Interop.Eext.Eext_Rotary_Event_Cb cb = (d, o, i) =>
119                 {
120                     RotaryEventHandler events;
121                     if (s_rotaryObjectEventHandlers.TryGetValue(obj, out events))
122                     {
123                         var info = Interop.Eext.FromIntPtr(i);
124                         events?.Invoke(new RotaryEventArgs
125                         {
126                             IsClockwise = info.Direction == Interop.Eext.Eext_Rotary_Event_Direction.Clockwise,
127                             Timestamp = info.TimeStamp
128                         });
129                     }
130                     return true;
131                 };
132                 Interop.Eext.eext_rotary_object_event_callback_add(obj, cb, IntPtr.Zero);
133                 s_rotaryObjectEventMap[obj] = cb;
134                 obj.Deleted += (s, e) => DisableRotaryEventHandler(obj, true);
135             }
136         }
137
138         static void DisableRotaryEventHandler(EvasObject obj, bool removeHandler)
139         {
140             Interop.Eext.Eext_Rotary_Event_Cb cb;
141             if (s_rotaryObjectEventMap.TryGetValue(obj, out cb))
142             {
143                 Interop.Eext.eext_rotary_object_event_callback_del(obj, cb);
144                 s_rotaryObjectEventMap.Remove(obj);
145             }
146             if (removeHandler && s_rotaryObjectEventHandlers.ContainsKey(obj))
147             {
148                 s_rotaryObjectEventHandlers.Remove(obj);
149             }
150         }
151     }
152
153     /// <summary>
154     /// Handler for Rotary event
155     /// </summary>
156     /// <param name="args">Rotary event information</param>
157     public delegate void RotaryEventHandler(RotaryEventArgs args);
158
159     /// <summary>
160     /// RotaryEventArgs serve information for triggered rotary event.
161     /// </summary>
162     public class RotaryEventArgs : EventArgs
163     {
164         /// <summary>
165         /// IsClockwise is true when Rotary device rotated clockwise direction or false on counter clockwise.
166         /// </summary>
167         public bool IsClockwise { get; set; }
168
169         /// <summary>
170         /// Timestamp of rotary event
171         /// </summary>
172         public uint Timestamp { get; set; }
173     }
174 }