~jpsamaroo/DBus.jl

27d88b403cffe036cb76e3ac14a074f163f610b9 — Julian P Samaroo 1 year, 11 months ago 9e52395 master
Add working connect() and request_name!()
3 files changed, 41 insertions(+), 3 deletions(-)

M src/DBus.jl
M src/api.jl
M test/runtests.jl
M src/DBus.jl => src/DBus.jl +20 -3
@@ 1,5 1,7 @@
module DBus

export connect, request_name!

using Dbus_jll

include("api.jl")


@@ 26,12 28,27 @@ function check(title, error=ERROR)
    end
end

function connect()
    conn = dbus_bus_get(DBUS_BUS_SESSION, ERROR)
    check("Connection Error")
function connect(; err=ERROR)
    conn = dbus_bus_get(DBUS_BUS_SESSION, err)
    check("Connection Opening")
    @assert conn != C_NULL
    conn
end
function connection_close!(conn; err=ERROR)
    dbus_connection_close(conn)
    check("Connection Closing")
end

function request_name!(conn, name; err=ERROR, strict=false)
    ret = dbus_bus_request_name(conn, name, DBUS_NAME_FLAG_REPLACE_EXISTING,
                                err)
    check("Name Request")
    if strict && (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER ||
                  ret != DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER)
        error("request_name! failed: $ret")
    end
    ret
end

function __init__()
    dbus_error_init(ERROR)

M src/api.jl => src/api.jl +20 -0
@@ 27,7 27,27 @@ const DBusConnection = Cvoid
    DBUS_BUS_SYSTEM = Cint(1)
    DBUS_BUS_STARTER = Cint(2)
end
@enum DBusNameFlag begin
    DBUS_NAME_FLAG_ALLOW_REPLACEMENT = Cuint(1)
    DBUS_NAME_FLAG_REPLACE_EXISTING = Cuint(2)
    DBUS_NAME_FLAG_DO_NOT_QUEUE = Cuint(4)
end
@enum DBusRequestNameReplyFlag begin
    DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = Cuint(1)
    DBUS_REQUEST_NAME_REPLY_IN_QUEUE = Cuint(2)
    DBUS_REQUEST_NAME_REPLY_EXISTS = Cuint(3)
    DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER = Cuint(4)
end

function dbus_bus_get(bus_type::DBusBusType, error)
    ccall((:dbus_bus_get, libdbus), Ptr{DBusConnection}, (DBusBusType, Ptr{DBusError},), bus_type, error)
end
function dbus_connection_close(conn)
    ccall((:dbus_connection_close, libdbus), Cvoid, (Ptr{DBusConnection},), conn)
end
function dbus_bus_request_name(conn, name, flag, error)
    ccall((:dbus_bus_request_name, libdbus),
          DBusRequestNameReplyFlag,
          (Ptr{DBusConnection}, Cstring, DBusNameFlag, Ptr{DBusError}),
          conn, name, flag, error)
end

M test/runtests.jl => test/runtests.jl +1 -0
@@ 3,3 3,4 @@ using Test

conn = DBus.connect()
@test conn != C_NULL
request_name!(conn, "test.julia.dbus")