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;
20 using System.Runtime.InteropServices;
25 /// The EcoreEventType is type of EcoreEvent.
26 /// It includes some predefined instance.
28 public class EcoreEventType
31 /// Key down Ecore event type.
33 public static readonly EcoreEventType KeyDown = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_KEY_DOWN");
35 /// Key Up Ecore event type.
37 public static readonly EcoreEventType KeyUp = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_KEY_UP");
39 /// Mouse Button Down Ecore event type.
41 public static readonly EcoreEventType MouseButtonDown = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_BUTTON_DOWN");
43 /// Mouse Button Up Ecore event type.
45 public static readonly EcoreEventType MouseButtonUp = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_BUTTON_UP");
47 /// Mouse Button Cancel Ecore event type.
49 public static readonly EcoreEventType MouseButtonCancel = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_BUTTON_CANCEL");
51 /// Mouse Move Ecore event type.
53 public static readonly EcoreEventType MouseMove = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_MOVE");
55 /// Mouse Wheel Ecore event type.
57 public static readonly EcoreEventType MouseWheel = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_WHEEL");
59 /// Mouse In Ecore event type.
61 public static readonly EcoreEventType MouseIn = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_IN");
63 /// Mouse Out Ecore event type.
65 public static readonly EcoreEventType MouseOut = new EcoreEventType(Interop.Libraries.EcoreInput, "ECORE_EVENT_MOUSE_OUT");
69 private int _typeValue;
71 private EcoreEventType(string lib, string name)
79 /// Gets the value associated with the specified type.
81 /// <returns>The value of type.</returns>
86 IntPtr hDll = Interop.Libdl.LoadLibrary(_lib);
87 if (hDll != IntPtr.Zero)
89 IntPtr pValue = Interop.Libdl.GetProcAddress(hDll, _name);
90 if (pValue != IntPtr.Zero)
92 _typeValue = Marshal.ReadInt32(pValue);
94 Interop.Libdl.FreeLibrary(hDll);
102 /// The EcoreEvent is a class to help to create events are being notified of events.
104 /// <typeparam name="TEventArgs">Kinds of EventArgs</typeparam>
105 public class EcoreEvent<TEventArgs> : IDisposable where TEventArgs : EventArgs
108 /// EventInfoParser delegate of EcoreEvent class
110 /// <param name="data">IntPtr</param>
111 /// <param name="type">EcoreEventType</param>
112 /// <param name="info">IntPtr</param>
113 /// <returns></returns>
114 public delegate TEventArgs EventInfoParser(IntPtr data, EcoreEventType type, IntPtr info);
116 private bool _disposed = false;
117 private EcoreEventType _eventType;
118 private readonly EventInfoParser _parser;
119 private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
122 /// Creates and initializes a new instance of the EcoreEvent class.
124 /// <param name="type">EcoreEventType</param>
125 public EcoreEvent(EcoreEventType type) : this(type, null)
130 /// Creates and initializes a new instance of the EcoreEvent class.
132 /// <param name="type">EcoreEventType</param>
133 /// <param name="parser">EventInfoParser</param>
134 public EcoreEvent(EcoreEventType type, EventInfoParser parser)
141 /// Destructor for EcoreEvent class.
148 private struct NativeCallback
150 public Interop.Ecore.EcoreEventCallback callback;
151 public IntPtr nativeHandler;
152 public EventHandler<TEventArgs> eventHandler;
156 /// On Event Handler of EcoreEvent.
158 public event EventHandler<TEventArgs> On
162 EventHandler<TEventArgs> handler = value;
163 var cb = new Interop.Ecore.EcoreEventCallback((data, type, info) =>
165 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(data, _eventType, info);
168 IntPtr hNative = Interop.Ecore.ecore_event_handler_add(_eventType.GetValue(), cb, IntPtr.Zero);
169 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler, nativeHandler = hNative });
173 EventHandler<TEventArgs> handler = value;
174 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
175 foreach (var cb in callbacks)
177 Interop.Ecore.ecore_event_handler_del(cb.nativeHandler);
183 /// Releases all resources currently used by this instance.
185 /// <param name="disposing">
186 /// true if managed resources should be disposed
187 /// otherwise, false.
189 protected virtual void Dispose(bool disposing)
195 // Place holder to dispose managed state (managed objects).
197 foreach (var cb in _nativeCallbacks)
199 Interop.Ecore.ecore_event_handler_del(cb.nativeHandler);
201 _nativeCallbacks.Clear();
207 /// Destroy current object
209 public void Dispose()
212 GC.SuppressFinalize(this);
217 /// Event class for EcoreEvent
219 public class EcoreEvent : EcoreEvent<EventArgs>
222 /// Creates and initializes a new instance of the EcoreEvent class.
224 /// <param name="type">EcoreEventType</param>
225 public EcoreEvent(EcoreEventType type) : base(type)