[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EcoreEvent.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 using System.Linq;
20 using System.Runtime.InteropServices;
21
22 namespace ElmSharp
23 {
24     /// <summary>
25     /// The EcoreEventType is a type of EcoreEvent.
26     /// It includes some predefined instance.
27     /// </summary>
28     /// <since_tizen> preview </since_tizen>
29     public class EcoreEventType
30     {
31         /// <summary>
32         /// Key down Ecore event type.
33         /// </summary>
34         /// <since_tizen> preview </since_tizen>
35         public static readonly EcoreEventType KeyDown = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_KEY_DOWN");
36         /// <summary>
37         /// Key Up Ecore event type.
38         /// </summary>
39         /// <since_tizen> preview </since_tizen>
40         public static readonly EcoreEventType KeyUp = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_KEY_UP");
41         /// <summary>
42         /// Mouse Button Down Ecore event type.
43         /// </summary>
44         /// <since_tizen> preview </since_tizen>
45         public static readonly EcoreEventType MouseButtonDown = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_BUTTON_DOWN");
46         /// <summary>
47         /// Mouse Button Up Ecore event type.
48         /// </summary>
49         /// <since_tizen> preview </since_tizen>
50         public static readonly EcoreEventType MouseButtonUp = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_BUTTON_UP");
51         /// <summary>
52         /// Mouse Button Cancel Ecore event type.
53         /// </summary>
54         /// <since_tizen> preview </since_tizen>
55         public static readonly EcoreEventType MouseButtonCancel = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_BUTTON_CANCEL");
56         /// <summary>
57         /// Mouse Move Ecore event type.
58         /// </summary>
59         /// <since_tizen> preview </since_tizen>
60         public static readonly EcoreEventType MouseMove = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_MOVE");
61         /// <summary>
62         /// Mouse Wheel Ecore event type.
63         /// </summary>
64         /// <since_tizen> preview </since_tizen>
65         public static readonly EcoreEventType MouseWheel = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_WHEEL");
66         /// <summary>
67         /// Mouse In Ecore event type.
68         /// </summary>
69         /// <since_tizen> preview </since_tizen>
70         public static readonly EcoreEventType MouseIn = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_IN");
71         /// <summary>
72         /// Mouse Out Ecore event type.
73         /// </summary>
74         /// <since_tizen> preview </since_tizen>
75         public static readonly EcoreEventType MouseOut = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_OUT");
76
77         private string _lib;
78         private string _name;
79         private int _typeValue;
80
81         private EcoreEventType(string lib, string name)
82         {
83             _lib = lib;
84             _name = name;
85             _typeValue = -1;
86         }
87
88         /// <summary>
89         /// Gets the value associated with the specified type.
90         /// </summary>
91         /// <returns>The value of type.</returns>
92         /// <since_tizen> preview </since_tizen>
93         public int GetValue()
94         {
95             if (_typeValue < 0)
96             {
97                 IntPtr hDll = Interop.Libdl.LoadLibrary(_lib);
98                 if (hDll != IntPtr.Zero)
99                 {
100                     IntPtr pValue = Interop.Libdl.GetProcAddress(hDll, _name);
101                     if (pValue != IntPtr.Zero)
102                     {
103                         _typeValue = Marshal.ReadInt32(pValue);
104                     }
105                     Interop.Libdl.FreeLibrary(hDll);
106                 }
107             }
108             return _typeValue;
109         }
110     }
111
112     /// <summary>
113     /// The EcoreEvent is a class to help create events that are being notified of events.
114     /// </summary>
115     /// <typeparam name="TEventArgs">Kinds of EventArgs.</typeparam>
116     /// <since_tizen> preview </since_tizen>
117     public class EcoreEvent<TEventArgs> : IDisposable where TEventArgs : EventArgs
118     {
119         /// <summary>
120         /// EventInfoParser delegate of the EcoreEvent class.
121         /// </summary>
122         /// <param name="data">IntPtr</param>
123         /// <param name="type">EcoreEventType</param>
124         /// <param name="info">IntPtr</param>
125         /// <returns></returns>
126         /// <since_tizen> preview </since_tizen>
127         public delegate TEventArgs EventInfoParser(IntPtr data, EcoreEventType type, IntPtr info);
128
129         private bool _disposed = false;
130         private EcoreEventType _eventType;
131         private readonly EventInfoParser _parser;
132         private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
133
134         /// <summary>
135         /// Creates and initializes a new instance of the EcoreEvent class.
136         /// </summary>
137         /// <param name="type">EcoreEventType</param>
138         /// <since_tizen> preview </since_tizen>
139         public EcoreEvent(EcoreEventType type) : this(type, null)
140         {
141         }
142
143         /// <summary>
144         /// Creates and initializes a new instance of the EcoreEvent class.
145         /// </summary>
146         /// <param name="type">EcoreEventType</param>
147         /// <param name="parser">EventInfoParser</param>
148         /// <since_tizen> preview </since_tizen>
149         public EcoreEvent(EcoreEventType type, EventInfoParser parser)
150         {
151             _eventType = type;
152             _parser = parser;
153         }
154
155         /// <summary>
156         /// Destructor for the EcoreEvent class.
157         /// </summary>
158         ~EcoreEvent()
159         {
160             Dispose(false);
161         }
162
163         private struct NativeCallback
164         {
165             public Interop.Ecore.EcoreEventCallback callback;
166             public IntPtr nativeHandler;
167             public EventHandler<TEventArgs> eventHandler;
168         }
169
170         /// <summary>
171         /// On Event Handler of the EcoreEvent.
172         /// </summary>
173         /// <since_tizen> preview </since_tizen>
174         public event EventHandler<TEventArgs> On
175         {
176             add
177             {
178                 EventHandler<TEventArgs> handler = value;
179                 var cb = new Interop.Ecore.EcoreEventCallback((data, type, info) =>
180                 {
181                     TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(data, _eventType, info);
182                     handler(this, ea);
183                     return true;
184                 });
185                 IntPtr hNative = Interop.Ecore.ecore_event_handler_add(_eventType.GetValue(), cb, IntPtr.Zero);
186                 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler, nativeHandler = hNative });
187             }
188             remove
189             {
190                 EventHandler<TEventArgs> handler = value;
191                 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
192                 foreach (var cb in callbacks)
193                 {
194                     Interop.Ecore.ecore_event_handler_del(cb.nativeHandler);
195                 }
196             }
197         }
198
199         /// <summary>
200         /// Releases all the resources currently used by this instance.
201         /// </summary>
202         /// <param name="disposing">
203         /// true if the managed resources should be disposed,
204         /// otherwise false.
205         /// </param>
206         /// <since_tizen> preview </since_tizen>
207         protected virtual void Dispose(bool disposing)
208         {
209             if (!_disposed)
210             {
211                 if (disposing)
212                 {
213                     // Place holder to dispose managed state (managed objects).
214                 }
215                 foreach (var cb in _nativeCallbacks)
216                 {
217                     Interop.Ecore.ecore_event_handler_del(cb.nativeHandler);
218                 }
219                 _nativeCallbacks.Clear();
220                 _disposed = true;
221             }
222         }
223
224         /// <summary>
225         /// Destroys the current object.
226         /// </summary>
227         /// <since_tizen> preview </since_tizen>
228         public void Dispose()
229         {
230             Dispose(true);
231             GC.SuppressFinalize(this);
232         }
233     }
234
235     /// <summary>
236     /// The event class for EcoreEvent.
237     /// </summary>
238     /// <since_tizen> preview </since_tizen>
239     public class EcoreEvent : EcoreEvent<EventArgs>
240     {
241         /// <summary>
242         /// Creates and initializes a new instance of the EcoreEvent class.
243         /// </summary>
244         /// <param name="type">EcoreEventType</param>
245         /// <since_tizen> preview </since_tizen>
246         public EcoreEvent(EcoreEventType type) : base(type)
247         {
248         }
249     }
250 }
251