DBus

From Inkscape Wiki
Jump to navigation Jump to search

Inkscape has supported a DBus interface for some time.... but information on how to use it is limited. It is also not clear how to use Gio::Actions which are suppose to work with DBus. This page is an investigation into using DBus with Inkscape.

It would be very useful if anyone who has experience with using the DBus interface add information here!

DBus Support in Inkscape

Inkscape must be compiled with DBus support. Most available pre-built versions of Inkscape do NOT include DBus support. To enable DBus support, use: cmake -DWITH_DBUS=ON path_to_source

Inkscape is using a deprecated library for DBus support. See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=955903

Use with Python

There is some Python documentation in the src/extension/dbus/doc directory. It's in DocBook format and incomplete. Here is a simple python program for interfacing with a running Inkscape program that has been compiled with DBUS support:

#!/usr/bin/env python

# Adapted from documentation in the src/extension/dbus/doc directory.

print("Inkscape DBus Interface Test")

import dbus


# Get the session bus.
try:
   bus = dbus.SessionBus()
except BaseException:
   print("No bus")
   exit()
print("Got DBus bus")

# Get the object for the application.
try:
   inkapp = bus.get_object('org.inkscape',
                           '/org/inkscape/application')
except BaseException:
   print("No Inkscape application found")
   exit();
print("Got connection to Inkscape application")

# Request a new desktop.
desk = inkapp.desktop_new(dbus_interface='org.inkscape.application')

# Get the object for that desktop.
inkdoc = bus.get_object('org.inkscape', desk)

# Tell it what interface it is using so we don't have to type it for every method.
doc = dbus.Interface(inkdoc, dbus_interface="org.inkscape.document")

# Use!

doc.document_set_css ("fill:#ff0000;stroke:#000000")
rect = doc.rectangle (25,  25, 50, 50)
#                      x    y   w   h

doc.document_set_css ("fill:#0000ff;stroke:#000000;stroke-width:3px")
circle = doc.ellipse (125, 25, 50, 50)
#                      x    y   w   h

doc.document_set_css ("fill:#ffff00;stroke:#000000;stroke-width:5px")
star = doc.star (50, 150, 40, 16,   0, -0.314,     5, 0.314)
#                cx   cy  r1  r2 rand    arg1  sides   arg2

Use with Emacs

Verification that DBus actually still works (on Fedora):

  • Start Inkscape.
  • Open an Emacs window.
  • Type:

    (message "%s on board." (cond   ((dbus-ping :session "org.inkscape" 100) "Inkscape")   (t "No")))

    Put the cursor at the very end of the function (just after ')))') and type Ctrl-X Ctrl-E.
  • The Emacs 'Message" buffer should show "Inkscape on board" if you have successfully connected to the running Inkscape app.

Open a New Inkscape Window:

  • Type:

    (dbus-call-method               :session "org.inkscape" "/org/inkscape/application"               "org.inkscape.application" "desktop_new")

    Put the cursor at the very end of the function and type Ctrl-X Ctrl-E.
  • The Emacs "Message" buffer should show "/org/inkscape/desktop_1" and a new window should have opened.

Add a rectangle:

  • Type:

    (dbus-call-method               :session "org.inkscape" "/org/inkscape/desktop_1"               "org.inkscape.document" "rectangle" :int32 100 :int32  100 :int32  100 :int32  100)

    Put the cursor at the very end of the function and type Ctrl-X Ctrl-E.
  • A rectangle should appear in the new Inkscape drawing.

Notes

Command line arguments:

  • --dbus-listen
  • --dbus-name=BUS-NAME
  • --display=DISPLAY

How to use these? At least the --dbus-name does not seem to work (bus name is always "org.inkscape").

Useful links: