Skip to content

Commit

Permalink
small improvements in GI mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed Feb 3, 2024
1 parent 8babe53 commit 56117e7
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 27 deletions.
8 changes: 7 additions & 1 deletion GI/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
Glib_jll = "7746bdde-850d-59dc-9ae8-88ece973131d"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
gobject_introspection_jll = "6306888c-5478-5f78-9aeb-34cb5688c579"

[compat]
julia = "1.6"
BitFlags = "0.1.5"
CEnum = "0.4, 0.5"
EzXML = "1"
Glib_jll = "2.74.0"
Libdl = "1.6"
MacroTools = "0.5"
gobject_introspection_jll = "1.74"
3 changes: 2 additions & 1 deletion GI/src/GI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module GI
import Base: convert, cconvert, show, length, getindex, setindex!, uppercase, unsafe_convert
using Libdl

uppercase(s::Symbol) = Symbol(uppercase(string(s)))
symuppercase(s::Symbol) = Symbol(uppercase(string(s)))
symuppercase(s::AbstractString) = Symbol(uppercase(s))

export GINamespace
export const_expr
Expand Down
5 changes: 2 additions & 3 deletions GI/src/GLibBase/GLibBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import Base: convert, size, length, getindex, setindex!, get,
getindex, setindex!

export GInterface, GType, GObject, GBoxed, GTypeInstance
export GEnum, GError, GValue, gvalue, g_type
export GEnum, GError, GValue, g_type
export GHashTable, GByteArray, GArray, GPtrArray
export GList, glist_iter, _GSList, _GList
export GConnectFlags
export GList, _GSList, _GList
export cfunction_


Expand Down
23 changes: 18 additions & 5 deletions GI/src/giimport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function enum_decl(enum)
if match(r"^[a-zA-Z_]",string(name)) === nothing
name = Symbol("_$name")
end
push!(body.args, :(const $(uppercase(name)) = $val) )
push!(body.args, :(const $(symuppercase(name)) = $val) )
end
Expr(:toplevel, Expr(:module, false, Symbol(enumname), body))
end

enum_fullname(enumname,name) = Symbol(enumname,"_",uppercase(name))
enum_fullname(enumname,name) = Symbol(enumname,"_",symuppercase(name))
enum_name(enum) = Symbol(string(get_namespace(enum),get_name(enum)))

function find_symbol(l,id)
Expand Down Expand Up @@ -710,7 +710,6 @@ function convert_from_c(name::Symbol, arginfo::ArgInfo, typeinfo::TypeDesc{T}) w
return nothing
end
end
println("$owns")
throw(NotImplementedError("Unknown array type"))
end

Expand Down Expand Up @@ -948,7 +947,21 @@ function convert_from_c(name::Symbol, arginfo::ArgInfo, typeinfo::TypeDesc{T}) w
end
end

function extract_type(typeinfo::TypeInfo, info::ObjectLike)
# avoid method ambiguity
function extract_type(typeinfo::GITypeInfo, info::ObjectLike)
if is_pointer(typeinfo)
if typename(info)===:GParam # GParamSpec is a GTypeInstance but we handle it differently
throw(NotImplementedError("ObjectLike but not a GObject"))
#return TypeDesc(info,:GParamSpec,:(Ptr{GParamSpec}))
end
t = get_toplevel(info)
TypeDesc(info,typename(info),typename(info),:(Ptr{$t}))
else
# a GList has implicitly pointers to all elements
TypeDesc(info,:INVALID,:INVALID,:GObject)
end
end
function extract_type(typeinfo::Type{InstanceType}, info::ObjectLike)
if is_pointer(typeinfo)
if typename(info)===:GParam # GParamSpec is a GTypeInstance but we handle it differently
throw(NotImplementedError("ObjectLike but not a GObject"))
Expand Down Expand Up @@ -1010,7 +1023,7 @@ function jparams(args::Array{Arg})
end

