2 * communicate with java skin process
4 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
7 * GiWoong Kim <giwoong.kim@samsung.com>
8 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include "maru_common.h"
37 #include "maruskin_client.h"
38 #include "maruskin_server.h"
42 #include "emul_state.h"
43 #include "maruskin_operation.h"
46 #include "maru_err_table.h"
50 MULTI_DEBUG_CHANNEL(qemu, skin_client);
52 #define SKIN_SERVER_READY_TIME 3 // second
53 #define SKIN_SERVER_SLEEP_TIME 10 // milli second
55 #define OPT_SVR_PORT "svr.port"
57 #define OPT_VM_PATH "vm.path"
58 #define OPT_NET_BASE_PORT "net.baseport"
59 #define OPT_MAX_TOUCHPOINT "max.touchpoint"
61 extern char tizen_target_path[];
64 static char** skin_argv;
67 static char* JAVA_EXEFILE_PATH = NULL;
70 static void* run_skin_client(void* arg)
72 char cmd[JAVA_MAX_COMMAND_LENGTH] = { 0, };
73 char argv[JAVA_MAX_COMMAND_LENGTH] = { 0, };
75 INFO("run skin client\n");
77 for (i = 0; i < skin_argc; ++i) {
78 strncat(argv, skin_argv[i], strlen(skin_argv[i]));
79 strncat(argv, " ", 1);
80 INFO("[skin args %d] %s\n", i, skin_argv[i]);
83 int skin_server_port = get_skin_server_port();
85 //srand( time( NULL ) );
86 int uid = 0; //rand();
87 //INFO( "generated skin uid:%d\n", uid );
89 char* vm_path = tizen_target_path;
90 //INFO( "vm_path:%s\n", vm_path );
91 char buf_skin_server_port[16];
93 char buf_tizen_base_port[16];
94 sprintf(buf_skin_server_port, "%d", skin_server_port);
95 sprintf(buf_uid, "%d", uid);
96 sprintf(buf_tizen_base_port, "%d", get_emul_vm_base_port());
99 // find java path in 64bit windows
100 JAVA_EXEFILE_PATH = malloc(JAVA_MAX_COMMAND_LENGTH);
101 memset(JAVA_EXEFILE_PATH, 0, JAVA_MAX_COMMAND_LENGTH);
103 INFO("This process is running under WOW64.\n");
104 if (!get_java_path(&JAVA_EXEFILE_PATH)) {
105 strcpy(JAVA_EXEFILE_PATH, "java");
108 strcpy(JAVA_EXEFILE_PATH, "java");
111 char* bin_dir = get_bin_path();
112 int bin_len = strlen(bin_dir);
113 char bin_dir_win[bin_len];
114 strcpy(bin_dir_win, bin_dir);
115 bin_dir_win[strlen(bin_dir_win) -1] = '\0';
117 char* bin_dir = get_bin_path();
119 INFO("bin directory : %s\n", bin_dir);
121 int maxtouchpoint = get_emul_max_touch_point();
122 int len_maxtouchpoint;
123 if (maxtouchpoint > 9) {
124 len_maxtouchpoint = 2;
126 len_maxtouchpoint = 1;
129 int len = strlen(JAVA_EXEFILE_PATH) + strlen(JAVA_EXEOPTION) +
131 strlen((char*)bin_dir_win) + strlen(bin_dir) + strlen(JAR_SKINFILE) +
133 strlen(bin_dir) + strlen(bin_dir) + strlen(JAR_SKINFILE) +
135 strlen(OPT_SVR_PORT) + strlen(buf_skin_server_port) + strlen(OPT_UID) + strlen(buf_uid) +
136 strlen(OPT_VM_PATH) + strlen(vm_path) + strlen(OPT_NET_BASE_PORT) + strlen(buf_tizen_base_port) +
137 strlen(OPT_MAX_TOUCHPOINT) + len_maxtouchpoint + strlen(argv) + 46;
138 if (len > JAVA_MAX_COMMAND_LENGTH) {
139 INFO("swt command length is too long! (%d)\n", len);
140 len = JAVA_MAX_COMMAND_LENGTH;
143 snprintf(cmd, len, "%s %s %s=\"%s\" \"%s%s\" %s=\"%d\" %s=\"%d\" %s=\"%s\" %s=\"%d\" %s=%d %s",
144 JAVA_EXEFILE_PATH, JAVA_EXEOPTION, JAVA_LIBRARY_PATH,
146 bin_dir_win, bin_dir, JAR_SKINFILE,
148 bin_dir, bin_dir, JAR_SKINFILE,
150 OPT_SVR_PORT, skin_server_port,
152 OPT_VM_PATH, vm_path,
153 OPT_NET_BASE_PORT, get_emul_vm_base_port(),
154 OPT_MAX_TOUCHPOINT, maxtouchpoint,
157 INFO("command for swt : %s\n", cmd);
161 free(JAVA_EXEFILE_PATH);
162 JAVA_EXEFILE_PATH = NULL;
164 //WinExec( cmd, SW_SHOW );
166 STARTUPINFO sti = { 0 };
167 PROCESS_INFORMATION pi = { 0 };
168 if (!CreateProcess(NULL,
173 NORMAL_PRIORITY_CLASS,
179 ERR("Unable to generate process! error %u\n", GetLastError());
180 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
181 "CreateProcess function failed. Unable to generate process.");
185 INFO("wait for single object..\n");
186 DWORD dwRet = WaitForSingleObject(
187 pi.hProcess, // process handle
192 INFO("the child thread state was signaled!\n");
195 INFO("time-out interval elapsed,\
196 and the child thread's state is nonsignaled.\n");
199 ERR("WaitForSingleObject() failed, error %u\n", GetLastError());
203 //retrieves the termination status of the specified process
204 if (GetExitCodeProcess(pi.hProcess, &dwRet) != 0) {
205 ERR("failed to GetExitCodeProcess, error %u\n", GetLastError());
207 INFO("child return value : %d\n", dwRet);
210 /* child process is terminated with some problem.
211 So qemu process will terminate, too. immediately. */
212 shutdown_qemu_gracefully();
215 if (CloseHandle(pi.hProcess) != 0) {
216 INFO("child thread handle was closed successfully!\n");
218 ERR("failed to close child thread handle, error %u\n", GetLastError());
222 #else //ifndef CONFIG_WIN32
223 int ret = system(cmd);
226 INFO("can't execute /bin/sh!\n");
227 } else if(ret == -1) {
228 INFO("fork error!\n");
230 ret = WEXITSTATUS(ret);
231 //The high-order 8 bits are the exit code from exit().
232 //The low-order 8 bits are zero if the process exited normally.
233 INFO("child return value : %d\n", ret);
236 /* child process is terminated with some problem.
237 So qemu process will terminate, too. immediately. */
238 shutdown_qemu_gracefully();
247 int start_skin_client(int argc, char* argv[])
250 int skin_server_ready = 0;
254 if (100 * SKIN_SERVER_READY_TIME < count) {
258 if (is_ready_skin_server()) {
259 skin_server_ready = 1;
263 INFO("sleep for ready. count:%d\n", count);
266 Sleep(SKIN_SERVER_SLEEP_TIME);
268 usleep(1000 * SKIN_SERVER_SLEEP_TIME);
274 if (!skin_server_ready) {
275 ERR("skin_server is not ready.\n");
284 if (0 != pthread_create(&thread_id, NULL, run_skin_client, NULL)) {
285 ERR( "fail to create skin_client pthread.\n" );
292 int start_simple_client(char* msg)
295 char cmd[JAVA_MAX_COMMAND_LENGTH] = { 0, };
297 INFO("run simple client\n");
300 // find java path in 64bit windows
301 JAVA_EXEFILE_PATH = malloc(JAVA_MAX_COMMAND_LENGTH);
302 memset(JAVA_EXEFILE_PATH, 0, JAVA_MAX_COMMAND_LENGTH);
304 INFO("This process is running under WOW64.\n");
305 if (!get_java_path(&JAVA_EXEFILE_PATH)) {
306 strcpy(JAVA_EXEFILE_PATH, "java");
309 strcpy(JAVA_EXEFILE_PATH, "java");
311 char* bin_dir = get_bin_path();
312 int bin_dir_len = strlen(bin_dir);
313 char bin_dir_win[bin_dir_len];
314 strcpy(bin_dir_win, bin_dir);
315 bin_dir_win[strlen(bin_dir_win) -1] = '\0';
317 char* bin_dir = get_bin_path();
319 INFO("bin directory : %s\n", bin_dir);
322 int len = strlen(JAVA_EXEFILE_PATH) + strlen(JAVA_EXEOPTION) + strlen(JAVA_LIBRARY_PATH) +
324 strlen((char*)bin_dir_win) + strlen(bin_dir) + strlen(JAR_SKINFILE) +
326 strlen(bin_dir) + strlen(bin_dir) + strlen(JAR_SKINFILE) +
328 strlen(bin_dir) + strlen(JAVA_SIMPLEMODE_OPTION) + strlen(msg) + 11;
329 if (len > JAVA_MAX_COMMAND_LENGTH) {
330 len = JAVA_MAX_COMMAND_LENGTH;
332 snprintf(cmd, len, "%s %s %s=\"%s\" %s%s %s=\"%s\"",
334 JAVA_EXEFILE_PATH, JAVA_EXEOPTION, JAVA_LIBRARY_PATH, bin_dir_win,
336 JAVA_EXEFILE_PATH, JAVA_EXEOPTION, JAVA_LIBRARY_PATH, bin_dir,
338 bin_dir, JAR_SKINFILE, JAVA_SIMPLEMODE_OPTION, msg);
339 INFO("command for swt : %s\n", cmd);
343 free(JAVA_EXEFILE_PATH);
346 ret = WinExec(cmd, SW_SHOW);
351 INFO("child return value : %d\n", ret);
357 typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
358 LPFN_ISWOW64PROCESS fnIsWow64Process;
364 //IsWow64Process is not available on all supported versions of Windows.
365 //Use GetModuleHandle to get a handle to the DLL that contains the function
366 //and GetProcAddress to get a pointer to the function if available.
368 fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
369 GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
371 if(NULL != fnIsWow64Process)
373 if (!fnIsWow64Process(GetCurrentProcess(),&result))
376 INFO("Can not find 'IsWow64Process'\n");
382 int get_java_path(char** java_path)
386 //char strJavaRuntimePath[JAVA_MAX_COMMAND_LENGTH] = {0};
387 char strChoosenName[JAVA_MAX_COMMAND_LENGTH] = {0};
388 char strSubKeyName[JAVA_MAX_COMMAND_LENGTH] = {0};
389 char strJavaHome[JAVA_MAX_COMMAND_LENGTH] = {0};
391 DWORD dwSubKeyNameMax = JAVA_MAX_COMMAND_LENGTH;
392 DWORD dwBufLen = JAVA_MAX_COMMAND_LENGTH;
394 RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\JavaSoft\\Java Runtime Environment", 0,
395 KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | MY_KEY_WOW64_64KEY, &hKey);
396 RegEnumKeyEx(hKey, 0, (LPSTR)strSubKeyName, &dwSubKeyNameMax, NULL, NULL, NULL, NULL);
397 strcpy(strChoosenName, strSubKeyName);
400 while (ERROR_SUCCESS == RegEnumKeyEx(hKey, index, (LPSTR)strSubKeyName, &dwSubKeyNameMax,
401 NULL, NULL, NULL, NULL)) {
402 if (strcmp(strChoosenName, strSubKeyName) < 0) {
403 strcpy(strChoosenName, strSubKeyName);
408 RegOpenKeyEx(hKey, strChoosenName, 0, KEY_QUERY_VALUE | MY_KEY_WOW64_64KEY, &hKeyNew);
409 RegQueryValueEx(hKeyNew, "JavaHome", NULL, NULL, (LPBYTE)strJavaHome, &dwBufLen);
411 if (strJavaHome[0] != '\0') {
412 sprintf(*java_path, "\"%s\\bin\\java\"", strJavaHome);
413 //strcpy(*java_path, strJavaHome);
414 //strcat(*java_path, "\\bin\\java");