78f8ac4bca2b29780c4856f6e1751ffd43dc241c
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.WindowSystem / src / public / KVMService.cs
1 /*
2  * Copyright(c) 2023 Samsung Electronics Co., Ltd.
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
18 using System;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.WindowSystem.Shell
22 {
23     /// <summary>
24     /// Class for the Tizen KVM service.
25     /// </summary>
26     /// This class is need to be hidden as inhouse API.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class KVMService : IDisposable
29     {
30         private TizenShell _tzsh;
31         private IntPtr _kvmService;
32         private int _tzshWin;
33         private bool disposed = false;
34         private bool isDisposeQueued = false;
35
36         private Interop.KVMService.KVMDragStartEventCallback _onDragStarted;
37         private Interop.KVMService.KVMDragEndEventCallback _onDragEnded;
38
39         private event EventHandler _dragStarted;
40         private event EventHandler _dragEnded;
41
42         /// <summary>
43         /// Creates a new KVM Service handle.
44         /// </summary>
45         /// <param name="tzShell">The TizenShell instance.</param>
46         /// <param name="win">The window to provide service of the quickpanel.</param>
47         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
48         /// <exception cref="ArgumentNullException">Thrown when a argument is null.</exception>
49         public KVMService(TizenShell tzShell, Window win)
50         {
51             if (tzShell == null)
52             {
53                 throw new ArgumentNullException(nameof(tzShell));
54             }
55             if (tzShell.GetNativeHandle() == IntPtr.Zero)
56             {
57                 throw new ArgumentException("tzShell is not initialized.");
58             }
59             if (win == null)
60             {
61                 throw new ArgumentNullException(nameof(win));
62             }
63
64             _tzsh = tzShell;
65             _tzshWin = win.GetNativeId();
66             _kvmService = Interop.KVMService.Create(_tzsh.GetNativeHandle(), (IntPtr)_tzshWin);
67             if (_kvmService == IntPtr.Zero)
68             {
69                 int err = Tizen.Internals.Errors.ErrorFacts.GetLastResult();
70                 _tzsh.ErrorCodeThrow(err);
71             }
72         }
73
74         /// <summary>
75         /// Destructor.
76         /// </summary>
77         ~KVMService()
78         {
79             if (!isDisposeQueued)
80             {
81                 isDisposeQueued = true;
82                 DisposeQueue.Instance.Add(this);
83             }
84         }
85
86         /// <summary>
87         /// Dispose.
88         /// </summary>
89         public void Dispose()
90         {
91             if (isDisposeQueued)
92             {
93                 Dispose(DisposeTypes.Implicit);
94             }
95             else
96             {
97                 Dispose(DisposeTypes.Explicit);
98                 GC.SuppressFinalize(this);
99             }
100         }
101
102         /// <inheritdoc/>
103         protected virtual void Dispose(DisposeTypes type)
104         {
105             if (!disposed)
106             {
107                 if (_kvmService != IntPtr.Zero)
108                 {
109                     int res = Interop.KVMService.Destroy(_kvmService);
110                     _kvmService = IntPtr.Zero;
111                 }
112                 disposed = true;
113             }
114         }
115
116         /// <summary>
117         /// Emits the event when the drag started from any window.
118         /// </summary>
119         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
120         public event EventHandler DragStarted
121         {
122             add
123             {
124                 if (_dragStarted == null)
125                 {
126                     _onDragStarted = OnDragStarted;
127                     int res = Interop.KVMService.SetDragStartEventHandler(_kvmService, _onDragStarted, IntPtr.Zero);
128                     _tzsh.ErrorCodeThrow(res);
129                 }
130                 _dragStarted += value;
131             }
132             remove
133             {
134                 _dragStarted -= value;
135                 if (_dragStarted == null)
136                 {
137                     int res = Interop.KVMService.SetDragStartEventHandler(_kvmService, null, IntPtr.Zero);
138                     _tzsh.ErrorCodeThrow(res);
139                 }
140             }
141         }
142
143         /// <summary>
144         /// Emits the event when the drag ended on any window except KVM window.
145         /// </summary>
146         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
147         public event EventHandler DragEnded
148         {
149             add
150             {
151                 if (_dragEnded == null)
152                 {
153                     _onDragEnded = OnDragEnded;
154                     int res = Interop.KVMService.SetDragEndEventHandler(_kvmService, _onDragEnded, IntPtr.Zero);
155                     _tzsh.ErrorCodeThrow(res);
156                 }
157                 _dragEnded += value;
158             }
159             remove
160             {
161                 _dragEnded -= value;
162                 if (_dragEnded == null)
163                 {
164                     int res = Interop.KVMService.SetDragEndEventHandler(_kvmService, null, IntPtr.Zero);
165                     _tzsh.ErrorCodeThrow(res);
166                 }
167             }
168         }
169
170         private void OnDragStarted(IntPtr data, IntPtr softkeyService)
171         {
172             _dragStarted?.Invoke(this, EventArgs.Empty);
173         }
174
175         private void OnDragEnded(IntPtr data, IntPtr softkeyService)
176         {
177             _dragEnded?.Invoke(this, EventArgs.Empty);
178         }
179
180         /// <summary>
181         /// Requests to perform drop to KVM window.
182         /// </summary>
183         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
184         public void PerformDrop()
185         {
186             int res = Interop.KVMService.PerformDrop(_kvmService);
187             _tzsh.ErrorCodeThrow(res);
188         }
189
190         /// <summary>
191         /// Requests to cancel current drag.
192         /// </summary>
193         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
194         public void CancelDrag()
195         {
196             int res = Interop.KVMService.CancelDrag(_kvmService);
197             _tzsh.ErrorCodeThrow(res);
198         }
199
200         /// <summary>
201         /// Requests to receive the current drag data.
202         /// the drag data will be received by the DragEvent of the window.
203         /// </summary>
204         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
205         public void ReceiveDragData(string mimeType)
206         {
207             int res = Interop.KVMService.ReceiveDragData(_kvmService, mimeType);
208             _tzsh.ErrorCodeThrow(res);
209         }
210
211         /// <summary>
212         /// Requests to set KVM window as secondary selection window.
213         /// </summary>
214         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
215         public void SetSecondarySelction()
216         {
217             int res = Interop.KVMService.SetSecondarySelection(_kvmService);
218             _tzsh.ErrorCodeThrow(res);
219         }
220
221         /// <summary>
222         /// Requests to unset secondary selection window of KVM window.
223         /// </summary>
224         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
225         public void UnsetSecondarySelction()
226         {
227             int res = Interop.KVMService.UnsetSecondarySelection(_kvmService);
228             _tzsh.ErrorCodeThrow(res);
229         }
230     }
231 }