lib/screen_tracker.rb in sensible-cinema-0.9.3 vs lib/screen_tracker.rb in sensible-cinema-0.9.4
- old
+ new
@@ -5,23 +5,22 @@
class ScreenTracker
def self.new_from_yaml yaml, callback
settings = YAML.load yaml
- # heigth is shared...
- height = settings["height"]
- digits = settings["digits"]
- return new(settings["name"], settings["x"], settings["y"], settings["width"], settings["height"], digits, callback)
+ return new(settings["name"], settings["x"], settings["y"], settings["width"],
+ settings["height"], settings["use_class_name"], settings["digits"], callback)
end
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
+ def initialize name_or_regex,x,y,width,height,use_class_name=nil,digits=nil,callback=nil
# cache to save us 0.00445136 per time LOL
@name_or_regex = name_or_regex
+ @use_class_name = use_class_name
get_hwnd
pps 'height', height, 'width', width if $VERBOSE
raise 'poor dimentia' if width <= 0 || height <= 0
max_x, max_y = Win32::Screenshot::Util.dimensions_for(@hwnd)
if(x < 0 || y < 0)
@@ -34,32 +33,37 @@
end
@height = height
@x = x; @y = y; @x2 = x+width; @y2 = y+height; @callback = callback
@max_x = max_x
raise 'poor width or wrong window' if @x2 > max_x || @x2 == x
- raise 'poor height or wrong window' if @y2 > max_y || @y2 == y
+ if @y2 > max_y || @y2 == y
+ raise 'poor height or wrong window'
+ end
@digits = digits
@displayed_warning = false
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
+ assert !@use_class_name # not an option
@hwnd = hwnd = Win32::Screenshot::BitmapMaker.desktop_window
+ return
else
- @hwnd = Win32::Screenshot::BitmapMaker.hwnd(@name_or_regex)
+ @hwnd = Win32::Screenshot::BitmapMaker.hwnd(@name_or_regex, @use_class_name)
end
+ # allow ourselves the 'found it message' selectively
unless @hwnd
until @hwnd
- print 'perhaps not running yet? [%s]' % @name_or_regex
+ print 'perhaps not running yet? [%s]' % @name_or_regex.inspect
sleep 1
STDOUT.flush
- @hwnd = Win32::Screenshot::BitmapMaker.hwnd(@name_or_regex)
+ @hwnd = Win32::Screenshot::BitmapMaker.hwnd(@name_or_regex, @use_class_name)
end
- puts 'found window'
+ puts 're-found window'
end
end
# gets the snapshot of "all the digits together"
@@ -75,22 +79,20 @@
# writes out all screen tracking info to various files in the current pwd
def dump_bmp filename = 'dump.bmp'
File.binwrite filename, get_bmp
File.binwrite 'all.' + filename, get_full_bmp
- dump_digits
+ dump_digits get_digits_as_bitmaps if @digits
end
- def dump_digits
- if @digits
+ def dump_digits digits
@digit_count ||= 1
- @digit_count += 1
for type, bitmap in get_digits_as_bitmaps
File.binwrite type.to_s + '.' + @digit_count.to_s + '.bmp', bitmap
end
- print 'wrote digits:', @digit_count, "\n"
- end
+ print 'debug dumped digits that Im about to parse:', @digit_count, "\n"
+ @digit_count += 1
end
DIGIT_TYPES = [:hours, :minute_tens, :minute_ones, :second_tens, :second_ones]
# returns like {:hours => nil, :minutes_tens => raw_bmp, ...
def get_digits_as_bitmaps
@@ -148,33 +150,42 @@
}
end
def attempt_to_get_time_from_screen
out = {}
- dump_digits if $DEBUG
- digits = get_digits_as_bitmaps # 0.08s [!] not too accurate...ltodo
+ # force it to have two matching in a row, to avoid race conditions grabbing the digits...
+ previous = nil # 0.08s [!] not too accurate...ltodo
start = Time.now
+ until previous == (temp = get_digits_as_bitmaps)
+ previous = temp
+ sleep 0.05 # allow youtube to update (sigh)
+ # lodo it should probably poll *before* this, not here...maybe?
+ end
+ digits = previous
+
+ dump_digits(digits) if $DEBUG
DIGIT_TYPES.each{|type|
if digits[type]
digit = identify_digit(digits[type])
unless digit
if $DEBUG || $VERBOSE
@a ||= 1
@a += 1
@already_wrote ||= {}
unless @already_wrote[digits[type]]
- p 'unable to identify capture!' + type.to_s + @a.to_s
+ p 'unable to identify capture!' + type.to_s + @a.to_s + ' capture no:' + @digit_count.to_s
File.binwrite("bad_digit#{@a}#{type}.bmp", digits[type]) unless type == :hours
@already_wrote[digits[type]] = true
end
end
if type == :hours
- digit = 0 # this one can fail in VLC
+ digit = 0 # this one can fail and that's ok in VLC bottom right
else
- # early return
- p 'identity failure ' + type.to_s if $VERBOSE
+ # early failure return
return
end
+ else
+ p " got digit #{type} as #{digit} which was captured as #{@digit_count} " if $DEBUG
end
out[type] = digit
else
# there isn't one specified as being on screen, so assume it is always zero (like youtube hour)...
out[type] = 0
\ No newline at end of file