VirtualBox

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

Last change on this file since 101881 was 101881, checked in by vboxsync, 17 months ago

Additions: X11/Wayland: fix building issues with older gcc (4.1.2), bugref:10194.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 26.0 KB
Line 
1/* $Id: vboxwl.cpp 101881 2023-11-06 16:29:36Z 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 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(" --verbose Increase verbosity level\n");
637 RTPrintf(" --version Print version number and exit\n");
638 RTPrintf(" --help Print this message\n");
639 RTPrintf("\n");
640
641 exit(1);
642}
643
644/**
645 * Print version and exit.
646 */
647static void vboxwl_version(void)
648{
649 RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
650 exit(0);
651}
652
653/**
654 * Parse command line options.
655 *
656 * @param argc Number of command line arguments.
657 * @param argv List of command line arguments.
658 */
659static void vboxwl_parse_params(int argc, char *argv[])
660{
661 /* Parse our option(s). */
662 static const RTGETOPTDEF s_aOptions[] =
663 {
664 { VBOXWL_ARG_CLIP_HG_COPY, 'p', RTGETOPT_REQ_NOTHING },
665 { VBOXWL_ARG_CLIP_GH_ANNOUNCE, 'a', RTGETOPT_REQ_NOTHING },
666 { VBOXWL_ARG_CLIP_GH_COPY, 'c', RTGETOPT_REQ_NOTHING },
667 { VBOXWL_ARG_SESSION_ID, 's', RTGETOPT_REQ_UINT32 },
668 { "--help", 'h', RTGETOPT_REQ_NOTHING },
669 { "--version", 'V', RTGETOPT_REQ_NOTHING },
670 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
671 };
672
673 int ch;
674 RTGETOPTUNION ValueUnion;
675 RTGETOPTSTATE GetState;
676
677 int rc;
678 uint8_t fArgsMask = 0;
679
680 rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
681 if (RT_SUCCESS(rc))
682 {
683 while ( RT_SUCCESS(rc)
684 && ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0))
685 {
686 switch (ch)
687 {
688 case 'p':
689 {
690 fArgsMask |= VBOXWL_ARG_CLIP_HG_COPY_BIT;
691 break;
692 }
693
694 case 'a':
695 {
696 fArgsMask |= VBOXWL_ARG_CLIP_GH_ANNOUNCE_BIT;
697 break;
698 }
699
700 case 'c':
701 {
702 fArgsMask |= VBOXWL_ARG_CLIP_GH_COPY_BIT;
703 break;
704 }
705
706 case 's':
707 {
708 g_uSessionId = ValueUnion.u32;
709 break;
710 }
711
712 case 'h':
713 {
714 vboxwl_usage();
715 break;
716 }
717
718 case 'V':
719 {
720 vboxwl_version();
721 break;
722 }
723
724 case 'v':
725 {
726 g_cVerbosity++;
727 break;
728 }
729
730 case VINF_GETOPT_NOT_OPTION:
731 break;
732
733 case VERR_GETOPT_UNKNOWN_OPTION:
734 RT_FALL_THROUGH();
735 default:
736 {
737 RTPrintf("\n");
738 RTGetOptPrintError(ch, &ValueUnion);
739 RTPrintf("\n");
740 }
741 }
742 }
743 }
744
745 /* Check if session ID was specified and command line has
746 * no syntax errors. */
747 if ( RT_FAILURE(rc)
748 || !g_uSessionId)
749 {
750 vboxwl_usage();
751 }
752
753 /* Make sure only one action was specified in command line,
754 * print usage and exit otherwise. */
755 if (fArgsMask == VBOXWL_ARG_CLIP_HG_COPY_BIT)
756 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST;
757 else if (fArgsMask == VBOXWL_ARG_CLIP_GH_ANNOUNCE_BIT)
758 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST;
759 else if (fArgsMask == VBOXWL_ARG_CLIP_GH_COPY_BIT)
760 g_enmSessionType = VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST;
761 else
762 vboxwl_usage();
763}
764
765/** Initialization step shortcut macro.
766 *
767 * Try to run initialization function if previous step was successful and print error if it occurs.
768 *
769 * @param _fn A function to call.
770 * @param _error Error message to print if function fails.
771 */
772#define VBOXWL_INIT(_fn, _error) \
773 if (RT_SUCCESS(rc)) \
774 { \
775 rc = _fn; \
776 if (RT_FAILURE(rc) && _error != NULL) \
777 RTPrintf("%s, rc=%Rrc\n", _error, rc); \
778 }
779
780int main(int argc, char *argv[])
781{
782 int rc = VINF_SUCCESS;
783
784 /** Custom log prefix to be used for logger instance of this process. */
785 static const char *pszLogPrefix = "vboxwl:";
786
787 /* Set program name. */
788 g_pszProgName = argv[0];
789
790 /* Initialize runtime. */
791 VBOXWL_INIT(RTR3InitExe(argc, &argv, 0), "cannot initialize runtime");
792
793 /* Go through command line parameters. */
794 vboxwl_parse_params(argc, argv);
795
796 if (!VBGHDisplayServerTypeIsGtkAvailable())
797 {
798 RTPrintf("Gtk3 library is required to run this tool, but can not be found\n");
799 return RTEXITCODE_FAILURE;
800 }
801
802 /* Initialize runtime before all else. */
803 VBOXWL_INIT(VbglR3InitUser(), "cannot to communicate with vboxguest kernel module");
804 VBOXWL_INIT(VBClLogCreateEx("", false), "cannot create logger instance");
805 VBOXWL_INIT(VBClLogModify("stdout", g_cVerbosity), "cannot setup log");
806
807 /* Set custom log prefix. */
808 VBClLogSetLogPrefix(pszLogPrefix);
809
810 VBOXWL_INIT(vboxwl_run_command(), NULL);
811
812 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
813}
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