VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/vboxwl.cpp@ 102092

Last change on this file since 102092 was 102044, checked in by vboxsync, 13 months ago

Additions: X11/Wayland: Another attempt to fix build (6), bugref:10194.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 27.3 KB
Line 
1/* $Id: vboxwl.cpp 102044 2023-11-09 17:17:11Z vboxsync $ */
2/** @file
3 * Guest Additions - Helper tool for grabbing input focus and perform
4 * drag-n-drop and clipboard sharing in Wayland.
5 */
6
7/*
8 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
9 *
10 * This file is part of VirtualBox base platform packages, as
11 * available from https://www.virtualbox.org.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation, in version 3 of the
16 * License.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <https://www.gnu.org/licenses>.
25 *
26 * SPDX-License-Identifier: GPL-3.0-only
27 */
28
29#include <stdlib.h>
30
31#include <iprt/initterm.h>
32#include <iprt/errcore.h>
33#include <iprt/message.h>
34#include <iprt/getopt.h>
35#include <iprt/stream.h>
36#include <iprt/assert.h>
37#include <iprt/asm.h>
38
39#include "product-generated.h"
40#include <iprt/buildconfig.h>
41
42#include <VBox/VBoxGuestLib.h>
43#include <VBox/version.h>
44#include <VBox/GuestHost/mime-type-converter.h>
45
46#include "VBoxClient.h"
47#include "wayland-helper-ipc.h"
48#include "vbox-gtk.h"
49#include "wayland-helper.h"
50#include "clipboard.h"
51
52/** Gtk App window default width. */
53#define VBOXWL_WINDOW_WIDTH (100)
54/** Gtk App window default height. */
55#define VBOXWL_WINDOW_HEIGHT (100)
56/** Gtk App window default transparency level. */
57#define VBOXWL_WINDOW_ALPHA (.1)
58/** Gtk App watchdog callback triggering interval. */
59#define VBOXWL_WATCHDOG_INTERVAL_MS (50)
60/** Gtk App exit timeout. */
61#define VBOXWL_EXIT_TIMEOUT_MS (500)
62
63#define VBOXWL_ARG_CLIP_HG_COPY_BIT RT_BIT(0)
64#define VBOXWL_ARG_CLIP_GH_ANNOUNCE_BIT RT_BIT(1)
65#define VBOXWL_ARG_CLIP_GH_COPY_BIT RT_BIT(2)
66
67/** Program name. */
68static char *g_pszProgName;
69
70/** Unused: just for linking purposes. */
71unsigned g_cRespawn = 0;
72
73/** A session ID which will be specified in communication messages
74 * with VBoxClient instance. */
75static uint32_t g_uSessionId = 0;
76
77/** One-shot session type. */
78static vbcl_wl_session_type_t g_enmSessionType = VBCL_WL_SESSION_TYPE_INVALID;
79
80/** Logging verbosity level. */
81unsigned g_cVerbosity = 0;
82
83/** Global flag to tell Gtk app to quit. */
84static uint64_t g_tsGtkQuit = 0;
85
86/** Gtk app thread. */
87static RTTHREAD g_AppThread;
88
89/** Gtk App window. */
90static GtkWidget *g_pWindow;
91
92/** Clipboard IPC flow object. */
93vbcl::ipc::clipboard::ClipboardIpc *g_oClipboardIpc;
94
95
96/************************************************************************************************
97 * Copy from guest clipboard.
98 ***********************************************************************************************/
99
100
101/**
102 * A callback to read guest clipboard data.
103 *
104 * @param pClipboard Pointer to Gtk clipboard object.
105 * @param pSelectionData Pointer to Gtk selection object.
106 * @param pvUser User data.
107 */
108static void vboxwl_gtk_clipboard_read(GtkClipboard* pClipboard,
109 GtkSelectionData* pSelectionData,
110 gpointer pvUser)
111{
112 guchar *pData;
113 gint cbData = -1;
114 int rc = VERR_INVALID_PARAMETER;
115
116 RT_NOREF(pClipboard, pvUser);
117
118 VBCL_LOG_CALLBACK;
119
120 /* Read data from guest clipboard. */
121 pData = (guchar *)gtk_selection_data_get_data_with_length(pSelectionData, &cbData);
122 if ( RT_VALID_PTR(pData)
123 && cbData > 0)
124 {
125 char *pcszMimeType = gdk_atom_name(gtk_selection_data_get_data_type(pSelectionData));
126 if (RT_VALID_PTR(pcszMimeType))
127 {
128 void *pvBufOut = NULL;
129 size_t cbBufOut = 0;
130
131 /* Convert guest clipboard into VBox representation. */
132 rc = VBoxMimeConvNativeToVBox(pcszMimeType, pData, cbData, &pvBufOut, &cbBufOut);
133 if (RT_SUCCESS(rc))
134 {
135 g_oClipboardIpc->m_pvClipboardBuf.set((uint64_t)pvBufOut);
136 g_oClipboardIpc->m_cbClipboardBuf.set((uint64_t)cbBufOut);
137 g_tsGtkQuit = RTTimeMilliTS();
138 }
139 else
140 VBClLogError("session %u: cannot convert guest clipboard: rc=%Rrc\n", g_uSessionId, rc);
141
142 g_free(pcszMimeType);
143 }
144 else
145 VBClLogError("session %u: guest provided no target type\n",
146 g_uSessionId);
147 }
148}
149
150/**
151 * Find first matching VBox format for given Gtk target.
152 *
153 * @returns VBox clipboard format or VBOX_SHCL_FMT_NONE if no match found..
154 * @param pTargets List of Gtk targets to match.
155 * @param cTargets Number of targets.
156 */
157static SHCLFORMATS vboxwl_gtk_match_formats(GdkAtom *pTargets, gint cTargets)
158{
159 SHCLFORMATS fFmts = VBOX_SHCL_FMT_NONE;
160
161 for (int i = 0; i < cTargets; i++)
162 {
163 gchar *sTargetName = gdk_atom_name(pTargets[i]);
164 if (RT_VALID_PTR(sTargetName))
165 {
166 fFmts |= VBoxMimeConvGetIdByMime(sTargetName);
167 g_free(sTargetName);
168 }
169 }
170
171 return fFmts;
172}
173
174/**
175 * Find matching Gtk target for given VBox format.
176 *
177 * @returns Gtk target or GDK_NONE if no match found.
178 * @param pTargets List of Gtk targets to match.
179 * @param cTargets Number of targets.
180 * @param uFmt VBox formats to match.
181 */
182static GdkAtom vboxwl_gtk_match_target(GdkAtom *pTargets, gint cTargets, SHCLFORMAT uFmt)
183{
184 GdkAtom match = GDK_NONE;
185
186 for (int i = 0; i < cTargets; i++)
187 {
188 gchar *sTargetName = gdk_atom_name(pTargets[i]);
189 if (RT_VALID_PTR(sTargetName))
190 {
191 if (uFmt == VBoxMimeConvGetIdByMime(sTargetName))
192 match = pTargets[i];
193
194 g_free(sTargetName);
195 }
196 }
197
198 return match;
199}
200
201/**
202 * Gtk callback to read guest clipboard content.
203 *
204 * @param pClipboard Pointer to Gtk clipboard object.
205 * @param pEvent Pointer to Gtk clipboard event.
206 * @param pvUser User data.
207 */
208static DECLCALLBACK(void) vboxwl_gtk_clipboard_get(GtkClipboard *pClipboard, GdkEvent *pEvent, gpointer pvUser)
209{
210 GdkAtom *pTargets;
211 gint cTargets;
212 gboolean fRc;
213
214 RT_NOREF(pEvent, pvUser);
215
216 VBCL_LOG_CALLBACK;
217
218 /* Wait for Gtk to offer available clipboard content. */
219 fRc = gtk_clipboard_wait_for_targets(pClipboard, &pTargets, &cTargets);
220 if (fRc)
221 {
222 /* Convert guest clipboard targets list into VBox representation. */
223 SHCLFORMATS fFormats = vboxwl_gtk_match_formats(pTargets, cTargets);
224 SHCLFORMAT uFmt;
225
226 /* Set formats to be sent to the host. */
227 g_oClipboardIpc->m_fFmts.set(fFormats);
228
229 /* Wait for host to send clipboard format it wants to copy from guest. */
230 uFmt = g_oClipboardIpc->m_uFmt.wait();
231 if (uFmt != g_oClipboardIpc->m_uFmt.defaults())
232 {
233 /* Find target which matches to host format among reported by guest. */
234 GdkAtom gtkFmt = vboxwl_gtk_match_target(pTargets, cTargets, uFmt);
235 if (gtkFmt != GDK_NONE)
236 gtk_clipboard_request_contents(pClipboard, gtkFmt, &vboxwl_gtk_clipboard_read, pvUser);
237 else
238 VBClLogVerbose(2, "session %u: will not send format 0x%x to host, not known to the guest\n",
239 g_uSessionId, uFmt);
240 }
241 else
242 VBClLogVerbose(2, "session %u: host did not send desired clipboard format in time\n", g_uSessionId);
243
244 g_free(pTargets);
245 }
246}
247
248
249/************************************************************************************************
250 * Paste into the guest clipboard.
251 ***********************************************************************************************/
252
253
254/**
255 * A callback to write data to the guest clipboard.
256 *
257 * @param pClipboard Pointer to Gtk clipboard object.
258 * @param pSelectionData Pointer to Gtk selection object.
259 * @param info Ignored.
260 * @param pvUser User data.
261 */
262static void vboxwl_gtk_clipboard_write(GtkClipboard *pClipboard,
263 GtkSelectionData *pSelectionData,
264 guint info, gpointer pvUser)
265{
266 GdkAtom target = gtk_selection_data_get_target(pSelectionData);
267 gchar *sTargetName = gdk_atom_name(target);
268 SHCLFORMAT uFmt = VBoxMimeConvGetIdByMime(sTargetName);
269 int rc;
270
271 RT_NOREF(info, pvUser);
272
273 VBCL_LOG_CALLBACK;
274
275 /* Set clipboard format which guest wants to send it to the host. */
276 g_oClipboardIpc->m_uFmt.set(uFmt);
277
278 /* Wait for the host to send clipboard data in requested format. */
279 uint32_t cbBuf = g_oClipboardIpc->m_cbClipboardBuf.wait();
280 void *pvBuf = (void *)g_oClipboardIpc->m_pvClipboardBuf.wait();
281
282 if ( cbBuf != g_oClipboardIpc->m_cbClipboardBuf.defaults()
283 && pvBuf != (void *)g_oClipboardIpc->m_pvClipboardBuf.defaults())
284 {
285 void *pBufOut;
286 size_t cbOut;
287
288 /* Convert clipboard data from VBox representation into guest format. */
289 rc = VBoxMimeConvVBoxToNative(sTargetName, pvBuf, cbBuf, &pBufOut, &cbOut);
290 if (RT_SUCCESS(rc))
291 {
292 gtk_selection_data_set(pSelectionData, target, 8, (const guchar *)pBufOut, cbOut);
293 gtk_clipboard_store(pClipboard);
294
295 gtk_window_iconify(GTK_WINDOW(g_pWindow));
296
297 /* Ask Gtk to quit on the next event loop iteration. */
298 g_tsGtkQuit = RTTimeMilliTS();
299
300 VBClLogVerbose(2, "session %u: paste %u bytes of mime-type '%s' into Gtk\n",
301 g_uSessionId, cbOut, sTargetName);
302 }
303 else
304 VBClLogError("session %u: cannot convert '%s' (%u bytes) into native representation, rc=%Rrc\n",
305 g_uSessionId, sTargetName, cbBuf, rc);
306 }
307 else
308 VBClLogError("session %u: cannot paste '%s' into Gtk: no data\n", g_uSessionId, sTargetName);
309
310 g_free(sTargetName);
311}
312
313/**
314 * Dummy Gtk callback.
315 *
316 * @param pClipboard Pointer to Gtk clipboard object.
317 * @param pvUser User data.
318 */
319static void vboxwl_gtk_clipboard_write_fini(GtkClipboard *pClipboard, gpointer pvUser)
320{
321 VBCL_LOG_CALLBACK;
322 RT_NOREF(pClipboard, pvUser);
323}
324
325/**
326 * Gtk clipboard target list builder,
327 *
328 * Triggered by VBoxMimeConvEnumerateMimeById() when matching VBox
329 * clipboard formats into Gtk representation.
330 *
331 * @param pcszMimeType Mime-type in Gtk representation.
332 * @param pvUser Output buffer.
333 */
334static DECLCALLBACK(void) vboxwl_gtk_build_target_list(const char *pcszMimeType, void *pvUser)
335{
336 GtkTargetList *aTargetList = (GtkTargetList *)pvUser;
337
338 VBClLogVerbose(2, "session %u: mime-type '%s' -> guest\n", g_uSessionId, pcszMimeType);
339 gtk_target_list_add(aTargetList, gdk_atom_intern(pcszMimeType, FALSE), 0, 0);
340}
341
342/**
343 * Gtk callback to paste into clipboard.
344 *
345 * Wait for host to announce its clipboard formats and advertise
346 * them to guest.
347 *
348 * @returns TRUE to stop other Gtk handlers from being invoked for the
349 * event. FALSE to propagate the event further.
350 * @param pSelf Pointer to Gtk widget object.
351 * @param event Gtk event structure.
352 * @param pvUser User data.
353 */
354static DECLCALLBACK(gboolean) vboxwl_gtk_clipboard_set(GtkWidget* pSelf, GdkEventWindowState event, gpointer pvUser)
355{
356 SHCLFORMATS fFmts;
357 GtkClipboard *pClipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
358
359 RT_NOREF(pSelf, event, pvUser);
360
361 VBCL_LOG_CALLBACK;
362
363 /* Wait for host to report available clipboard formats from its buffer. */
364 fFmts = g_oClipboardIpc->m_fFmts.wait();
365 if (fFmts != g_oClipboardIpc->m_fFmts.defaults())
366 {
367 GtkTargetList *aTargetList = gtk_target_list_new(0, 0);
368 GtkTargetEntry *aTargets;
369 int cTargets = 0;
370
371 /* Convert host clipboard formats bitmask into Gtk mime-types list. */
372 VBoxMimeConvEnumerateMimeById(fFmts, vboxwl_gtk_build_target_list, aTargetList);
373
374 aTargets = gtk_target_table_new_from_list(aTargetList, &cTargets);
375 if (RT_VALID_PTR(aTargets))
376 {
377 gboolean fRc;
378
379 /* Announce clipboard content to the guest. */
380 fRc = gtk_clipboard_set_with_data(pClipboard, aTargets, cTargets,
381 &vboxwl_gtk_clipboard_write,
382 &vboxwl_gtk_clipboard_write_fini, NULL);
383 if (!fRc)
384 VBClLogVerbose(2, "session %u: cannot announce clipboard to Gtk\n", g_uSessionId);
385
386 gtk_target_table_free(aTargets, cTargets);
387 }
388 }
389
390 return TRUE;
391}
392
393
394/************************************************************************************************
395 * Gtk App.
396 ***********************************************************************************************/
397
398
399/**
400 * Gtk App watchdog.
401 *
402 * Responsible for quitting the app in the end of Gtk event loop cycle.
403 *
404 * @returns FALSE to stop watchdog, TRUE otherwise.
405 * @param pvUser User data.
406 */
407static gboolean vboxwl_gtk_watchdog(gpointer pvUser)
408{
409 RT_NOREF(pvUser);
410
411 if ( g_tsGtkQuit > 0
412 && (RTTimeMilliTS() - g_tsGtkQuit) > VBOXWL_EXIT_TIMEOUT_MS)
413 {
414 g_application_quit(G_APPLICATION(g_application_get_default()));
415 }
416
417 return TRUE;
418}
419
420/**
421 * Construct visible Gtk app window.
422 *
423 * @param pApp Application object.
424 * @param pvUser User data.
425 */
426static DECLCALLBACK(void) vboxwl_gtk_app_start(GtkApplication* pApp, gpointer pvUser)
427{
428 GtkWidget *pButton, *pBox;
429
430 /* Construct a simple window with a single button element. */
431 g_pWindow = gtk_application_window_new(pApp);
432 if (RT_VALID_PTR(g_pWindow))
433 {
434 g_signal_connect(g_pWindow, "delete_event", gtk_main_quit, NULL);
435
436 gtk_window_set_default_size(GTK_WINDOW(g_pWindow), VBOXWL_WINDOW_WIDTH, VBOXWL_WINDOW_HEIGHT);
437 gtk_window_resize(GTK_WINDOW(g_pWindow), VBOXWL_WINDOW_WIDTH, VBOXWL_WINDOW_HEIGHT);
438
439 pBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
440 pButton = gtk_button_new();
441
442 if ( RT_VALID_PTR(pBox)
443 && RT_VALID_PTR(pButton))
444 {
445 /* Add button to the window. */
446 gtk_container_add(GTK_CONTAINER(g_pWindow), pBox);
447 gtk_box_pack_start(GTK_BOX(pBox), pButton, TRUE, TRUE, 0);
448
449 /* Set elements opacity. */
450 gtk_widget_set_opacity(g_pWindow, VBOXWL_WINDOW_ALPHA);
451 gtk_widget_set_opacity(pButton, VBOXWL_WINDOW_ALPHA);
452
453 /* Setup watchdog handler. */
454 gdk_threads_add_timeout(VBOXWL_WATCHDOG_INTERVAL_MS, &vboxwl_gtk_watchdog, NULL);
455
456 /* Subscribe to Gtk events depending on session type. */
457 if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST)
458 {
459 g_signal_connect_after(g_pWindow, "window-state-event", G_CALLBACK(vboxwl_gtk_clipboard_set), pvUser);
460 }
461 else if ( g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST
462 || g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST)
463 {
464 GtkClipboard *pClipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
465 g_signal_connect(pClipboard, "owner-change", G_CALLBACK(vboxwl_gtk_clipboard_get), pvUser);
466 }
467 else
468 {
469 VBClLogError("unknown session type, requesting app quit\n");
470 g_tsGtkQuit = RTTimeMilliTS();
471 }
472
473 gtk_window_present(GTK_WINDOW(g_pWindow));
474 gtk_widget_show_all(g_pWindow);
475 }
476 }
477}
478
479/**
480 * Gtk App event loop handler.
481 *
482 * @returns IPRT status code.
483 * @param hThreadSelf Running thread handle.
484 * @param pvUser User data.
485 */
486static DECLCALLBACK(int) vboxwl_gtk_worker(RTTHREAD hThreadSelf, void *pvUser)
487{
488 int rc = VERR_NO_MEMORY;
489 GtkApplication *pApp;
490
491 /* Tell parent we are ready. */
492 RTThreadUserSignal(hThreadSelf);
493
494 pApp = gtk_application_new("org.virtualbox.vboxwl", G_APPLICATION_FLAGS_NONE);
495 if (RT_VALID_PTR(pApp))
496 {
497 /* Create app visual instance when ready. */
498 g_signal_connect(pApp, "activate", G_CALLBACK(vboxwl_gtk_app_start), pvUser);
499
500 /* Run gtk main loop. */
501 rc = g_application_run(G_APPLICATION (pApp), 0, NULL);
502
503 g_object_unref(pApp);
504 }
505
506 return rc;
507}
508
509
510/************************************************************************************************
511 * IPC handling.
512 ***********************************************************************************************/
513
514
515/**
516 * Process IPC commands flow for session type.
517 *
518 * @returns IPRT status code.
519 * @param hIpcSession IPC connection handle.
520 */
521static int vboxwl_ipc_flow(RTLOCALIPCSESSION hIpcSession)
522{
523 int rc = VERR_INVALID_PARAMETER;
524
525 if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST)
526 rc = g_oClipboardIpc->flow(vbcl::ipc::clipboard::HGCopyFlow, hIpcSession);
527 else if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST)
528 rc = g_oClipboardIpc->flow(vbcl::ipc::clipboard::GHAnnounceAndCopyFlow, hIpcSession);
529 else if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST)
530 rc = g_oClipboardIpc->flow(vbcl::ipc::clipboard::GHCopyFlow, hIpcSession);
531
532 return rc;
533}
534
535/**
536 * Connect to VBoxClient service.
537 *
538 * @returns IPRT status code.
539 * @param phIpcSession Pointer to IPC connection handle (out).
540 */
541static int vboxwl_connect_ipc(PRTLOCALIPCSESSION phIpcSession)
542{
543 int rc;
544 char szIpcServerName[128];
545
546 AssertPtrReturn(phIpcSession, VERR_INVALID_PARAMETER);
547
548 rc = vbcl_wayland_hlp_gtk_ipc_srv_name(szIpcServerName, sizeof(szIpcServerName));
549 if (RT_SUCCESS(rc))
550 rc = RTLocalIpcSessionConnect(phIpcSession, szIpcServerName, 0);
551
552 VBClLogInfo("session %u: ipc connect: rc=%Rrc\n", g_uSessionId, rc);
553
554 return rc;
555}
556
557
558/************************************************************************************************
559 * Generic initialization.
560 ***********************************************************************************************/
561
562
563/**
564 * Execute requested command.
565 *
566 * @returns IPRT status code.
567 */
568static int vboxwl_run_command(void)
569{
570 int rc;
571 int rcThread = -1;
572
573 RTLOCALIPCSESSION hIpcSession;
574
575 rc = VBClClipboardThreadStart(&g_AppThread, vboxwl_gtk_worker, "gtk-app", NULL);
576 if (RT_SUCCESS(rc))
577 {
578 rc = vboxwl_connect_ipc(&hIpcSession);
579 if (RT_SUCCESS(rc))
580 {
581 g_oClipboardIpc = new vbcl::ipc::clipboard::ClipboardIpc();
582 if (RT_VALID_PTR(g_oClipboardIpc))
583 {
584 g_oClipboardIpc->init(vbcl::ipc::FLOW_DIRECTION_CLIENT, g_uSessionId);
585
586 rc = vboxwl_ipc_flow(hIpcSession);
587 VBClLogVerbose(2, "session %u: ended with rc=%Rrc\n", g_uSessionId, rc);
588
589 /* Ask Gtk app to quit if IPC task has failed. */
590 if (RT_FAILURE(rc))
591 g_tsGtkQuit = RTTimeMilliTS();
592
593 /* Wait for app thread termination first, it uses resources we just created. */
594 rc = RTThreadWait(g_AppThread, RT_MS_30SEC, &rcThread);
595 VBClLogInfo("session %u: gtk app exited: rc=%Rrc, rcThread=%Rrc\n",
596 g_uSessionId, rc, rcThread);
597
598 g_oClipboardIpc->reset();
599 delete g_oClipboardIpc;
600 }
601 else
602 VBClLogError("session %u: unable to create ipc clipboard object\n", g_uSessionId);
603
604 rc = RTLocalIpcSessionClose(hIpcSession);
605 VBClLogVerbose(1, "session %u: ipc disconnected: rc=%Rrc\n", g_uSessionId, rc);
606 }
607 }
608 else
609 VBClLogError("session %u: gtk app start: rc=%Rrc\n", g_uSessionId, rc);
610
611 return rc;
612}
613
614/**
615 * Print command line usage and exit.
616 */
617static void vboxwl_usage(void)
618{
619 RTPrintf(VBOX_PRODUCT " %s "
620 VBOX_VERSION_STRING "\n"
621 "Copyright (C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n\n", g_pszProgName);
622
623 RTPrintf("Usage: %s [ %s %s|%s|%s ] | [--help|-h] [--version|-V] [--verbose|-v]\n\n",
624 g_pszProgName, VBOXWL_ARG_SESSION_ID, VBOXWL_ARG_CLIP_HG_COPY,
625 VBOXWL_ARG_CLIP_GH_ANNOUNCE, VBOXWL_ARG_CLIP_GH_COPY);
626
627 /* Using '%-20s' if pretty much hardcoded here to make output look accurate. Please
628 * feel free to adjust if needed later on. */
629 RTPrintf("Options:\n");
630 RTPrintf(" %-20s Required with --clipboad-paste or --clipboad-copy \n", VBOXWL_ARG_SESSION_ID);
631 RTPrintf(" command, used for communication with VBoxClient instance\n");
632
633 RTPrintf(" %-20s Paste content into clipboard\n", VBOXWL_ARG_CLIP_HG_COPY);
634 RTPrintf(" %-20s Announce clipboard content to the host\n", VBOXWL_ARG_CLIP_GH_ANNOUNCE);
635 RTPrintf(" %-20s Copy content from clipboard\n", VBOXWL_ARG_CLIP_GH_COPY);
636
637 RTPrintf(" --check Check if active Wayland session is running\n");
638 RTPrintf(" --verbose Increase verbosity level\n");
639 RTPrintf(" --version Print version number and exit\n");
640 RTPrintf(" --help Print this message\n");
641 RTPrintf("\n");
642
643 exit(1);
644}
645
646/**
647 * Check if active Wayland session is running.
648 *
649 * This check is used in order to detect whether X11 or Wayland
650 * version of VBoxClient should be started when user logs-in.
651 * It will print out either WL or X11 and exit. Startup script(s)
652 * should rely on this output.
653 */
654static void vboxwl_check(void)
655{
656 VBGHDISPLAYSERVERTYPE enmType = VBGHDisplayServerTypeDetect();
657 bool fWayland = false;
658
659 /* In pure Wayland environment X11 version of VBoxClient will not
660 * work, so fallback on Wayland version. */
661 if (enmType == VBGHDISPLAYSERVERTYPE_PURE_WAYLAND)
662 fWayland = true;
663 else if (enmType == VBGHDISPLAYSERVERTYPE_XWAYLAND)
664 {
665 /* In case of XWayland, X11 version of VBoxClient still can
666 * work, however with some DEs, such as Plasma on Wayland,
667 * this will no longer work. Detect such DEs here. */
668
669 /* Try to detect Plasma. */
670 const char *pcszDesktopSession = RTEnvGet(VBGH_ENV_DESKTOP_SESSION);
671 if (RT_VALID_PTR(pcszDesktopSession) && RTStrIStr(pcszDesktopSession, "plasmawayland"))
672 fWayland = true;
673 }
674
675 RTPrintf("%s\n", fWayland ? "WL" : "X11");
676 exit (0);
677}
678
679/**
680 * Print version and exit.
681 */
682static void vboxwl_version(void)
683{
684 RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
685 exit(0);
686}
687
688/**
689 * Parse command line options.
690 *
691 * @param argc Number of command line arguments.
692 * @param argv List of command line arguments.
693 */
694static void vboxwl_parse_params(int argc, char *argv[])
695{
696 /* Parse our option(s). */
697 static const RTGETOPTDEF s_aOptions[] =
698 {
699 { VBOXWL_ARG_CLIP_HG_COPY, 'p', RTGETOPT_REQ_NOTHING },
700 { VBOXWL_ARG_CLIP_GH_ANNOUNCE, 'a', RTGETOPT_REQ_NOTHING },
701 { VBOXWL_ARG_CLIP_GH_COPY, 'c', RTGETOPT_REQ_NOTHING },
702 { VBOXWL_ARG_SESSION_ID, 's', RTGETOPT_REQ_UINT32 },
703 { "--check", 'C', RTGETOPT_REQ_NOTHING },
704 { "--help", 'h', RTGETOPT_REQ_NOTHING },
705 { "--version", 'V', RTGETOPT_REQ_NOTHING },
706 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
707 };
708
709 int ch;
710 RTGETOPTUNION ValueUnion;
711 RTGETOPTSTATE GetState;
712
713 int rc;
714 uint8_t fArgsMask = 0;
715
716 rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
717 if (RT_SUCCESS(rc))
718 {
719 while ( RT_SUCCESS(rc)
720 && ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0))
721 {
722 switch (ch)
723 {
724 case 'p':
725 {
726 fArgsMask |= VBOXWL_ARG_CLIP_HG_COPY_BIT;
727 break;
728 }
729
730 case 'a':
731 {
732 fArgsMask |= VBOXWL_ARG_CLIP_GH_ANNOUNCE_BIT;
733 break;
734 }
735
736 case 'c':
737 {
738 fArgsMask |= VBOXWL_ARG_CLIP_GH_COPY_BIT;
739 break;
740 }
741
742 case 's':
743 {
744 g_uSessionId = ValueUnion.u32;
745 break;
746 }
747
748 case 'C':
749 {
750 vboxwl_check();
751 break;
752 }
753
754 case 'h':
755 {
756 vboxwl_usage();
757 break;
758 }
759
760 case 'V':
761 {
762 vboxwl_version();
763 break;
764 }
765
766 case 'v':
767 {
768 g_cVerbosity++;
769 break;
770 }
771
772 case VINF_GETOPT_NOT_OPTION:
773 break;
774
775 case VERR_GETOPT_UNKNOWN_OPTION:
776 RT_FALL_THROUGH();
777 default:
778 {
779 RTPrintf("\n");
780 RTGetOptPrintError(ch, &ValueUnion);
781 RTPrintf("\n");
782 }
783 }
784 }
785 }
786
787 /* Check if session ID was specified and command line has
788 * no syntax errors. */
789 if ( RT_FAILURE(rc)
790 || !g_uSessionId)
791 {
792 vboxwl_usage();
793 }
794
795 /* Make sure only one action was specified in command line,
796 * print usage and exit otherwise. */
797 if (fArgsMask == VBOXWL_ARG_CLIP_HG_COPY_BIT)
798 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST;
799 else if (fArgsMask == VBOXWL_ARG_CLIP_GH_ANNOUNCE_BIT)
800 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST;
801 else if (fArgsMask == VBOXWL_ARG_CLIP_GH_COPY_BIT)
802 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST;
803 else
804 vboxwl_usage();
805}
806
807/** Initialization step shortcut macro.
808 *
809 * Try to run initialization function if previous step was successful and print error if it occurs.
810 *
811 * @param _fn A function to call.
812 * @param _error Error message to print if function fails.
813 */
814#define VBOXWL_INIT(_fn, _error) \
815 if (RT_SUCCESS(rc)) \
816 { \
817 rc = _fn; \
818 if (RT_FAILURE(rc)) \
819 RTPrintf("%s, rc=%Rrc\n", _error, rc); \
820 }
821
822int main(int argc, char *argv[])
823{
824 int rc = VINF_SUCCESS;
825
826 /** Custom log prefix to be used for logger instance of this process. */
827 static const char *pszLogPrefix = "vboxwl:";
828
829 /* Set program name. */
830 g_pszProgName = argv[0];
831
832 /* Initialize runtime. */
833 VBOXWL_INIT(RTR3InitExe(argc, &argv, 0), "cannot initialize runtime");
834
835 /* Go through command line parameters. */
836 vboxwl_parse_params(argc, argv);
837
838 if (!VBGHDisplayServerTypeIsGtkAvailable())
839 {
840 RTPrintf("Gtk3 library is required to run this tool, but can not be found\n");
841 return RTEXITCODE_FAILURE;
842 }
843
844 /* Initialize runtime before all else. */
845 VBOXWL_INIT(VbglR3InitUser(), "cannot to communicate with vboxguest kernel module");
846 VBOXWL_INIT(VBClLogCreateEx("", false), "cannot create logger instance");
847 VBOXWL_INIT(VBClLogModify("stdout", g_cVerbosity), "cannot setup log");
848
849 /* Set custom log prefix. */
850 VBClLogSetLogPrefix(pszLogPrefix);
851
852 VBOXWL_INIT(vboxwl_run_command(), "cannot run command");
853
854 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
855}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette