Wrapper to start/restart/stop the server easily
This commit is contained in:
parent
87670ae027
commit
dd86164a02
11
README.md
11
README.md
|
@ -37,7 +37,7 @@ $ cd remote-i3wm-ws/
|
||||||
$ cd client && composer install && cd ..
|
$ cd client && composer install && cd ..
|
||||||
$ cd server && composer install && cd ..
|
$ cd server && composer install && cd ..
|
||||||
$ php -S 0.0.0.0:15000 -t client/&
|
$ php -S 0.0.0.0:15000 -t client/&
|
||||||
$ php server/server&
|
$ php server/src/resource/server.php
|
||||||
```
|
```
|
||||||
|
|
||||||
The server can be run with verbosity:
|
The server can be run with verbosity:
|
||||||
|
@ -46,3 +46,12 @@ The server can be run with verbosity:
|
||||||
* `--vv` to show handlers messages
|
* `--vv` to show handlers messages
|
||||||
|
|
||||||
Open `http://your.local.ip:15000` and enjoy!
|
Open `http://your.local.ip:15000` and enjoy!
|
||||||
|
|
||||||
|
If you want to start/stop/restart the websocket easily:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ export REMOTE_I3WM_WS_OPTIONS="-v"
|
||||||
|
$ server/server start
|
||||||
|
$ server/server stop
|
||||||
|
$ server/server restart
|
||||||
|
```
|
||||||
|
|
226
server/server
226
server/server
|
@ -1,200 +1,42 @@
|
||||||
#!/usr/bin/env php
|
#!/bin/sh
|
||||||
<?php
|
|
||||||
|
|
||||||
require __DIR__.'/vendor/autoload.php';
|
ACTION=start
|
||||||
|
PID_PATH=/tmp/remote_i3wm_ws.pid
|
||||||
|
|
||||||
use Ratchet\Server\IoServer;
|
ri3wmws_stop() {
|
||||||
use Ratchet\Http\HttpServer;
|
if [ -f "$PID_PATH" ]; then
|
||||||
use Ratchet\WebSocket\WsServer;
|
PID=$(cat "$PID_PATH")
|
||||||
use Ratchet\ConnectionInterface;
|
|
||||||
|
|
||||||
$options = getopt(
|
if [ -n "$PID" -a $(egrep "^[0-9]+$" "$PID_PATH") ]; then
|
||||||
'v',
|
kill -9 "$PID"
|
||||||
['verbose', 'vv']
|
rm "$PID_PATH"
|
||||||
);
|
printf "Server stopped\n"
|
||||||
|
else
|
||||||
|
printf "Server not started\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
printf "Server not started\n"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
$serverOutput = new Output(isset($options['v']) || isset($options['verbose']));
|
ri3wmws_start() {
|
||||||
$messageOutput = new Output(isset($options['vv']));
|
php src/resource/server.php "$REMOTE_I3WM_WS_OPTIONS" &
|
||||||
$server = new Server($serverOutput);
|
PID=$!
|
||||||
$shell = new Shell();
|
|
||||||
|
|
||||||
$server->addMessageHandler('pointer', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
printf $PID > "$PID_PATH"
|
||||||
$x = $data['x'] ?? null;
|
chmod 600 "$PID_PATH"
|
||||||
$y = $data['y'] ?? null;
|
printf "Server is running\n"
|
||||||
$click = $data['click'] ?? null;
|
}
|
||||||
|
|
||||||
if ($x !== null && $y !== null) {
|
ri3wmws_restart() {
|
||||||
$mouseLocations = $shell->exec('xdotool getmouselocation');
|
ri3wmws_stop
|
||||||
preg_match('/x:(\d+) y:(\d+) /', $mouseLocations, $matches);
|
ri3wmws_start
|
||||||
$mouseX = (int) ($matches[1] + $x * 2.5);
|
}
|
||||||
$mouseY = (int) ($matches[2] + $y * 2.5);
|
|
||||||
|
|
||||||
$shell->exec('xdotool mousemove %s %s', $mouseX, $mouseY);
|
cd "$(dirname "$0")"
|
||||||
$messageOutput->writeln('Pointer moved');
|
|
||||||
} elseif ($click !== null) {
|
|
||||||
if ($click === 'left') {
|
|
||||||
$shell->exec('xdotool click 1');
|
|
||||||
$messageOutput->writeln('Left click');
|
|
||||||
} elseif ($click === 'middle') {
|
|
||||||
$shell->exec('xdotool click 2');
|
|
||||||
$messageOutput->writeln('Middle click');
|
|
||||||
} elseif ($click === 'right') {
|
|
||||||
$shell->exec('xdotool click 3');
|
|
||||||
$messageOutput->writeln('Right click');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('scroll', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
case "$1" in
|
||||||
$value = $data['value'] ?? null;
|
start) ri3wmws_start;;
|
||||||
|
restart) ri3wmws_restart;;
|
||||||
if ($value === 'down') {
|
stop) ri3wmws_stop;;
|
||||||
$shell->exec('xdotool click 5 && xdotool click 5');
|
esac;
|
||||||
$messageOutput->writeln('Scrolling down');
|
|
||||||
} elseif ($value === 'up') {
|
|
||||||
$shell->exec('xdotool click 4 && xdotool click 4');
|
|
||||||
$messageOutput->writeln('Scrolling up');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('workspace', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if (!empty($value)) {
|
|
||||||
$shell->exec("i3-msg 'workspace \"%s\"'", $value);
|
|
||||||
$messageOutput->writeln('Workspace changed');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('volume', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if ($value === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($value === 'up') {
|
|
||||||
$shell->exec('amixer set Master 2%%+');
|
|
||||||
$from->send(json_encode(['type' => 'response', 'value' => 'Volume up']));
|
|
||||||
$messageOutput->writeln('Volume up');
|
|
||||||
} elseif ($value === 'down') {
|
|
||||||
$shell->exec('amixer set Master 2%%-');
|
|
||||||
$from->send(json_encode(['type' => 'response', 'value' => 'Volume down']));
|
|
||||||
$messageOutput->writeln('Volume down');
|
|
||||||
} else {
|
|
||||||
$shell->exec('amixer set Master %d%%', (int) $value);
|
|
||||||
$from->send(json_encode(['type' => 'response', 'value' => sprintf('Volume set to %d%%', $value)]));
|
|
||||||
$messageOutput->writeln(sprintf('Volume set to %d%%', $value));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('media', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if ($value === 'playpause') {
|
|
||||||
$cmd = 'play-pause';
|
|
||||||
} elseif ($value === 'next') {
|
|
||||||
$cmd = 'next';
|
|
||||||
} elseif ($value === 'prev') {
|
|
||||||
$cmd = 'previous';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($cmd)) {
|
|
||||||
$shell->exec('playerctl -p spotify %s', $cmd);
|
|
||||||
|
|
||||||
usleep(500000);
|
|
||||||
$status = trim($shell->exec('playerctl -p spotify status'));
|
|
||||||
|
|
||||||
if ($status === 'Playing') {
|
|
||||||
$song = $shell->exec('playerctl -p spotify metadata xesam:title');
|
|
||||||
$from->send(json_encode(['type' => 'response', 'value' => 'Playing: '.$song]));
|
|
||||||
$messageOutput->writeln('Spotify playing');
|
|
||||||
} else {
|
|
||||||
$from->send(json_encode(['type' => 'response', 'value' => 'Paused']));
|
|
||||||
$messageOutput->writeln('Spotify paused');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('keys', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
|
||||||
$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);
|
|
||||||
|
|
||||||
$shell->exec('xdotool key %s', escapeshellarg($value));
|
|
||||||
$messageOutput->writeln('Key pressed');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('key', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
$map = [
|
|
||||||
'up' => 'Up',
|
|
||||||
'down' => 'Down',
|
|
||||||
'left' => 'Left',
|
|
||||||
'right' => 'Right',
|
|
||||||
'tab' => 'Tab',
|
|
||||||
'backspace' => 'BackSpace',
|
|
||||||
'enter' => 'Return',
|
|
||||||
'space' => 'space',
|
|
||||||
'escape' => 'Escape',
|
|
||||||
];
|
|
||||||
|
|
||||||
if (!empty($value) && isset($map[$value])) {
|
|
||||||
$shell->exec('xdotool key %s', $map[$value]);
|
|
||||||
$messageOutput->writeln('Key pressed');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('text', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
|
||||||
$value = $data['value'] ?? null;
|
|
||||||
|
|
||||||
if (trim($value) !== '') {
|
|
||||||
$shell->exec('xdotool type %s', escapeshellarg($value));
|
|
||||||
$messageOutput->writeln('Text wrote');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('screenshot', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
|
||||||
$tmpFilename = sprintf('%s/remote_i3wm_ws_screenshot.jpg', sys_get_temp_dir());
|
|
||||||
$shell->exec('import -window root -quality 10 -display :0 %1$s && chmod 600 %1$s', $tmpFilename);
|
|
||||||
|
|
||||||
if (file_exists($tmpFilename)) {
|
|
||||||
$base64 = base64_encode(file_get_contents($tmpFilename));
|
|
||||||
|
|
||||||
$from->send(json_encode([
|
|
||||||
'type' => 'screenshot',
|
|
||||||
'value' => $base64,
|
|
||||||
]));
|
|
||||||
|
|
||||||
unlink($tmpFilename);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$server->addMessageHandler('messages', function (ConnectionInterface $from, array $data) use ($server) {
|
|
||||||
$value = $data['value'] ?? [];
|
|
||||||
|
|
||||||
foreach ($value as $msg) {
|
|
||||||
$server->onMessage($from, json_encode($msg));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$webSocker = IoServer::factory(
|
|
||||||
new HttpServer(new WsServer($server)),
|
|
||||||
14598
|
|
||||||
);
|
|
||||||
|
|
||||||
$webSocker->run();
|
|
||||||
|
|
199
server/src/resource/server.php
Normal file
199
server/src/resource/server.php
Normal file
|
@ -0,0 +1,199 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require __DIR__.'/../../vendor/autoload.php';
|
||||||
|
|
||||||
|
use Ratchet\Server\IoServer;
|
||||||
|
use Ratchet\Http\HttpServer;
|
||||||
|
use Ratchet\WebSocket\WsServer;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
|
$options = getopt(
|
||||||
|
'v',
|
||||||
|
['verbose', 'vv']
|
||||||
|
);
|
||||||
|
|
||||||
|
$serverOutput = new Output(isset($options['v']) || isset($options['verbose']));
|
||||||
|
$messageOutput = new Output(isset($options['vv']));
|
||||||
|
$server = new Server($serverOutput);
|
||||||
|
$shell = new Shell();
|
||||||
|
|
||||||
|
$server->addMessageHandler('pointer', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$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);
|
||||||
|
|
||||||
|
$shell->exec('xdotool mousemove %s %s', $mouseX, $mouseY);
|
||||||
|
$messageOutput->writeln('Pointer moved');
|
||||||
|
} elseif ($click !== null) {
|
||||||
|
if ($click === 'left') {
|
||||||
|
$shell->exec('xdotool click 1');
|
||||||
|
$messageOutput->writeln('Left click');
|
||||||
|
} elseif ($click === 'middle') {
|
||||||
|
$shell->exec('xdotool click 2');
|
||||||
|
$messageOutput->writeln('Middle click');
|
||||||
|
} elseif ($click === 'right') {
|
||||||
|
$shell->exec('xdotool click 3');
|
||||||
|
$messageOutput->writeln('Right click');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('scroll', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if ($value === 'down') {
|
||||||
|
$shell->exec('xdotool click 5 && xdotool click 5');
|
||||||
|
$messageOutput->writeln('Scrolling down');
|
||||||
|
} elseif ($value === 'up') {
|
||||||
|
$shell->exec('xdotool click 4 && xdotool click 4');
|
||||||
|
$messageOutput->writeln('Scrolling up');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('workspace', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if (!empty($value)) {
|
||||||
|
$shell->exec("i3-msg 'workspace \"%s\"'", $value);
|
||||||
|
$messageOutput->writeln('Workspace changed');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('volume', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if ($value === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value === 'up') {
|
||||||
|
$shell->exec('amixer set Master 2%%+');
|
||||||
|
$from->send(json_encode(['type' => 'response', 'value' => 'Volume up']));
|
||||||
|
$messageOutput->writeln('Volume up');
|
||||||
|
} elseif ($value === 'down') {
|
||||||
|
$shell->exec('amixer set Master 2%%-');
|
||||||
|
$from->send(json_encode(['type' => 'response', 'value' => 'Volume down']));
|
||||||
|
$messageOutput->writeln('Volume down');
|
||||||
|
} else {
|
||||||
|
$shell->exec('amixer set Master %d%%', (int) $value);
|
||||||
|
$from->send(json_encode(['type' => 'response', 'value' => sprintf('Volume set to %d%%', $value)]));
|
||||||
|
$messageOutput->writeln(sprintf('Volume set to %d%%', $value));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('media', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if ($value === 'playpause') {
|
||||||
|
$cmd = 'play-pause';
|
||||||
|
} elseif ($value === 'next') {
|
||||||
|
$cmd = 'next';
|
||||||
|
} elseif ($value === 'prev') {
|
||||||
|
$cmd = 'previous';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($cmd)) {
|
||||||
|
$shell->exec('playerctl -p spotify %s', $cmd);
|
||||||
|
|
||||||
|
usleep(500000);
|
||||||
|
$status = trim($shell->exec('playerctl -p spotify status'));
|
||||||
|
|
||||||
|
if ($status === 'Playing') {
|
||||||
|
$song = $shell->exec('playerctl -p spotify metadata xesam:title');
|
||||||
|
$from->send(json_encode(['type' => 'response', 'value' => 'Playing: '.$song]));
|
||||||
|
$messageOutput->writeln('Spotify playing');
|
||||||
|
} else {
|
||||||
|
$from->send(json_encode(['type' => 'response', 'value' => 'Paused']));
|
||||||
|
$messageOutput->writeln('Spotify paused');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('keys', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$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);
|
||||||
|
|
||||||
|
$shell->exec('xdotool key %s', escapeshellarg($value));
|
||||||
|
$messageOutput->writeln('Key pressed');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('key', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
$map = [
|
||||||
|
'up' => 'Up',
|
||||||
|
'down' => 'Down',
|
||||||
|
'left' => 'Left',
|
||||||
|
'right' => 'Right',
|
||||||
|
'tab' => 'Tab',
|
||||||
|
'backspace' => 'BackSpace',
|
||||||
|
'enter' => 'Return',
|
||||||
|
'space' => 'space',
|
||||||
|
'escape' => 'Escape',
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!empty($value) && isset($map[$value])) {
|
||||||
|
$shell->exec('xdotool key %s', $map[$value]);
|
||||||
|
$messageOutput->writeln('Key pressed');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('text', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$value = $data['value'] ?? null;
|
||||||
|
|
||||||
|
if (trim($value) !== '') {
|
||||||
|
$shell->exec('xdotool type %s', escapeshellarg($value));
|
||||||
|
$messageOutput->writeln('Text wrote');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('screenshot', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
||||||
|
$tmpFilename = sprintf('%s/remote_i3wm_ws_screenshot.jpg', sys_get_temp_dir());
|
||||||
|
$shell->exec('import -window root -quality 10 -display :0 %1$s && chmod 600 %1$s', $tmpFilename);
|
||||||
|
|
||||||
|
if (file_exists($tmpFilename)) {
|
||||||
|
$base64 = base64_encode(file_get_contents($tmpFilename));
|
||||||
|
|
||||||
|
$from->send(json_encode([
|
||||||
|
'type' => 'screenshot',
|
||||||
|
'value' => $base64,
|
||||||
|
]));
|
||||||
|
|
||||||
|
unlink($tmpFilename);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$server->addMessageHandler('messages', function (ConnectionInterface $from, array $data) use ($server) {
|
||||||
|
$value = $data['value'] ?? [];
|
||||||
|
|
||||||
|
foreach ($value as $msg) {
|
||||||
|
$server->onMessage($from, json_encode($msg));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$webSocker = IoServer::factory(
|
||||||
|
new HttpServer(new WsServer($server)),
|
||||||
|
14598
|
||||||
|
);
|
||||||
|
|
||||||
|
$webSocker->run();
|
Loading…
Reference in a new issue