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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
#
# tkafter.rb : methods for Tcl/Tk after command
#
# $Id$
#
require 'tk'
class TkTimer
include TkCore
extend TkCore
TkCommandNames = ['after'.freeze].freeze
Tk_CBID = ['a'.freeze, '00000'.taint].freeze
Tk_CBTBL = {}.taint
TkCore::INTERP.add_tk_procs('rb_after', 'id', <<-'EOL')
if {[set st [catch {ruby [format "TkTimer.callback %%Q!%s!" $id]} ret]] != 0} {
return -code $st $ret
} {
return $ret
}
EOL
###############################
# class methods
###############################
def self.callback(obj_id)
@after_id = nil
ex_obj = Tk_CBTBL[obj_id]
return "" if ex_obj == nil; # canceled
ex_obj.cb_call
end
def self.info
tk_call('after', 'info').split(' ').collect!{|id|
ret = Tk_CBTBL.find{|key,val| val.after_id == id}
(ret == nil)? id: ret[1]
}
end
###############################
# instance methods
###############################
def do_callback
@in_callback = true
begin
@return_value = @current_proc.call(self)
rescue Exception => e
if @cancel_on_exception
cancel
@return_value = e
return e
else
fail e
end
end
if @set_next
set_next_callback(@current_args)
else
@set_next = true
end
@in_callback = false
@return_value
end
def set_callback(sleep, args=nil)
@after_script = "rb_after #{@id}"
@after_id = tk_call('after', sleep, @after_script)
@current_args = args
@current_script = [sleep, @after_script]
self
end
def set_next_callback(args)
if @running == false || @proc_max == 0 || @do_loop == 0
Tk_CBTBL.delete(@id) ;# for GC
@running = false
@wait_var.value = 0
return
end
if @current_pos >= @proc_max
if @do_loop < 0 || (@do_loop -= 1) > 0
@current_pos = 0
else
Tk_CBTBL.delete(@id) ;# for GC
@running = false
@wait_var.value = 0
return
end
end
@current_args = args
if @sleep_time.kind_of? Proc
sleep = @sleep_time.call(self)
else
sleep = @sleep_time
end
@current_sleep = sleep
cmd, *cmd_args = @loop_proc[@current_pos]
@current_pos += 1
@current_proc = cmd
set_callback(sleep, cmd_args)
end
def initialize(*args)
@id = Tk_CBID.join
Tk_CBID[1].succ!
@wait_var = TkVariable.new(0)
@cb_cmd = TkCore::INTERP.get_cb_entry(self.method(:do_callback))
@set_next = true
@init_sleep = 0
@init_proc = nil
@init_args = []
@current_script = []
@current_proc = nil
@current_args = nil
@return_value = nil
@sleep_time = 0
@current_sleep = 0
@loop_exec = 0
@do_loop = 0
@loop_proc = []
@proc_max = 0
@current_pos = 0
@after_id = nil
@after_script = nil
@cancel_on_exception = true
set_procs(*args) if args != []
@running = false
@in_callback = false
end
attr :after_id
attr :after_script
attr :current_proc
attr :current_args
attr :current_sleep
alias :current_interval :current_sleep
attr :return_value
attr_accessor :loop_exec
def cb_call
@cb_cmd.call
end
def get_procs
[@init_sleep, @init_proc, @init_args, @sleep_time, @loop_exec, @loop_proc]
end
def current_status
[@running, @current_sleep, @current_proc, @current_args,
@do_loop, @cancel_on_exception]
end
def cancel_on_exception?
@cancel_on_exception
end
def cancel_on_exception=(mode)
@cancel_on_exception = mode
self
end
def running?
@running
end
def loop_rest
@do_loop
end
def loop_rest=(rest)
@do_loop = rest
self
end
def set_procs(interval, loop_exec, *procs)
if !interval == 'idle' \
&& !interval.kind_of?(Integer) && !interval.kind_of?(Proc)
fail format("%s need to be Integer or Proc", interval.inspect)
end
@sleep_time = interval
@loop_proc = []
procs.each{|e|
if e.kind_of? Proc
@loop_proc.push([e])
else
@loop_proc.push(e)
end
}
@proc_max = @loop_proc.size
@current_pos = 0
@do_loop = 0
if loop_exec
if loop_exec.kind_of?(Integer) && loop_exec < 0
@loop_exec = -1
elsif loop_exec == nil || loop_exec == false || loop_exec == 0
@loop_exec = 1
else
if not loop_exec.kind_of?(Integer)
fail format("%s need to be Integer", loop_exec.inspect)
end
@loop_exec = loop_exec
end
@do_loop = @loop_exec
end
self
end
def add_procs(*procs)
procs.each{|e|
if e.kind_of? Proc
@loop_proc.push([e])
else
@loop_proc.push(e)
end
}
@proc_max = @loop_proc.size
self
end
def delete_procs(*procs)
procs.each{|e|
if e.kind_of? Proc
@loop_proc.delete([e])
else
@loop_proc.delete(e)
end
}
@proc_max = @loop_proc.size
cancel if @proc_max == 0
self
end
def delete_at(n)
@loop_proc.delete_at(n)
@proc_max = @loop_proc.size
cancel if @proc_max == 0
self
end
def set_start_proc(sleep, init_proc, *init_args)
if !sleep == 'idle' && !sleep.kind_of?(Integer)
fail format("%s need to be Integer", sleep.inspect)
end
@init_sleep = sleep
@init_proc = init_proc
@init_args = init_args
self
end
def start(*init_args)
return nil if @running
Tk_CBTBL[@id] = self
@do_loop = @loop_exec
@current_pos = 0
argc = init_args.size
if argc > 0
sleep = init_args.shift
if !sleep == 'idle' && !sleep.kind_of?(Integer)
fail format("%s need to be Integer", sleep.inspect)
end
@init_sleep = sleep
end
@init_proc = init_args.shift if argc > 1
@init_args = init_args if argc > 0
@current_sleep = @init_sleep
@running = true
if @init_proc
if not @init_proc.kind_of? Proc
fail format("%s need to be Proc", @init_proc.inspect)
end
@current_proc = @init_proc
set_callback(@init_sleep, @init_args)
@set_next = false if @in_callback
else
set_next_callback(@init_args)
end
self
end
def reset(*reset_args)
restart() if @running
if @init_proc
@return_value = @init_proc.call(self)
else
@return_value = nil
end
@current_pos = 0
@current_args = @init_args
@set_next = false if @in_callback
self
end
def restart(*restart_args)
cancel if @running
if restart_args == []
start(@init_sleep, @init_proc, *@init_args)
else
start(*restart_args)
end
end
def cancel
@running = false
@wait_var.value = 0
tk_call 'after', 'cancel', @after_id if @after_id
@after_id = nil
Tk_CBTBL.delete(@id) ;# for GC
self
end
alias stop cancel
def continue(wait=nil)
fail RuntimeError, "is already running" if @running
sleep, cmd = @current_script
fail RuntimeError, "no procedure to continue" unless cmd
if wait
if not wait.kind_of? Integer
fail RuntimeError, format("%s need to be Integer", wait.inspect)
end
sleep = wait
end
Tk_CBTBL[@id] = self
@running = true
@after_id = tk_call('after', sleep, cmd)
self
end
def skip
fail RuntimeError, "is not running now" unless @running
cancel
Tk_CBTBL[@id] = self
@running = true
set_next_callback(@current_args)
self
end
def info
if @after_id
inf = tk_split_list(tk_call('after', 'info', @after_id))
[Tk_CBTBL[inf[0][1]], inf[1]]
else
nil
end
end
def wait(on_thread = true, check_root = false)
if $SAFE >= 4
fail SecurityError, "can't wait timer at $SAFE >= 4"
end
unless @running
if @return_value.kind_of?(Exception)
fail @return_value
else
return @return_value
end
end
@wait_var.wait(on_thread, check_root)
if @return_value.kind_of?(Exception)
fail @return_value
else
@return_value
end
end
def eventloop_wait(check_root = false)
wait(false, check_root)
end
def thread_wait(check_root = false)
wait(true, check_root)
end
def tkwait(on_thread = true)
wait(on_thread, true)
end
def eventloop_tkwait
wait(false, true)
end
def thread_tkwait
wait(true, true)
end
end
TkAfter = TkTimer
|