display: added blank guide image
[sdk/emulator/qemu.git] / tizen / src / osutil-win32.c
1 /* 
2  * Emulator
3  *
4  * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * SeokYeon Hwang <syeon.hwang@samsung.com>
8  * MunKyu Im <munkyu.im@samsung.com>
9  * GiWoong Kim <giwoong.kim@samsung.com>
10  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11  * HyunJun Son
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26  *
27  * Contributors:
28  * - S-Core Co., Ltd
29  *
30  */
31
32 /**
33   @file     osutil-win32.c
34   @brief    Collection of utilities for win32
35  */
36
37 #include <png.h>
38 #include "maru_common.h"
39 #include "osutil.h"
40 #include "emulator.h"
41 #include "debug_ch.h"
42 #include "maru_err_table.h"
43 #include "sdb.h"
44
45 #ifndef CONFIG_WIN32
46 #error
47 #endif
48
49 #include <windows.h>
50
51 MULTI_DEBUG_CHANNEL (emulator, osutil);
52
53 extern char tizen_target_img_path[];
54 extern int tizen_base_port;
55
56 static const char *pactempfile = ".autoproxy";
57
58 void check_vm_lock_os(void)
59 {
60     uint32_t port;
61     char *base_port = NULL;
62     char *pBuf;
63     HANDLE hMapFile;
64
65     for (port = 26100; port < 26200; port += 10) {
66         base_port = g_strdup_printf("%d", port);
67         hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port);
68         if (hMapFile == NULL) {
69             INFO("port %s is not used.\n", base_port);
70             continue;
71         } else {
72              pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50);
73             if (pBuf == NULL) {
74                 ERR("Could not map view of file (%d).\n", GetLastError());
75                 CloseHandle(hMapFile);
76             }
77
78             if (strcmp(pBuf, tizen_target_img_path) == 0) {
79                 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
80                     "Can not execute this VM.\n"
81                     "The same name is running now.");
82                 UnmapViewOfFile(pBuf);
83                 CloseHandle(hMapFile);
84                 free(base_port);
85                 exit(0);
86             } else {
87                 UnmapViewOfFile(pBuf);
88             }
89         }
90
91         CloseHandle(hMapFile);
92         free(base_port);
93     }
94 }
95
96 void make_vm_lock_os(void)
97 {
98     HANDLE hMapFile;
99     char *pBuf;
100     char *port_in_use;
101     char *shared_memory;
102
103     shared_memory = g_strdup_printf("%s", tizen_target_img_path);
104     port_in_use =  g_strdup_printf("%d", tizen_base_port);
105     hMapFile = CreateFileMapping(
106                  INVALID_HANDLE_VALUE, /* use paging file */
107                  NULL,                 /* default security */
108                  PAGE_READWRITE,       /* read/write access */
109                  0,                /* maximum object size (high-order DWORD) */
110                  50,               /* maximum object size (low-order DWORD) */
111                  port_in_use);         /* name of mapping object */
112     if (hMapFile == NULL) {
113         ERR("Could not create file mapping object (%d).\n", GetLastError());
114         return;
115     }
116     pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
117
118     if (pBuf == NULL) {
119         ERR("Could not map view of file (%d).\n", GetLastError());
120         CloseHandle(hMapFile);
121         return;
122     }
123
124     CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
125     free(port_in_use);
126     free(shared_memory);
127 }
128
129 void set_bin_path_os(gchar * exec_argv)
130 {
131     gchar link_path[PATH_MAX] = { 0, };
132     gchar *file_name = NULL;
133
134     if (!GetModuleFileName(NULL, link_path, PATH_MAX)) {
135         return;
136     }
137
138     file_name = g_strrstr(link_path, "\\");
139     g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
140
141     g_strlcat(bin_path, "\\", PATH_MAX);
142 }
143
144 void print_system_info_os(void)
145 {
146     INFO("* Windows\n");
147
148     INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
149
150     /* Retrieves information about the current os */
151     OSVERSIONINFO osvi;
152     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
153     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
154
155     if (GetVersionEx(&osvi)) {
156         INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, "
157             "PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion,
158             osvi.dwMinorVersion, osvi.dwBuildNumber,
159             osvi.dwPlatformId, osvi.szCSDVersion);
160     }
161
162     /* Retrieves information about the current system */
163     SYSTEM_INFO sysi;
164     ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
165
166     GetSystemInfo(&sysi);
167     INFO("* Processor type : %d, Number of processors : %d\n",
168             sysi.dwProcessorType, sysi.dwNumberOfProcessors);
169
170     MEMORYSTATUSEX memInfo;
171     memInfo.dwLength = sizeof(MEMORYSTATUSEX);
172     GlobalMemoryStatusEx(&memInfo);
173     INFO("* Total Ram : %llu kB, Free: %lld kB\n",
174             memInfo.ullTotalPhys / 1024, memInfo.ullAvailPhys / 1024);
175 }
176
177 static int get_auto_proxy(BYTE *url, char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
178 {
179     char type[MAXLEN];
180     char proxy[MAXLEN];
181     char line[MAXLEN];
182     FILE *fp_pacfile;
183     char *p = NULL;
184
185     INFO("pac address: %s\n", (char*)url);
186     download_url((char*)url);
187
188     fp_pacfile = fopen(pactempfile, "r");
189     if(fp_pacfile != NULL) {
190         while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
191             if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
192                 INFO("line found %s", line);
193                 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
194             }
195         }
196
197         if(g_str_has_prefix(type, DIRECT)) {
198             INFO("auto proxy is set to direct mode\n");
199             fclose(fp_pacfile);
200         }
201         else if(g_str_has_prefix(type, PROXY)) {
202             INFO("auto proxy is set to proxy mode\n");
203             INFO("type: %s, proxy: %s\n", type, proxy);
204             p = strtok(proxy, "\";");
205             if(p != NULL) {
206                 INFO("auto proxy to set: %s\n",p);
207                 strcpy(http_proxy, p);
208                 strcpy(https_proxy, p);
209                 strcpy(ftp_proxy, p);
210                 strcpy(socks_proxy, p);
211             }
212             fclose(fp_pacfile);
213         }
214         else
215         {
216             ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
217             fclose(fp_pacfile);
218         }
219     } 
220     else {
221         ERR("fail to get pacfile fp\n");
222         return -1;
223     }
224
225     remove(pactempfile);
226
227     return 0;
228 }
229
230 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
231 {
232     HKEY hKey;
233     int nRet;
234     LONG lRet;
235     BYTE *proxyenable, *proxyserver;
236     char *p;
237     char *real_proxy;
238     BYTE *url;
239
240     DWORD dwLength = 0;
241     nRet = RegOpenKeyEx(HKEY_CURRENT_USER,
242             "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
243             0, KEY_QUERY_VALUE, &hKey);
244     if (nRet != ERROR_SUCCESS) {
245         ERR("Failed to open registry from %s\n",
246                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
247         return 0;
248     }
249     //check auto proxy key exists
250     lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, NULL, &dwLength);
251     if (lRet != ERROR_SUCCESS && dwLength == 0) {
252         ERR("Failed to query value from %s\n",
253                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AutoConfigURL");
254     }
255     else {
256         //if exists
257         url = (char*)malloc(dwLength);
258         if (url == NULL) {
259             ERR( "Failed to allocate a buffer\n");
260         }
261         else {
262             memset(url, 0x00, dwLength);
263             lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, url, &dwLength);
264             if (lRet == ERROR_SUCCESS && dwLength != 0) {
265                 get_auto_proxy(url, http_proxy, https_proxy, ftp_proxy, socks_proxy);
266                 RegCloseKey(hKey);      
267                 return 0;
268             }
269         }
270     }
271     //check manual proxy key exists
272     lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
273     if (lRet != ERROR_SUCCESS && dwLength == 0) {
274         ERR(stderr, "Failed to query value from %s\n",
275                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
276         RegCloseKey(hKey);
277         return 0;
278     }
279     proxyenable = (BYTE*)malloc(dwLength);
280     if (proxyenable == NULL) {
281         ERR( "Failed to allocate a buffer\n");
282         RegCloseKey(hKey);
283         return 0;
284     }
285
286     lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, proxyenable, &dwLength);
287     if (lRet != ERROR_SUCCESS) {
288         free(proxyenable);
289         ERR("Failed to query value from %s\n",
290                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
291         RegCloseKey(hKey);
292         return 0;
293     }
294     if (*(char*)proxyenable == 0) {
295         free(proxyenable);
296         RegCloseKey(hKey);      
297         return 0;
298     }
299
300     dwLength = 0;
301     lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, NULL, &dwLength);
302     if (lRet != ERROR_SUCCESS && dwLength == 0) {
303         ERR("Failed to query value from from %s\n",
304                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
305         RegCloseKey(hKey);      
306         return 0;
307     }
308
309     proxyserver = (BYTE*)malloc(dwLength);
310     if (proxyserver == NULL) {
311         ERR( "Failed to allocate a buffer\n");
312         RegCloseKey(hKey);      
313         return 0;
314     }
315
316     memset(proxyserver, 0x00, dwLength);
317     lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, proxyserver, &dwLength);
318     if (lRet != ERROR_SUCCESS) {
319         free(proxyserver);
320         ERR( "Failed to query value from from %s\n",
321                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
322         RegCloseKey(hKey);
323         return 0;
324     }
325     
326     if((char*)proxyserver != NULL) {
327         INFO("proxy value: %s\n", (char*)proxyserver);
328         real_proxy = malloc(MAXLEN);
329         
330         for(p = strtok((char*)proxyserver, ";"); p; p = strtok(NULL, ";")){
331             if(strstr(p, HTTP_PROTOCOL)) {
332                 remove_string(p, real_proxy, HTTP_PROTOCOL);
333                 strcpy(http_proxy, real_proxy);
334             }
335             else if(strstr(p, HTTPS_PROTOCOL)) {
336                 remove_string(p, real_proxy, HTTPS_PROTOCOL);
337                 strcpy(https_proxy, real_proxy);
338             }
339             else if(strstr(p, FTP_PROTOCOL)) {
340                 remove_string(p, real_proxy, FTP_PROTOCOL);
341                 strcpy(ftp_proxy, real_proxy);
342             }
343             else if(strstr(p, SOCKS_PROTOCOL)) {
344                 remove_string(p, real_proxy, SOCKS_PROTOCOL);
345                 strcpy(socks_proxy, real_proxy);
346             }
347             else {
348                 INFO("all protocol uses the same proxy server: %s\n", p);
349                 strcpy(http_proxy, p);
350                 strcpy(https_proxy, p);
351                 strcpy(ftp_proxy, p);
352                 strcpy(socks_proxy, p);
353             }
354         }
355         free(real_proxy);
356     }
357     else {
358         INFO("proxy is null\n");
359         return 0;
360     }
361     RegCloseKey(hKey);
362 }