1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
<html>
<head>
<link href="../lg.css" rel="stylesheet" type="text/css" media="screen, projection" />
<title>Using DCOP from the command line LG #97</title>
<style type="text/css" media="screen, projection">
<!--
.articlecontent {
position:absolute;
top:143px;
}
-->
</style>
</head>
<body>
<img src="../gx/2003/newlogo-blank-200-gold2.jpg" id="logo" alt="Linux Gazette"/>
<p id="fun">...making Linux just a little more fun!</p>
<div class="content articlecontent">
<div id="previousnexttop">
<A HREF="oregan.html" ><-- prev</A> | <A HREF="pramode.html" >next --></A>
</div>
<h1>Using DCOP from the command line</h1>
<p id="by"><b>By <A HREF="../authors/oregan.html">Jimmy O'Regan</A></b></p>
<p>
<h2>Introduction</h2>
<p>DCOP is KDE's IPC/RPC mechanism. If you understood that sentence, please feel free to skip the next paragraph.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<h2>Using DCOP from the command line</h2>
<p>The 'dcop' program provides command line access to the DCOP system. The syntax is:</p>
<pre>dcop [application] [object] [function] [arguments ...]</pre>
<p>To see which applications are available, enter 'dcop' with no arguments</p>
<pre>
$ dcop
khelpcenter
kwin
kicker
kword-4354
kded
knotify
kio_uiserver
kalarmd
kcookiejar
konsole-4300
korgac
klauncher
kdesktop
klipper
ksmserver
</pre>
<p>Let's have a look at klipper, the KDE clipboard service:</p>
<pre>
$ dcop klipper
qt
klipper
</pre>
<p>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":</p>
<pre>
$dcop klipper klipper
QCStringList interfaces()
QCStringList functions()
QString getClipboardContents()
void setClipboardContents(QString s)
void clearClipboardContents()
int newInstance()
void quitProcess()
</pre>
<p>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:</p>
<pre>dcop klipper klipper setClipboardContents "$(dcop klipper klipper)"</pre>
<p>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.</p>
<pre>alias klip="dcop klipper klipper setClipboardContents"</pre>
<p>I've always thought it would be useful to have post-it notes available from the shell - DCOP makes that possible.</p>
<pre>
$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<int,QString> 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)
</pre>
<p>So let's add a note:</p>
<pre>dcop knotes KNotesIface newNote "A note" "Stuff I want to keep track of"</pre>
<p>Sure enough, a bright yellow note pops up. </p>
<p>Let's see what notes are available:</p>
<pre>
$dcop knotes KNotesIface notes
1->A note
</pre>
<p>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:</p>
<pre>
#!/bin/bash
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
</pre>
<h2>Summary</h2>
<p>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.</p>
<h2>Links</h2>
<ul>
<li><a href="http://developer.kde.org/documentation/library/kdeqt/dcop.html">Programming with DCOP</a>
<li><a href="http://developer.kde.org/documentation/books/kde-2.0-development/ch13.html">Chapter from KDE 2.0 Development</a>
</ul>
</p>
<!-- *** BEGIN author bio *** -->
<P>
<P>
<!-- *** BEGIN bio *** -->
<P>
<img ALIGN="LEFT" ALT="[BIO]" SRC="../gx/2002/note.png">
<em>
</em>
<br CLEAR="all">
<!-- *** END bio *** -->
<!-- *** END author bio *** -->
<div id="articlefooter">
<p>
Copyright © 2003, Jimmy O'Regan. Copying license
<a href="http://linuxgazette.net/copying.html">http://linuxgazette.net/copying.html</a>
</p>
<p>
Published in Issue 97 of Linux Gazette, December 2003
</p>
</div>
<div id="previousnextbottom">
<A HREF="oregan.html" ><-- prev</A> | <A HREF="pramode.html" >next --></A>
</div>
</div>
<div id="navigation">
<a href="../index.html">Home</a>
<a href="../faq/index.html">FAQ</a>
<a href="../lg_index.html">Site Map</a>
<a href="../mirrors.html">Mirrors</a>
<a href="../mirrors.html">Translations</a>
<a href="../search.html">Search</a>
<a href="../archives.html">Archives</a>
<a href="../authors/index.html">Authors</a>
<a href="../contact.html">Contact Us</a>
</div>
<div id="breadcrumbs">
<a href="../index.html">Home</a> >
<a href="index.html">December 2003 (#97)</a> >
Article
</div>
<img src="../gx/2003/sit3-shine.7-2.gif" id="tux" alt="Tux"/>
</body>
</html>
|