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
31 static internal void Preload()
33 // Do nothing. Just call for load static values.
39 /// <since_tizen> 3 </since_tizen>
40 [Obsolete("Deprecated in API9, will be removed in API11, Use SwigCMemOwn")]
41 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
42 protected bool swigCMemOwn;
45 /// The flag to check if it is already disposed of.
47 /// <since_tizen> 3 </since_tizen>
48 [Obsolete("Deprecated in API9, will be removed in API11, Use Disposed")]
49 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
50 protected bool disposed = false;
52 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
53 private bool registerMe;
55 //The flag to check who called Dispose(). (By User or DisposeQueue)
56 private bool isDisposeQueued = false;
59 /// Create an instance of BaseHandle.
61 /// <since_tizen> 3 </since_tizen>
62 public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle(), true, false)
64 // Note : Empty BaseHandle instance don't need to be register in Registry.
65 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
69 /// Create an instance of BaseHandle.
71 /// <param name="handle">The BaseHandle instance.</param>
72 /// <since_tizen> 3 </since_tizen>
73 public BaseHandle(BaseHandle handle) : this(Interop.BaseHandle.NewBaseHandle(BaseHandle.getCPtr(handle)), true, false)
75 // Note : Copyed BaseHandle instance don't need to be register in Registry.
76 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
79 internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister)
81 //to catch derived classes dali native exceptions
82 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
84 DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor with cMemeryOwn:{cMemoryOwn} and cRegister:{cRegister} START");
86 registerMe = cRegister;
87 swigCMemOwn = cMemoryOwn;
88 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
89 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
93 // Register this instance of BaseHandle in the registry.
94 Registry.Register(this);
97 disposeDebuggingCtor();
98 DebugFileLogging.Instance.WriteLog($" BaseHandle.contructor with cMemeryOwn and cRegister END");
99 DebugFileLogging.Instance.WriteLog($"=============================");
102 internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
104 //to catch derived classes dali native exceptions
105 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
107 DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor with cMemeryOwn:{cMemoryOwn} START");
109 registerMe = swigCMemOwn = cMemoryOwn;
110 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
111 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
115 // Register this instance of BaseHandle in the registry.
116 Registry.Register(this);
119 disposeDebuggingCtor();
120 DebugFileLogging.Instance.WriteLog($" BaseHandle.contructor with cMemeryOwn END");
121 DebugFileLogging.Instance.WriteLog($"=============================");
124 internal BaseHandle(global::System.IntPtr cPtr)
126 DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor START");
128 registerMe = swigCMemOwn = true;
130 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
131 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
135 // Register this instance of BaseHandle in the registry.
136 Registry.Register(this);
139 disposeDebuggingCtor();
140 DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor END");
141 DebugFileLogging.Instance.WriteLog($"=============================");
147 /// <since_tizen> 3 </since_tizen>
148 // following this guide: https://docs.microsoft.com/ko-kr/dotnet/fundamentals/code-analysis/quality-rules/ca1063?view=vs-2019 (CA1063)
149 ~BaseHandle() => Dispose(false);
152 /// Event when a property is set.
154 /// <since_tizen> 5 </since_tizen>
155 /// <seealso cref="BindableObject.PropertyChanged"/>
156 [Obsolete("Deprecated in API9, will be removed in API11, Use BindableObject.PropertyChanged instead.")]
157 public event PropertyChangedEventHandler PropertySet;
159 internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
168 /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
170 /// <since_tizen> 3 </since_tizen>
171 public static bool operator true(BaseHandle handle)
173 // if the C# object is null, return false
174 if (BaseHandle.ReferenceEquals(handle, null))
178 // returns true if the handle has a body, false otherwise
179 return handle.HasBody();
183 /// Returns the bool false to indicate that an operand is false and returns true otherwise.
185 /// <since_tizen> 3 </since_tizen>
186 public static bool operator false(BaseHandle handle)
188 // if the C# object is null, return true
189 if (BaseHandle.ReferenceEquals(handle, null))
193 return !handle.HasBody();
197 /// Explicit conversion from Handle to bool.
199 /// <since_tizen> 3 </since_tizen>
200 public static explicit operator bool(BaseHandle handle)
202 // if the C# object is null, return false
203 if (BaseHandle.ReferenceEquals(handle, null))
207 // returns true if the handle has a body, false otherwise
208 return handle.HasBody();
212 /// Equality operator
214 /// <since_tizen> 3 </since_tizen>
215 public static bool operator ==(BaseHandle x, BaseHandle y)
217 // if the C# objects are the same return true
218 if (BaseHandle.ReferenceEquals(x, y))
222 if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
224 // drop into native code to see if both handles point to the same body
228 if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
230 if (y.HasBody()) return false;
233 if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
235 if (x.HasBody()) return false;
243 /// Inequality operator. Returns Null if either operand is Null
245 /// <since_tizen> 3 </since_tizen>
246 public static bool operator !=(BaseHandle x, BaseHandle y)
252 /// Logical AND operator.<br />
253 /// It's possible when doing a operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
255 /// <since_tizen> 3 </since_tizen>
256 public static BaseHandle operator &(BaseHandle x, BaseHandle y)
266 /// Logical OR operator for ||.<br />
267 /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
269 /// <since_tizen> 3 </since_tizen>
270 public static BaseHandle operator |(BaseHandle x, BaseHandle y)
272 if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
274 if (x != null && x.HasBody())
278 if (y != null && y.HasBody())
288 /// Logical ! operator
290 /// <since_tizen> 3 </since_tizen>
291 public static bool operator !(BaseHandle x)
293 // if the C# object is null, return true
294 if (BaseHandle.ReferenceEquals(x, null))
308 /// <param name="o">The object should be compared.</param>
309 /// <returns>True if equal.</returns>
310 /// <since_tizen> 5 </since_tizen>
311 public override bool Equals(object o)
313 return base.Equals(o);
317 /// Gets the hash code of this baseHandle.
319 /// <returns>The hash code.</returns>
320 /// <since_tizen> 5 </since_tizen>
321 public override int GetHashCode()
323 return base.GetHashCode();
329 /// <since_tizen> 3 </since_tizen>
330 public void Dispose()
334 Dispose(DisposeTypes.Implicit);
340 GC.SuppressFinalize(this);
344 /// Hidden API (Inhouse API).
348 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
349 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
351 /// <param name="disposing">true in order to free managed objects</param>
352 // Protected implementation of Dispose pattern.
353 [EditorBrowsable(EditorBrowsableState.Never)]
354 protected virtual void Dispose(bool disposing)
363 // TODO: dispose managed state (managed objects).
364 // Explicit call. user calls Dispose()
366 //Throw exception if Dispose() is called in separate thread.
367 if (!Window.IsInstalled())
369 using var process = global::System.Diagnostics.Process.GetCurrentProcess();
370 var processId = process.Id;
371 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
372 var me = this.GetType().FullName;
374 DebugFileLogging.Instance.WriteLog("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
375 $" process:{processId} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
377 Tizen.Log.Fatal("NUI", $"[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
378 $" process:{processId} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
380 //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
381 // throw new global::System.InvalidOperationException("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
382 // $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
387 DebugFileLogging.Instance.WriteLog($"should not be here! (dead code) this will be removed!");
389 Tizen.Log.Fatal("NUI", $"[NUI] should not be here! (dead code) this will be removed!");
391 //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
392 // throw new global::System.Exception($"[NUI] should not be here! (dead code) this will be removed!");
393 Dispose(DisposeTypes.Implicit);
397 Dispose(DisposeTypes.Explicit);
402 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
403 if (!isDisposeQueued)
405 isDisposeQueued = true;
406 DisposeQueue.Instance.Add(this);
410 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
411 // TODO: set large fields to null.
416 /// Performs an action on this object with the given action name and attributes.
418 /// <param name="actionName">The command for the action.</param>
419 /// <param name="attributes">The list of attributes for the action.</param>
420 /// <returns>The action is performed by the object or not.</returns>
421 /// <since_tizen> 3 </since_tizen>
422 public bool DoAction(string actionName, PropertyMap attributes)
424 bool ret = Interop.BaseHandle.DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
425 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
430 /// Returns the type name for the Handle.<br />
431 /// Will return an empty string if the typename does not exist. This will happen for types that
432 /// have not registered with type-registry.
434 /// <returns>The type name. Empty string if the typename does not exist.</returns>
435 /// <since_tizen> 3 </since_tizen>
436 public string GetTypeName()
438 string ret = Interop.BaseHandle.GetTypeName(swigCPtr);
439 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
444 /// Returns the type info for the Handle.<br />
446 /// <param name="info">The type information.</param>
447 /// <returns>True If get the type info.</returns>
448 /// <since_tizen> 3 </since_tizen>
449 public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
451 bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtr, Tizen.NUI.TypeInfo.getCPtr(info));
452 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
457 /// Resets the handle.
459 /// <since_tizen> 3 </since_tizen>
461 /// This will be deprecated, please use Dispose() instead.
466 NUILog.Error("[ERROR] This(BaseHandle.Reset) will be deprecated, please use Dispose() instead!");
470 /// To check the BaseHandle instance is equal or not.
472 /// <param name="rhs">The baseHandle instance.</param>
473 /// <returns>True If equal.</returns>
474 /// <since_tizen> 3 </since_tizen>
475 public bool EqualTo(BaseHandle rhs)
477 bool ret = Interop.BaseHandle.EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
478 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
483 /// To check the BaseHandle instance is equal or not.
485 /// <param name="rhs">The baseHandle instance.</param>
486 /// <returns>True If not equal.</returns>
487 /// <since_tizen> 3 </since_tizen>
488 public bool NotEqualTo(BaseHandle rhs)
490 bool ret = Interop.BaseHandle.NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
491 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
496 /// To check the BaseHandle instance has body or not.
498 /// <returns>True If the baseHandle instance has body.</returns>
499 /// <since_tizen> 3 </since_tizen>
500 public bool HasBody()
502 if (swigCPtr.Handle == IntPtr.Zero)
507 if (disposed == true)
511 bool ret = Interop.BaseHandle.HasBody(swigCPtr);
512 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
517 /// To check the BaseHandle instance is equal or not.
519 /// <param name="rhs">The baseHandle instance.</param>
520 /// <returns>True If equal.</returns>
521 /// <since_tizen> 3 </since_tizen>
522 public bool IsEqual(BaseHandle rhs)
524 if (disposed == true || rhs == null || !rhs.HasBody())
529 bool ret = Interop.BaseHandle.IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
530 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
534 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
536 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
539 internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
541 PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
544 internal void UnregisterFromRegistry()
548 Registry.Unregister(this);
556 /// <since_tizen> 3 </since_tizen>
557 protected virtual void Dispose(DisposeTypes type)
564 DebugFileLogging.Instance.WriteLog($"BaseHandle.Dispose({type}) START");
566 if (type == DisposeTypes.Explicit)
569 //Release your own managed resources here.
570 //You should release all of your own disposable objects here.
574 //Release your own unmanaged resources here.
575 //You should not access any managed member here except static instance.
576 //because the execution order of Finalizes is non-deterministic.
578 //Unreference this instance from Registry.
579 UnregisterFromRegistry();
581 disposeDebuggingDispose(type);
583 if (SwigCPtr.Handle != IntPtr.Zero)
585 var nativeSwigCPtr = swigCPtr.Handle;
586 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
590 ReleaseSwigCPtr(new global::System.Runtime.InteropServices.HandleRef(this, nativeSwigCPtr));
595 var me = this.GetType().FullName;
596 DebugFileLogging.Instance.WriteLog($"[ERR] SwigCPtr is NULL, need to check! me:{me}");
597 Log.Error("NUI", $"[ERR] SwigCPtr is NULL, need to check! me:{me}");
601 DebugFileLogging.Instance.WriteLog($"BaseHandle.Dispose({type}) END");
602 DebugFileLogging.Instance.WriteLog($"=============================");
603 NUILog.Debug($"BaseHandle.Dispose({type}) END");
604 NUILog.Debug($"=============================");
608 /// Release swigCPtr.
610 /// <since_tizen> 6 </since_tizen>
611 /// This will not be public opened.
612 [EditorBrowsable(EditorBrowsableState.Never)]
613 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
615 Interop.BaseHandle.DeleteBaseHandle(swigCPtr.Handle);
619 /// Contains event arguments for the FocusChangeRequested event.
621 [Obsolete("Deprecated in API9; Will be removed in API11.")]
622 public class FocusRequestArgs : EventArgs
626 /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
628 public bool Focus { get; set; }
631 /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
633 public bool Result { get; set; }
636 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
640 if (swigCPtr.Handle == IntPtr.Zero)
642 using var process = global::System.Diagnostics.Process.GetCurrentProcess();
643 var processId = process.Id;
644 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
645 var me = this.GetType().FullName;
647 Tizen.Log.Fatal("NUI", $"SwigCPtr Error! NUI's native dali object is already disposed. " +
648 $"OR the native dali object handle of NUI becomes null! \n" +
649 $" process:{processId} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
651 Tizen.Log.Fatal("NUI", $"[ERROR] back trace!");
652 global::System.Diagnostics.StackTrace st = new global::System.Diagnostics.StackTrace(true);
653 for (int i = 0; i < st.FrameCount; i++)
655 global::System.Diagnostics.StackFrame sf = st.GetFrame(i);
656 Tizen.Log.Fatal("NUI", " Method " + sf.GetMethod() + ":" + sf.GetFileName() + ":" + sf.GetFileLineNumber());
658 Tizen.Log.Fatal("NUI", "Error! just return here with null swigCPtr! this can cause unknown error or crash in next step");
660 //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
661 // throw new ObjectDisposedException(nameof(SwigCPtr), $"Error! NUI's native dali object is already disposed. " +
662 // $"OR the native dali object handle of NUI becomes null! \n" +
663 // $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
669 internal bool IsNativeHandleInvalid()
671 return swigCPtr.Handle == IntPtr.Zero;
677 [EditorBrowsable(EditorBrowsableState.Never)]
678 protected internal bool SwigCMemOwn => swigCMemOwn;
681 /// The flag to check if it is already disposed of.
683 [EditorBrowsable(EditorBrowsableState.Never)]
684 protected internal bool Disposed => disposed;
687 /// The flag to check if it is disposed by DisposeQueue.
689 [EditorBrowsable(EditorBrowsableState.Never)]
690 protected internal bool IsDisposeQueued => isDisposeQueued;
692 [Conditional("NUI_DISPOSE_DEBUG_ON")]
693 private void disposeDebuggingCtor()
695 DebugFileLogging.Instance.WriteLog($"type:{GetType()} copyNativeHandle:{swigCPtr.Handle.ToString("X8")}");
696 if (this is BaseComponents.View view)
698 DebugFileLogging.Instance.WriteLog($"ID:{view.ID} Name:{view.Name}");
700 global::System.Diagnostics.StackTrace st = new global::System.Diagnostics.StackTrace(true);
701 for (int i = 0; i < st.FrameCount; i++)
703 global::System.Diagnostics.StackFrame sf = st.GetFrame(i);
704 DebugFileLogging.Instance.WriteLog($"[{i}] {sf.GetMethod()}:{sf.GetFileName()}:{sf.GetFileLineNumber()}");
709 [Conditional("NUI_DISPOSE_DEBUG_ON")]
710 private void disposeDebuggingDispose(DisposeTypes type)
712 DebugFileLogging.Instance.WriteLog($"swigCMemOwn:{swigCMemOwn} type:{GetType()} copyNativeHandle:{swigCPtr.Handle.ToString("X8")} HasBody:{HasBody()}");
716 using var currentProcess = global::System.Diagnostics.Process.GetCurrentProcess();
717 var process = currentProcess.Id;
718 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
719 var me = this.GetType().FullName;
720 var daliId = "unknown";
721 var hash = this.GetType().GetHashCode();
722 var name = "unknown";
724 if (this is BaseComponents.View)
726 daliId = Interop.Actor.GetId(swigCPtr).ToString();
727 name = Interop.Actor.GetName(swigCPtr);
728 BaseObject tmp = new BaseObject(Interop.BaseHandle.GetBaseObject(swigCPtr), false);
729 var refCnt = tmp.ReferenceCount();
733 DebugFileLogging.Instance.WriteLog($"[ERR] reference count is over than 2. Could be a memory leak. Need to check! \n" +
734 $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me} \n" +
735 $" disposeType:{type}, name:{name}, daliID:{daliId}, hash:{hash}, refCnt:{refCnt}");
737 Log.Error("NUI", $"[ERR] reference count is over than 2. Could be a memory leak. Need to check! \n" +
738 $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me} \n" +
739 $" disposeType:{type}, name:{name}, daliID:{daliId}, hash:{hash}, refCnt:{refCnt}");