202 lines
5.3 KiB
PHP
202 lines
5.3 KiB
PHP
<?php
|
|
/**
|
|
* InputDeviceServices.API.php
|
|
*
|
|
* @package default
|
|
*/
|
|
|
|
|
|
$inputserver_sock = null;
|
|
$inputserver_ref = null;
|
|
$inputclients = null;
|
|
$inputserver_sock = null;
|
|
$inputserver_conn = null;
|
|
$stream = null;
|
|
class InputDeviceServices {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
*
|
|
* @return unknown
|
|
*/
|
|
function loadInst() {
|
|
static $inst = null;
|
|
|
|
if ($inst === null) {
|
|
$inst = new self;
|
|
}
|
|
return $inst;
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
* @return unknown
|
|
*/
|
|
function module_loaded() {
|
|
self::API_Register();
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
*/
|
|
function API_Register() {
|
|
apiql::register("!OPEN/!INPUT/!SERVER[json]", "InputDeviceServices::InputServer");
|
|
apiql::register("!CLOSE/!INPUT/!SERVER", "InputDeviceServices::CloseInputServer");
|
|
apiql::register("!CONNECT/!INPUT/!DEVICES[json]", "InputDeviceServices::Connect");
|
|
apiql::register("!DISCONNECT/!INPUT/!DEVICES", "InputDeviceServices::Disconnect");
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
* @param unknown $sql
|
|
* @return unknown
|
|
*/
|
|
function CloseInputServer($sql) {
|
|
global $inputserver_ref;
|
|
global $inputserver_sock;
|
|
$inputserver_sock->shutdown();
|
|
$inputserver_ref->terminate();
|
|
return true;
|
|
}
|
|
|
|
|
|
function InputServer($sql) {
|
|
//echo(var_dump($sql));
|
|
global $loop;
|
|
global $clients;
|
|
global $inputserver_ref;
|
|
global $inputserver_sock;
|
|
global $inputclients;
|
|
$keyboard = $sql['SERVER']['keyboard'];
|
|
$mouse = $sql['SERVER']['mouse'];
|
|
$inputserver_ref = new React\ChildProcess\Process('exec /opt/uinput-mapper/input-read '.$keyboard.' -G '.$mouse.' -D -C');
|
|
$inputserver_ref->on('exit', function($exitCode, $termSignal) use ($clients) {
|
|
$ResponseArray = array("response_type"=>"success",
|
|
"response_msg"=>"Event server has stopped.",
|
|
"event_server_code"=>"EVT_201",
|
|
"event_server_status"=>"stopped");
|
|
foreach ($clients as $client) {
|
|
$client->write(json_encode($ResponseArray)."\r\n");
|
|
}
|
|
return json_encode($ResponseArray);
|
|
});
|
|
|
|
$inputserver_sock = new React\Socket\Server($loop);
|
|
$inputclients = new SplObjectStorage();
|
|
$i = 0;
|
|
$inputserver_sock->on('connection', function($connection) use ($inputclients, &$i) {
|
|
global $inputserver_ref;
|
|
global $loop;
|
|
|
|
LogEcho("Connection to Event Server.", "INPUTS");
|
|
$connection->id = ++$i;
|
|
$connection->on('end',function() use ($inputserver_ref) {
|
|
$inputserver_ref->terminate();
|
|
});
|
|
$inputclients->attach($connection);
|
|
|
|
if($inputserver_ref->isRunning() == false)
|
|
{$inputserver_ref->start($loop);}
|
|
|
|
$inputserver_ref->stdout->on('data', function($output) use ($inputclients, $connection, &$i) {
|
|
global $inputclients;
|
|
foreach ($inputclients as $client) {
|
|
if ($client->id == $connection->id) {
|
|
$client->write($output);
|
|
} else { continue; }
|
|
}
|
|
});
|
|
$inputserver_ref->stderr->on('data', function($output) {
|
|
if($output != "") LogEcho($output,"INPUTS");
|
|
});
|
|
});
|
|
$inputserver_sock->listen("5055", "0.0.0.0");
|
|
LogEcho("Event server started on port 5055", "INPUTS");
|
|
|
|
//$loop->addTimer(0.001, function($timer) use ($inputserver_ref) {
|
|
|
|
//});
|
|
|
|
$ResponseArray = array("response_type"=>"success",
|
|
"response_msg"=>"Event server started on port 5055",
|
|
"event_server_code"=>"EVT_200",
|
|
"event_server_port"=>5055,
|
|
"event_server_status"=>"started");
|
|
return json_encode($ResponseArray);
|
|
}
|
|
|
|
|
|
function Connect($sql) {
|
|
$IP = $sql['DEVICES']['host'];
|
|
$PORT = $sql['DEVICES']['port'];
|
|
global $loop;
|
|
global $input_client_process;
|
|
global $input_client;
|
|
global $stream;
|
|
//$client = new steam_socket_client('tcp://'.$IP.":".$PORT);
|
|
//$client_connection = new React\Stream\Steam($client, $loop);
|
|
$input_client_process = new React\ChildProcess\Process('/opt/uinput-mapper/input-create -C');
|
|
$input_client_process->start($loop);
|
|
$input_client_process->stderr->on('data', function ($output) {
|
|
if($output != "") LogEcho($output,"INPUTC");
|
|
});
|
|
|
|
$dnsResolverFactory = new React\Dns\Resolver\Factory();
|
|
$dns = $dnsResolverFactory->createCached($IP, $loop);
|
|
|
|
$input_client = new React\SocketClient\Connector($loop, $dns);
|
|
$input_client->create($IP, $PORT)->then(function (React\Stream\Stream $stream) use ($IP, $PORT) {
|
|
LogEcho("Connected to event server on: ".$IP. ":". $PORT,"INPUTC");
|
|
|
|
$GLOBALS['stream'] = &$stream;
|
|
|
|
$stream->on('data', function($data) {
|
|
global $input_client_process;
|
|
#if(defined("DEBUG")) LogEcho($data, "INPUTC");
|
|
$input_client_process->stdin->write($data);
|
|
});
|
|
});
|
|
|
|
#$netcat_process = new React\ChildProcess\Process('exec nc '.$IP.' '.$PORT);
|
|
#$netcat_process->start($loop);
|
|
#$netcat_process->stdout->on('data', function($output) {
|
|
# $input_client_process->stdin->write($output);
|
|
# });
|
|
|
|
//$connection->on('data', function($output) {
|
|
// $client_process->stdin->write($output);
|
|
//});
|
|
$ResponseArray = array("response_type"=>"success",
|
|
"response_msg"=>"Successfully connected to Event Server socket.",
|
|
"event_server_code"=>"EVT_200C");
|
|
|
|
return json_encode($ResponseArray);
|
|
|
|
}
|
|
|
|
|
|
function Disconnect($sql) {
|
|
global $input_client_process;
|
|
global $input_client;
|
|
global $stream;
|
|
|
|
$input_client_process->terminate();
|
|
$stream->end();
|
|
//$input_client->end();
|
|
$ResponseArray = array("response_type"=>"success",
|
|
"response_msg"=>"Successfully disconnected from Event Server socket.",
|
|
"event_server_code"=>"EVT_200D");
|
|
|
|
return json_encode($ResponseArray);
|
|
}
|
|
|
|
|
|
}
|