DesktopCommunication updates

Submitted by Robert Szeleney on Mon, 2007-08-20 12:25.

DesktopCommunication updates

With the new C++ API it is now very easy to make your application controllable from other applications. With the old API you had to create various Interface and parse manually through messages sent to your application. The new API now does all this work for you automatically.
Lets look at a simple example:

class MyApp : public Application
{
public:
MyApp(int argc, char *argv[]);
};

MyApp::MyApp(int argc, char *argv[]) : Application("application/my-application", argc, argv)
{
ApplicationWindow* pApplicationWindow = new ApplicationWindow(Rect(100, 100, 300, 300), "My Application", WINDOW_LAYOUT_NOTHING);

pApplicationWindow->Show();
}

int main(int argc, char *argv[])
{
MyApp pApp(argc, argv);

return pApp.Run();
}

This code creates a very simple window with just a window title and nothing else.

Now, let us add a function which when called with just terminate the application with a specified exit code.

class MyApp : public Application
{
public:
MyApp(int argc, char *argv[]);

void Terminate(Variant pVariant)
{
exit(pVariant.Int8());
}
};

MyApp::MyApp(int argc, char *argv[]) : Application("application/my-application", argc, argv)
{
ApplicationWindow* pApplicationWindow = new ApplicationWindow(Rect(100, 100, 300, 300), "My Application", WINDOW_LAYOUT_NOTHING);

pApplicationWindow->Show();
}

int main(int argc, char *argv[])
{
MyApp pApp(argc, argv);

return pApp.Run();
}

If you call the function Terminate the function will obviously terminate.
But how can this function be called from other applications?
Well, this is the part where the new API helps you a lot. Lets take a look at the command line utility dcssend. Using dcssend one can send messages from any type to registered interfaces.
The new API automatically registered an interface for us, using the applications name. (in our case this is application/my-application). Additionally a new EventSource has been created which
just means that the primary event loop is waiting for messages from the Window manager and listens on this desktop communication interface.

Now we will just register this Terminate function and publish it using a known name with a parameter named exitcode.

MyApp::MyApp(int argc, char *argv[]) : Application("application/my-application", argc, argv)
{
ApplicationWindow* pApplicationWindow = new ApplicationWindow(Rect(100, 100, 300, 300), "My Application", WINDOW_LAYOUT_NOTHING);

GetApplicationInterface()->RegisterFunction(
new DesktopCommunicationFunction (this, &MyApp::Terminate, "terminate", "exitcode", NULL));

pApplicationWindow->Show();
}

As you can see we registered a Function with the name "terminate" and with a parameter called exit code.
To actually call this function from a foreign application (in this case the terminal), just use:

dcssend application/my-application terminate exitcode 17

This will construct and send a message to the running application, and resulting in a call to the Terminate function.
It is important to note the this call happens synchronosuly which makes it very uncomplicated to use such events. (Of course you can use asynchronously remote functions in seperate threads too).

The new API is capable of doing a lot more, like sending messages back to the sender, using multiple interfaces, using interfaces in threads, etc...
Much design work has gone into the API to make use of the Desktop Communication system as easy as possible.

So, after reading this blog you should have got an idea how easy it is to remote control media applications, implement services, etc.



WCsketb

ATZJMhp WCsketb

unwxcr

tmcJcWY unwxcr

scripting the arrangement

scripting the arrangement application dcssend, and simple accretion of advice for built-in applications
download movies

very cool. this should

very cool.
this should definitely get used. lots of potential for beautiful applications.

scripting the system using dcssend, and easy gain of information for native applications - apps may feel as part of the OS and the code should be small and stable. wonderful! awesome for the users.
now all i need is to learn c++ :)

This sounds great!

This sounds great!

I already have a few ideas where this could come in handy :)