Browse Source

Wrapper to start/restart/stop the server easily

Simon Vieille 1 year ago
parent
commit
dd86164a02
No known key found for this signature in database
3 changed files with 251 additions and 201 deletions
  1. 10
    1
      README.md
  2. 42
    200
      server/server
  3. 199
    0
      server/src/resource/server.php

+ 10
- 1
README.md View File

@@ -37,7 +37,7 @@ $ cd remote-i3wm-ws/
37 37
 $ cd client && composer install && cd ..
38 38
 $ cd server && composer install && cd ..
39 39
 $ php -S 0.0.0.0:15000 -t client/&
40
-$ php server/server&
40
+$ php server/src/resource/server.php
41 41
 ```
42 42
 
43 43
 The server can be run with verbosity:
@@ -46,3 +46,12 @@ The server can be run with verbosity:
46 46
 * `--vv` to show handlers messages
47 47
 
48 48
 Open `http://your.local.ip:15000` and enjoy!
49
+
50
+If you want to start/stop/restart the websocket easily:
51
+
52
+```
53
+$ export REMOTE_I3WM_WS_OPTIONS="-v"
54
+$ server/server start
55
+$ server/server stop
56
+$ server/server restart
57
+```

+ 42
- 200
server/server View File

@@ -1,200 +1,42 @@
1
-#!/usr/bin/env php
2
-<?php
3
-
4
-require __DIR__.'/vendor/autoload.php';
5
-
6
-use Ratchet\Server\IoServer;
7
-use Ratchet\Http\HttpServer;
8
-use Ratchet\WebSocket\WsServer;
9
-use Ratchet\ConnectionInterface;
10
-
11
-$options = getopt(
12
-    'v',
13
-    ['verbose', 'vv']
14
-);
15
-
16
-$serverOutput = new Output(isset($options['v']) || isset($options['verbose']));
17
-$messageOutput = new Output(isset($options['vv']));
18
-$server = new Server($serverOutput);
19
-$shell = new Shell();
20
-
21
-$server->addMessageHandler('pointer', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
22
-    $x = $data['x'] ?? null;
23
-    $y = $data['y'] ?? null;
24
-    $click = $data['click'] ?? null;
25
-
26
-    if ($x !== null && $y !== null) {
27
-        $mouseLocations = $shell->exec('xdotool getmouselocation');
28
-        preg_match('/x:(\d+) y:(\d+) /', $mouseLocations, $matches);
29
-        $mouseX = (int) ($matches[1] + $x * 2.5);
30
-        $mouseY = (int) ($matches[2] + $y * 2.5);
31
-
32
-        $shell->exec('xdotool mousemove %s %s', $mouseX, $mouseY);
33
-        $messageOutput->writeln('Pointer moved');
34
-    } elseif ($click !== null) {
35
-        if ($click === 'left') {
36
-            $shell->exec('xdotool click 1');
37
-            $messageOutput->writeln('Left click');
38
-        } elseif ($click === 'middle') {
39
-            $shell->exec('xdotool click 2');
40
-            $messageOutput->writeln('Middle click');
41
-        } elseif ($click === 'right') {
42
-            $shell->exec('xdotool click 3');
43
-            $messageOutput->writeln('Right click');
44
-        }
45
-    }
46
-});
47
-
48
-$server->addMessageHandler('scroll', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
49
-    $value = $data['value'] ?? null;
50
-
51
-    if ($value === 'down') {
52
-        $shell->exec('xdotool click 5 && xdotool click 5');
53
-        $messageOutput->writeln('Scrolling down');
54
-    } elseif ($value === 'up') {
55
-        $shell->exec('xdotool click 4 && xdotool click 4');
56
-        $messageOutput->writeln('Scrolling up');
57
-    }
58
-});
59
-
60
-$server->addMessageHandler('workspace', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
61
-    $value = $data['value'] ?? null;
62
-
63
-    if (!empty($value)) {
64
-        $shell->exec("i3-msg 'workspace \"%s\"'", $value);
65
-        $messageOutput->writeln('Workspace changed');
66
-    }
67
-});
68
-
69
-$server->addMessageHandler('volume', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
70
-    $value = $data['value'] ?? null;
71
-
72
-    if ($value === null) {
73
-        return;
74
-    }
75
-
76
-    if ($value === 'up') {
77
-        $shell->exec('amixer set Master 2%%+');
78
-        $from->send(json_encode(['type' => 'response', 'value' => 'Volume up']));
79
-        $messageOutput->writeln('Volume up');
80
-    } elseif ($value === 'down') {
81
-        $shell->exec('amixer set Master 2%%-');
82
-        $from->send(json_encode(['type' => 'response', 'value' => 'Volume down']));
83
-        $messageOutput->writeln('Volume down');
84
-    } else {
85
-        $shell->exec('amixer set Master %d%%', (int) $value);
86
-        $from->send(json_encode(['type' => 'response', 'value' => sprintf('Volume set to %d%%', $value)]));
87
-        $messageOutput->writeln(sprintf('Volume set to %d%%', $value));
88
-    }
89
-});
90
-
91
-$server->addMessageHandler('media', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
92
-    $value = $data['value'] ?? null;
93
-
94
-    if ($value === 'playpause') {
95
-        $cmd = 'play-pause';
96
-    } elseif ($value === 'next') {
97
-        $cmd = 'next';
98
-    } elseif ($value === 'prev') {
99
-        $cmd = 'previous';
100
-    }
101
-
102
-    if (!empty($cmd)) {
103
-        $shell->exec('playerctl -p spotify %s', $cmd);
104
-
105
-        usleep(500000);
106
-        $status = trim($shell->exec('playerctl -p spotify status'));
107
-
108
-        if ($status === 'Playing') {
109
-            $song = $shell->exec('playerctl -p spotify metadata xesam:title');
110
-            $from->send(json_encode(['type' => 'response', 'value' => 'Playing: '.$song]));
111
-            $messageOutput->writeln('Spotify playing');
112
-        } else {
113
-            $from->send(json_encode(['type' => 'response', 'value' => 'Paused']));
114
-            $messageOutput->writeln('Spotify paused');
115
-        }
116
-    }
117
-});
118
-
119
-$server->addMessageHandler('keys', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
120
-    $value = $data['value'] ?? null;
121
-
122
-    if (!empty($value)) {
123
-        $keys = explode(',', $value);
124
-
125
-        foreach ($keys as $k => $v) {
126
-            if ($v === 'win') {
127
-                $keys[$k] = 'super';
128
-            } elseif ($v === 'ctrl') {
129
-                $keys[$k] = 'Control_L';
130
-            } elseif ($v === 'alt') {
131
-                $keys[$k] = 'Alt_L';
132
-            }
133
-        }
134
-
135
-        $value = implode('+', $keys);
136
-
137
-        $shell->exec('xdotool key %s', escapeshellarg($value));
138
-        $messageOutput->writeln('Key pressed');
139
-    }
140
-});
141
-
142
-$server->addMessageHandler('key', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
143
-    $value = $data['value'] ?? null;
144
-    $map = [
145
-        'up' => 'Up',
146
-        'down' => 'Down',
147
-        'left' => 'Left',
148
-        'right' => 'Right',
149
-        'tab' => 'Tab',
150
-        'backspace' => 'BackSpace',
151
-        'enter' => 'Return',
152
-        'space' => 'space',
153
-        'escape' => 'Escape',
154
-    ];
155
-
156
-    if (!empty($value) && isset($map[$value])) {
157
-        $shell->exec('xdotool key %s', $map[$value]);
158
-        $messageOutput->writeln('Key pressed');
159
-    }
160
-});
161
-
162
-$server->addMessageHandler('text', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
163
-    $value = $data['value'] ?? null;
164
-
165
-    if (trim($value) !== '') {
166
-        $shell->exec('xdotool type %s', escapeshellarg($value));
167
-        $messageOutput->writeln('Text wrote');
168
-    }
169
-});
170
-
171
-$server->addMessageHandler('screenshot', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
172
-    $tmpFilename = sprintf('%s/remote_i3wm_ws_screenshot.jpg', sys_get_temp_dir());
173
-    $shell->exec('import -window root -quality 10 -display :0 %1$s && chmod 600 %1$s', $tmpFilename);
174
-
175
-    if (file_exists($tmpFilename)) {
176
-        $base64 = base64_encode(file_get_contents($tmpFilename));
177
-
178
-        $from->send(json_encode([
179
-            'type' => 'screenshot',
180
-            'value' => $base64,
181
-        ]));
182
-
183
-        unlink($tmpFilename);
184
-    }
185
-});
186
-
187
-$server->addMessageHandler('messages', function (ConnectionInterface $from, array $data) use ($server) {
188
-    $value = $data['value'] ?? [];
189
-
190
-    foreach ($value as $msg) {
191
-        $server->onMessage($from, json_encode($msg));
192
-    }
193
-});
194
-
195
-$webSocker = IoServer::factory(
196
-    new HttpServer(new WsServer($server)),
197
-    14598
198
-);
199
-
200
-$webSocker->run();
1
+#!/bin/sh
2
+
3
+ACTION=start
4
+PID_PATH=/tmp/remote_i3wm_ws.pid
5
+
6
+ri3wmws_stop() {
7
+    if [ -f "$PID_PATH" ]; then
8
+        PID=$(cat "$PID_PATH")
9
+
10
+        if [ -n "$PID" -a $(egrep "^[0-9]+$" "$PID_PATH") ]; then
11
+            kill -9 "$PID"
12
+            rm "$PID_PATH"
13
+            printf "Server stopped\n"
14
+        else
15
+            printf "Server not started\n"
16
+        fi
17
+    else
18
+        printf "Server not started\n"
19
+    fi
20
+}
21
+
22
+ri3wmws_start() {
23
+    php src/resource/server.php "$REMOTE_I3WM_WS_OPTIONS" &
24
+    PID=$!
25
+
26
+    printf $PID > "$PID_PATH"
27
+    chmod 600 "$PID_PATH"
28
+    printf "Server is running\n"
29
+}
30
+
31
+ri3wmws_restart() {
32
+    ri3wmws_stop
33
+    ri3wmws_start
34
+}
35
+
36
+cd "$(dirname "$0")"
37
+
38
+case "$1" in
39
+    start) ri3wmws_start;;
40
+    restart) ri3wmws_restart;;
41
+    stop) ri3wmws_stop;;
42
+esac;

