Debian Codegraph @

lib/ninix/dll/osuwari.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # osuwari.rb - a Osuwari compatible Saori module for ninix
4 # Copyright (C) 2006-2019 by Shyouzou Sugitani <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License (version 2) as
8 # published by the Free Software Foundation. It is distributed in the
9 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the GNU General Public License for more details.
12 #
13 #
14
15 # TODO: MOVE, NOCLIP, FIX, etc.
16
17 require "gtk4"
18
19 require_relative "../dll"
20 require_relative "../pix"
21
22
23 module Osuwari
24
25 class Saori < DLL::SAORI
26
27 def initialize
28 super()
29 @timeout_id = nil
30 @settings = {}
31 @__sakura = nil
32 end
33
34 def need_ghost_backdoor(sakura)
35 @__sakura = sakura
36 end
37
38 def check_import
39 if not @__sakura.nil?
40 return 1
41 else
42 return 0
43 end
44 end
45
46 def setup
47 return 1
48 end
49
50 def execute(argument)
51 return RESPONSE[400] if argument.nil? or argument.empty?
52 if argument[0] == 'START'
53 return RESPONSE[400] if argument.length < 7
54 begin ## FIXME
55 fail "assert" unless ['ACTIVE', 'FIX'].include?(argument[2]) or
56 argument[2].start_with?('@') or
57 argument[2].start_with?('#')
58 fail "assert" unless ['TL', 'TR', 'BL', 'BR'].include?(argument[3])
59 @settings['hwnd'] = argument[1]
60 @settings['target'] = argument[2]
61 @settings['position'] = argument[3]
62 @settings['offset_x'] = Integer(argument[4])
63 @settings['offset_y'] = Integer(argument[5])
64 @settings['timeout'] = Integer(argument[6])
65 @settings['xmove'] = 0
66 @settings['ymove'] = 0
67 @settings['noclip'] = 0
68 if argument.length > 7
69 if argument[7].include?('XMOVE')
70 @settings['xmove'] = 1
71 end
72 if argument[7].include?('YMOVE')
73 @settings['ymove'] = 1
74 end
75 if argument[7].include?('NOCLIP')
76 @settings['noclip'] = 1
77 end
78 end
79 @settings['except'] = ['DESKTOP', 'CENTER']
80 if argument.length > 8
81 #target, position = argument[8].split() # spec
82 position, target = argument[8].split(nil, 2) # real world
83 fail "assert" unless ['DESKTOP', 'WORKAREA'].include?(target)
84 fail "assert" unless ['TOP', 'LEFT', 'RIGHT', 'BOTTOM'].include?(position)
85 @settings['except'] = [target, position]
86 end
87 rescue
88 return RESPONSE[400]
89 end
90 #@timeout_id = GLib::Timeout.add(@settings['timeout']) { do_idle_tasks }
91 @timeout_id = GLib::Timeout.add(100) { do_idle_tasks }
92 return RESPONSE[204]
93 elsif argument[0] == 'STOP'
94 GLib::Source.remove(@timeout_id) unless @timeout_id.nil?
95 @timeout_id = nil
96 @settings = {}
97 return RESPONSE[204]
98 else
99 return RESPONSE[400]
100 end
101 end
102
103 def do_idle_tasks
104 return false if @timeout_id.nil?
105 target = @settings['target']
106 left, top, scrn_w, scrn_h = @__sakura.get_workarea
107 target_flag = [false, false]
108 if target == 'ACTIVE'
109 active_window = get_active_window()
110 unless active_window.nil?
111 if @__sakura.identify_window(active_window)
112 target_flag[1] = true
113 else
114 rect = active_window.frame_extents
115 target_x = rect.x
116 target_y = rect.y
117 target_w = rect.width
118 target_h = rect.height
119 target_flag[0] = true
120 end
121 else
122 target_flag[1] = true
123 end
124 elsif target == 'FIX' ## FIXME
125 target_x = left
126 target_y = top
127 target_w = scrn_w
128 target_h = scrn_h
129 target_flag[0] = true
130 elsif target.start_with?('@') ## FIXME
131 #win = get_window_by_name(target[1..-1])
132 #if @__sakura.identify_window(win)
133 # return true
134 #end
135 #target_x, target_y = active_window.root_origin
136 #rect = active_window.frame_extents
137 #target_w = rect.width
138 #target_h = rect.height
139 #pass
140 elsif target.start_with?('#') ## FIXME
141 #pass
142 else
143 #pass # should not reach here
144 end
145 return target_flag[1] unless target_flag[0]
146 pos = @settings['position']
147 scale = @__sakura.get_surface_scale()
148 offset_x = (@settings['offset_x'] * scale / 100).to_i
149 offset_y = (@settings['offset_y'] * scale / 100).to_i
150 if @settings['hwnd'].start_with?('s')
151 begin
152 side = Integer(@settings['hwnd'][1..-1])
153 rescue
154 return false
155 end
156 else
157 begin
158 side = Integer(@settings['hwnd'])
159 rescue
160 return false
161 end
162 end
163 w, h = @__sakura.get_surface_size(side)
164 case pos[0]
165 when 'T'
166 y = (target_y + offset_y)
167 when 'B'
168 y = (target_y + target_h + offset_y - h)
169 else
170 return false # should not reach here
171 end
172 case pos[1]
173 when 'L'
174 x = (target_x + offset_x)
175 when 'R'
176 x = (target_x + target_w + offset_x - w)
177 else
178 return false # should not reach here
179 end
180 if not @settings['noclip']
181 if x < left or x > left+ scrn_w or \
182 y < top or y > top + scrn_h
183 case @settings['except'][1]
184 when 'BOTTOM'
185 #pass ## FIXME: not supported yet
186 when 'TOP'
187 #pass ## FIXME: not supported yet
188 when 'LEFT'
189 #pass ## FIXME: not supported yet
190 when 'RIGTH'
191 #pass ## FIXME: not supported yet
192 when 'CENTER'
193 #pass ## FIXME: not supported yet
194 else
195 #pass # should not reach here
196 end
197 end
198 end
199 @__sakura.set_surface_position(side, x, y)
200 @__sakura.raise_surface(side)
201 return true
202 end
203
204 def get_window_by_name(name) ## FIXME: not supported yet
205 return nil
206 end
207
208 def get_active_window
209 scrn = Gdk::Screen.default
210 active_window = scrn.active_window
211 return active_window
212 end
213 end
214 end