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
107 public delegate TEventArgs EventInfoParser(IntPtr data, EcoreEventType type, IntPtr info);
109 private bool _disposed = false;
110 private EcoreEventType _eventType;
111 private readonly EventInfoParser _parser;
112 private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
115 /// Creates and initializes a new instance of the EcoreEvent class.
117 /// <param name="type">EcoreEventType</param>
118 public EcoreEvent(EcoreEventType type) : this(type, null)
123 /// Creates and initializes a new instance of the EcoreEvent class.
125 /// <param name="type">EcoreEventType</param>
126 /// <param name="parser">EventInfoParser</param>
127 public EcoreEvent(EcoreEventType type, EventInfoParser parser)
138 private struct NativeCallback
140 public Interop.Ecore.EcoreEventCallback callback;
141 public IntPtr nativeHandler;
142 public EventHandler<TEventArgs> eventHandler;
146 /// On Event Handler of EcoreEvent.
148 public event EventHandler<TEventArgs> On
152 EventHandler<TEventArgs> handler = value;
153 var cb = new Interop.Ecore.EcoreEventCallback((data, type, info) =>
155 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(data, _eventType, info);
158 IntPtr hNative = Interop.Ecore.ecore_event_handler_add(_eventType.GetValue(), cb, IntPtr.Zero);
159 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler, nativeHandler = hNative });
163 EventHandler<TEventArgs> handler = value;
164 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
165 foreach (var cb in callbacks)
167 Interop.Ecore.ecore_event_handler_del(cb.nativeHandler);
172 protected virtual void Dispose(bool disposing)
178 // Place holder to dispose managed state (managed objects).
180 foreach (var cb in _nativeCallbacks)
182 Interop.Ecore.ecore_event_handler_del(cb.nativeHandler);
184 _nativeCallbacks.Clear();
189 public void Dispose()
192 GC.SuppressFinalize(this);
197 /// Event class for EcoreEvent
199 public class EcoreEvent : EcoreEvent<EventArgs>
202 /// Creates and initializes a new instance of the EcoreEvent class.
204 /// <param name="type">EcoreEventType</param>
205 public EcoreEvent(EcoreEventType type) : base(type)