2 * Copyright 2009-2017, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
6 * Michael Lotz <mmlr@mlotz.ch>
9 #include <Application.h>
10 #include <FindDirectory.h>
15 #include "RemoteView.h"
26 print_usage(const char *app
)
28 printf("usage:\t%s <host> [-p <port>] [-w <width>] [-h <height>]\n", app
);
29 printf("usage:\t%s <user@host> -s [<sshPort>] [-p <port>] [-w <width>]"
30 " [-h <height>] [-c <command>]\n", app
);
31 printf("\t%s --help\n\n", app
);
33 printf("Connect to & run applications from a different computer\n\n");
34 printf("Arguments available for use:\n\n");
35 printf("\t-p\t\tspecify the port to communicate on (default 10900)\n");
36 printf("\t-c\t\tsend a command to the other computer (default Terminal)\n");
37 printf("\t-s\t\tuse SSH, optionally specify the SSH port to use (22)\n");
38 printf("\t-w\t\tmake the virtual desktop use the specified width\n");
39 printf("\t-h\t\tmake the virtual desktop use the specified height\n");
40 printf("\nIf no width and height are specified, the window is opened with"
41 " the size of the the local screen.\n");
46 main(int argc
, char *argv
[])
48 if (argc
< 2 || strcmp(argv
[1], "--help") == 0) {
58 const char *command
= NULL
;
59 const char *host
= argv
[1];
61 for (int32 i
= 2; i
< argc
; i
++) {
62 if (strcmp(argv
[i
], "-p") == 0) {
63 if (argc
< i
+ 1 || sscanf(argv
[i
+ 1], "%" B_SCNu16
, &port
)
73 if (strcmp(argv
[i
], "-w") == 0) {
74 if (argc
< i
+ 1 || sscanf(argv
[i
+ 1], "%" B_SCNd32
, &width
) != 1)
84 if (strcmp(argv
[i
], "-h") == 0) {
85 if (argc
< i
+ 1 || sscanf(argv
[i
+ 1], "%" B_SCNd32
, &height
) != 1)
95 if (strcmp(argv
[i
], "-s") == 0) {
97 && sscanf(argv
[i
+ 1], "%" B_SCNu16
, &sshPort
) == 1) {
105 if (strcmp(argv
[i
], "-c") == 0) {
107 print_usage(argv
[0]);
116 print_usage(argv
[0]);
120 if (command
!= NULL
&& !useSSH
) {
121 print_usage(argv
[0]);
128 if (command
== NULL
) {
129 if (find_directory(B_SYSTEM_APPS_DIRECTORY
, &terminalPath
)
131 printf("failed to determine system-apps directory\n");
134 if (terminalPath
.Append("Terminal") != B_OK
) {
135 printf("failed to append to system-apps path\n");
138 command
= terminalPath
.Path();
141 char shellCommand
[4096];
142 snprintf(shellCommand
, sizeof(shellCommand
),
143 "echo connected; export TARGET_SCREEN=%" B_PRIu16
"; %s\n", port
,
147 if (pipe(&pipes
[0]) != 0 || pipe(&pipes
[2]) != 0) {
148 printf("failed to create redirection pipes\n");
154 printf("failed to fork ssh process\n");
159 // child code, redirect std* and execute ssh
160 close(STDOUT_FILENO
);
162 dup2(pipes
[1], STDOUT_FILENO
);
163 dup2(pipes
[1], STDERR_FILENO
);
164 dup2(pipes
[2], STDIN_FILENO
);
165 for (int32 i
= 0; i
< 4; i
++)
168 char localRedirect
[50];
169 sprintf(localRedirect
, "localhost:%" B_PRIu16
":localhost:%"
170 B_PRIu16
, port
, port
);
173 sprintf(portNumber
, "%" B_PRIu16
, sshPort
);
175 int result
= execl("ssh", "-C", "-L", localRedirect
,
176 "-p", portNumber
, "-o", "ExitOnForwardFailure=yes", host
,
179 // we don't get here unless there was an error in executing
180 printf("failed to execute ssh process in child\n");
187 read(pipes
[0], buffer
, sizeof(buffer
));
188 // block until connected/error message from ssh
194 BApplication
app("application/x-vnd.Haiku-RemoteDesktop");
195 BRect windowFrame
= BRect(0, 0, width
- 1, height
- 1);
196 if (!windowFrame
.IsValid()) {
198 windowFrame
= screen
.Frame();
201 BWindow
*window
= new(std::nothrow
) BWindow(windowFrame
, "RemoteDesktop",
202 B_TITLED_WINDOW
, B_QUIT_ON_WINDOW_CLOSE
);
204 if (window
== NULL
) {
205 printf("no memory to allocate window\n");
209 RemoteView
*view
= new(std::nothrow
) RemoteView(window
->Bounds(), host
,
212 printf("no memory to allocate remote view\n");
216 status_t init
= view
->InitCheck();
218 printf("initialization of remote view failed: %s\n", strerror(init
));
223 window
->AddChild(view
);
229 kill(sshPID
, SIGHUP
);