Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

The big julia-0.5 update #231

Merged
merged 10 commits into from
May 10, 2016
39 changes: 14 additions & 25 deletions src/GLib/GLib.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module GLib

if VERSION < v"0.5.0-dev+3876"
include("../compat_string.jl")
end

if false
function include(x)
println("including $x")
Expand All @@ -20,7 +24,7 @@ export signal_handler_block, signal_handler_unblock
export setproperty!, getproperty
export GConnectFlags

module Compat
module CompatGLib
export @assign_if_unassigned
macro assign_if_unassigned(expr)
# BinDeps often fails and generates corrupt deps.jl files
Expand All @@ -35,33 +39,18 @@ module Compat
end
end
end
if VERSION >= v"0.4-"
export TupleType, int, int8, int32, uint32, uint64, dlopen, dlsym_e, unsafe_convert
TupleType(types...) = Tuple{types...}
int(v) = Int(v)
int8(v) = Int8(v)
int32(v) = Int32(v)
uint32(v) = UInt32(v)
uint64(v) = UInt64(v)
const unsafe_convert = Base.unsafe_convert
import Base.Libdl: dlopen, dlsym_e
else
export TupleType, unsafe_convert
const TupleType = tuple
const unsafe_convert = Base.cconvert
end
if VERSION < v"0.3-"
export QuoteNode
QuoteNode(x) = Base.qn(x)
end
export TupleType, dlopen, dlsym_e, unsafe_convert
TupleType(types...) = Tuple{types...}
const unsafe_convert = Base.unsafe_convert
import Base.Libdl: dlopen, dlsym_e
end
importall .Compat
importall .CompatGLib

# local function, handles Symbol and makes UTF8-strings easier
typealias AbstractStringLike Union{AbstractString,Symbol}
bytestring(s) = Base.bytestring(s)
bytestring(s::Symbol) = s
bytestring(s::Ptr{UInt8},own::Bool) = UTF8String(pointer_to_array(s,int(ccall(:strlen,Csize_t,(Ptr{UInt8},),s)),own))
bytestring(s::Ptr{UInt8},own::Bool) = String(pointer_to_array(s,Int(ccall(:strlen,Csize_t,(Ptr{UInt8},),s)),own))

include(joinpath("..","..","deps","ext_glib.jl"))

Expand All @@ -80,10 +69,10 @@ macro g_type_delegate(eq)
@assert isa(eq,Expr) && eq.head == :(=) && length(eq.args) == 2
new = eq.args[1]
real = eq.args[2]
newleaf = esc(symbol(string(new,current_module().suffix)))
realleaf = esc(symbol(string(real,current_module().suffix)))
newleaf = esc(Symbol(string(new,current_module().suffix)))
realleaf = esc(Symbol(string(real,current_module().suffix)))
new = esc(new)
macroreal = QuoteNode(symbol(string('@',real)))
macroreal = QuoteNode(Symbol(string('@',real)))
quote
const $newleaf = $realleaf
macro $new(args...)
Expand Down
12 changes: 6 additions & 6 deletions src/GLib/glist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ setindex!(list::GList,x,i::Real) = setindex!(list,x,nth(list,i))
## More Array-like declarations, this time involving ccall

### Non-modifying functions
length{L<:_GSList}(list::LList{L}) = int(ccall((:g_slist_length,libglib),Cuint,(Ptr{L},),list))
length{L<:_GList}(list::LList{L}) = int(ccall((:g_list_length,libglib),Cuint,(Ptr{L},),list))
length{L<:_GSList}(list::LList{L}) = Int(ccall((:g_slist_length,libglib),Cuint,(Ptr{L},),list))
length{L<:_GList}(list::LList{L}) = Int(ccall((:g_list_length,libglib),Cuint,(Ptr{L},),list))
copy{L<:_GSList}(list::GList{L}) = typeof(list)(ccall((:g_slist_copy,libglib),Ptr{L},(Ptr{L},),list), false)
copy{L<:_GList}(list::GList{L}) = typeof(list)(ccall((:g_list_copy,libglib),Ptr{L},(Ptr{L},),list), false)
check_undefref(p::Ptr) = (p==C_NULL ? error(UndefRefError()) : p)
Expand Down Expand Up @@ -215,14 +215,14 @@ empty!{N<:Number}(li::Ptr{_GSList{Ptr{N}}}) = c_free(unsafe_load(li).data)
empty!{N<:Number}(li::Ptr{_GList{Ptr{N}}}) = c_free(unsafe_load(li).data)

