VirtualBox

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

Last change on this file since 102026 was 102026, checked in by vboxsync, 6 months ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 27.2 KB
Line 
1/* $Id: vboxwl.cpp 102026 2023-11-09 12:10:01Z 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/** 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 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 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 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 gboolean vboxwl_gtk_watchdog(gpointer pvUser)
405{
406 RT_NOREF(pvUser);
407
408 if ( g_tsGtkQuit > 0
409 && (RTTimeMilliTS() - g_tsGtkQuit) > VBOXWL_EXIT_TIMEOUT_MS)
410 {
411 g_application_quit(G_APPLICATION(g_application_get_default()));
412 }
413
414 return TRUE;
415}
416
417/**
418 * Construct visible Gtk app window.
419 *
420 * @param pApp Application object.
421 * @param pvUser User data.
422 */
423static DECLCALLBACK(void) vboxwl_gtk_app_start(GtkApplication* pApp, gpointer pvUser)
424{
425 GtkWidget *pButton, *pBox;
426
427 /* Construct a simple window with a single button element. */
428 g_pWindow = gtk_application_window_new(pApp);
429 if (RT_VALID_PTR(g_pWindow))
430 {
431 g_signal_connect(g_pWindow, "delete_event", gtk_main_quit, NULL);
432
433 gtk_window_set_default_size(GTK_WINDOW(g_pWindow), VBOXWL_WINDOW_WIDTH, VBOXWL_WINDOW_HEIGHT);
434 gtk_window_resize(GTK_WINDOW(g_pWindow), VBOXWL_WINDOW_WIDTH, VBOXWL_WINDOW_HEIGHT);
435
436 pBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
437 pButton = gtk_button_new();
438
439 if ( RT_VALID_PTR(pBox)
440 && RT_VALID_PTR(pButton))
441 {
442 /* Add button to the window. */
443 gtk_container_add(GTK_CONTAINER(g_pWindow), pBox);
444 gtk_box_pack_start(GTK_BOX(pBox), pButton, TRUE, TRUE, 0);
445
446 /* Set elements opacity. */
447 gtk_widget_set_opacity(g_pWindow, VBOXWL_WINDOW_ALPHA);
448 gtk_widget_set_opacity(pButton, VBOXWL_WINDOW_ALPHA);
449
450 /* Setup watchdog handler. */
451 gdk_threads_add_timeout(VBOXWL_WATCHDOG_INTERVAL_MS, &vboxwl_gtk_watchdog, NULL);
452
453 /* Subscribe to Gtk events depending on session type. */
454 if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST)
455 {
456 g_signal_connect_after(g_pWindow, "window-state-event", G_CALLBACK(vboxwl_gtk_clipboard_set), pvUser);
457 }
458 else if ( g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST
459 || g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST)
460 {
461 GtkClipboard *pClipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
462 g_signal_connect(pClipboard, "owner-change", G_CALLBACK(vboxwl_gtk_clipboard_get), pvUser);
463 }
464 else
465 {
466 VBClLogError("unknown session type, requesting app quit\n");
467 g_tsGtkQuit = RTTimeMilliTS();
468 }
469
470 gtk_window_present(GTK_WINDOW(g_pWindow));
471 gtk_widget_show_all(g_pWindow);
472 }
473 }
474}
475
476/**
477 * Gtk App event loop handler.
478 *
479 * @returns IPRT status code.
480 * @param hThreadSelf Running thread handle.
481 * @param pvUser User data.
482 */
483static DECLCALLBACK(int) vboxwl_gtk_worker(RTTHREAD hThreadSelf, void *pvUser)
484{
485 int rc = VERR_NO_MEMORY;
486 GtkApplication *pApp;
487
488 /* Tell parent we are ready. */
489 RTThreadUserSignal(hThreadSelf);
490
491 pApp = gtk_application_new("org.virtualbox.vboxwl", G_APPLICATION_FLAGS_NONE);
492 if (RT_VALID_PTR(pApp))
493 {
494 /* Create app visual instance when ready. */
495 g_signal_connect(pApp, "activate", G_CALLBACK(vboxwl_gtk_app_start), pvUser);
496
497 /* Run gtk main loop. */
498 rc = g_application_run(G_APPLICATION (pApp), 0, NULL);
499
500 g_object_unref(pApp);
501 }
502
503 return rc;
504}
505
506
507/************************************************************************************************
508 * IPC handling.
509 ***********************************************************************************************/
510
511
512/**
513 * Process IPC commands flow for session type.
514 *
515 * @returns IPRT status code.
516 * @param hIpcSession IPC connection handle.
517 */
518static int vboxwl_ipc_flow(RTLOCALIPCSESSION hIpcSession)
519{
520 int rc = VERR_INVALID_PARAMETER;
521
522 if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST)
523 rc = g_oClipboardIpc->flow(vbcl::ipc::clipboard::HGCopyFlow, hIpcSession);
524 else if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST)
525 rc = g_oClipboardIpc->flow(vbcl::ipc::clipboard::GHAnnounceAndCopyFlow, hIpcSession);
526 else if (g_enmSessionType == VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST)
527 rc = g_oClipboardIpc->flow(vbcl::ipc::clipboard::GHCopyFlow, hIpcSession);
528
529 return rc;
530}
531
532/**
533 * Connect to VBoxClient service.
534 *
535 * @returns IPRT status code.
536 * @param phIpcSession Pointer to IPC connection handle (out).
537 */
538static int vboxwl_connect_ipc(PRTLOCALIPCSESSION phIpcSession)
539{
540 int rc;
541 char szIpcServerName[128];
542
543 AssertPtrReturn(phIpcSession, VERR_INVALID_PARAMETER);
544
545 rc = vbcl_wayland_hlp_gtk_ipc_srv_name(szIpcServerName, sizeof(szIpcServerName));
546 if (RT_SUCCESS(rc))
547 rc = RTLocalIpcSessionConnect(phIpcSession, szIpcServerName, 0);
548
549 VBClLogInfo("session %u: ipc connect: rc=%Rrc\n", g_uSessionId, rc);
550
551 return rc;
552}
553
554
555/************************************************************************************************
556 * Generic initialization.
557 ***********************************************************************************************/
558
559
560/**
561 * Execute requested command.
562 *
563 * @returns IPRT status code.
564 */
565static int vboxwl_run_command(void)
566{
567 int rc;
568 int rcThread = -1;
569
570 RTLOCALIPCSESSION hIpcSession;
571
572 rc = VBClClipboardThreadStart(&g_AppThread, vboxwl_gtk_worker, "gtk-app", NULL);
573 if (RT_SUCCESS(rc))
574 {
575 rc = vboxwl_connect_ipc(&hIpcSession);
576 if (RT_SUCCESS(rc))
577 {
578 g_oClipboardIpc = new vbcl::ipc::clipboard::ClipboardIpc();
579 if (RT_VALID_PTR(g_oClipboardIpc))
580 {
581 g_oClipboardIpc->init(vbcl::ipc::FLOW_DIRECTION_CLIENT, g_uSessionId);
582
583 rc = vboxwl_ipc_flow(hIpcSession);
584 VBClLogVerbose(2, "session %u: ended with rc=%Rrc\n", g_uSessionId, rc);
585
586 /* Ask Gtk app to quit if IPC task has failed. */
587 if (RT_FAILURE(rc))
588 g_tsGtkQuit = RTTimeMilliTS();
589
590 /* Wait for app thread termination first, it uses resources we just created. */
591 rc = RTThreadWait(g_AppThread, RT_MS_30SEC, &rcThread);
592 VBClLogInfo("session %u: gtk app exited: rc=%Rrc, rcThread=%Rrc\n",
593 g_uSessionId, rc, rcThread);
594
595 g_oClipboardIpc->reset();
596 delete g_oClipboardIpc;
597 }
598 else
599 VBClLogError("session %u: unable to create ipc clipboard object\n", g_uSessionId);
600
601 rc = RTLocalIpcSessionClose(hIpcSession);
602 VBClLogVerbose(1, "session %u: ipc disconnected: rc=%Rrc\n", g_uSessionId, rc);
603 }
604 }
605 else
606 VBClLogError("session %u: gtk app start: rc=%Rrc\n", g_uSessionId, rc);
607
608 return rc;
609}
610
611/**
612 * Print command line usage and exit.
613 */
614static void vboxwl_usage(void)
615{
616 RTPrintf(VBOX_PRODUCT " %s "
617 VBOX_VERSION_STRING "\n"
618 "Copyright (C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n\n", g_pszProgName);
619
620 RTPrintf("Usage: %s [ %s %s|%s|%s ] | [--help|-h] [--version|-V] [--verbose|-v]\n\n",
621 g_pszProgName, VBOXWL_ARG_SESSION_ID, VBOXWL_ARG_CLIP_HG_COPY,
622 VBOXWL_ARG_CLIP_GH_ANNOUNCE, VBOXWL_ARG_CLIP_GH_COPY);
623
624 /* Using '%-20s' if pretty much hardcoded here to make output look accurate. Please
625 * feel free to adjust if needed later on. */
626 RTPrintf("Options:\n");
627 RTPrintf(" %-20s Required with --clipboad-paste or --clipboad-copy \n", VBOXWL_ARG_SESSION_ID);
628 RTPrintf(" command, used for communication with VBoxClient instance\n");
629
630 RTPrintf(" %-20s Paste content into clipboard\n", VBOXWL_ARG_CLIP_HG_COPY);
631 RTPrintf(" %-20s Announce clipboard content to the host\n", VBOXWL_ARG_CLIP_GH_ANNOUNCE);
632 RTPrintf(" %-20s Copy content from clipboard\n", VBOXWL_ARG_CLIP_GH_COPY);
633
634 RTPrintf(" --check Check if active Wayland session is running\n");
635 RTPrintf(" --verbose Increase verbosity level\n");
636 RTPrintf(" --version Print version number and exit\n");
637 RTPrintf(" --help Print this message\n");
638 RTPrintf("\n");
639
640 exit(1);
641}
642
643/**
644 * Check if active Wayland session is running.
645 *
646 * This check is used in order to detect whether X11 or Wayland
647 * version of VBoxClient should be started when user logs-in.
648 * It will print out either WL or X11 and exit. Startup script(s)
649 * should rely on this output.
650 */
651static void vboxwl_check(void)
652{
653 VBGHDISPLAYSERVERTYPE enmType = VBGHDisplayServerTypeDetect();
654 bool fWayland = false;
655
656 /* In pure Wayland environment X11 version of VBoxClient will not
657 * work, so fallback on Wayland version. */
658 if (enmType == VBGHDISPLAYSERVERTYPE_PURE_WAYLAND)
659 fWayland = true;
660 else if (enmType == VBGHDISPLAYSERVERTYPE_XWAYLAND)
661 {
662 /* In case of XWayland, X11 version of VBoxClient still can
663 * work, however with some DEs, such as Plasma on Wayland,
664 * this will no longer work. Detect such DEs here. */
665
666 /* Try to detect Plasma. */
667 const char *pcszDesktopSession = RTEnvGet(VBGH_ENV_DESKTOP_SESSION);
668 if (RT_VALID_PTR(pcszDesktopSession) && RTStrIStr(pcszDesktopSession, "plasmawayland"))
669 fWayland = true;
670 }
671
672 RTPrintf("%s\n", fWayland ? "WL" : "X11");
673 exit (0);
674}
675
676/**
677 * Print version and exit.
678 */
679static void vboxwl_version(void)
680{
681 RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
682 exit(0);
683}
684
685/**
686 * Parse command line options.
687 *
688 * @param argc Number of command line arguments.
689 * @param argv List of command line arguments.
690 */
691static void vboxwl_parse_params(int argc, char *argv[])
692{
693 /* Parse our option(s). */
694 static const RTGETOPTDEF s_aOptions[] =
695 {
696 { VBOXWL_ARG_CLIP_HG_COPY, 'p', RTGETOPT_REQ_NOTHING },
697 { VBOXWL_ARG_CLIP_GH_ANNOUNCE, 'a', RTGETOPT_REQ_NOTHING },
698 { VBOXWL_ARG_CLIP_GH_COPY, 'c', RTGETOPT_REQ_NOTHING },
699 { VBOXWL_ARG_SESSION_ID, 's', RTGETOPT_REQ_UINT32 },
700 { "--check", 'C', RTGETOPT_REQ_NOTHING },
701 { "--help", 'h', RTGETOPT_REQ_NOTHING },
702 { "--version", 'V', RTGETOPT_REQ_NOTHING },
703 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
704 };
705
706 int ch;
707 RTGETOPTUNION ValueUnion;
708 RTGETOPTSTATE GetState;
709
710 int rc;
711 uint8_t fArgsMask = 0;
712
713 rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
714 if (RT_SUCCESS(rc))
715 {
716 while ( RT_SUCCESS(rc)
717 && ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0))
718 {
719 switch (ch)
720 {
721 case 'p':
722 {
723 fArgsMask |= VBOXWL_ARG_CLIP_HG_COPY_BIT;
724 break;
725 }
726
727 case 'a':
728 {
729 fArgsMask |= VBOXWL_ARG_CLIP_GH_ANNOUNCE_BIT;
730 break;
731 }
732
733 case 'c':
734 {
735 fArgsMask |= VBOXWL_ARG_CLIP_GH_COPY_BIT;
736 break;
737 }
738
739 case 's':
740 {
741 g_uSessionId = ValueUnion.u32;
742 break;
743 }
744
745 case 'C':
746 {
747 vboxwl_check();
748 break;
749 }
750
751 case 'h':
752 {
753 vboxwl_usage();
754 break;
755 }
756
757 case 'V':
758 {
759 vboxwl_version();
760 break;
761 }
762
763 case 'v':
764 {
765 g_cVerbosity++;
766 break;
767 }
768
769 case VINF_GETOPT_NOT_OPTION:
770 break;
771
772 case VERR_GETOPT_UNKNOWN_OPTION:
773 RT_FALL_THROUGH();
774 default:
775 {
776 RTPrintf("\n");
777 RTGetOptPrintError(ch, &ValueUnion);
778 RTPrintf("\n");
779 }
780 }
781 }
782 }
783
784 /* Check if session ID was specified and command line has
785 * no syntax errors. */
786 if ( RT_FAILURE(rc)
787 || !g_uSessionId)
788 {
789 vboxwl_usage();
790 }
791
792 /* Make sure only one action was specified in command line,
793 * print usage and exit otherwise. */
794 if (fArgsMask == VBOXWL_ARG_CLIP_HG_COPY_BIT)
795 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST;
796 else if (fArgsMask == VBOXWL_ARG_CLIP_GH_ANNOUNCE_BIT)
797 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST;
798 else if (fArgsMask == VBOXWL_ARG_CLIP_GH_COPY_BIT)
799 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST;
800 else
801 vboxwl_usage();
802}
803
804/** Initialization step shortcut macro.
805 *
806 * Try to run initialization function if previous step was successful and print error if it occurs.
807 *
808 * @param _fn A function to call.
809 * @param _error Error message to print if function fails.
810 */
811#define VBOXWL_INIT(_fn, _error) \
812 if (RT_SUCCESS(rc)) \
813 { \
814 rc = _fn; \
815 if (RT_FAILURE(rc)) \
816 RTPrintf("%s, rc=%Rrc\n", _error, rc); \
817 }
818
819int main(int argc, char *argv[])
820{
821 int rc = VINF_SUCCESS;
822
823 /** Custom log prefix to be used for logger instance of this process. */
824 static const char *pszLogPrefix = "vboxwl:";
825
826 /* Set program name. */
827 g_pszProgName = argv[0];
828
829 /* Initialize runtime. */
830 VBOXWL_INIT(RTR3InitExe(argc, &argv, 0), "cannot initialize runtime");
831
832 /* Go through command line parameters. */
833 vboxwl_parse_params(argc, argv);
834
835 if (!VBGHDisplayServerTypeIsGtkAvailable())
836 {
837 RTPrintf("Gtk3 library is required to run this tool, but can not be found\n");
838 return RTEXITCODE_FAILURE;
839 }
840
841 /* Initialize runtime before all else. */
842 VBOXWL_INIT(VbglR3InitUser(), "cannot to communicate with vboxguest kernel module");
843 VBOXWL_INIT(VBClLogCreateEx("", false), "cannot create logger instance");
844 VBOXWL_INIT(VBClLogModify("stdout", g_cVerbosity), "cannot setup log");
845
846 /* Set custom log prefix. */
847 VBClLogSetLogPrefix(pszLogPrefix);
848
849 VBOXWL_INIT(vboxwl_run_command(), "cannot run command");
850
851 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
852}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use