-
Notifications
You must be signed in to change notification settings - Fork 59
/
eventloop.jl
96 lines (86 loc) · 3.05 KB
/
eventloop.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
polled_events()::Cvoid
Event Callback: allows R to process Julia events when R is busy. For example, writing output to stdout while running an expensive R command.
See [Writing R Extensions: Calling R.dll directly](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Calling-R_002edll-directly)
and [Writing R Extensions: Meshing Event Loops](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Meshing-event-loops)
"""
function polled_events()::Cvoid
# dump output buffer to stdout when available
handle_eval_stdout()
nothing
end
# there is no use now, maybe useful for the future.
function interrupts_pending(s::Bool=true)
@static if Sys.iswindows()
unsafe_store!(cglobal((:UserBreak,libR),Cint), s ? 1 : 0)
else
unsafe_store!(cglobal((:R_interrupts_pending,libR),Cint), s ? 1 : 0)
end
nothing
end
# this shouldn't exist if we could hook into Julia eventloop.
function process_events()
##FIXME: a dirty fix to prevent segfault right after a sigint
if unsafe_load(cglobal((:R_interrupts_pending,libR),Cint)) == 0
@static if Sys.iswindows() || Sys.isapple()
ccall((:R_ProcessEvents, libR), Nothing, ())
end
@static if Sys.isunix()
what = ccall((:R_checkActivity,libR),Ptr{Cvoid},(Cint,Cint),0,1)
if what != C_NULL
R_InputHandlers = unsafe_load(cglobal((:R_InputHandlers,libR),Ptr{Cvoid}))
ccall((:R_runHandlers,libR),Nothing,(Ptr{Cvoid},Ptr{Cvoid}),R_InputHandlers,what)
end
end
end
nothing
end
global timeout = nothing
function rgui_start(silent=false)
global timeout
if timeout == nothing
timeout = Base.Timer(x -> process_events(), 0.05, interval = 0.05)
return true
else
silent || error("eventloop is already running.")
return false
end
end
function rgui_stop(silent=false)
global timeout
if timeout != nothing
close(timeout)
timeout = nothing
return true
else
silent || error("eventloop is not running.")
return false
end
end
function set_hook(hookname, value)
l = rparse("""setHook(hookname, function(...) foo)""")
l[1][2] = hookname
l[1][3][3] = value
reval(l)
end
function rgui_init()
f = rlang(rgui_start, true)
set_hook("plot.new", f)
set_hook("persp", f)
set_hook("grid.newpage", f)
set_hook(rlang(:packageEvent, "rgl", "onLoad"), f)
# inject rgui_start(TRUE) to utils::help
help_type = rcopy(rcall(:options, "help_type")[1])
if help_type == "html"
# need to hack both as.environment('package:utils') and getNamespace("utils")
# to make ?foo and help("foo") to work
l = rparse("help <- function(...) { foo(); bar(...) }")
l[1][3][3][2] = f
l[1][3][3][3][1] = reval("utils:::help")
for env in (reval("as.environment('package:utils')"), getNamespace("utils"))
rcall(:unlockBinding, "help", env)
reval(l, env)
rcall(:lockBinding, "help", env)
end
end
end