Class: Terminitor::WindowsConsole

Inherits:
Object
  • Object
show all
Includes:
Input, Windows::Handle, Windows::Process, Windows::Window
Defined in:
lib/terminitor/cores/cmd_core/windows_console.rb

Constant Summary

Constants included from Input

Input::KEYBD_EVENT_KEYDOWN, Input::KEYBD_EVENT_KEYUP, Input::KEYBD_KEYDELAY, Input::VK_0, Input::VK_1, Input::VK_2, Input::VK_3, Input::VK_4, Input::VK_5, Input::VK_6, Input::VK_7, Input::VK_8, Input::VK_9, Input::VK_A, Input::VK_ADD, Input::VK_APPS, Input::VK_B, Input::VK_BACK, Input::VK_C, Input::VK_CANCEL, Input::VK_CLEAR, Input::VK_CONTROL, Input::VK_D, Input::VK_DECIMAL, Input::VK_DELETE, Input::VK_DIVIDE, Input::VK_DOWN, Input::VK_E, Input::VK_END, Input::VK_ESCAPE, Input::VK_EXECUTE, Input::VK_F, Input::VK_F1, Input::VK_F10, Input::VK_F11, Input::VK_F12, Input::VK_F2, Input::VK_F3, Input::VK_F4, Input::VK_F5, Input::VK_F6, Input::VK_F7, Input::VK_F8, Input::VK_F9, Input::VK_G, Input::VK_H, Input::VK_HELP, Input::VK_HOME, Input::VK_I, Input::VK_INSERT, Input::VK_J, Input::VK_K, Input::VK_L, Input::VK_LBUTTON, Input::VK_LCONTROL, Input::VK_LEFT, Input::VK_LMENU, Input::VK_LSHIFT, Input::VK_LWIN, Input::VK_M, Input::VK_MENU, Input::VK_MULTIPLY, Input::VK_N, Input::VK_NEXT, Input::VK_NUMLOCK, Input::VK_NUMPAD0, Input::VK_NUMPAD1, Input::VK_NUMPAD2, Input::VK_NUMPAD3, Input::VK_NUMPAD4, Input::VK_NUMPAD5, Input::VK_NUMPAD6, Input::VK_NUMPAD7, Input::VK_NUMPAD8, Input::VK_NUMPAD9, Input::VK_O, Input::VK_OEM_1, Input::VK_OEM_2, Input::VK_OEM_3, Input::VK_OEM_4, Input::VK_OEM_5, Input::VK_OEM_6, Input::VK_OEM_7, Input::VK_OEM_8, Input::VK_OEM_COMMA, Input::VK_OEM_EQU, Input::VK_OEM_MINUS, Input::VK_OEM_PERIOD, Input::VK_OEM_PLUS, Input::VK_P, Input::VK_PAUSE, Input::VK_PRIOR, Input::VK_Q, Input::VK_R, Input::VK_RBUTTON, Input::VK_RCONTROL, Input::VK_RETURN, Input::VK_RIGHT, Input::VK_RMENU, Input::VK_RSHIFT, Input::VK_RWIN, Input::VK_S, Input::VK_SCROLL, Input::VK_SELECT, Input::VK_SEPARATOR, Input::VK_SHIFT, Input::VK_SNAPSHOT, Input::VK_SPACE, Input::VK_SUBTRACT, Input::VK_T, Input::VK_TAB, Input::VK_U, Input::VK_UP, Input::VK_V, Input::VK_W, Input::VK_X, Input::VK_Y, Input::VK_Z

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Input

#keystroke, #type_in

Constructor Details

#initialize(options = {}) ⇒ WindowsConsole

Returns a new instance of WindowsConsole.



28
29
30
31
32
33
34
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 28

def initialize(options = {})
  @name = options[:name] || name
  @title = options[:title] || name
  @parameters = options[:parameters]

  start
end

Instance Attribute Details

#hwndObject (readonly)

Returns the value of attribute hwnd.



26
27
28
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 26

def hwnd
  @hwnd
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 21

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



22
23
24
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 22

def parameters
  @parameters
end

#pidObject (readonly)

Returns the value of attribute pid.



24
25
26
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 24

def pid
  @pid
end

#thread_idObject (readonly)

Returns the value of attribute thread_id.



25
26
27
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 25

def thread_id
  @thread_id
end

#titleObject

Returns the value of attribute title.



23
24
25
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 23

def title
  @title
end

Instance Method Details

#find_window(process_id) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 61

def find_window(process_id)
  sleep 0.4 #todo - find a better way to wait for console to show up!!
  child_after = 0
  while (child_after = FindWindowEx(nil, child_after, nil, nil)) > 0 do 
    process_id = 0.chr * 4
    GetWindowThreadProcessId(child_after, process_id)
    process_id = process_id.unpack('L').first
    return child_after if process_id == @pid 
  end
  

  return nil
end

#kill!Object



54
55
56
57
58
59
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 54

def kill!
  CloseHandle(@process_handle)
  CloseHandle(@thread_handle)

  Process::kill(9, pid)
end

#send_command(cmd) ⇒ Object



75
76
77
78
79
80
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 75

def send_command(cmd)
  @hwnd ||= find_window(@thread_id)
  SetForegroundWindow(@hwnd) 
  # puts "[#{@hwnd}] #{cmd}"
  type_in(cmd)
end

#startObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/terminitor/cores/cmd_core/windows_console.rb', line 36

def start
  command_line = name
  command_line = name + ' ' + parameters if parameters

  # returns a struct, raises an error if fails
  process_info = Process.create(
     :command_line => command_line,
     :close_handles => false,
     :creation_flags => Process::CREATE_NEW_CONSOLE
  )
  @pid = process_info.process_id
  @thread_id = process_info.thread_id
  @process_handle = process_info.process_handle
  @thread_handle = process_info.thread_handle

  @pid
end