2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
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.ComponentModel;
19 using System.Runtime.CompilerServices;
20 using Tizen.NUI.Binding;
21 using global::System.Diagnostics;
26 /// BaseHandle is a handle to an internal Dali resource.
28 /// <since_tizen> 3 </since_tizen>
29 public class BaseHandle : Element, global::System.IDisposable
34 /// <since_tizen> 3 </since_tizen>
35 [Obsolete("Deprecated in API9, will be removed in API11, Use SwigCMemOwn")]
36 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
37 protected bool swigCMemOwn;
40 /// The flag to check if it is already disposed of.
42 /// <since_tizen> 3 </since_tizen>
43 [Obsolete("Deprecated in API9, will be removed in API11, Use Disposed")]
44 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
45 protected bool disposed = false;
47 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
48 private global::System.Runtime.InteropServices.HandleRef swigCPtrCopy;
49 private bool registerMe;
51 //The flag to check who called Dispose(). (By User or DisposeQueue)
52 private bool isDisposeQueued = false;
55 /// Create an instance of BaseHandle.
57 /// <since_tizen> 3 </since_tizen>
58 public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle())
60 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
64 /// Create an instance of BaseHandle.
66 /// <param name="handle">The BaseHandle instance.</param>
67 /// <since_tizen> 3 </since_tizen>
68 public BaseHandle(BaseHandle handle) : this(Interop.BaseHandle.NewBaseHandle(BaseHandle.getCPtr(handle)))
70 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
73 internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
75 //to catch derived classes dali native exceptions
76 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
78 DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor with cMemeryOwn:{cMemoryOwn} START");
80 registerMe = swigCMemOwn = cMemoryOwn;
81 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
82 // using copy constructor to create another native handle so Registry.Unregister works fine.
83 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(swigCPtr));
84 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
88 // Register this instance of BaseHandle in the registry.
89 Registry.Register(this);
92 disposeDebuggingCtor();
93 DebugFileLogging.Instance.WriteLog($" BaseHandle.contructor with cMemeryOwn END");
94 DebugFileLogging.Instance.WriteLog($"=============================");
97 internal BaseHandle(global::System.IntPtr cPtr)
99 DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor START");
101 registerMe = swigCMemOwn = true;
103 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
105 // using copy constructor to create another native handle so Registry.Unregister works fine.
106 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(SwigCPtr));
107 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
111 // Register this instance of BaseHandle in the registry.
112 Registry.Register(this);
115 disposeDebuggingCtor();
116 DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor END");
117 DebugFileLogging.Instance.WriteLog($"=============================");
123 /// <since_tizen> 3 </since_tizen>
124 // following this guide: https://docs.microsoft.com/ko-kr/dotnet/fundamentals/code-analysis/quality-rules/ca1063?view=vs-2019 (CA1063)
125 ~BaseHandle() => Dispose(false);
128 /// Event when a property is set.
130 /// <since_tizen> 5 </since_tizen>
131 /// <seealso cref="BindableObject.PropertyChanged"/>
132 [Obsolete("Deprecated in API9, will be removed in API11, Use BindableObject.PropertyChanged instead.")]
133 public event PropertyChangedEventHandler PropertySet;
135 internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
144 /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
146 /// <since_tizen> 3 </since_tizen>
147 public static bool operator true(BaseHandle handle)
149 // if the C# object is null, return false
150 if (BaseHandle.ReferenceEquals(handle, null))
154 // returns true if the handle has a body, false otherwise
155 return handle.HasBody();
159 /// Returns the bool false to indicate that an operand is false and returns true otherwise.
161 /// <since_tizen> 3 </since_tizen>
162 public static bool operator false(BaseHandle handle)
164 // if the C# object is null, return true
165 if (BaseHandle.ReferenceEquals(handle, null))
169 return !handle.HasBody();
173 /// Explicit conversion from Handle to bool.
175 /// <since_tizen> 3 </since_tizen>
176 public static explicit operator bool(BaseHandle handle)
178 // if the C# object is null, return false
179 if (BaseHandle.ReferenceEquals(handle, null))
183 // returns true if the handle has a body, false otherwise
184 return handle.HasBody();
188 /// Equality operator
190 /// <since_tizen> 3 </since_tizen>
191 public static bool operator ==(BaseHandle x, BaseHandle y)
193 // if the C# objects are the same return true
194 if (BaseHandle.ReferenceEquals(x, y))
198 if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
200 // drop into native code to see if both handles point to the same body
204 if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
206 if (y.HasBody()) return false;
209 if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
211 if (x.HasBody()) return false;
219 /// Inequality operator. Returns Null if either operand is Null
221 /// <since_tizen> 3 </since_tizen>
222 public static bool operator !=(BaseHandle x, BaseHandle y)
228 /// Logical AND operator.<br />
229 /// It's possible when doing a operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
231 /// <since_tizen> 3 </since_tizen>
232 public static BaseHandle operator &(BaseHandle x, BaseHandle y)
242 /// Logical OR operator for ||.<br />
243 /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
245 /// <since_tizen> 3 </since_tizen>
246 public static BaseHandle operator |(BaseHandle x, BaseHandle y)
248 if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
250 if (x != null && x.HasBody())
254 if (y != null && y.HasBody())
264 /// Logical ! operator
266 /// <since_tizen> 3 </since_tizen>
267 public static bool operator !(BaseHandle x)
269 // if the C# object is null, return true
270 if (BaseHandle.ReferenceEquals(x, null))
284 /// <param name="o">The object should be compared.</param>
285 /// <returns>True if equal.</returns>
286 /// <since_tizen> 5 </since_tizen>
287 public override bool Equals(object o)
289 return base.Equals(o);
293 /// Gets the hash code of this baseHandle.
295 /// <returns>The hash code.</returns>
296 /// <since_tizen> 5 </since_tizen>
297 public override int GetHashCode()
299 return base.GetHashCode();
305 /// <since_tizen> 3 </since_tizen>
306 public void Dispose()
310 Dispose(DisposeTypes.Implicit);
316 GC.SuppressFinalize(this);
320 /// Hidden API (Inhouse API).
324 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
325 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
327 /// <param name="disposing">true in order to free managed objects</param>
328 // Protected implementation of Dispose pattern.
329 [EditorBrowsable(EditorBrowsableState.Never)]
330 protected virtual void Dispose(bool disposing)
339 // TODO: dispose managed state (managed objects).
340 // Explicit call. user calls Dispose()
342 //Throw exception if Dispose() is called in separate thread.
343 if (!Window.IsInstalled())
345 var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
346 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
347 var me = this.GetType().FullName;
349 DebugFileLogging.Instance.WriteLog("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
350 $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
352 Tizen.Log.Fatal("NUI", $"[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
353 $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
355 //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
356 // throw new global::System.InvalidOperationException("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
357 // $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
362 DebugFileLogging.Instance.WriteLog($"should not be here! (dead code) this will be removed!");
364 Tizen.Log.Fatal("NUI", $"[NUI] should not be here! (dead code) this will be removed!");
366 //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
367 // throw new global::System.Exception($"[NUI] should not be here! (dead code) this will be removed!");
368 Dispose(DisposeTypes.Implicit);
372 Dispose(DisposeTypes.Explicit);
377 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
378 if (!isDisposeQueued)
380 isDisposeQueued = true;
381 DisposeQueue.Instance.Add(this);
385 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
386 // TODO: set large fields to null.
391 /// Performs an action on this object with the given action name and attributes.
393 /// <param name="actionName">The command for the action.</param>
394 /// <param name="attributes">The list of attributes for the action.</param>
395 /// <returns>The action is performed by the object or not.</returns>
396 /// <since_tizen> 3 </since_tizen>
397 public bool DoAction(string actionName, PropertyMap attributes)
399 bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
400 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
405 /// Returns the type name for the Handle.<br />
406 /// Will return an empty string if the typename does not exist. This will happen for types that
407 /// have not registered with type-registry.
409 /// <returns>The type name. Empty string if the typename does not exist.</returns>
410 /// <since_tizen> 3 </since_tizen>
411 public string GetTypeName()
413 string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
414 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
419 /// Returns the type info for the Handle.<br />
421 /// <param name="info">The type information.</param>
422 /// <returns>True If get the type info.</returns>
423 /// <since_tizen> 3 </since_tizen>
424 public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
426 bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
427 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
432 /// Resets the handle.
434 /// <since_tizen> 3 </since_tizen>
436 /// This will be deprecated in next version, please use Dipose() instead.
440 NUILog.Error("[ERROR] This(BaseHandle.Reset) will be deprecated, please use Dispose() instead!");
444 /// To check the BaseHandle instance is equal or not.
446 /// <param name="rhs">The baseHandle instance.</param>
447 /// <returns>True If equal.</returns>
448 /// <since_tizen> 3 </since_tizen>
449 public bool EqualTo(BaseHandle rhs)
451 bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
452 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
457 /// To check the BaseHandle instance is equal or not.
459 /// <param name="rhs">The baseHandle instance.</param>
460 /// <returns>True If not equal.</returns>
461 /// <since_tizen> 3 </since_tizen>
462 public bool NotEqualTo(BaseHandle rhs)
464 bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
465 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
470 /// To check the BaseHandle instance has body or not.
472 /// <returns>True If the baseHandle instance has body.</returns>
473 /// <since_tizen> 3 </since_tizen>
474 public bool HasBody()
476 if (swigCPtrCopy.Handle == IntPtr.Zero)
481 if (disposed == true)
485 bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
486 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
491 /// To check the BaseHandle instance is equal or not.
493 /// <param name="rhs">The baseHandle instance.</param>
494 /// <returns>True If equal.</returns>
495 /// <since_tizen> 3 </since_tizen>
496 public bool IsEqual(BaseHandle rhs)
498 if (disposed == true || rhs == null || !rhs.HasBody())
503 bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
504 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
508 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
510 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
513 internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
515 PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
521 /// <since_tizen> 3 </since_tizen>
522 protected virtual void Dispose(DisposeTypes type)
529 DebugFileLogging.Instance.WriteLog($"BaseHandle.Dispose({type}) START");
531 if (type == DisposeTypes.Explicit)
534 //Release your own managed resources here.
535 //You should release all of your own disposable objects here.
539 //Release your own unmanaged resources here.
540 //You should not access any managed member here except static instance.
541 //because the execution order of Finalizes is non-deterministic.
543 //Unreference this instance from Registry.
546 Registry.Unregister(this);
549 disposeDebuggingDispose(type);
551 if (SwigCPtr.Handle != IntPtr.Zero)
556 ReleaseSwigCPtr(SwigCPtr);
558 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
562 var me = this.GetType().FullName;
563 DebugFileLogging.Instance.WriteLog($"[ERR] SwigCPtr is NULL, need to check! me:{me}");
564 Log.Error("NUI", $"[ERR] SwigCPtr is NULL, need to check! me:{me}");
567 if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
570 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
571 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
575 var me = this.GetType().FullName;
576 DebugFileLogging.Instance.WriteLog($"[ERR] swigCPtrCopy is NULL, need to check! me:{me}");
577 Log.Error("NUI", $"[ERR] swigCPtrCopy is NULL, need to check! me:{me}");
581 DebugFileLogging.Instance.WriteLog($"BaseHandle.Dispose({type}) END");
582 DebugFileLogging.Instance.WriteLog($"=============================");
583 NUILog.Debug($"BaseHandle.Dispose({type}) END");
584 NUILog.Debug($"=============================");
588 /// Release swigCPtr.
590 /// <since_tizen> 6 </since_tizen>
591 /// This will not be public opened.
592 [EditorBrowsable(EditorBrowsableState.Never)]
593 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
598 /// Contains event arguments for the FocusChangeRequested event.
600 [Obsolete("Deprecated in API9; Will be removed in API11.")]
601 public class FocusRequestArgs : EventArgs
605 /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
607 public bool Focus { get; set; }
610 /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
612 public bool Result { get; set; }
615 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
619 if (swigCPtr.Handle == IntPtr.Zero)
621 var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
622 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
623 var me = this.GetType().FullName;
625 Tizen.Log.Fatal("NUI", $"SwigCPtr Error! NUI's native dali object is already disposed. " +
626 $"OR the native dali object handle of NUI becomes null! \n" +
627 $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
629 Tizen.Log.Fatal("NUI", $"[ERROR] back trace!");
630 global::System.Diagnostics.StackTrace st = new global::System.Diagnostics.StackTrace(true);
631 for (int i = 0; i < st.FrameCount; i++)
633 global::System.Diagnostics.StackFrame sf = st.GetFrame(i);
634 Tizen.Log.Fatal("NUI", " Method " + sf.GetMethod());
636 Tizen.Log.Fatal("NUI", "Error! just return here with null swigCPtr! this can cause unknown error or crash in next step");
638 //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
639 // throw new ObjectDisposedException(nameof(SwigCPtr), $"Error! NUI's native dali object is already disposed. " +
640 // $"OR the native dali object handle of NUI becomes null! \n" +
641 // $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
647 internal bool IsNativeHandleInvalid()
649 return swigCPtr.Handle == IntPtr.Zero;
655 [EditorBrowsable(EditorBrowsableState.Never)]
656 protected internal bool SwigCMemOwn => swigCMemOwn;
659 /// The flag to check if it is already disposed of.
661 [EditorBrowsable(EditorBrowsableState.Never)]
662 protected internal bool Disposed => disposed;
665 /// The flag to check if it is disposed by DisposeQueue.
667 [EditorBrowsable(EditorBrowsableState.Never)]
668 protected internal bool IsDisposeQueued => isDisposeQueued;
670 [Conditional("NUI_DISPOSE_DEBUG_ON")]
671 private void disposeDebuggingCtor()
673 DebugFileLogging.Instance.WriteLog($"type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")}");
674 if (this is BaseComponents.View view)
676 DebugFileLogging.Instance.WriteLog($"ID:{view.ID} Name:{view.Name}");
678 global::System.Diagnostics.StackTrace st = new global::System.Diagnostics.StackTrace(true);
679 for (int i = 0; i < st.FrameCount; i++)
681 global::System.Diagnostics.StackFrame sf = st.GetFrame(i);
682 DebugFileLogging.Instance.WriteLog($"[{i}] {sf.GetMethod()}");
687 [Conditional("NUI_DISPOSE_DEBUG_ON")]
688 private void disposeDebuggingDispose(DisposeTypes type)
690 DebugFileLogging.Instance.WriteLog($"swigCMemOwn:{swigCMemOwn} type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")} HasBody:{HasBody()}");
692 if (swigCPtr.Handle != IntPtr.Zero && swigCPtrCopy.Handle != IntPtr.Zero && HasBody())
694 using var currentProcess = global::System.Diagnostics.Process.GetCurrentProcess();
695 var process = currentProcess.Id;
696 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
697 var me = this.GetType().FullName;
698 var daliId = "unknown";
699 var hash = this.GetType().GetHashCode();
700 var name = "unknown";
702 if (this is BaseComponents.View)
704 daliId = Interop.Actor.GetId(swigCPtrCopy).ToString();
705 name = Interop.Actor.GetName(swigCPtrCopy);
706 BaseObject tmp = new BaseObject(Interop.BaseHandle.GetBaseObject(swigCPtrCopy), false);
707 var refCnt = tmp.ReferenceCount();
711 DebugFileLogging.Instance.WriteLog($"[ERR] reference count is over than 2. Could be a memory leak. Need to check! \n" +
712 $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me} \n" +
713 $" disposeType:{type}, name:{name}, daliID:{daliId}, hash:{hash}, refCnt:{refCnt}");
715 Log.Error("NUI", $"[ERR] reference count is over than 2. Could be a memory leak. Need to check! \n" +
716 $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me} \n" +
717 $" disposeType:{type}, name:{name}, daliID:{daliId}, hash:{hash}, refCnt:{refCnt}");