| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# wmove.rb - a wmove.dll compatible Saori module for ninix |
| 4 |
# Copyright (C) 2003-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 |
# TODO: |
| 15 |
# - STANDBY, STANDBY_INSIDE |
| 16 |
|
| 17 |
|
| 18 |
require "gtk4" |
| 19 |
|
| 20 |
require_relative "../dll" |
| 21 |
require_relative "../pix" |
| 22 |
|
| 23 |
|
| 24 |
module Wmove |
| 25 |
|
| 26 |
class Saori < DLL::SAORI |
| 27 |
|
| 28 |
def initialize |
| 29 |
super() |
| 30 |
@__sakura = nil |
| 31 |
end |
| 32 |
|
| 33 |
def need_ghost_backdoor(sakura) |
| 34 |
@__sakura = sakura |
| 35 |
end |
| 36 |
|
| 37 |
def load(dir: nil) |
| 38 |
@commands = [[], []] |
| 39 |
@timeout_id = nil |
| 40 |
@dir = dir |
| 41 |
result = 0 |
| 42 |
if @__sakura.nil? |
| 43 |
#pass |
| 44 |
elsif @loaded == 1 |
| 45 |
result = 2 |
| 46 |
else |
| 47 |
@sakura_name = @__sakura.get_selfname() |
| 48 |
@kero_name = @__sakura.get_keroname() |
| 49 |
@loaded = 1 |
| 50 |
result = 1 |
| 51 |
end |
| 52 |
return result |
| 53 |
end |
| 54 |
|
| 55 |
def finalize |
| 56 |
GLib::Source.remove(@timeout_id) unless @timeout_id.nil? |
| 57 |
@timeout_id = nil |
| 58 |
@commands = [[], []] |
| 59 |
@sakura_name = '' |
| 60 |
@kero_name = '' |
| 61 |
return 1 |
| 62 |
end |
| 63 |
|
| 64 |
def __check_argument(argument) |
| 65 |
name = argument[0] |
| 66 |
result = true |
| 67 |
list_hwnd = [@sakura_name, @kero_name] ## FIXME: HWND support |
| 68 |
case name |
| 69 |
when 'MOVE', 'MOVE_INSIDE', 'MOVETO', 'MOVETO_INSIDE' |
| 70 |
if argument.length != 4 or not list_hwnd.include?(argument[1]) |
| 71 |
result = false |
| 72 |
end |
| 73 |
when 'ZMOVE', 'WAIT' |
| 74 |
if argument.length != 3 or not list_hwnd.include?(argument[1]) |
| 75 |
result = false |
| 76 |
end |
| 77 |
when 'STANDBY', 'STANDBY_INSIDE' |
| 78 |
if argument.length != 6 or \ |
| 79 |
not list_hwnd.include?(argument[1]) or \ |
| 80 |
not list_hwnd.include?(argument[2]) |
| 81 |
result = false |
| 82 |
end |
| 83 |
when 'GET_POSITION', 'CLEAR' |
| 84 |
if argument.length != 2 or not list_hwnd.include?(argument[1]) |
| 85 |
result = false |
| 86 |
end |
| 87 |
when 'GET_DESKTOP_SIZE' |
| 88 |
if argument.length != 1 |
| 89 |
result = false |
| 90 |
end |
| 91 |
when 'NOTIFY' |
| 92 |
if argument.length < 3 or argument.length > 8 or \ |
| 93 |
not list_hwnd.include?(argument[1]) |
| 94 |
result = false |
| 95 |
end |
| 96 |
end |
| 97 |
return result |
| 98 |
end |
| 99 |
|
| 100 |
def request(req) |
| 101 |
req_type, argument = evaluate_request(req) |
| 102 |
result = |
| 103 |
case req_type |
| 104 |
when nil |
| 105 |
RESPONSE[400] |
| 106 |
when 'GET Version' |
| 107 |
RESPONSE[204] |
| 108 |
when 'EXECUTE' |
| 109 |
execute(argument) |
| 110 |
else |
| 111 |
RESPONSE[400] |
| 112 |
end |
| 113 |
return result |
| 114 |
end |
| 115 |
|
| 116 |
def execute(args) |
| 117 |
if args.nil? or args.empty? |
| 118 |
result = RESPONSE[400] |
| 119 |
elsif not __check_argument(args) |
| 120 |
result = RESPONSE[400] |
| 121 |
else |
| 122 |
name = args[0] |
| 123 |
if name == 'GET_POSITION' |
| 124 |
if args[1] == @sakura_name ## FIXME: HWND support |
| 125 |
side = 0 |
| 126 |
elsif args[1] == @kero_name ## FIXME: HWND support |
| 127 |
side = 1 |
| 128 |
else |
| 129 |
return RESPONSE[400] |
| 130 |
end |
| 131 |
begin |
| 132 |
x, y = @__sakura.get_surface_position(side) |
| 133 |
w, h = @__sakura.get_surface_size(side) |
| 134 |
result = ("SAORI/1.0 200 OK\r\n" \ |
| 135 |
+ "Result: " + x.to_s + "\r\n" \ |
| 136 |
+ "Value0: " + x.to_s + "\r\n" \ |
| 137 |
+ "Value1: " + (x + (w / 2).to_i).to_s + "\r\n" \ |
| 138 |
+ "Value2: " + (x + w).to_s + "\r\n\r\n") |
| 139 |
result = result.encode('ascii', :invalid => :replace, :undef => :replace) |
| 140 |
rescue |
| 141 |
result = RESPONSE[500] |
| 142 |
end |
| 143 |
elsif name == 'GET_DESKTOP_SIZE' |
| 144 |
begin |
| 145 |
left, top, scrn_w, scrn_h = @__sakura.get_workarea |
| 146 |
result = ("SAORI/1.0 200 OK\r\n" \ |
| 147 |
+ "Result: " + scrn_w.to_s + "\r\n" \ |
| 148 |
+ "Value0: " + scrn_w.to_s + "\r\n" \ |
| 149 |
+ "Value1: " + scrn_h.to_s + "\r\n\r\n") |
| 150 |
result = result.encode('ascii', :invalid => :replace, :undef => :replace) |
| 151 |
rescue |
| 152 |
result = RESPONSE[500] |
| 153 |
end |
| 154 |
else |
| 155 |
enqueue_commands(name, args[1..-1]) |
| 156 |
do_idle_tasks() if @timeout_id.nil? |
| 157 |
result = RESPONSE[204] |
| 158 |
end |
| 159 |
return result |
| 160 |
end |
| 161 |
end |
| 162 |
|
| 163 |
def enqueue_commands(command, args) |
| 164 |
#fail "assert" unless ['MOVE', 'MOVE_INSIDE', 'MOVETO', 'MOVETO_INSIDE', |
| 165 |
# 'ZMOVE', 'WAIT', 'NOTIFY', |
| 166 |
# 'STANDBY', 'STANDBY_INSIDE', |
| 167 |
# 'CLEAR'].include?(command) |
| 168 |
if args[0] == @sakura_name ## FIXME: HWND support |
| 169 |
side = 0 |
| 170 |
elsif args[0] == @kero_name ## FIXME: HWND support |
| 171 |
side = 1 |
| 172 |
else |
| 173 |
return # XXX |
| 174 |
end |
| 175 |
if command == 'CLEAR' |
| 176 |
@commands[side] = [] |
| 177 |
else |
| 178 |
if ['STANDBY', 'STANDBY_INSIDE'].include?(command) |
| 179 |
@commands[0] = [] |
| 180 |
@commands[1] = [] |
| 181 |
end |
| 182 |
@commands[side] << [command, args[1..-1]] |
| 183 |
end |
| 184 |
end |
| 185 |
|
| 186 |
def do_idle_tasks |
| 187 |
for side in [0, 1] |
| 188 |
if not @commands[side].empty? |
| 189 |
command, args = @commands[side].shift |
| 190 |
case command |
| 191 |
when 'MOVE', 'MOVE_INSIDE' |
| 192 |
x, y = @__sakura.get_surface_position(side) |
| 193 |
vx = args[0].to_i |
| 194 |
speed = args[1].to_i |
| 195 |
if command == 'MOVE_INSIDE' |
| 196 |
w, h = @__sakura.get_surface_size(side) |
| 197 |
left, top, scrn_w, scrn_h = @__sakura.get_workarea |
| 198 |
if vx < 0 and x + vx <0 |
| 199 |
vx = [-x, 0].min |
| 200 |
elsif vx > 0 and x + vx + w > left + scrn_w |
| 201 |
vx = [left + scrn_w - w - x, 0].max |
| 202 |
end |
| 203 |
end |
| 204 |
if vx.abs > speed |
| 205 |
if vx > 0 |
| 206 |
@__sakura.set_surface_position( |
| 207 |
side, x + speed, y) |
| 208 |
@commands[side].insert( |
| 209 |
0, [command, [(vx - speed).to_s, args[1]]]) |
| 210 |
elsif vx < 0 |
| 211 |
@__sakura.set_surface_position( |
| 212 |
side, x - speed, y) |
| 213 |
@commands[side].insert( |
| 214 |
0, [command, [(vx + speed).to_s, args[1]]]) |
| 215 |
end |
| 216 |
else |
| 217 |
@__sakura.set_surface_position(side, x + vx, y) |
| 218 |
end |
| 219 |
when 'MOVETO', 'MOVETO_INSIDE' |
| 220 |
x, y = @__sakura.get_surface_position(side) |
| 221 |
to = args[0].to_i |
| 222 |
speed = args[1].to_i |
| 223 |
if command == 'MOVETO_INSIDE' |
| 224 |
w, h = @__sakura.get_surface_size(side) |
| 225 |
left, top, scrn_w, scrn_h = @__sakura.get_workarea |
| 226 |
if to < 0 |
| 227 |
to = 0 |
| 228 |
elsif to > left + scrn_w - w |
| 229 |
to = (left + scrn_w - w) |
| 230 |
end |
| 231 |
end |
| 232 |
if (to - x).abs > speed |
| 233 |
if to - x > 0 |
| 234 |
@__sakura.set_surface_position( |
| 235 |
side, x + speed, y) |
| 236 |
@commands[side].insert(0, [command, args]) |
| 237 |
elsif to - x < 0 |
| 238 |
@__sakura.set_surface_position( |
| 239 |
side, x - speed, y) |
| 240 |
@commands[side].insert(0, [command, args]) |
| 241 |
end |
| 242 |
else |
| 243 |
@__sakura.set_surface_position(side, to, y) |
| 244 |
end |
| 245 |
when 'STANDBY', 'STANDBY_INSIDE' |
| 246 |
#pass ## FIXME: not supported yet |
| 247 |
when 'ZMOVE' |
| 248 |
if args[0] == '1' |
| 249 |
@__sakura.raise_surface(side) |
| 250 |
elsif args[0] == '2' |
| 251 |
@__sakura.lower_surface(side) |
| 252 |
else |
| 253 |
#pass |
| 254 |
end |
| 255 |
when 'WAIT' |
| 256 |
begin |
| 257 |
wait = Integer(args[0]) # ms |
| 258 |
rescue |
| 259 |
wait = 0 |
| 260 |
end |
| 261 |
if wait < 25 |
| 262 |
#pass |
| 263 |
else |
| 264 |
@commands[side].insert(0, ['WAIT', (wait - 20).to_s]) |
| 265 |
end |
| 266 |
when 'NOTIFY' |
| 267 |
@__sakura.notify_event(*args) |
| 268 |
end |
| 269 |
end |
| 270 |
if @commands[0].empty? and @commands[1].empty? |
| 271 |
GLib::Source.remove(@timeout_id) unless @timeout_id.nil? |
| 272 |
@timeout_id = nil |
| 273 |
else |
| 274 |
if @timeout_id.nil? |
| 275 |
@timeout_id = GLib::Timeout.add(20) { do_idle_tasks } |
| 276 |
end |
| 277 |
end |
| 278 |
end |
| 279 |
end |
| 280 |
end |
| 281 |
end |