lib/screen_tracker.rb in sensible-cinema-0.25.0 vs lib/screen_tracker.rb in sensible-cinema-0.25.1
- old
+ new
@@ -17,40 +17,46 @@
=end
require 'win32/screenshot'
require 'sane'
require 'yaml'
require File.dirname(__FILE__)+ '/ocr'
+require 'ffi'
class ScreenTracker
- def self.new_from_yaml yaml, callback
+ extend FFI::Library
+ ffi_lib 'user32'
+ # second parameter, pointer, LPRECT is FFI::MemoryPointer.new(:long, 4)
+ # read it like rect.read_array_of_long(4)
+ attach_function :GetWindowRect, [:long, :pointer], :int # returns a BOOL
+
+ def self.new_from_yaml yaml, callback # callback can be nil, is used for timestamp changed stuff
settings = YAML.load yaml
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,use_class_name=nil,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
+ get_hwnd_loop_forever
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
y = max_y + y
end
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 #{@x2} #{max_x} #{x}" if @x2 > max_x || @x2 == x
if @y2 > max_y || @y2 == y || @y2 <= 0
raise "poor height or wrong window selected #{@y2} > #{max_y} || #{@y2} == #{y} || #{@y2} <= 0"
@@ -63,11 +69,11 @@
@previously_displayed_warning = false
@dump_digit_count = 1
pps 'using x',@x, 'from x', x, 'y', @y, 'from y', y,'x2',@x2,'y2',@y2,'digits', @digits.inspect if $VERBOSE
end
- def get_hwnd
+ def get_hwnd_loop_forever
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
@@ -87,11 +93,11 @@
p width, height
@hwnd = hwnd
end
puts 're-established contact with window'
end
-
+ true
end
# gets the snapshot of "all the digits together"
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.
@@ -102,11 +108,11 @@
def get_full_bmp
Win32::Screenshot::BitmapMaker.capture_all(@hwnd) {|h,w,bmp| return bmp}
end
# writes out all screen tracking info to various files in the current pwd
- def dump_bmp filename = 'dump.bmp'
+ def dump_bmps filename = 'dump.bmp'
File.binwrite filename, get_bmp
File.binwrite 'all.' + filename, get_full_bmp
dump_digits(get_digits_as_bitmaps, 'dump_bmp') if @digits
end
@@ -142,14 +148,20 @@
end
end
out
end
- def get_relative_coords
+ def get_relative_coords_of_timestamp_window
[@x,@y,@x2,@y2]
end
+ def get_coords_of_window_on_display # yea
+ out = FFI::MemoryPointer.new(:long, 4)
+ ScreenTracker.GetWindowRect @hwnd, out
+ out.read_array_of_long(4)
+ end
+
def identify_digit bitmap
OCR.identify_digit(bitmap, @digits)
end
# we have to wait until the next change, because when we start, it might be half-way through
@@ -182,10 +194,10 @@
p 'warning--unable to track screen time for some reason [perhaps screen obscured or it\'s not playing yet, or paused?] ' + @hwnd.to_s
p Win32::Screenshot::Util.dimensions_for(hwnd)
@previously_displayed_warning = true
time_since_last_screen_change = Time.now
# also reget window hwnd, just in case that's the problem...(can be with VLC moving from title to title)
- get_hwnd
+ get_hwnd_loop_forever
end
end
}
end
\ No newline at end of file