lib/screen_tracker.rb in sensible-cinema-0.7.4 vs lib/screen_tracker.rb in sensible-cinema-0.7.5

- old
+ new

@@ -11,33 +11,21 @@ height = settings["height"] digits = settings["digits"] return new(settings["name"], settings["x"], settings["y"], settings["width"], settings["height"], digits, callback) end - # digits like {:hours => [100,5], :minute_tens, :minute_ones, :second_tens, :second_ones} - # digits share the height... + attr_accessor :hwnd + + # digits are like {:hours => [100,5], :minute_tens, :minute_ones, :second_tens, :second_ones} + # digits share the height start point, have their own x and width... def initialize name_or_regex,x,y,width,height,digits=nil,callback=nil # cache to save us 0.00445136 per time LOL - if name_or_regex.to_s.downcase == 'desktop' - # full screen option - @hwnd = hwnd = Win32::Screenshot::BitmapMaker.desktop_window - else - @hwnd = Win32::Screenshot::BitmapMaker.hwnd(name_or_regex) - end - unless @hwnd - print 'perhaps not running yet? [%s] START IT FAST' % name_or_regex - until @hwnd - sleep 4 - print ' trying again .' - STDOUT.flush - @hwnd = Win32::Screenshot::BitmapMaker.hwnd(name_or_regex) - end - puts 'found window' - end + @name_or_regex = name_or_regex + get_hwnd pps 'height', height, 'width', width if $VERBOSE raise 'poor dimentia' if width <= 0 || height <= 0 - always_zero, always_zero, max_x, max_y = Win32::Screenshot::BitmapMaker.dimensions_for(@hwnd) + max_x, max_y = Win32::Screenshot::Util.dimensions_for(@hwnd) if(x < 0 || y < 0) if x < 0 x = max_x + x end if y < 0 @@ -51,10 +39,30 @@ raise 'poor height or wrong window' if @y2 > max_y || @y2 == y @digits = digits pps 'using x',@x, 'from x', x, 'y', @y, 'from y', y,'x2',@x2,'y2',@y2,'digits', @digits if $VERBOSE end + def get_hwnd + if @name_or_regex.to_s.downcase == 'desktop' + # full screen option + @hwnd = hwnd = Win32::Screenshot::BitmapMaker.desktop_window + else + @hwnd = Win32::Screenshot::BitmapMaker.hwnd(@name_or_regex) + end + unless @hwnd + print 'perhaps not running yet? [%s] START IT FAST' % @name_or_regex + until @hwnd + sleep 4 + print ' trying again .' + STDOUT.flush + @hwnd = Win32::Screenshot::BitmapMaker.hwnd(@name_or_regex) + end + puts 'found window' + end + + end + def get_bmp # Note: we no longer bring the window to the front tho...which it needs to be in both XP and Vista to work...sigh. Win32::Screenshot::BitmapMaker.capture_area(@hwnd,@x,@y,@x2,@y2) {|h,w,bmp| return bmp} end @@ -102,22 +110,28 @@ def identify_digit bitmap OCR.identify_digit(bitmap, @digits) end def wait_till_next_change + # dies in jruby currently...sigh...get_hwnd # reget it, just in case... original = get_bmp + time_since_last = Time.now loop { current = get_bmp if current != original if @digits return attempt_to_get_time_from_screen else - puts 'screen time change only detected...' if $VERBOSE + puts 'screen time change only detected...' end return end sleep 0.02 + if(Time.now - time_since_last > 5) + p 'warning--unable to track for some reason--may need to restart sensible-cinema' + time_since_last = Time.now + end } end def attempt_to_get_time_from_screen out = {} @@ -130,15 +144,16 @@ unless digit if $DEBUG || $VERBOSE @a ||= 1 @a += 1 p 'unable to identify digit!' + type.to_s + @a.to_s - File.binwrite("bad_digit#{@a}#{type}.bmp", digits[type]) + File.binwrite("bad_digit#{@a}#{type}.bmp", digits[type]) unless type == :hours end if type == :hours digit = 0 # this one can fail in VLC else # early return + p 'identity failure' if $VERBOSE return end end out[type] = digit else \ No newline at end of file