tags:

views:

106

answers:

3

I'm reading through a tutorial on using voice commands to control applications and, in an example of controlling rhythmbox, it suggests commands such as the following can be executed:

rhythmbox-client --play
rhythmbox-client --pause

Why does this not simply open a new instance of the program, and how can I emulate the functionality in my own programs? For example, how could I pass a string to a particular instance of a program?

Thanks

A: 

There are several techniques to have only one application instance running. In these terms calling yyy --play would generally mean the same as

INSTANCE = GET_RUNNING_INSTANCE()
IF INSTANCE == NULL
   INSTANCE = CREATE_NEW_INSTANCE()
SEND_MESSAGE(INSTANCE, PLAY)
Kotti
+4  A: 

Rhythmbox uses inter-process communictation to achieve this type of functionality, and this can be implemented in a number of different ways. One of them is to use D-Bus, like Rhythmbox does.

Using D-Bus is not very easy, but the basic idea is that you register your application in D-Bus, so other applications can call different procedures your app exports (for example play/stop actions), and then in the same application implement a client. This way, if arguments like --play are passed, you don't run the usual code, but just check for an instance of the running app and send a command to your already running program.

On the other hand, when no arguments are passed, your program just starts and registers the proper triggers, so that a later called instance can control it.

Here is a tutorial on dbus, and the DBus homepage

Michał Trybus
A: 

For example, how could I pass a string to a particular instance of a program?

You'll need to use whatever interprocess communication facilities your operating system offers. Sockets or named pipes, for example, or messages.

SigTerm