]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/main_windows.c
7 #include <ventoy_define.h>
8 #include <ventoy_util.h>
9 #include <ventoy_json.h>
10 #include <ventoy_disk.h>
11 #include <ventoy_http.h>
13 char g_ventoy_dir
[MAX_PATH
];
15 static BOOL g_running
= FALSE
;
16 static HWND g_refresh_button
;
17 static HWND g_start_button
;
18 static HWND g_openlink_button
;
19 static HWND g_exit_button
;
20 static HWND g_ComboxHwnd
;
45 const WCHAR
*g_msg_cn
[MSGID_BUTT
] =
49 L
"ÇëÔÚ Ventoy Å̸ùĿ¼ÏÂÔËÐб¾³ÌÐò£¡£¨´æ·ÅISOÎļþµÄλÖã©",
50 L
"´´½¨ ventoy Ŀ¼ʧ°Ü£¬ÎÞ·¨¼ÌÐø£¡",
51 L
"ventoy Ŀ¼´æÔÚ£¬µ«ÊÇ´óСд²»Æ¥Å䣬ÇëÏȽ«ÆäÖØÃüÃû£¡",
52 L
"ÄÚ²¿´íÎ󣬳ÌÐò¼´½«Í˳ö£¡",
59 L
"Í£Ö¹ÔËÐкóä¯ÀÀÆ÷Ò³Ãæ½«»á¹Ø±Õ£¬ÊÇ·ñ¼ÌÐø£¿",
60 L
"µ±Ç°·þÎñÕýÔÚÔËÐУ¬ÊÇ·ñÍ˳ö£¿",
61 L
"ÇëÏȹرÕÕýÔÚÔËÐÐµÄ VentoyPlugson ³ÌÐò£¡",
62 L
"ventoy\\plugson.tar.xz Îļþ²»´æÔÚ£¬ÇëÔÚÕýÈ·µÄĿ¼ÏÂÔËÐУ¡",
64 const WCHAR
*g_msg_en
[MSGID_BUTT
] =
68 L
"Please run me at the root of Ventoy partition.",
69 L
"Failed to create ventoy directory!",
70 L
"ventoy directory case mismatch, please rename it first!",
71 L
"Internal error, the program will exit!",
78 L
"The browser page will close after stop, continue?",
79 L
"Service is running, continue?",
80 L
"Please close another running VentoyPlugson instance!",
81 L
"ventoy\\plugson.tar.xz does not exist, please run under the correct directory!",
84 const WCHAR
**g_msg_lang
= NULL
;
88 char g_log_file
[MAX_PATH
];
89 char g_cur_dir
[MAX_PATH
];
91 int ventoy_log_init(void);
92 void ventoy_log_exit(void);
94 static BOOL
OnDestroyDialog()
106 static void OpenURL(void)
110 const static char * Browsers
[] =
112 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
113 "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
114 "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
115 "C:\\Program Files\\Mozilla Firefox\\firefox.exe",
119 sprintf_s(url
, sizeof(url
), "http://%s:%s/index.html", g_sysinfo
.ip
, g_sysinfo
.port
);
121 for (i
= 0; Browsers
[i
] != NULL
; i
++)
123 if (ventoy_is_file_exist("%s", Browsers
[i
]))
125 ShellExecuteA(NULL
, "open", Browsers
[i
], url
, NULL
, SW_SHOW
);
130 ShellExecuteA(NULL
, "open", url
, NULL
, NULL
, SW_SHOW
);
134 static void FillCombox(HWND hWnd
)
138 const ventoy_disk
*list
= NULL
;
139 CHAR DeviceName
[256];
142 SendMessage(g_ComboxHwnd
, CB_RESETCONTENT
, 0, 0);
144 list
= ventoy_get_disk_list(&num
);
145 if (NULL
== list
|| num
<= 0)
150 for (i
= 0; i
< num
; i
++)
152 sprintf_s(DeviceName
, sizeof(DeviceName
),
155 list
[i
].cur_capacity
,
157 SendMessageA(g_ComboxHwnd
, CB_ADDSTRING
, 0, (LPARAM
)DeviceName
);
159 SendMessage(g_ComboxHwnd
, CB_SETCURSEL
, 0, 0);
163 static BOOL
InitDialog(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
167 g_ComboxHwnd
= GetDlgItem(hWnd
, IDC_COMBO1
);
168 g_refresh_button
= GetDlgItem(hWnd
, IDC_BUTTON1
);
169 g_start_button
= GetDlgItem(hWnd
, IDC_BUTTON2
);
170 g_openlink_button
= GetDlgItem(hWnd
, IDC_BUTTON3
);
171 g_exit_button
= GetDlgItem(hWnd
, IDC_BUTTON4
);
173 hIcon
= LoadIcon(g_hInst
, MAKEINTRESOURCE(IDI_ICON1
));
174 SendMessage(hWnd
, WM_SETICON
, ICON_BIG
, (LPARAM
)hIcon
);
175 SendMessage(hWnd
, WM_SETICON
, ICON_SMALL
, (LPARAM
)hIcon
);
177 SetWindowTextW(g_refresh_button
, g_msg_lang
[MSGID_BTN_REFRESH
]);
178 SetWindowTextW(g_start_button
, g_msg_lang
[MSGID_BTN_START
]);
179 SetWindowTextW(g_openlink_button
, g_msg_lang
[MSGID_BTN_LINK
]);
180 SetWindowTextW(g_exit_button
, g_msg_lang
[MSGID_BTN_EXIT
]);
182 EnableWindow(g_openlink_button
, FALSE
);
189 static void VentoyStopService()
194 static int VentoyStartService(int sel
)
199 char CurDir
[MAX_PATH
];
200 const ventoy_disk
*disk
= NULL
;
202 vlog("VentoyStartService ...\n");
204 disk
= ventoy_get_disk_node(sel
);
210 vlog("Start service at %C: %s %s ...\n", disk
->devname
[0], disk
->cur_model
, disk
->cur_ventoy_ver
);
212 g_cur_dir
[0] = disk
->devname
[0];
217 g_sysinfo
.pathcase
= disk
->pathcase
;
218 g_sysinfo
.cur_secureboot
= disk
->cur_secureboot
;
219 g_sysinfo
.cur_part_style
= disk
->cur_part_style
;
220 strlcpy(g_sysinfo
.cur_fsname
, disk
->cur_fsname
);
221 strlcpy(g_sysinfo
.cur_capacity
, disk
->cur_capacity
);
222 strlcpy(g_sysinfo
.cur_model
, disk
->cur_model
);
223 strlcpy(g_sysinfo
.cur_ventoy_ver
, disk
->cur_ventoy_ver
);
225 bRet
= SetCurrentDirectoryA(g_cur_dir
);
226 vlog("SetCurrentDirectoryA %u <%s>\n", bRet
, g_cur_dir
);
229 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
230 vlog("CurDir=<%s>\n", CurDir
);
232 if (strcmp(g_cur_dir
, CurDir
))
234 vlog("Failed to change current directory.");
239 if (ventoy_is_directory_exist("ventoy"))
241 if (g_sysinfo
.pathcase
)
243 vlog("ventoy directory already exist, check case sensitive.\n");
244 strlcpy(Path
, "ventoy");
246 rc
= ventoy_path_case(Path
, 0);
247 vlog("ventoy_path_case actual path is <%s> <count:%d>\n", Path
, rc
);
251 vlog("ventoy directory case mismatch, rename<%s>--><%s>\n", Path
, "ventoy");
252 if (MoveFileA(Path
, "ventoy"))
254 vlog("Rename <%s>--><%s> success\n", Path
, "ventoy");
258 vlog("Rename <%s>--><%s> failed %u\n", Path
, "ventoy", LASTERR
);
259 MessageBoxW(NULL
, g_msg_lang
[MSGID_RENAME_VENTOY
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
266 vlog("ventoy directory already exist, no need to check case sensitive.\n");
271 if (CreateDirectoryA("ventoy", NULL
))
273 vlog("Create ventoy directory success.\n");
277 vlog("Create ventoy directory failed %u.\n", LASTERR
);
278 MessageBoxW(NULL
, g_msg_lang
[MSGID_NEW_DIR_FAIL
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
283 return ventoy_http_start(g_sysinfo
.ip
, g_sysinfo
.port
);
286 INT_PTR CALLBACK
DialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
302 code
= ((LPNMHDR
)lParam
)->code
;
303 idFrom
= ((LPNMHDR
)lParam
)->idFrom
;
306 if (idFrom
== IDC_SYSLINK1
&& (NM_CLICK
== code
|| NM_RETURN
== code
))
314 NotifyCode
= HIWORD(wParam
);
315 CtrlID
= LOWORD(wParam
);
317 if (NotifyCode
== BN_CLICKED
)
319 if (CtrlID
== IDC_BUTTON1
)
329 else if (CtrlID
== IDC_BUTTON2
)
333 if (IDYES
== MessageBoxW(NULL
, g_msg_lang
[MSGID_BTN_STOP_TIP
], g_msg_lang
[MSGID_INFO
], MB_YESNO
| MB_ICONINFORMATION
))
338 SetWindowTextW(g_start_button
, g_msg_lang
[MSGID_BTN_START
]);
339 EnableWindow(g_ComboxHwnd
, TRUE
);
340 EnableWindow(g_refresh_button
, TRUE
);
341 EnableWindow(g_openlink_button
, FALSE
);
346 nCurSel
= (int)SendMessage(g_ComboxHwnd
, CB_GETCURSEL
, 0, 0);
347 if (CB_ERR
!= nCurSel
)
349 rc
= VentoyStartService(nCurSel
);
352 vlog("Ventoy failed to start http server, check %s for detail\n", g_log_file
);
353 MessageBoxW(NULL
, g_msg_lang
[MSGID_INTERNAL_ERR
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
358 SetWindowTextW(g_start_button
, g_msg_lang
[MSGID_BTN_STOP
]);
360 EnableWindow(g_ComboxHwnd
, FALSE
);
361 EnableWindow(g_refresh_button
, FALSE
);
362 EnableWindow(g_openlink_button
, TRUE
);
369 else if (CtrlID
== IDC_BUTTON3
)
376 else if (CtrlID
== IDC_BUTTON4
)
380 if (IDYES
!= MessageBoxW(NULL
, g_msg_lang
[MSGID_BTN_EXIT_TIP
], g_msg_lang
[MSGID_INFO
], MB_YESNO
| MB_ICONINFORMATION
))
396 InitDialog(hWnd
, wParam
, lParam
);
403 if (IDYES
!= MessageBoxW(NULL
, g_msg_lang
[MSGID_BTN_EXIT_TIP
], g_msg_lang
[MSGID_INFO
], MB_YESNO
| MB_ICONINFORMATION
))
419 static int ParseCmdLine(LPSTR lpCmdLine
, char *ip
, char *port
)
431 pos
= strstr(lpCmdLine
, "-H");
434 pos
= strstr(lpCmdLine
, "-h");
440 while (*pos
== ' ' || *pos
== '\t')
445 while (isdigit(*pos
) || *pos
== '.')
452 pos
= strstr(lpCmdLine
, "-P");
455 pos
= strstr(lpCmdLine
, "-p");
460 portnum
= (int)strtol(pos
+ 3, NULL
, 10);
461 sprintf_s(port
, 16, "%d", portnum
);
467 int APIENTRY
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, INT nCmdShow
)
471 WCHAR CurDir
[MAX_PATH
];
473 UNREFERENCED_PARAMETER(hPrevInstance
);
475 if (GetUserDefaultUILanguage() == 0x0804)
477 g_sysinfo
.language
= LANGUAGE_CN
;
478 g_msg_lang
= g_msg_cn
;
482 g_sysinfo
.language
= LANGUAGE_EN
;
483 g_msg_lang
= g_msg_en
;
486 hMutex
= CreateMutexA(NULL
, TRUE
, "PlugsonMUTEX");
487 if ((hMutex
!= NULL
) && (GetLastError() == ERROR_ALREADY_EXISTS
))
489 MessageBoxW(NULL
, g_msg_lang
[MSGID_RUNNING_TIP
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
493 GetCurrentDirectoryW(MAX_PATH
, CurDir
);
494 WideCharToMultiByte(CP_UTF8
, 0, CurDir
, -1, g_cur_dir
, MAX_PATH
, NULL
, 0);
496 sprintf_s(g_ventoy_dir
, sizeof(g_ventoy_dir
), "%s", g_cur_dir
);
497 sprintf_s(g_log_file
, sizeof(g_log_file
), "%s\\%s", g_cur_dir
, LOG_FILE
);
500 if (!ventoy_is_file_exist("%s\\ventoy\\%s", g_ventoy_dir
, PLUGSON_TXZ
))
502 MessageBoxW(NULL
, g_msg_lang
[MSGID_NO_TARXZ_TIP
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
506 ParseCmdLine(lpCmdLine
, g_sysinfo
.ip
, g_sysinfo
.port
);
507 if (g_sysinfo
.ip
[0] == 0)
509 strlcpy(g_sysinfo
.ip
, "127.0.0.1");
511 if (g_sysinfo
.port
[0] == 0)
513 strlcpy(g_sysinfo
.port
, "24681");
516 vlog("===============================================\n");
517 vlog("===== Ventoy Plugson %s:%s =====\n", g_sysinfo
.ip
, g_sysinfo
.port
);
518 vlog("===============================================\n");
523 rc
= ventoy_www_init();
526 vlog("Failed to init www\n");
527 MessageBoxW(NULL
, g_msg_lang
[MSGID_INTERNAL_ERR
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
537 DialogBoxA(hInstance
, MAKEINTRESOURCE(IDD_DIALOG1
), NULL
, DialogProc
);