4

I'm using GREE Labs' Dbus PHP Extension in my attempts to make a PHP class that is able to create desktop notifications.

$dbus = $dbus = dbus_bus_get(DBUS_BUS_SESSION);

$message = new \DBusMessage(DBUS_MESSAGE_TYPE_SIGNAL);
$message->setDestination("org.freedesktop.DBus");
$message->setAutoStart(true);

$dbus->sendWithReplyAndBlock($message, 1);

When my code is run I get the following error:

Warning: dbus_bus_get() [function.dbus-bus-get]: failed to create dbus connection object [Unable to autolaunch a dbus-daemon without a $DISPLAY for X11] in [...COI/GTK/Notify.php on line 39

This is the first time I've used anything related to dbus, and am rather lost.

I'm aiming for an effect similar to what occurs when one executes the following in a terminal (on Ubuntu 11.10):

/usr/bin/notify-send -t 2000 'title' 'message'

I did initially use notify-send & exec, but switched when I found the GREE Dbus extension as I thought it may provide a cleaner interface. Also notify-send would only work properly if I changed my apache user to be the same as the user I'm currently logged in as - not an ideal solution.

Would anyone be able to either tell me what modifications are required to achieve my desired result, or alternatively tell me if what I want to do is in fact impossible?

Or, is there another way I should be doing this?

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
  • You could try: http://stackoverflow.com/questions/2701059/how-to-send-a-notification-to-another-user-with-notify-send-bash – Petah Dec 19 '11 at 03:26

2 Answers2

5

Dbus does not like being run while in a command line environment, without X. It's frustrating, but this is what I wrote in python to override that. It comes down to setting two environmental variables.

def run(self):
    os.environ['DBUS_SESSION_BUS_ADDRESS'] = "unix:path=/run/dbus/system_bus_socket"
    os.environ["DISPLAY"] = ":0"

    try:
        bus_name = dbus.service.BusName(INTERFACE,
                                    bus = dbus.SessionBus())
        dbus.service.Object.__init__(self, bus_name, 
                                    '/com/your/path/here')
        gobject.MainLoop().run()
    except Exception, E:
        f = file('/tmp/bus.log', 'a')
        f.write(str(E))
        f.close()

EDIT: I forgot another very useful way to run dbus on the command line

eval 'dbus-launch --auto-syntax' [command]

I use it on a raspberry pi to run my custom dbus deamons. dbus-launch --auto-syntax is a command that outputs environmental variables and files applicable to dbus in bash. The eval command will take that output and evaluate it so your command will run with those environmental variables.

A simple test would be to run something like this:

eval 'dbus-launch --auto-syntax' python /usr/bin/my-dbus-daemon.py
eval 'dbus-launch --auto-syntax' qdbus org.dbus.method /org/dbus/method/test
NuclearPeon
  • 5,743
  • 4
  • 44
  • 52
  • 1
    Or on the command line: `DISPLAY=":0" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/dbus/system_bus_socket" /your/bin/program` – NuclearPeon Jul 19 '13 at 18:31
1

Use dbus-launch in the script that starts your web server in order to start an appropriate DBus daemon at the same time. See the dbus-launch(1) man page for details.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358