tstXPCOMCEvent.c revision 180d75c44949a43d3a0780eea8a4d1ae8a96d87f
/* $Revision$ */
/** @file tstXPCOMCEvent.c
* Demonstrator program to illustrate use of C bindings of Main API,
* and in particular how to handle active event listeners (event delivery
* through callbacks). The code is derived from tstXPCOMCGlue.c, so keep
* the diffs to the original code as small as possible.
*
* 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;
int volatile g_refcount = 0;
/* #define for printing nsID type UUID's */
{\
}\
{
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";
}
}
/**
* Event handler function
*/
{
enum VBoxEventType evType;
if (!event)
{
printf("event null\n");
return 0;
}
{
return 0;
}
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 0;
}
if (!ev)
{
printf("StateChangedEvent reference null\n");
return 0;
}
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 0;
}
{
nsresult c;
c = ++g_refcount;
printf("AddRef: %d\n", c);
return c;
}
{
nsresult c;
c = --g_refcount;
printf("Release: %d\n", c);
if (c == 0)
{
/* delete object */
}
return c;
}
{
/* match iid */
{
++g_refcount;
return NS_OK;
}
/* printf("event listener QueryInterface didn't find a matching interface\n"); */
return NS_NOINTERFACE;
}
/**
* Signal handler, terminate event listener.
*
* @param iSig The signal number (ignored).
*/
static void sigIntHandler(int iSig)
{
printf("sigIntHandler\n");
(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)
{
{
{
{
};
{
g_refcount = 1;
sizeof(interestingEvents) / sizeof(interestingEvents[0]),
if (NS_SUCCEEDED(rc))
{
/* crude way to show how it works, but any
* great ideas anyone?
*/
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)
{
/*printf("event: %p rc=%x\n", (void *)pEvent, rc);*/
if (NS_SUCCEEDED(rc))
}
}
}
}
else
{
printf("Failed while allocating memory for console event listener.\n");
}
}
}
}
/**
* Start a 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 startVM(IVirtualBox *virtualBox, ISession *session, PRUnichar *id, nsIEventQueue *queue)
{
{
return;
}
env,
);
{
}
else
{
printf("Waiting for the remote session to open...\n");
{
}
if (NS_FAILED(resultCode))
{
char *text;
}
else
{
}
}
/* It's important to always release resources. */
}
/**
* List the registered VMs.
*
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param queue handle to the event queue
*/
{
PRUint32 machineCnt = 0;
PRUint32 i;
unsigned start_id;
/*
* Get the list of all registered VMs.
*/
{
(unsigned)rc);
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");
}
{
}
if (isAccessible)
{
{
}
{
}
{
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)
{
}
}
}
/* Main - Start the ball rolling. */
{
printf("Starting Main\n");
/*
* VBoxComInitialize does all the necessary startup action and
* provides us with pointers to vbox and session handles.
* It should be matched by a call to VBoxComUninitialize(vbox)
* when done.
*/
if (VBoxCGlueInit() != 0)
{
argv[0], g_szVBoxErrMsg);
return EXIT_FAILURE;
}
{
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.
*/
printf("----------------------------------------------------\n");
/* 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.
*/
printf("Finished Main\n");
return 0;
}
/* vim: set ts=4 sw=4 et: */