Refactoring: add message handler and disconnected message
This commit is contained in:
parent
4e76919142
commit
8dfd3318bc
|
@ -33,3 +33,17 @@
|
||||||
margin: auto;
|
margin: auto;
|
||||||
background: #ccc;
|
background: #ccc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#disconneced {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
width: 100%;
|
||||||
|
background: #ff6161;
|
||||||
|
color: #fff;
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#disconneced a {
|
||||||
|
color: #fff;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
|
@ -19,7 +19,13 @@ $(function() {
|
||||||
$(this).addClass('active');
|
$(this).addClass('active');
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.onclose = function(event) {}
|
ws.onopen = function(event) {
|
||||||
|
$('#disconneced').hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.onclose = function(event) {
|
||||||
|
$('#disconneced').show();
|
||||||
|
}
|
||||||
|
|
||||||
ws.onmessage = function(event) {}
|
ws.onmessage = function(event) {}
|
||||||
|
|
||||||
|
|
|
@ -152,6 +152,10 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="disconneced">
|
||||||
|
You are not connected. <a href="?">Refresh</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script src="vendor/components/jquery/jquery.min.js"></script>
|
<script src="vendor/components/jquery/jquery.min.js"></script>
|
||||||
<script src="vendor/select2/select2/dist/js/select2.full.min.js"></script>
|
<script src="vendor/select2/select2/dist/js/select2.full.min.js"></script>
|
||||||
<script src="vendor/twbs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
|
<script src="vendor/twbs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
|
|
@ -16,9 +16,9 @@ class Server implements MessageComponentInterface
|
||||||
protected $clients;
|
protected $clients;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \SplObjectStorage
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $authenticatedClients;
|
protected $messageHandlers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
|
@ -28,6 +28,25 @@ class Server implements MessageComponentInterface
|
||||||
$this->clients = new \SplObjectStorage();
|
$this->clients = new \SplObjectStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a message handle.
|
||||||
|
*
|
||||||
|
* @param string $type
|
||||||
|
* @param callable $callable
|
||||||
|
*
|
||||||
|
* @return Server;
|
||||||
|
*/
|
||||||
|
public function addMessageHandler($type, callable $callable)
|
||||||
|
{
|
||||||
|
if (!isset($this->messageHandlers[$type])) {
|
||||||
|
$this->messageHandlers[$type] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->messageHandlers[$type][] = $callable;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -49,110 +68,14 @@ class Server implements MessageComponentInterface
|
||||||
|
|
||||||
$type = $data['type'] ?? null;
|
$type = $data['type'] ?? null;
|
||||||
|
|
||||||
if ($type === 'pointer') {
|
if ($type === null) {
|
||||||
$x = $data['x'] ?? null;
|
return;
|
||||||
$y = $data['y'] ?? null;
|
}
|
||||||
$click = $data['click'] ?? null;
|
|
||||||
|
|
||||||
if ($x !== null && $y !== null) {
|
$handlers = $this->messageHandlers[$type] ?? [];
|
||||||
$mouseLocations = shell_exec('xdotool getmouselocation');
|
|
||||||
preg_match('/x:(\d+) y:(\d+) /', $mouseLocations, $matches);
|
|
||||||
$mouseX = (int) ($matches[1] + $x * 2.5);
|
|
||||||
$mouseY = (int) ($matches[2] + $y * 2.5);
|
|
||||||
|
|
||||||
return shell_exec(sprintf('xdotool mousemove %s %s', $mouseX, $mouseY));
|
foreach ($handlers as $handler) {
|
||||||
} elseif ($click !== null) {
|
$handler($from, $data);
|
||||||
if ($click === 'left') {
|
|
||||||
return shell_exec('xdotool click 1');
|
|
||||||
} elseif ($click === 'dleft') {
|
|
||||||
// return shell_exec("xdotool click 1; xdotool click 1");
|
|
||||||
} elseif ($click === 'middle') {
|
|
||||||
return shell_exec('xdotool click 2');
|
|
||||||
} elseif ($click === 'right') {
|
|
||||||
return shell_exec('xdotool click 3');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elseif ($type === 'workspace') {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if (!empty($value)) {
|
|
||||||
return shell_exec(sprintf("i3-msg 'workspace \"%s\"'", $value));
|
|
||||||
}
|
|
||||||
} elseif ($type === 'volume') {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if ($value === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($value === 'up') {
|
|
||||||
return shell_exec('amixer set Master 2%+');
|
|
||||||
} elseif ($value === 'down') {
|
|
||||||
return shell_exec('amixer set Master 2%-');
|
|
||||||
} else {
|
|
||||||
return shell_exec('amixer set Master '.((int) $value).'%');
|
|
||||||
}
|
|
||||||
} elseif ($type === 'media') {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if ($value === 'playpause') {
|
|
||||||
$cmd = 'PlayPause';
|
|
||||||
} elseif ($value === 'next') {
|
|
||||||
$cmd = 'Next';
|
|
||||||
} elseif ($value === 'prev') {
|
|
||||||
$cmd = 'Previous';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($cmd)) {
|
|
||||||
return shell_exec('dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.'.$cmd);
|
|
||||||
}
|
|
||||||
} elseif ($type === 'keys') {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if (!empty($value)) {
|
|
||||||
$keys = explode(',', $value);
|
|
||||||
|
|
||||||
foreach ($keys as $k => $v) {
|
|
||||||
if ($v === 'win') {
|
|
||||||
$keys[$k] = 'super';
|
|
||||||
} elseif ($v === 'ctrl') {
|
|
||||||
$keys[$k] = 'Control_L';
|
|
||||||
} elseif ($v === 'alt') {
|
|
||||||
$keys[$k] = 'Alt_L';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = implode('+', $keys);
|
|
||||||
|
|
||||||
return shell_exec(sprintf('xdotool key %s', escapeshellarg($value)));
|
|
||||||
}
|
|
||||||
} elseif ($type === 'key') {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if (!empty($value)) {
|
|
||||||
switch ($value) {
|
|
||||||
case 'up':
|
|
||||||
return shell_exec('xdotool key Up');
|
|
||||||
case 'down':
|
|
||||||
return shell_exec('xdotool key Down');
|
|
||||||
case 'left':
|
|
||||||
return shell_exec('xdotool key Left');
|
|
||||||
case 'right':
|
|
||||||
return shell_exec('xdotool key Right');
|
|
||||||
case 'tab':
|
|
||||||
return shell_exec('xdotool key Tab');
|
|
||||||
case 'backspace':
|
|
||||||
return shell_exec('xdotool key BackSpace');
|
|
||||||
case 'enter':
|
|
||||||
return shell_exec('xdotool key Return');
|
|
||||||
case 'space':
|
|
||||||
return shell_exec('xdotool key space');
|
|
||||||
case 'escape':
|
|
||||||
return shell_exec('xdotool key Escape');
|
|
||||||
}
|
|
||||||
|
|
||||||
return shell_exec(sprintf('xdotool type %s', escapeshellarg($value)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,10 +7,129 @@ require __DIR__.'/Server.php';
|
||||||
use Ratchet\Server\IoServer;
|
use Ratchet\Server\IoServer;
|
||||||
use Ratchet\Http\HttpServer;
|
use Ratchet\Http\HttpServer;
|
||||||
use Ratchet\WebSocket\WsServer;
|
use Ratchet\WebSocket\WsServer;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
$server = IoServer::factory(
|
$server = new \Server();
|
||||||
new HttpServer(new WsServer(new \Server())),
|
|
||||||
14598
|
$server->addMessageHandler('pointer', function (ConnectionInterface $from, array $data) {
|
||||||
|
$x = $data['x'] ?? null;
|
||||||
|
$y = $data['y'] ?? null;
|
||||||
|
$click = $data['click'] ?? null;
|
||||||
|
|
||||||
|
if ($x !== null && $y !== null) {
|
||||||
|
$mouseLocations = shell_exec('xdotool getmouselocation');
|
||||||
|
preg_match('/x:(\d+) y:(\d+) /', $mouseLocations, $matches);
|
||||||
|
$mouseX = (int) ($matches[1] + $x * 2.5);
|
||||||
|
$mouseY = (int) ($matches[2] + $y * 2.5);
|
||||||
|
|
||||||
|
return shell_exec(sprintf('xdotool mousemove %s %s', $mouseX, $mouseY));
|
||||||
|
} elseif ($click !== null) {
|
||||||
|
if ($click === 'left') {
|
||||||
|
return shell_exec('xdotool click 1');
|
||||||
|
} elseif ($click === 'dleft') {
|
||||||
|
// return shell_exec("xdotool click 1; xdotool click 1");
|
||||||
|
} elseif ($click === 'middle') {
|
||||||
|
return shell_exec('xdotool click 2');
|
||||||
|
} elseif ($click === 'right') {
|
||||||
|
return shell_exec('xdotool click 3');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('workspace', function (ConnectionInterface $from, array $data) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if (!empty($value)) {
|
||||||
|
return shell_exec(sprintf("i3-msg 'workspace \"%s\"'", $value));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('volume', function (ConnectionInterface $from, array $data) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if ($value === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value === 'up') {
|
||||||
|
return shell_exec('amixer set Master 2%+');
|
||||||
|
} elseif ($value === 'down') {
|
||||||
|
return shell_exec('amixer set Master 2%-');
|
||||||
|
} else {
|
||||||
|
return shell_exec('amixer set Master '.((int) $value).'%');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('media', function (ConnectionInterface $from, array $data) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if ($value === 'playpause') {
|
||||||
|
$cmd = 'PlayPause';
|
||||||
|
} elseif ($value === 'next') {
|
||||||
|
$cmd = 'Next';
|
||||||
|
} elseif ($value === 'prev') {
|
||||||
|
$cmd = 'Previous';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($cmd)) {
|
||||||
|
return shell_exec('dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.'.$cmd);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('keys', function (ConnectionInterface $from, array $data) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if (!empty($value)) {
|
||||||
|
$keys = explode(',', $value);
|
||||||
|
|
||||||
|
foreach ($keys as $k => $v) {
|
||||||
|
if ($v === 'win') {
|
||||||
|
$keys[$k] = 'super';
|
||||||
|
} elseif ($v === 'ctrl') {
|
||||||
|
$keys[$k] = 'Control_L';
|
||||||
|
} elseif ($v === 'alt') {
|
||||||
|
$keys[$k] = 'Alt_L';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = implode('+', $keys);
|
||||||
|
|
||||||
|
return shell_exec(sprintf('xdotool key %s', escapeshellarg($value)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('key', function (ConnectionInterface $from, array $data) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if (!empty($value)) {
|
||||||
|
switch ($value) {
|
||||||
|
case 'up':
|
||||||
|
return shell_exec('xdotool key Up');
|
||||||
|
case 'down':
|
||||||
|
return shell_exec('xdotool key Down');
|
||||||
|
case 'left':
|
||||||
|
return shell_exec('xdotool key Left');
|
||||||
|
case 'right':
|
||||||
|
return shell_exec('xdotool key Right');
|
||||||
|
case 'tab':
|
||||||
|
return shell_exec('xdotool key Tab');
|
||||||
|
case 'backspace':
|
||||||
|
return shell_exec('xdotool key BackSpace');
|
||||||
|
case 'enter':
|
||||||
|
return shell_exec('xdotool key Return');
|
||||||
|
case 'space':
|
||||||
|
return shell_exec('xdotool key space');
|
||||||
|
case 'escape':
|
||||||
|
return shell_exec('xdotool key Escape');
|
||||||
|
}
|
||||||
|
|
||||||
|
return shell_exec(sprintf('xdotool type %s', escapeshellarg($value)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$webSocker = IoServer::factory(
|
||||||
|
new HttpServer(new WsServer($server)),
|
||||||
|
14598
|
||||||
);
|
);
|
||||||
|
|
||||||
$server->run();
|
$webSocker->run();
|
||||||
|
|
Loading…
Reference in a new issue