~jpsamaroo/DBus.jl

9e5239501e185e95ce1b6d1a321da3de82b169ba — Julian P Samaroo 1 year, 11 months ago
Initial commit
5 files changed, 113 insertions(+), 0 deletions(-)

A LICENSE.md
A Project.toml
A src/DBus.jl
A src/api.jl
A test/runtests.jl
A  => LICENSE.md +22 -0
@@ 1,22 @@
The DBus.jl package is licensed under the MIT "Expat" License:

> Copyright (c) 2020: Julian P Samaroo.
> 
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
> 
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
> 
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> SOFTWARE.
> 

A  => Project.toml +13 -0
@@ 1,13 @@
name = "DBus"
uuid = "e1709666-7621-4c4b-92f5-7386f2fe0aaa"
authors = ["Julian P Samaroo <jpsamaroo@jpsamaroo.me>"]
version = "0.1.0"

[deps]
Dbus_jll = "ee1fde0b-3d02-5ea6-8484-8dfef6360eab"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]

A  => src/DBus.jl +40 -0
@@ 1,40 @@
module DBus

using Dbus_jll

include("api.jl")

const ERROR = Ref{DBusError}()

struct DBusException
    title::String
    name::Cstring
    msg::Cstring
end
function Base.show(io::IO, ex::DBusException)
    name = Base.unsafe_string(ex.name)
    msg = Base.unsafe_string(ex.msg)
    print(io, ex.title, " (", name, "): ", msg)
end

function check(title, error=ERROR)
    if dbus_error_is_set(error)
        name = error[].name
        msg = error[].message
        dbus_error_free(error)
        throw(DBusException(title, name, msg))
    end
end

function connect()
    conn = dbus_bus_get(DBUS_BUS_SESSION, ERROR)
    check("Connection Error")
    @assert conn != C_NULL
    conn
end

function __init__()
    dbus_error_init(ERROR)
end

end # module

A  => src/api.jl +33 -0
@@ 1,33 @@
struct DBusError
    name::Cstring
    message::Cstring

    dummy1::Cuint
    dummy2::Cuint
    dummy3::Cuint
    dummy4::Cuint
    dummy5::Cuint

    padding::Ptr{Cvoid}
end

function dbus_error_init(error)
    ccall((:dbus_error_init, libdbus), Cvoid, (Ptr{DBusError},), error)
end
function dbus_error_free(error)
    ccall((:dbus_error_free, libdbus), Cvoid, (Ptr{DBusError},), error)
end
function dbus_error_is_set(error)
    ccall((:dbus_error_is_set, libdbus), Cint, (Ptr{DBusError},), error) != 0
end

const DBusConnection = Cvoid
@enum DBusBusType begin
    DBUS_BUS_SESSION = Cint(0)
    DBUS_BUS_SYSTEM = Cint(1)
    DBUS_BUS_STARTER = Cint(2)
end

function dbus_bus_get(bus_type::DBusBusType, error)
    ccall((:dbus_bus_get, libdbus), Ptr{DBusConnection}, (DBusBusType, Ptr{DBusError},), bus_type, error)
end

A  => test/runtests.jl +5 -0
@@ 1,5 @@
using DBus
using Test

conn = DBus.connect()
@test conn != C_NULL