|
17 | 17 | #include "virtualFileSystem.h"
|
18 | 18 | #include "filename.h"
|
19 | 19 | #include "thread.h"
|
| 20 | +#include "urlSpec.h" |
20 | 21 |
|
21 | 22 | #include "config_display.h"
|
22 | 23 | // #define OPENGLES_1 #include "config_androiddisplay.h"
|
23 | 24 |
|
24 | 25 | #include <android_native_app_glue.h>
|
| 26 | +#include <sys/socket.h> |
| 27 | +#include <arpa/inet.h> |
25 | 28 |
|
26 | 29 | // struct android_app* panda_android_app = NULL;
|
27 | 30 |
|
@@ -67,6 +70,55 @@ void android_main(struct android_app* app) {
|
67 | 70 | android_cat.info()
|
68 | 71 | << "New native activity started on " << *current_thread << "\n";
|
69 | 72 |
|
| 73 | + // Were we given an optional location to write the stdout/stderr streams? |
| 74 | + methodID = env->GetMethodID(activity_class, "getIntentOutputUri", "()Ljava/lang/String;"); |
| 75 | + jstring joutput_uri = (jstring) env->CallObjectMethod(activity->clazz, methodID); |
| 76 | + if (joutput_uri != nullptr) { |
| 77 | + const char *output_uri = env->GetStringUTFChars(joutput_uri, nullptr); |
| 78 | + |
| 79 | + if (output_uri != nullptr && output_uri[0] != 0) { |
| 80 | + URLSpec spec(output_uri); |
| 81 | + |
| 82 | + if (spec.get_scheme() == "file") { |
| 83 | + string path = spec.get_path(); |
| 84 | + int fd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY); |
| 85 | + if (fd != -1) { |
| 86 | + android_cat.info() |
| 87 | + << "Writing standard output to file " << path << "\n"; |
| 88 | + |
| 89 | + dup2(fd, 1); |
| 90 | + dup2(fd, 2); |
| 91 | + } else { |
| 92 | + android_cat.error() |
| 93 | + << "Failed to open output path " << path << "\n"; |
| 94 | + } |
| 95 | + } else if (spec.get_scheme() == "tcp") { |
| 96 | + string host = spec.get_server(); |
| 97 | + int fd = socket(AF_INET, SOCK_STREAM, 0); |
| 98 | + struct sockaddr_in serv_addr = {0}; |
| 99 | + serv_addr.sin_family = AF_INET; |
| 100 | + serv_addr.sin_port = htons(spec.get_port()); |
| 101 | + serv_addr.sin_addr.s_addr = inet_addr(host.c_str()); |
| 102 | + if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0) { |
| 103 | + android_cat.info() |
| 104 | + << "Writing standard output to socket " |
| 105 | + << spec.get_server_and_port() << "\n"; |
| 106 | + dup2(fd, 1); |
| 107 | + dup2(fd, 2); |
| 108 | + } else { |
| 109 | + android_cat.error() |
| 110 | + << "Failed to open output socket " |
| 111 | + << spec.get_server_and_port() << "\n"; |
| 112 | + } |
| 113 | + close(fd); |
| 114 | + } else { |
| 115 | + android_cat.error() |
| 116 | + << "Unsupported scheme in output URI: " << output_uri << "\n"; |
| 117 | + } |
| 118 | + env->ReleaseStringUTFChars(joutput_uri, output_uri); |
| 119 | + } |
| 120 | + } |
| 121 | + |
70 | 122 | // Fetch the data directory.
|
71 | 123 | jmethodID get_appinfo = env->GetMethodID(activity_class, "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;");
|
72 | 124 |
|
@@ -186,28 +238,6 @@ void android_main(struct android_app* app) {
|
186 | 238 | }
|
187 | 239 | }
|
188 | 240 |
|
189 |
| - // Were we given an optional location to write the stdout/stderr streams? |
190 |
| - methodID = env->GetMethodID(activity_class, "getIntentOutputPath", "()Ljava/lang/String;"); |
191 |
| - jstring joutput_path = (jstring) env->CallObjectMethod(activity->clazz, methodID); |
192 |
| - if (joutput_path != nullptr) { |
193 |
| - const char *output_path = env->GetStringUTFChars(joutput_path, nullptr); |
194 |
| - |
195 |
| - if (output_path != nullptr && output_path[0] != 0) { |
196 |
| - int fd = open(output_path, O_CREAT | O_TRUNC | O_WRONLY); |
197 |
| - if (fd != -1) { |
198 |
| - android_cat.info() |
199 |
| - << "Writing standard output to file " << output_path << "\n"; |
200 |
| - |
201 |
| - dup2(fd, 1); |
202 |
| - dup2(fd, 2); |
203 |
| - } else { |
204 |
| - android_cat.error() |
205 |
| - << "Failed to open output path " << output_path << "\n"; |
206 |
| - } |
207 |
| - env->ReleaseStringUTFChars(joutput_path, output_path); |
208 |
| - } |
209 |
| - } |
210 |
| - |
211 | 241 | // Create bogus argc and argv for calling the main function.
|
212 | 242 | const char *argv[] = {"pview", nullptr, nullptr};
|
213 | 243 | int argc = 1;
|
@@ -266,6 +296,9 @@ void android_main(struct android_app* app) {
|
266 | 296 | env->ReleaseStringUTFChars(filename, filename_str);
|
267 | 297 | }
|
268 | 298 |
|
| 299 | + close(1); |
| 300 | + close(2); |
| 301 | + |
269 | 302 | // Detach the thread before exiting.
|
270 | 303 | activity->vm->DetachCurrentThread();
|
271 | 304 | }
|
0 commit comments