]>
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 static BOOL g_running
= FALSE
;
14 static HWND g_refresh_button
;
15 static HWND g_start_button
;
16 static HWND g_openlink_button
;
17 static HWND g_exit_button
;
18 static HWND g_ComboxHwnd
;
42 const WCHAR
*g_msg_cn
[MSGID_BUTT
] =
46 L
"ÇëÔÚ Ventoy Å̸ùĿ¼ÏÂÔËÐб¾³ÌÐò£¡£¨´æ·ÅISOÎļþµÄλÖã©",
47 L
"´´½¨ ventoy Ŀ¼ʧ°Ü£¬ÎÞ·¨¼ÌÐø£¡",
48 L
"ventoy Ŀ¼´æÔÚ£¬µ«ÊÇ´óСд²»Æ¥Å䣬ÇëÏȽ«ÆäÖØÃüÃû£¡",
49 L
"ÄÚ²¿´íÎ󣬳ÌÐò¼´½«Í˳ö£¡",
56 L
"Í£Ö¹ÔËÐкóä¯ÀÀÆ÷Ò³Ãæ½«»á¹Ø±Õ£¬ÊÇ·ñ¼ÌÐø£¿",
57 L
"µ±Ç°·þÎñÕýÔÚÔËÐУ¬ÊÇ·ñÍ˳ö£¿",
58 L
"ÇëÏȹرÕÕýÔÚÔËÐÐµÄ VentoyPlugson ³ÌÐò£¡",
60 const WCHAR
*g_msg_en
[MSGID_BUTT
] =
64 L
"Please run me at the root of Ventoy partition.",
65 L
"Failed to create ventoy directory!",
66 L
"ventoy directory case mismatch, please rename it first!",
67 L
"Internal error, the program will exit!",
74 L
"The browser page will close after stop, continue?",
75 L
"Service is running, continue?",
76 L
"Please close another running VentoyPlugson instance!",
79 const WCHAR
**g_msg_lang
= NULL
;
83 char g_log_file
[MAX_PATH
];
84 char g_cur_dir
[MAX_PATH
];
86 int ventoy_log_init(void);
87 void ventoy_log_exit(void);
89 static BOOL
OnDestroyDialog()
101 static void OpenURL(void)
105 const static char * Browsers
[] =
107 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
108 "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
109 "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
110 "C:\\Program Files\\Mozilla Firefox\\firefox.exe",
114 sprintf_s(url
, sizeof(url
), "http://%s:%s/index.html", g_sysinfo
.ip
, g_sysinfo
.port
);
116 for (i
= 0; Browsers
[i
] != NULL
; i
++)
118 if (ventoy_is_file_exist("%s", Browsers
[i
]))
120 ShellExecuteA(NULL
, "open", Browsers
[i
], url
, NULL
, SW_SHOW
);
125 ShellExecuteA(NULL
, "open", url
, NULL
, NULL
, SW_SHOW
);
129 static void FillCombox(HWND hWnd
)
133 const ventoy_disk
*list
= NULL
;
134 CHAR DeviceName
[256];
137 SendMessage(g_ComboxHwnd
, CB_RESETCONTENT
, 0, 0);
139 list
= ventoy_get_disk_list(&num
);
140 if (NULL
== list
|| num
<= 0)
145 for (i
= 0; i
< num
; i
++)
147 sprintf_s(DeviceName
, sizeof(DeviceName
),
150 list
[i
].cur_capacity
,
152 SendMessageA(g_ComboxHwnd
, CB_ADDSTRING
, 0, (LPARAM
)DeviceName
);
154 SendMessage(g_ComboxHwnd
, CB_SETCURSEL
, 0, 0);
158 static BOOL
InitDialog(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
162 g_ComboxHwnd
= GetDlgItem(hWnd
, IDC_COMBO1
);
163 g_refresh_button
= GetDlgItem(hWnd
, IDC_BUTTON1
);
164 g_start_button
= GetDlgItem(hWnd
, IDC_BUTTON2
);
165 g_openlink_button
= GetDlgItem(hWnd
, IDC_BUTTON3
);
166 g_exit_button
= GetDlgItem(hWnd
, IDC_BUTTON4
);
168 hIcon
= LoadIcon(g_hInst
, MAKEINTRESOURCE(IDI_ICON1
));
169 SendMessage(hWnd
, WM_SETICON
, ICON_BIG
, (LPARAM
)hIcon
);
170 SendMessage(hWnd
, WM_SETICON
, ICON_SMALL
, (LPARAM
)hIcon
);
172 SetWindowTextW(g_refresh_button
, g_msg_lang
[MSGID_BTN_REFRESH
]);
173 SetWindowTextW(g_start_button
, g_msg_lang
[MSGID_BTN_START
]);
174 SetWindowTextW(g_openlink_button
, g_msg_lang
[MSGID_BTN_LINK
]);
175 SetWindowTextW(g_exit_button
, g_msg_lang
[MSGID_BTN_EXIT
]);
177 EnableWindow(g_openlink_button
, FALSE
);
184 static void VentoyStopService()
189 static int VentoyStartService(int sel
)
194 char CurDir
[MAX_PATH
];
195 const ventoy_disk
*disk
= NULL
;
197 vlog("VentoyStartService ...\n");
199 disk
= ventoy_get_disk_node(sel
);
205 vlog("Start service at %C: %s %s ...\n", disk
->devname
[0], disk
->cur_model
, disk
->cur_ventoy_ver
);
207 g_cur_dir
[0] = disk
->devname
[0];
212 g_sysinfo
.pathcase
= disk
->pathcase
;
213 g_sysinfo
.cur_secureboot
= disk
->cur_secureboot
;
214 g_sysinfo
.cur_part_style
= disk
->cur_part_style
;
215 strlcpy(g_sysinfo
.cur_fsname
, disk
->cur_fsname
);
216 strlcpy(g_sysinfo
.cur_capacity
, disk
->cur_capacity
);
217 strlcpy(g_sysinfo
.cur_model
, disk
->cur_model
);
218 strlcpy(g_sysinfo
.cur_ventoy_ver
, disk
->cur_ventoy_ver
);
220 bRet
= SetCurrentDirectoryA(g_cur_dir
);
221 vlog("SetCurrentDirectoryA %u <%s>\n", bRet
, g_cur_dir
);
224 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
225 vlog("CurDir=<%s>\n", CurDir
);
227 if (strcmp(g_cur_dir
, CurDir
))
229 vlog("Failed to change current directory.");
234 if (ventoy_is_directory_exist("ventoy"))
236 if (g_sysinfo
.pathcase
)
238 vlog("ventoy directory already exist, check case sensitive.\n");
239 strlcpy(Path
, "ventoy");
241 rc
= ventoy_path_case(Path
, 0);
242 vlog("ventoy_path_case actual path is <%s> <count:%d>\n", Path
, rc
);
246 vlog("ventoy directory case mismatch, rename<%s>--><%s>\n", Path
, "ventoy");
247 if (MoveFileA(Path
, "ventoy"))
249 vlog("Rename <%s>--><%s> success\n", Path
, "ventoy");
253 vlog("Rename <%s>--><%s> failed %u\n", Path
, "ventoy", LASTERR
);
254 MessageBoxW(NULL
, g_msg_lang
[MSGID_RENAME_VENTOY
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
261 vlog("ventoy directory already exist, no need to check case sensitive.\n");
266 if (CreateDirectoryA("ventoy", NULL
))
268 vlog("Create ventoy directory success.\n");
272 vlog("Create ventoy directory failed %u.\n", LASTERR
);
273 MessageBoxW(NULL
, g_msg_lang
[MSGID_NEW_DIR_FAIL
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
278 return ventoy_http_start(g_sysinfo
.ip
, g_sysinfo
.port
);
281 INT_PTR CALLBACK
DialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
297 code
= ((LPNMHDR
)lParam
)->code
;
298 idFrom
= ((LPNMHDR
)lParam
)->idFrom
;
301 if (idFrom
== IDC_SYSLINK1
&& (NM_CLICK
== code
|| NM_RETURN
== code
))
309 NotifyCode
= HIWORD(wParam
);
310 CtrlID
= LOWORD(wParam
);
312 if (NotifyCode
== BN_CLICKED
)
314 if (CtrlID
== IDC_BUTTON1
)
324 else if (CtrlID
== IDC_BUTTON2
)
328 if (IDYES
== MessageBoxW(NULL
, g_msg_lang
[MSGID_BTN_STOP_TIP
], g_msg_lang
[MSGID_INFO
], MB_YESNO
| MB_ICONINFORMATION
))
333 SetWindowTextW(g_start_button
, g_msg_lang
[MSGID_BTN_START
]);
334 EnableWindow(g_ComboxHwnd
, TRUE
);
335 EnableWindow(g_refresh_button
, TRUE
);
336 EnableWindow(g_openlink_button
, FALSE
);
341 nCurSel
= (int)SendMessage(g_ComboxHwnd
, CB_GETCURSEL
, 0, 0);
342 if (CB_ERR
!= nCurSel
)
344 rc
= VentoyStartService(nCurSel
);
347 vlog("Ventoy failed to start http server, check %s for detail\n", g_log_file
);
348 MessageBoxW(NULL
, g_msg_lang
[MSGID_INTERNAL_ERR
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
353 SetWindowTextW(g_start_button
, g_msg_lang
[MSGID_BTN_STOP
]);
355 EnableWindow(g_ComboxHwnd
, FALSE
);
356 EnableWindow(g_refresh_button
, FALSE
);
357 EnableWindow(g_openlink_button
, TRUE
);
364 else if (CtrlID
== IDC_BUTTON3
)
371 else if (CtrlID
== IDC_BUTTON4
)
375 if (IDYES
!= MessageBoxW(NULL
, g_msg_lang
[MSGID_BTN_EXIT_TIP
], g_msg_lang
[MSGID_INFO
], MB_YESNO
| MB_ICONINFORMATION
))
391 InitDialog(hWnd
, wParam
, lParam
);
398 if (IDYES
!= MessageBoxW(NULL
, g_msg_lang
[MSGID_BTN_EXIT_TIP
], g_msg_lang
[MSGID_INFO
], MB_YESNO
| MB_ICONINFORMATION
))
414 static int ParseCmdLine(LPSTR lpCmdLine
, char *ip
, char *port
)
426 pos
= strstr(lpCmdLine
, "-H");
429 pos
= strstr(lpCmdLine
, "-h");
435 while (*pos
== ' ' || *pos
== '\t')
440 while (isdigit(*pos
) || *pos
== '.')
447 pos
= strstr(lpCmdLine
, "-P");
450 pos
= strstr(lpCmdLine
, "-p");
455 portnum
= (int)strtol(pos
+ 3, NULL
, 10);
456 sprintf_s(port
, 16, "%d", portnum
);
462 int APIENTRY
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, INT nCmdShow
)
467 UNREFERENCED_PARAMETER(hPrevInstance
);
469 if (GetUserDefaultUILanguage() == 0x0804)
471 g_sysinfo
.language
= LANGUAGE_CN
;
472 g_msg_lang
= g_msg_cn
;
476 g_sysinfo
.language
= LANGUAGE_EN
;
477 g_msg_lang
= g_msg_en
;
480 hMutex
= CreateMutexA(NULL
, TRUE
, "PlugsonMUTEX");
481 if ((hMutex
!= NULL
) && (GetLastError() == ERROR_ALREADY_EXISTS
))
483 MessageBoxW(NULL
, g_msg_lang
[MSGID_RUNNING_TIP
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
487 GetCurrentDirectoryA(MAX_PATH
, g_cur_dir
);
488 sprintf_s(g_log_file
, sizeof(g_log_file
), "%s\\%s", g_cur_dir
, LOG_FILE
);
492 ParseCmdLine(lpCmdLine
, g_sysinfo
.ip
, g_sysinfo
.port
);
493 if (g_sysinfo
.ip
[0] == 0)
495 strlcpy(g_sysinfo
.ip
, "127.0.0.1");
497 if (g_sysinfo
.port
[0] == 0)
499 strlcpy(g_sysinfo
.port
, "24681");
502 vlog("===============================================\n");
503 vlog("===== Ventoy Plugson %s:%s =====\n", g_sysinfo
.ip
, g_sysinfo
.port
);
504 vlog("===============================================\n");
509 rc
= ventoy_www_init();
512 vlog("Failed to init www\n");
513 MessageBoxW(NULL
, g_msg_lang
[MSGID_INTERNAL_ERR
], g_msg_lang
[MSGID_ERROR
], MB_OK
| MB_ICONERROR
);
523 DialogBoxA(hInstance
, MAKEINTRESOURCE(IDD_DIALOG1
), NULL
, DialogProc
);