VirtualBox

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

Last change on this file since 102025 was 102025, checked in by vboxsync, 19 months ago

Additions: X11/Wayland: Attempt to address build issues, bugref:10194.

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

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