tstXPCOMCGlue.c revision 0bb65e71fd8bb74172b634aa7cba33cb5161c76b
/* $Revision$ */
/** @file tstXPCOMCGlue.c
* Demonstrator program to illustrate use of C bindings of Main API, including
* how to retrieve all available error information.
*
* This includes code which shows how to handle active event listeners
* (event delivery through callbacks), which is not so frequently seen in
* other samples which mostly use passive event listeners.
*
* Linux only at the moment due to shared library magic in the Makefile.
*/
/*
* Copyright (C) 2009-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "VBoxXPCOMCGlue.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Set by signal handler. */
static volatile int g_fStop = 0;
{
switch (machineState)
{
case MachineState_Null: return "<null>";
case MachineState_PoweredOff: return "PoweredOff";
case MachineState_Saved: return "Saved";
case MachineState_Teleported: return "Teleported";
case MachineState_Aborted: return "Aborted";
case MachineState_Running: return "Running";
case MachineState_Paused: return "Paused";
case MachineState_Stuck: return "Stuck";
case MachineState_Teleporting: return "Teleporting";
case MachineState_LiveSnapshotting: return "LiveSnapshotting";
case MachineState_Starting: return "Starting";
case MachineState_Stopping: return "Stopping";
case MachineState_Saving: return "Saving";
case MachineState_Restoring: return "Restoring";
case MachineState_TeleportingPausedVM: return "TeleportingPausedVM";
case MachineState_TeleportingIn: return "TeleportingIn";
case MachineState_FaultTolerantSyncing: return "FaultTolerantSyncing";
case MachineState_DeletingSnapshotOnline: return "DeletingSnapshotOnline";
case MachineState_DeletingSnapshotPaused: return "DeletingSnapshotPaused";
case MachineState_RestoringSnapshot: return "RestoringSnapshot";
case MachineState_DeletingSnapshot: return "DeletingSnapshot";
case MachineState_SettingUp: return "SettingUp";
default: return "no idea";
}
}
struct IEventListenerDemo_vtbl
{
struct IEventListener_vtbl ieventlistener;
};
typedef struct IEventListenerDemo
{
struct IEventListenerDemo_vtbl *vtbl;
int refcount;
/**
* Event handler function
*/
{
enum VBoxEventType evType;
if (!event)
{
printf("event null\n");
return NS_OK;
}
{
return NS_OK;
}
switch (evType)
{
printf("OnMousePointerShapeChanged\n");
break;
printf("OnMouseCapabilityChanged\n");
break;
printf("OnMouseCapabilityChanged\n");
break;
{
enum MachineState state;
rc = event->vtbl->nsisupports.QueryInterface((nsISupports *)event, &istateChangedEventUUID, (void **)&ev);
{
return NS_OK;
}
if (!ev)
{
printf("StateChangedEvent reference null\n");
return NS_OK;
}
if ( state == MachineState_PoweredOff
|| state == MachineState_Saved
|| state == MachineState_Teleported
|| state == MachineState_Aborted
)
g_fStop = 1;
break;
}
printf("OnAdditionsStateChanged\n");
break;
printf("OnNetworkAdapterChanged\n");
break;
printf("OnSerialPortChanged\n");
break;
printf("OnParallelPortChanged\n");
break;
printf("OnStorageControllerChanged\n");
break;
printf("OnMediumChanged\n");
break;
printf("OnVRDEServerChanged\n");
break;
printf("OnUSBControllerChanged\n");
break;
printf("OnUSBDeviceStateChanged\n");
break;
printf("OnSharedFolderChanged\n");
break;
printf("OnRuntimeError\n");
break;
printf("OnCanShowWindow\n");
break;
printf("OnShowWindow\n");
break;
default:
}
return NS_OK;
}
static nsresult IEventListenerDemo_QueryInterface(nsISupports *pThis, const nsID *iid, void **resultp)
{
/* match iid */
{
return NS_OK;
}
return NS_NOINTERFACE;
}
{
}
{
nsresult c;
if (c == 0)
return c;
}
{
void *typeinfo;
struct IEventListenerDemo_vtbl vtbl;
};
static struct IEventListenerDemo_vtbl_int_gcc g_IEventListenerDemo_vtbl_int_gcc =
{
0, /* offset_to_top */
NULL, /* typeinfo, not vital */
{
{
{
},
}
}
};
/**
* Signal handler, terminate event listener.
*
* @param iSig The signal number (ignored).
*/
static void sigIntHandler(int iSig)
{
(void)iSig;
g_fStop = 1;
}
/**
* Register event listener for the selected VM.
*
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param id identifies the machine to start
* @param queue handle to the event queue
*/
static void registerEventListener(IVirtualBox *virtualBox, ISession *session, PRUnichar *machineId, nsIEventQueue *queue)
{
{
{
{
};
if (consoleListener)
{
sizeof(interestingEvents) / sizeof(interestingEvents[0]),
if (NS_SUCCEEDED(rc))
{
/* Just wait here for events, no easy way to do this better
* as there's not much to do after this completes. */
int ret;
printf("Entering event loop, PowerOff the machine to exit or press Ctrl-C to terminate\n");
if (fd >= 0)
{
while (!g_fStop)
{
if (ret <= 0)
continue;
g_fStop = 1;
}
}
else
{
while (!g_fStop)
{
if (NS_SUCCEEDED(rc))
}
}
}
}
else
{
printf("Failed while allocating memory for console event listener.\n");
}
}
}
}
/**
* Print detailed error information if available.
* @param pszExecutable string with the executable name
* @param pszErrorMsg string containing the code location specific error message
* @param rc XPCOM result code
*/
{
if (NS_SUCCEEDED(rc2))
{
if (ei)
{
/* got extended error info, maybe multiple infos */
do
{
}
while (ei);
}
else
{
/* got basic error info */
}
}
}
/**
* Start a VM.
*
* @param argv0 executable name
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param id identifies the machine to start
* @param queue ptr to event queue
*/
static void startVM(const char *argv0, IVirtualBox *virtualBox, ISession *session, PRUnichar *id, nsIEventQueue *queue)
{
{
return;
}
env,
);
if (NS_SUCCEEDED(rc))
{
printf("Waiting for the remote session to open...\n");
if (NS_FAILED(resultCode))
{
char *text;
}
else
{
/* Kick off the event listener demo part, which is quite separate.
* Ignore it if you need a more basic sample. */
}
}
else
/* It's important to always release resources. */
}
/**
* List the registered VMs.
*
* @param argv0 executable name
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param queue ptr to event queue
*/
static void listVMs(const char *argv0, IVirtualBox *virtualBox, ISession *session, nsIEventQueue *queue)
{
PRUint32 machineCnt = 0;
PRUint32 i;
unsigned start_id;
/*
* Get the list of all registered VMs.
*/
{
return;
}
if (machineCnt == 0)
{
printf("\tNo VMs\n");
return;
}
printf("VM List:\n\n");
/*
* Iterate through the collection.
*/
for (i = 0; i < machineCnt; ++i)
{
printf("\tMachine #%u\n", (unsigned)i);
if (!machine)
{
printf("\t(skipped, NULL)\n");
continue;
}
if (isAccessible)
{
char *machineName;
}
else
{
printf("\tName: <inaccessible>\n");
}
{
char *uuidUtf8;
}
if (isAccessible)
{
{
char *configFileUtf8;
}
{
}
{
char *osName;
}
}
}
/*
* Let the user chose a machine to start.
*/
printf("Type Machine# to start (0 - %u) or 'quit' to do nothing: ",
(unsigned)(machineCnt - 1));
{
if (machine)
{
}
}
/*
* Don't forget to release the objects in the array.
*/
for (i = 0; i < machineCnt; ++i)
{
if (machine)
{
}
}
if (machines)
}
/* Main - Start the ball rolling. */
{
printf("Starting main()\n");
if (VBoxCGlueInit() != 0)
{
argv[0], g_szVBoxErrMsg);
return EXIT_FAILURE;
}
{
}
if (vboxclient == NULL)
{
return EXIT_FAILURE;
}
printf("----------------------------------------------------\n");
{
return EXIT_FAILURE;
}
{
return EXIT_FAILURE;
}
/*
* Now ask for revision, version and home folder information of
* this vbox. Were not using fancy macros here so it
* remains easy to see how we access C++'s vtable.
*/
/* 1. Revision */
if (NS_SUCCEEDED(rc))
else
/* 2. Version */
if (NS_SUCCEEDED(rc))
{
}
else
/* 3. Home Folder */
if (NS_SUCCEEDED(rc))
{
char *homefolder = NULL;
}
else
printf("----------------------------------------------------\n");
/*
* Do as mom told us: always clean up after yourself.
*/
if (session)
{
}
if (vbox)
{
}
if (vboxclient)
{
vboxclient = NULL;
}
printf("Finished main()\n");
return 0;
}
/* vim: set ts=4 sw=4 et: */