Class: TkRTTimer

Inherits:
TkTimer show all
Defined in:
ext/lib/tk/timer.rb

Constant Summary collapse

DEFAULT_OFFSET_LIST_SIZE =
5

Constants inherited from TkTimer

TkTimer::DEFAULT_IGNORE_EXCEPTIONS, TkTimer::TkCommandNames, TkTimer::Tk_CBTBL

Constants included from TkCore

TkCore::EventFlag, TkCore::INTERP, TkCore::INTERP_MUTEX, TkCore::INTERP_ROOT_CHECK, TkCore::INTERP_THREAD, TkCore::INTERP_THREAD_STATUS, TkCore::RUN_EVENTLOOP_ON_MAIN_THREAD, TkCore::WIDGET_DESTROY_HOOK, TkCore::WITH_ENCODING, TkCore::WITH_RUBY_VM

Constants included from TkComm

TkComm::GET_CONFIGINFO_AS_ARRAY, TkComm::GET_CONFIGINFOwoRES_AS_ARRAY, TkComm::TkExtlibAutoloadModule, TkComm::Tk_CMDTBL, TkComm::Tk_IDs, TkComm::Tk_WINDOWS, TkComm::USE_TCLs_LIST_FUNCTIONS, TkComm::WidgetClassNames

Constants included from TkUtil

TkUtil::None, TkUtil::RELEASE_DATE

Instance Attribute Summary

Attributes inherited from TkTimer

#after_id, #after_script, #current_args, #current_proc, #current_sleep, #loop_exec, #return_value

Instance Method Summary collapse

Methods inherited from TkTimer

#add_procs, #at_end, callback, #cancel_on_exception=, #cancel_on_exception?, #current_status, #delete_at, #delete_procs, #do_callback, #eventloop_tkwait, #eventloop_wait, #get_procs, info, #info, #loop_rest, #loop_rest=, #reset, #restart, #running?, #set_callback, #set_procs, #set_start_proc, #skip, start, #thread_tkwait, #thread_wait, #tkwait, #wait

Methods included from TkCore

#_tk_call_to_list_core, #after, #after_cancel, #after_idle, #appname, #appsend, #appsend_deny, #appsend_displayof, callback, #callback_break, #callback_continue, #callback_return, #chooseColor, #chooseDirectory, #do_one_event, #event_generate, #getMultipleOpenFile, #getMultipleSaveFile, #getOpenFile, #getSaveFile, #get_eventloop_tick, #get_eventloop_weight, #get_no_event_wait, #inactive, #inactive_displayof, #info, #ip_eval, #ip_eval_with_enc, #ip_eval_without_enc, #ip_invoke, #ip_invoke_with_enc, #ip_invoke_without_enc, #is_mainloop?, #load_cmd_on_ip, #mainloop, #mainloop_exist?, #mainloop_thread?, #mainloop_watchdog, #messageBox, #rb_appsend, #rb_appsend_displayof, #reset_inactive, #reset_inactive_displayof, #restart, #scaling, #scaling_displayof, #set_eventloop_tick, #set_eventloop_weight, #set_no_event_wait, #tk_call, #tk_call_to_list, #tk_call_to_list_with_enc, #tk_call_to_list_without_enc, #tk_call_to_simplelist, #tk_call_to_simplelist_with_enc, #tk_call_to_simplelist_without_enc, #tk_call_with_enc, #tk_call_without_enc, #windowingsystem

Methods included from TkComm

_at, _callback_entry?, _callback_entry_class?, _curr_cmd_id, _fromUTF8, _genobj_for_tkwidget, _next_cmd_id, _toUTF8, array2tk_list, #bind, #bind_all, #bind_append, #bind_append_all, #bind_remove, #bind_remove_all, #bindinfo, #bindinfo_all, bool, image_obj, install_cmd, #install_cmd, list, num_or_str, number, procedure, simplelist, string, #subst, tk_tcl2ruby, uninstall_cmd, #uninstall_cmd, window

Methods included from TkUtil

_conv_args, #_conv_args, #_fromUTF8, _get_eval_enc_str, #_get_eval_enc_str, #_get_eval_string, _get_eval_string, _symbolkey2str, #_symbolkey2str, #_toUTF8, bool, #bool, callback, eval_cmd, #hash_kv, hash_kv, install_cmd, num_or_str, #num_or_str, number, #number, string, #string, uninstall_cmd

Methods included from TkEvent

#install_bind, #install_bind_for_event_class

Constructor Details

#initialize(*args, &b) ⇒ TkRTTimer

Returns a new instance of TkRTTimer.



543
544
545
546
547
548
549
550
# File 'ext/lib/tk/timer.rb', line 543

def initialize(*args, &b)
  super(*args, &b)

  @offset_list = Array.new(DEFAULT_OFFSET_LIST_SIZE){ [0, 0] }
  @offset_s = 0
  @offset_u = 0
  @est_time = nil
end

Instance Method Details

#cancelObject Also known as: stop



559
560
561
562
563
564
# File 'ext/lib/tk/timer.rb', line 559

def cancel
  super()
  @est_time = nil
  @cb_start_time = Time.now
  self
end

#cb_callObject



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'ext/lib/tk/timer.rb', line 648

def cb_call
  if @est_time
    @offset_list.shift

    @cb_start_time = Time.now

    if @current_sleep == 0
      @offset_list.push([
                          @offset_s - @cb_start_time.to_i, 
                          @offset_u - @cb_start_time.usec
                        ])
    else
      @offset_list.push([
                          @offset_s + (@est_time.to_i - @cb_start_time.to_i),
                          @offset_u + (@est_time.usec - @cb_start_time.usec)
                        ])
    end
  end

  @cb_cmd.call
end

#continue(wait = nil) ⇒ Object



567
568
569
570
571
# File 'ext/lib/tk/timer.rb', line 567

def continue(wait=nil)
  fail RuntimeError, "is already running" if @running
  @cb_start_time = Time.now
  super(wait)
end

#set_interval(interval) ⇒ Object



573
574
575
576
# File 'ext/lib/tk/timer.rb', line 573

def set_interval(interval)
  super(interval)
  @est_time = nil
end

#set_next_callback(args) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'ext/lib/tk/timer.rb', line 594

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
    __at_end__
    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
      __at_end__
      return
    end
  end

  @current_args = args

  cmd, *cmd_args = @loop_proc[@current_pos]
  @current_pos += 1
  @current_proc = cmd

  @offset_s, @offset_u = _offset_ave

  if TkComm._callback_entry?(@sleep_time)
    sleep = @sleep_time.call(self)
  else
    sleep = @sleep_time
  end

  if @est_time
    @est_time = Time.at(@est_time.to_i, @est_time.usec + sleep*1000)
  else
    @est_time = Time.at(@cb_start_time.to_i, 
                        @cb_start_time.usec + sleep*1000)
  end

  now = Time.now
  real_sleep = ((@est_time.to_i - now.to_i + @offset_s)*1000.0 + 
                (@est_time.usec - now.usec + @offset_u)/1000.0).round
  if real_sleep <= 0
    real_sleep = 0
    @offset_s = now.to_i
    @offset_u = now.usec
  end
  @current_sleep = real_sleep

  set_callback(real_sleep, cmd_args)
end

#start(*args, &b) ⇒ Object



552
553
554
555
556
557
# File 'ext/lib/tk/timer.rb', line 552

def start(*args, &b)
  return nil if @running
  @est_time = nil
  @cb_start_time = Time.now
  super(*args, &b)
end