### Store (byte)strings as pointers
deref_to{S<:ByteString}(::Type{S}, p::Ptr) = bytestring(convert(Ptr{UInt8},p))
function ref_to{S<:ByteString}(::Type{S}, x)
deref_to{S<:String}(::Type{S}, p::Ptr) = bytestring(convert(Ptr{UInt8},p))
function ref_to{S<:String}(::Type{S}, x)
s = bytestring(x)
l = sizeof(s)
p = convert(Ptr{UInt8},c_malloc(l+1))
unsafe_copy!(p,convert(Ptr{UInt8},pointer(s)),l)
unsafe_store!(p,'\0',l+1)
return p
end
empty!{S<:ByteString}(li::Ptr{_GSList{S}}) = c_free(unsafe_load(li).data)
empty!{S<:ByteString}(li::Ptr{_GList{S}}) = c_free(unsafe_load(li).data)
empty!{S<:String}(li::Ptr{_GSList{S}}) = c_free(unsafe_load(li).data)
empty!{S<:String}(li::Ptr{_GList{S}}) = c_free(unsafe_load(li).data)
24 changes: 16 additions & 8 deletions src/GLib/gtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ g_type_from_name(name::Symbol) = ccall((:g_type_from_name,libgobject),GType,(Ptr
const fundamental_ids = tuple(GType[g_type_from_name(name) for (name,c,j,f) in fundamental_types]...)

g_type(gtyp::GType) = gtyp
let jtypes = Expr(:block, :( g_type(::Type{Void}) = $(g_type_from_name(:void)) ))
let handled=Set(), jtypes = Expr(:block, :( g_type(::Type{Void}) = $(g_type_from_name(:void)) ))
for i = 1:length(fundamental_types)
(name, ctype, juliatype, g_value_fn) = fundamental_types[i]
if juliatype !== Union{}
if juliatype !== Union{} && !(juliatype in handled)
push!(jtypes.args, :( g_type{T<:$juliatype}(::Type{T}) = convert(GType,$(fundamental_ids[i])) ))
push!(handled, juliatype)
end
end
eval(jtypes)
Expand All @@ -63,7 +64,7 @@ G_OBJECT_CLASS_TYPE(w) = G_TYPE_FROM_CLASS(G_OBJECT_GET_CLASS(w))
g_isa(gtyp::GType, is_a_type::GType) = ccall((:g_type_is_a,libgobject),Cint,(GType,GType),gtyp,is_a_type) != 0
g_isa(gtyp, is_a_type) = g_isa(g_type(gtyp), g_type(is_a_type))
g_type_parent(child::GType) = ccall((:g_type_parent, libgobject), GType, (GType,), child)
g_type_name(g_type::GType) = symbol(bytestring(ccall((:g_type_name,libgobject),Ptr{UInt8},(GType,),g_type),false))
g_type_name(g_type::GType) = Symbol(bytestring(ccall((:g_type_name,libgobject),Ptr{UInt8},(GType,),g_type),false))

g_type_test_flags(g_type::GType, flag) = ccall((:g_type_test_flags,libgobject), Bool, (GType, GEnum), g_type, flag)
const G_TYPE_FLAG_CLASSED = 1 << 0
Expand Down Expand Up @@ -177,9 +178,16 @@ end

get_gtype_decl(name::Symbol, lib, symname::Expr) =
:( GLib.g_type{T<:$(esc(name))}(::Type{T}) = $(esc(symname)) )
get_gtype_decl(name::Symbol, lib, symname::Symbol) =
:( GLib.g_type{T<:$(esc(name))}(::Type{T}) =
ccall(($(QuoteNode(symbol(string(symname,"_get_type")))), $(esc(lib))), GType, ()) )
let handled=Set()
function get_gtype_decl(name::Symbol, lib, symname::Symbol)
if !(name in handled)
push!(handled, name)
return :( GLib.g_type{T<:$(esc(name))}(::Type{T}) =
ccall(($(QuoteNode(Symbol(string(symname,"_get_type")))), $(esc(lib))), GType, ()) )
end
nothing
end
end #let

function get_type_decl(name,iname,gtyp,gtype_decl)
ename = esc(name)
Expand Down Expand Up @@ -220,7 +228,7 @@ function get_type_decl(name,iname,gtyp,gtype_decl)
end

macro Gtype_decl(name,gtyp,gtype_decl)
get_type_decl(name,symbol(string(name,current_module().suffix)),gtyp,gtype_decl)
get_type_decl(name,Symbol(string(name,current_module().suffix)),gtyp,gtype_decl)
end

macro Gtype(iname,lib,symname)
Expand All @@ -234,7 +242,7 @@ macro Gtype(iname,lib,symname)
error("GType is currently only implemented for G_TYPE_FLAG_CLASSED")
end
gtype_decl = get_gtype_decl(iname, lib, symname)
name = symbol(string(iname,current_module().suffix))
name = Symbol(string(iname,current_module().suffix))
get_type_decl(name, iname, gtyp, gtype_decl)
end

Expand Down
17 changes: 10 additions & 7 deletions src/GLib/gvalues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ getindex{T}(gv::GV, i::Int, ::Type{T}) = getindex(mutable(gv,i), T)
getindex(gv::GV, i::Int) = getindex(mutable(gv,i))
getindex(v::GV,i::Int, ::Type{Void}) = nothing

#let
#global make_gvalue, getindex
let handled=Set()
global make_gvalue, getindex
function make_gvalue(pass_x,as_ctype,to_gtype,with_id,allow_reverse::Bool=true,fundamental::Bool=false)
with_id === :error && return
if isa(with_id,Tuple)
with_id = with_id::TupleType(Symbol,Any)
with_id = :(ccall($(Expr(:tuple, Meta.quot(symbol(string(with_id[1],"_get_type"))), with_id[2])),GType,()))
with_id = :(ccall($(Expr(:tuple, Meta.quot(Symbol(string(with_id[1],"_get_type"))), with_id[2])),GType,()))
end
if pass_x !== Union{}
if pass_x !== Union{} && !(pass_x in handled)
eval(current_module(),quote
function Base.setindex!{T<:$pass_x}(v::GLib.GV, ::Type{T})
ccall((:g_value_init,GLib.libgobject),Void,(Ptr{GLib.GValue},Csize_t), v, $with_id)
Expand All @@ -79,14 +79,15 @@ function make_gvalue(pass_x,as_ctype,to_gtype,with_id,allow_reverse::Bool=true,f
if to_gtype == :static_string
to_gtype = :string
end
if pass_x !== Union{}
if pass_x !== Union{} && !(pass_x in handled)
push!(handled, pass_x)
eval(current_module(),quote
function Base.getindex{T<:$pass_x}(v::GLib.GV,::Type{T})
x = ccall(($(string("g_value_get_",to_gtype)),GLib.libgobject),$as_ctype,(Ptr{GLib.GValue},), v)
$( if to_gtype == :string
:(x = GLib.bytestring(x))
elseif pass_x == Symbol
:(x = symbol(x))
:(x = Symbol(x))
end)
return Base.convert(T,x)
end
Expand All @@ -109,6 +110,8 @@ function make_gvalue(pass_x,as_ctype,to_gtype,with_id,allow_reverse::Bool=true,f
end
return nothing
end
end #let

const gvalue_types = Any[]
const fundamental_fns = tuple(Function[begin
(name, ctype, juliatype, g_value_fn) = fundamental_types[i]
Expand Down Expand Up @@ -181,7 +184,7 @@ function show(io::IO, w::GObject)
n = mutable(Cuint)
props = ccall((:g_object_class_list_properties,libgobject),Ptr{Ptr{GParamSpec}},
(Ptr{Void},Ptr{Cuint}),G_OBJECT_GET_CLASS(w),n)
v = gvalue(ByteString)
v = gvalue(String)
first = true
for i = 1:unsafe_load(n)
param = unsafe_load(unsafe_load(props,i))
Expand Down
66 changes: 36 additions & 30 deletions src/GLib/signals.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# id = VERSION >= v"0.4-"get, :event, Void, (ArgsT...)) do ptr, evt_args..., closure
# stuff
# end
function signal_connect{CT,RT}(cb::Function,w::GObject,sig::AbstractStringLike,
::Type{RT},param_types::Tuple,after::Bool=false,user_data::CT=w) #TODO: assert that length(param_types) is correct
if isgeneric(cb)
if !isbits(user_data) || VERSION >= v"0.4-"
if VERSION >= v"0.4-"
callback = cfunction(cb,RT,tuple(Ptr{GObject},param_types...,Ref{CT}))
else
callback = cfunction(cb,RT,tuple(Ptr{GObject},param_types...,CT))
end
ref, deref = gc_ref_closure(user_data)
return ccall((:g_signal_connect_data,libgobject), Culong,
(Ptr{GObject}, Ptr{UInt8}, Ptr{Void}, Ptr{Void}, Ptr{Void}, GEnum),
w,
bytestring(sig),
callback,
ref,
deref,
after*GConnectFlags.AFTER)
if VERSION < v"0.5.0-dev"
function signal_connect{CT,RT}(cb::Function,w::GObject,sig::AbstractStringLike,
::Type{RT},param_types::Tuple,after::Bool=false,user_data::CT=w)
if isgeneric(cb)
return signal_connect_generic(cb, w, sig, RT, param_types, after, user_data)
end
# oops, Julia doesn't support this natively yet -- fake it instead
return _signal_connect(cb, w, sig, after, true,param_types,user_data)
end
# oops, Julia doesn't support this natively yet -- fake it instead
return _signal_connect(cb, w, sig, after, true,param_types,user_data)
else
function signal_connect{CT,RT}(cb::Function,w::GObject,sig::AbstractStringLike,
::Type{RT},param_types::Tuple,after::Bool=false,user_data::CT=w)
signal_connect_generic(cb, w, sig, RT, param_types, after, user_data)
end
end

function signal_connect_generic{CT,RT}(cb::Function,w::GObject,sig::AbstractStringLike,
::Type{RT},param_types::Tuple,after::Bool=false,user_data::CT=w) #TODO: assert that length(param_types) is correct
callback = cfunction(cb,RT,tuple(Ptr{GObject},param_types...,Ref{CT}))
ref, deref = gc_ref_closure(user_data)
return ccall((:g_signal_connect_data,libgobject), Culong,
(Ptr{GObject}, Ptr{UInt8}, Ptr{Void}, Ptr{Void}, Ptr{Void}, GEnum),
w,
bytestring(sig),
callback,
ref,
deref,
after*GConnectFlags.AFTER)
end

# id = signal_connect(widget, :event) do obj, evt_args...
Expand Down Expand Up @@ -137,7 +143,7 @@ function signal_emit(w::GObject, sig::AbstractStringLike, RT::Type, args...)
detail = @quark_str sig[last(i)+1:end]
sig = sig[1:first(i)-1]
else
detail = uint32(0)
detail = UInt32(0)
end
signal_id = ccall((:g_signal_lookup,libgobject),Cuint,(Ptr{UInt8},Csize_t), sig, G_OBJECT_CLASS_TYPE(w))
return_value = RT===Void ? C_NULL : gvalue(RT)
Expand Down Expand Up @@ -329,7 +335,7 @@ function new_gsource(source_funcs::_GSourceFuncs)
gsource
end

expiration = uint64(0)
expiration = UInt64(0)
if VERSION < v"0.3-"
_isempty_workqueue() = isempty(Base.Workqueue) || (length(Base.Workqueue) == 1 && Base.Workqueue[1] === Base.Scheduler)
function uv_loop_alive(evt)
Expand Down Expand Up @@ -367,24 +373,24 @@ function uv_prepare(src::Ptr{Void},timeout::Ptr{Cint})
now = ccall((:g_source_get_time,GLib.libglib),UInt64,(Ptr{Void},),src)
expiration = convert(UInt64,now + tmout_ms*1000)
else #tmout_ms == 0
expiration = uint64(0)
expiration = UInt64(0)
end
int32(tmout_ms == 0)
Int32(tmout_ms == 0)
end
function uv_check(src::Ptr{Void})
global expiration
ex = expiration::UInt64
if !_isempty_workqueue()
return int32(1)
return Int32(1)
elseif !uv_loop_alive(Base.eventloop())
return int32(0)
return Int32(0)
elseif ex == 0
return int32(1)
return Int32(1)
elseif uv_pollfd.revents != 0
return int32(1)
return Int32(1)
else
now = ccall((:g_source_get_time,GLib.libglib),UInt64,(Ptr{Void},),src)
return int32(ex <= now)
return Int32(ex <= now)
end
end
function uv_dispatch{T}(src::Ptr{Void},callback::Ptr{Void},data::T)
Expand All @@ -393,7 +399,7 @@ end

function g_yield(data)
g_siginterruptible(yield, yield)
return int32(true)
return Int32(true)
end

sizeof_gclosure = 0
Expand Down
6 changes: 5 additions & 1 deletion src/Gtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const suffix = :Leaf
include("GLib/GLib.jl")
using .GLib
using .GLib.MutableTypes
importall .GLib.Compat
importall .GLib.CompatGLib
import .GLib: setproperty!, getproperty, AbstractStringLike, bytestring
import .GLib:
signal_connect, signal_handler_disconnect,
Expand All @@ -28,6 +28,10 @@ import .Graphics: width, height, getgc
using Cairo
import Cairo: destroy

if VERSION < v"0.5.0-dev+3876"
include("compat_string.jl")
end

typealias Index Union{Integer,AbstractVector{TypeVar(:I,Integer)}}

export GAccessor
Expand Down
2 changes: 1 addition & 1 deletion src/buttons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ length(w::GtkRadioButtonGroup) = length(start(w))
getindex!(w::GtkRadioButtonGroup, i::Integer) = convert(GtkRadioButton,start(w)[i])
isempty(grp::GtkRadioButtonGroup) = !isdefined(grp,:anchor)
function getproperty(grp::GtkRadioButtonGroup,name::AbstractStringLike)
k = symbol(name)
k = Symbol(name)
if k == :active
for b in grp
if getproperty(b,:active,Bool)
Expand Down
4 changes: 2 additions & 2 deletions src/cairo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function canvas_on_draw_event(::Ptr{GObject},cc::Ptr{Void},widget::GtkCanvas) #
ccall((:cairo_set_source_surface,Cairo._jl_libcairo), Void,
(Ptr{Void},Ptr{Void},Float64,Float64), cc, widget.back.ptr, 0, 0)
ccall((:cairo_paint,Cairo._jl_libcairo),Void, (Ptr{Void},), cc)
int32(false) # propagate the event further
Int32(false) # propagate the event further
end

function canvas_on_expose_event(::Ptr{GObject},e::Ptr{Void},widget::GtkCanvas) # e is a GdkEventExpose
Expand All @@ -108,7 +108,7 @@ function canvas_on_expose_event(::Ptr{GObject},e::Ptr{Void},widget::GtkCanvas) #
(Ptr{Void},Ptr{Void},Float64,Float64), cc, widget.back.ptr, 0, 0)
ccall((:cairo_paint,Cairo._jl_libcairo),Void, (Ptr{Void},), cc)
ccall((:cairo_destroy,Cairo._jl_libcairo),Void, (Ptr{Void},), cc)
int32(false) # propagate the event further
Int32(false) # propagate the event further
end

getgc(c::GtkCanvas) = c.backcc
Expand Down
5 changes: 5 additions & 0 deletions src/compat_string.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CompatString
typealias String Base.UTF8String
export String
end
import .CompatString.String
Loading