Class: XDo::FFILib::XDoSearch

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/x_do/ffi_lib.rb,
lib/x_do/ffi_lib_ext.rb

Overview

:nodoc: extending with better constructor

Class Method Summary collapse

Class Method Details

.from_options(options = {}) ⇒ Object

Creates an XDoSearch structure from options passed to XDo#find_window.



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
# File 'lib/x_do/ffi_lib_ext.rb', line 12

def self.from_options(options = {})
  search = self.new
  search[:max_depth] = (options[:depth] || -1).to_i
  
  # SEARCH_ANY vs SEARCH_ALL in xdo.h
  search[:require] = (options[:require] == :any) ? 0 : 1

  search[:searchmask] = 0
  [
    [:title, :title, XDo::FFILib::Consts::SEARCH_TITLE],
    [:name, :winname, XDo::FFILib::Consts::SEARCH_NAME],
    [:class, :winclass, XDo::FFILib::Consts::SEARCH_CLASS],
    [:class_name, :winclassname, XDo::FFILib::Consts::SEARCH_CLASSNAME]
  ].each do |option, struct_key, bit|
    if options[option]
      search[:searchmask] |= bit

      c_string = FFI::MemoryPointer.new options[option].length + 1
      c_string.put_string 0, options[option]
      search[struct_key] = c_string
    else
      search[struct_key] = nil
    end
  end
  
  if options[:visible]
    search[:searchmask] |= XDo::FFILib::Consts::SEARCH_ONLYVISIBLE
    search[:only_visible] = 1
  else
    search[:only_visible] = 0
  end
  
  [
    [:pid, :pid, XDo::FFILib::Consts::SEARCH_PID],
    [:screen, :screen, XDo::FFILib::Consts::SEARCH_SCREEN],
    [:desktop, :desktop, XDo::FFILib::Consts::SEARCH_DESKTOP]

  ].each do |option, struct_key, bit|
    if options[option]
      search[:searchmask] |= bit
      search[struct_key] = options[option].to_i
    else
      search[struct_key] = 0
    end
  end
  
  search[:limit] = options[:limit].to_i || 0
  
  search
end