+ 199
- 0
server/src/resource/server.php View File

@@ -0,0 +1,199 @@
1
+<?php
2
+
3
+require __DIR__.'/../../vendor/autoload.php';
4
+
5
+use Ratchet\Server\IoServer;
6
+use Ratchet\Http\HttpServer;
7
+use Ratchet\WebSocket\WsServer;
8
+use Ratchet\ConnectionInterface;
9
+
10
+$options = getopt(
11
+    'v',
12
+    ['verbose', 'vv']
13
+);
14
+
15
+$serverOutput = new Output(isset($options['v']) || isset($options['verbose']));
16
+$messageOutput = new Output(isset($options['vv']));
17
+$server = new Server($serverOutput);
18
+$shell = new Shell();
19
+
20
+$server->addMessageHandler('pointer', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
21
+    $x = $data['x'] ?? null;
22
+    $y = $data['y'] ?? null;
23
+    $click = $data['click'] ?? null;
24
+
25
+    if ($x !== null && $y !== null) {
26
+        $mouseLocations = $shell->exec('xdotool getmouselocation');
27
+        preg_match('/x:(\d+) y:(\d+) /', $mouseLocations, $matches);
28
+        $mouseX = (int) ($matches[1] + $x * 2.5);
29
+        $mouseY = (int) ($matches[2] + $y * 2.5);
30
+
31
+        $shell->exec('xdotool mousemove %s %s', $mouseX, $mouseY);
32
+        $messageOutput->writeln('Pointer moved');
33
+    } elseif ($click !== null) {
34
+        if ($click === 'left') {
35
+            $shell->exec('xdotool click 1');
36
+            $messageOutput->writeln('Left click');
37
+        } elseif ($click === 'middle') {
38
+            $shell->exec('xdotool click 2');
39
+            $messageOutput->writeln('Middle click');
40
+        } elseif ($click === 'right') {
41
+            $shell->exec('xdotool click 3');
42
+            $messageOutput->writeln('Right click');
43
+        }
44
+    }
45
+});
46
+
47
+$server->addMessageHandler('scroll', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
48
+    $value = $data['value'] ?? null;
49
+
50
+    if ($value === 'down') {
51
+        $shell->exec('xdotool click 5 && xdotool click 5');
52
+        $messageOutput->writeln('Scrolling down');
53
+    } elseif ($value === 'up') {
54
+        $shell->exec('xdotool click 4 && xdotool click 4');
55
+        $messageOutput->writeln('Scrolling up');
56
+    }
57
+});
58
+
59
+$server->addMessageHandler('workspace', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
60
+    $value = $data['value'] ?? null;
61
+
62
+    if (!empty($value)) {
63
+        $shell->exec("i3-msg 'workspace \"%s\"'", $value);
64
+        $messageOutput->writeln('Workspace changed');
65
+    }
66
+});
67
+
68
+$server->addMessageHandler('volume', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
69
+    $value = $data['value'] ?? null;
70
+
71
+    if ($value === null) {
72
+        return;
73
+    }
74
+
75
+    if ($value === 'up') {
76
+        $shell->exec('amixer set Master 2%%+');
77
+        $from->send(json_encode(['type' => 'response', 'value' => 'Volume up']));
78
+        $messageOutput->writeln('Volume up');
79
+    } elseif ($value === 'down') {
80
+        $shell->exec('amixer set Master 2%%-');
81
+        $from->send(json_encode(['type' => 'response', 'value' => 'Volume down']));
82
+        $messageOutput->writeln('Volume down');
83
+    } else {
84
+        $shell->exec('amixer set Master %d%%', (int) $value);
85
+        $from->send(json_encode(['type' => 'response', 'value' => sprintf('Volume set to %d%%', $value)]));
86
+        $messageOutput->writeln(sprintf('Volume set to %d%%', $value));
87
+    }
88
+});
89
+
90
+$server->addMessageHandler('media', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
91
+    $value = $data['value'] ?? null;
92
+
93
+    if ($value === 'playpause') {
94
+        $cmd = 'play-pause';
95
+    } elseif ($value === 'next') {
96
+        $cmd = 'next';
97
+    } elseif ($value === 'prev') {
98
+        $cmd = 'previous';
99
+    }
100
+
101
+    if (!empty($cmd)) {
102
+        $shell->exec('playerctl -p spotify %s', $cmd);
103
+
104
+        usleep(500000);
105
+        $status = trim($shell->exec('playerctl -p spotify status'));
106
+
107
+        if ($status === 'Playing') {
108
+            $song = $shell->exec('playerctl -p spotify metadata xesam:title');
109
+            $from->send(json_encode(['type' => 'response', 'value' => 'Playing: '.$song]));
110
+            $messageOutput->writeln('Spotify playing');
111
+        } else {
112
+            $from->send(json_encode(['type' => 'response', 'value' => 'Paused']));
113
+            $messageOutput->writeln('Spotify paused');
114
+        }
115
+    }
116
+});
117
+
118
+$server->addMessageHandler('keys', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
119
+    $value = $data['value'] ?? null;
120
+
121
+    if (!empty($value)) {
122
+        $keys = explode(',', $value);
123
+
124
+        foreach ($keys as $k => $v) {
125
+            if ($v === 'win') {
126
+                $keys[$k] = 'super';
127
+            } elseif ($v === 'ctrl') {
128
+                $keys[$k] = 'Control_L';
129
+            } elseif ($v === 'alt') {
130
+                $keys[$k] = 'Alt_L';
131
+            }
132
+        }
133
+
134
+        $value = implode('+', $keys);
135
+
136
+        $shell->exec('xdotool key %s', escapeshellarg($value));
137
+        $messageOutput->writeln('Key pressed');
138
+    }
139
+});
140
+
141
+$server->addMessageHandler('key', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
142
+    $value = $data['value'] ?? null;
143
+    $map = [
144
+        'up' => 'Up',
145
+        'down' => 'Down',
146
+        'left' => 'Left',
147
+        'right' => 'Right',
148
+        'tab' => 'Tab',
149
+        'backspace' => 'BackSpace',
150
+        'enter' => 'Return',
151
+        'space' => 'space',
152
+        'escape' => 'Escape',
153
+    ];
154
+
155
+    if (!empty($value) && isset($map[$value])) {
156
+        $shell->exec('xdotool key %s', $map[$value]);
157
+        $messageOutput->writeln('Key pressed');
158
+    }
159
+});
160
+
161
+$server->addMessageHandler('text', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
162
+    $value = $data['value'] ?? null;
163
+
164
+    if (trim($value) !== '') {
165
+        $shell->exec('xdotool type %s', escapeshellarg($value));
166
+        $messageOutput->writeln('Text wrote');
167
+    }
168
+});
169
+
170
+$server->addMessageHandler('screenshot', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
171
+    $tmpFilename = sprintf('%s/remote_i3wm_ws_screenshot.jpg', sys_get_temp_dir());
172
+    $shell->exec('import -window root -quality 10 -display :0 %1$s && chmod 600 %1$s', $tmpFilename);
173
+
174
+    if (file_exists($tmpFilename)) {
175
+        $base64 = base64_encode(file_get_contents($tmpFilename));
176
+
177
+        $from->send(json_encode([
178
+            'type' => 'screenshot',
179
+            'value' => $base64,
180
+        ]));
181
+
182
+        unlink($tmpFilename);
183
+    }
184
+});
185
+
186
+$server->addMessageHandler('messages', function (ConnectionInterface $from, array $data) use ($server) {
187
+    $value = $data['value'] ?? [];
188
+
189
+    foreach ($value as $msg) {
190
+        $server->onMessage($from, json_encode($msg));
191
+    }
192
+});
193
+
194
+$webSocker = IoServer::factory(
195
+    new HttpServer(new WsServer($server)),
196
+    14598
197
+);
198
+
199
+$webSocker->run();

Loading…
Cancel
Save