...making Linux just a little more fun!

<-- prev | next -->

Using DCOP from the command line

By Jimmy O'Regan


DCOP is KDE's IPC/RPC mechanism. If you understood that sentence, please feel free to skip the next paragraph.

DCOP stands for Desktop COmmunications Protocol, IPC stands for Inter-Process Communication; RPC stands for Remote Procedure Call. In essence, they are means by which two programs can communicate, whether on the same machine or across a network. In this regard, DCOP is similar to Microsoft's OLE Automation: it provides a simple way for developers to provide access to the functions available in an application.

KDE was originally attempting to build a CORBA-based component system (the GNOME project originally started as this project, but became a desktop project in a disagreement over the license the Qt widget set was using), but it was decided that CORBA was too complicated, and two simpler systems were introduced; KParts, for embedded components; and DCOP.

A DCOP enabled application registers at launch time with the DCOP daemon, and registers its functions. When queried, the dcop daemon provides a list of running processes and the functions they provide. This enables a range of applications, such as the DCOP browser, or the DCOP to XMLRPC daemon; but the one I'm going to look at is 'dcop', which allows shell access to DCOP.

Unlike other systems, such as GNOME's Bonobo or Microsoft's Automation, DCOP does not provide an activation system; only running processes are listed by DCOP. Activating the application is left up to the user.

This article is about using DCOP to control KDE applications from the command line; links are provided at the end of the page to developer information for those who wish to delve deeper into this technology.

Using DCOP from the command line

The 'dcop' program provides command line access to the DCOP system. The syntax is:

dcop [application] [object] [function] [arguments ...]

To see which applications are available, enter 'dcop' with no arguments

$ dcop

Let's have a look at klipper, the KDE clipboard service:

$ dcop klipper

We can assume that klipper is the object we're looking for here; the default object (i.e., the one you want, to control the application) is normally given the same name as the application, with or without "IFace" appended, or is marked as "default":

$dcop klipper klipper
QCStringList interfaces()
QCStringList functions()
QString getClipboardContents()
void setClipboardContents(QString s)
void clearClipboardContents()
int newInstance()
void quitProcess()

Now, I think the most interesting functions here are setClipboardContents and getClipboardContents. In fact, I've been using setClipboardContents in a konsole window to add the shell output above:

dcop klipper klipper setClipboardContents "$(dcop klipper klipper)"

Now, to be honest, this isn't the best example, as it's a lot quicker to use the mouse than to type that, but it can become a lot more useful if set as an alias, e.g.

alias klip="dcop klipper klipper setClipboardContents"

I've always thought it would be useful to have post-it notes available from the shell - DCOP makes that possible.

$dcop knotes KNotesIface
QCStringList interfaces()
QCStringList functions()
int newNote(QString name,QString text)
int newNoteFromClipboard(QString name)
ASYNC showNote(int noteId)
ASYNC hideNote(int noteId)
ASYNC killNote(int noteId)
QMap notes()
ASYNC setName(int noteId,QString newName)
ASYNC setText(int noteId,QString newText)
QString text(int noteId)
ASYNC sync(QString app)
bool isNew(QString app,int noteId)
bool isModified(QString app,int noteId)

So let's add a note:

dcop knotes KNotesIface newNote "A note" "Stuff I want to keep track of"

Sure enough, a bright yellow note pops up.

Let's see what notes are available:

$dcop knotes KNotesIface notes
1->A note

After adding a few more nonsense notes, maybe I want to have them in a text file, so I wrote a silly script to go with a silly idea:

n=$(dcop knotes KNotesIface notes|awk -F- '{print $1}'|tail -1)
dcop knotes KNotesIface notes > $1
echo >> $1
for ((i=1;i<=$n;i++));do echo -n "$i: "; dcop knotes KNotesIface text $i;echo;done >> $1


DCOP is a powerful means to control KDE applications. As more and more developers recognise its usefulness, we get closer to a nice blend of desktop eye candy and scriptability.



[BIO] Jimmy is a single father of one, who enjoys long walks... Oh, right.

Jimmy has been using computers from the tender age of seven, when his father inherited an Amstrad PCW8256. After a few brief flirtations with an Atari ST and numerous versions of DOS and Windows, Jimmy was introduced to Linux in 1998 and hasn't looked back.

In his spare time, Jimmy likes to play guitar and read: not at the same time, but the picks make handy bookmarks.

Copyright © 2003, Jimmy O'Regan. Released under the Open Publication license unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 97 of Linux Gazette, December 2003

<-- prev | next -->