# Map library names onto exports of *_jll
libnames = Dict("libglib"=>:libglib, "libgobject"=>:libgobject,
const libnames = Dict("libglib"=>:libglib, "libgobject"=>:libgobject,
"libgio"=>:libgio, "libcairo-gobject"=>:libcairo_gobject,
"libpangocairo"=>:libpangocairo, "libpangoft"=>:libpangoft,
"libpango"=>:libpango, "libatk"=>:libatk,
Expand Down
38 changes: 28 additions & 10 deletions GI/src/girepo.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Julia wrapper for libgirepository

abstract type GIRepository end
# https://gi.readthedocs.io/en/latest/
# https://gnome.pages.gitlab.gnome.org/gobject-introspection/girepository/

abstract type GIRepository end
abstract type GITypelib end

abstract type GIBaseInfo end

# a GIBaseInfo we own a reference to
mutable struct GIInfo{Typeid}
handle::Ptr{GIBaseInfo}
Expand Down Expand Up @@ -104,7 +106,7 @@ function show(io::IO, info::GICallbackInfo)
show(io, get_return_type(info))
end


"""Represents a C library namespace, like "gtk" or "pango"."""
struct GINamespace
name::Symbol
function GINamespace(namespace::Symbol, version = nothing)
Expand Down Expand Up @@ -149,10 +151,22 @@ Base.eltype(::Type{GINamespace}) = GIInfo

getindex(ns::GINamespace, name::Symbol) = gi_find_by_name(ns, name)

"""
prepend_search_path(s::AbstractString)
Add a directory that contains *.typelib files to libgirepository's search
path.
"""
function prepend_search_path(s::AbstractString)
ccall((:g_irepository_prepend_search_path, libgi), Cvoid, (Cstring,), s)
end

"""
prepend_search_path(s::Module)
For a JLL module that includes GObject introspection data, add the directory
that contains *.typelib files to libgirepository's search path.
"""
function prepend_search_path(mod::Module)
d = mod.find_artifact_dir()*"/lib/girepository-1.0"
d === Missing && error("Artifact directory not found")
Expand All @@ -163,6 +177,12 @@ function get_all(ns::GINamespace, t::Type{T},exclude_deprecated=true) where {T<:
[info for info=ns if isa(info,t) && (exclude_deprecated ? !is_deprecated(info) : true)]
end

"""
get_c_prefix(ns)
Get the C prefix for a namespace, which, for example, is "G" for GLib and "Gtk"
for GTK.
"""
function get_c_prefix(ns)
ret = ccall((:g_irepository_get_c_prefix, libgi), Ptr{UInt8}, (Ptr{GIRepository}, Cstring), C_NULL, ns)
if ret != C_NULL
Expand Down Expand Up @@ -498,12 +518,10 @@ function show(io::IO,info::GITypeInfo)
end
end

function get_constant_value(typ,info)
eval(quote
x = Ref{$typ}(0)
siz = ccall((:g_constant_info_get_value,libgi),Cint,(Ptr{GIBaseInfo}, Ref{$typ}), $info, x)
x[]
end)
function get_constant_value(::Type{T}, info) where T
x=Ref{T}(0)
siz = ccall((:g_constant_info_get_value,libgi),Cint,(Ptr{GIBaseInfo}, Ref{T}), info, x)
x[]
end

function get_value(info::GIConstantInfo)
Expand All @@ -522,7 +540,7 @@ function get_value(info::GIConstantInfo)
ccall((:g_constant_info_free_value,libgi), Nothing, (Ptr{GIBaseInfo}, Ptr{Nothing}), info, x)
val
else
nothing#unimplemented
throw(NotImplementedError("Constant with type $typ not supported"))
end
end

Expand Down
7 changes: 3 additions & 4 deletions gen/gen_gobject.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ disguised = GI.read_disguised(d)
special = [:Value]
import_as_opaque = [:ObjectClass]
struct_skiplist=vcat(disguised, special, [:CClosure,:Closure,:ClosureNotifyData,
:InterfaceInfo,:ObjectConstructParam,:ParamSpecTypeInfo,:TypeInstance,
:TypeInterface,:WeakRef])
:ObjectConstructParam,:TypeInstance,:TypeInterface,:WeakRef])

# these struct types are members in other structs, so we export them first
first_list=[:EnumValue,:TypeClass,:FlagsValue,:TypeValueTable]
first_list=[:EnumValue,:TypeClass,:TypeInterface,:FlagsValue,:TypeValueTable]
GI.struct_exprs!(exprs,exports,ns,first_list)

struct_skiplist=vcat(struct_skiplist,first_list)
Expand Down Expand Up @@ -76,7 +75,7 @@ skiplist=vcat(handled_list,[:enum_complete_type_info,:enum_register_static,:flag
:signal_accumulator_true_handled,:signal_connect_closure,:signal_connect_closure_by_id,
:signal_handler_find,:signal_handlers_block_matched,:signal_handlers_disconnect_matched,
:signal_handlers_unblock_matched,:signal_override_class_closure,
:source_set_closure,:source_set_dummy_callback,:type_add_interface_static,
:source_set_closure,:source_set_dummy_callback,
:type_check_instance,:type_check_instance_is_a,:type_check_instance_is_fundamentally_a,
:type_default_interface_unref,:type_free_instance,
:type_name_from_instance,:type_register_fundamental,
Expand Down
1 change: 1 addition & 0 deletions src/GLib/gio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ end

cancel(c::GCancellable) = G_.cancel(c)
iscancelled(c::GCancellable) = G_.is_cancelled(c)

2 changes: 0 additions & 2 deletions src/GLib/gtype.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
abstract type GTypeInstance end
abstract type GObject <: GTypeInstance end
abstract type GTypeInterface end
abstract type _GTypeInterface end
abstract type GInterface <: GObject end
abstract type GBoxed end

Expand Down
13 changes: 13 additions & 0 deletions src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ function GtkShortcutController(widget::GtkWidget)
g
end

"""
add_action_shortcut(scc::GtkShortcutController,trigger::AbstractString,action::AbstractString)
Adds a shortcut specified by a string like "<Control>S" for an action (such as
"app.save") to a `GtkShortcutController`.
"""
function add_action_shortcut(scc::GtkShortcutController,trigger::AbstractString,action::AbstractString)
t = GtkShortcutTrigger(trigger)
a = GtkShortcutAction("action($action)")
sc = GtkShortcut(t,a)
Gtk4.G_.add_shortcut(scc,sc)
end

push!(sc::GtkShortcutController, s::GtkShortcut) = (G_.add_shortcut(sc,s);sc)

function GtkGestureClick(widget::GtkWidget,button=1)
Expand Down
4 changes: 4 additions & 0 deletions src/gen/gobject_functions
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ $(Expr(:toplevel, quote
ret = ccall(("g_type_add_interface_dynamic", libgobject), Nothing, (UInt64, UInt64, Ptr{GObject}), _instance_type, _interface_type, _plugin)
nothing
end
function type_add_interface_static(_instance_type::Integer, _interface_type::Integer, _info::Union{GInterfaceInfo, Ref{_GInterfaceInfo}})
ret = ccall(("g_type_add_interface_static", libgobject), Nothing, (UInt64, UInt64, Ptr{_GInterfaceInfo}), _instance_type, _interface_type, _info)
nothing
end
function type_check_class_is_a(_g_class::Union{GTypeClass, Ref{_GTypeClass}}, _is_a_type::Integer)
ret = ccall(("g_type_check_class_is_a", libgobject), Cint, (Ptr{_GTypeClass}, UInt64), _g_class, _is_a_type)
ret2 = convert(Bool, ret)
Expand Down
47 changes: 46 additions & 1 deletion src/gen/gobject_structs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ $(Expr(:toplevel, quote
convert(Ptr{_GTypeClass}, box.handle)
end
end
begin
struct _GTypeInterface
g_type::UInt64
g_instance_type::UInt64
end
mutable struct GTypeInterface
handle::Ptr{_GTypeInterface}
end
unsafe_convert(::Type{Ptr{_GTypeInterface}}, box::GTypeInterface) = begin
convert(Ptr{_GTypeInterface}, box.handle)
end
end
begin
struct _GFlagsValue
value::UInt32
Expand Down Expand Up @@ -84,6 +96,37 @@ $(Expr(:toplevel, quote
convert(Ptr{_GFlagsClass}, box.handle)
end
end
begin
struct _GInterfaceInfo
interface_init::Ptr{Cvoid}
interface_finalize::Ptr{Cvoid}
interface_data::Ptr{Nothing}
end
mutable struct GInterfaceInfo
handle::Ptr{_GInterfaceInfo}
end
unsafe_convert(::Type{Ptr{_GInterfaceInfo}}, box::GInterfaceInfo) = begin
convert(Ptr{_GInterfaceInfo}, box.handle)
end
end
begin
struct _GParamSpecTypeInfo
instance_size::UInt16
n_preallocs::UInt16
instance_init::Ptr{Cvoid}
value_type::UInt64
finalize::Ptr{Cvoid}
value_set_default::Ptr{Cvoid}
value_validate::Ptr{Cvoid}
values_cmp::Ptr{Cvoid}
end
mutable struct GParamSpecTypeInfo
handle::Ptr{_GParamSpecTypeInfo}
end
unsafe_convert(::Type{Ptr{_GParamSpecTypeInfo}}, box::GParamSpecTypeInfo) = begin
convert(Ptr{_GParamSpecTypeInfo}, box.handle)
end
end
begin
struct _GSignalInvocationHint
signal_id::UInt32
Expand Down Expand Up @@ -204,6 +247,8 @@ $(Expr(:toplevel, quote
end
@doc "See the [GTK docs](https://docs.gtk.org/gobject/struct.EnumClass.html)." GEnumClass
@doc "See the [GTK docs](https://docs.gtk.org/gobject/struct.FlagsClass.html)." GFlagsClass
@doc "See the [GTK docs](https://docs.gtk.org/gobject/struct.InterfaceInfo.html)." GInterfaceInfo
@doc "See the [GTK docs](https://docs.gtk.org/gobject/struct.ParamSpecTypeInfo.html)." GParamSpecTypeInfo
@doc "See the [GTK docs](https://docs.gtk.org/gobject/struct.SignalInvocationHint.html)." GSignalInvocationHint
@doc "See the [GTK docs](https://docs.gtk.org/gobject/struct.SignalQuery.html)." GSignalQuery
@doc "See the [GTK docs](https://docs.gtk.org/gobject/struct.TypeFundamentalInfo.html)." GTypeFundamentalInfo
Expand Down Expand Up @@ -378,6 +423,6 @@ $(Expr(:toplevel, quote
ret = f(ihint, n_param_values, param_values)
convert(Cint, ret)
end
export GEnumValue, _GEnumValue, GTypeClass, _GTypeClass, GFlagsValue, _GFlagsValue, GTypeValueTable, _GTypeValueTable, GEnumClass, _GEnumClass, GFlagsClass, _GFlagsClass, GSignalInvocationHint, _GSignalInvocationHint, GSignalQuery, _GSignalQuery, GTypeFundamentalInfo, _GTypeFundamentalInfo, GTypeInfo, _GTypeInfo, GTypePluginClass, _GTypePluginClass, GTypeQuery, _GTypeQuery, GValueArray, _GValueArray, GBinding, GBindingLeaf, GBindingGroup, GBindingGroupLeaf, GInitiallyUnowned, GInitiallyUnownedLeaf, GTypeModule, GTypePlugin, GBindingTransformFunc, GClassFinalizeFunc, GClassInitFunc, GClosureMarshal, GInterfaceFinalizeFunc, GInterfaceInitFunc, GSignalAccumulator, GSignalEmissionHook
export GEnumValue, _GEnumValue, GTypeClass, _GTypeClass, GTypeInterface, _GTypeInterface, GFlagsValue, _GFlagsValue, GTypeValueTable, _GTypeValueTable, GEnumClass, _GEnumClass, GFlagsClass, _GFlagsClass, GInterfaceInfo, _GInterfaceInfo, GParamSpecTypeInfo, _GParamSpecTypeInfo, GSignalInvocationHint, _GSignalInvocationHint, GSignalQuery, _GSignalQuery, GTypeFundamentalInfo, _GTypeFundamentalInfo, GTypeInfo, _GTypeInfo, GTypePluginClass, _GTypePluginClass, GTypeQuery, _GTypeQuery, GValueArray, _GValueArray, GBinding, GBindingLeaf, GBindingGroup, GBindingGroupLeaf, GInitiallyUnowned, GInitiallyUnownedLeaf, GTypeModule, GTypePlugin, GBindingTransformFunc, GClassFinalizeFunc, GClassInitFunc, GClosureMarshal, GInterfaceFinalizeFunc, GInterfaceInitFunc, GSignalAccumulator, GSignalEmissionHook
end))
end

0 comments on commit 56117e7

Please sign in to comment.