gui.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #include "nssm.h"
  2. static enum { NSSM_TAB_APPLICATION, NSSM_TAB_DETAILS, NSSM_TAB_SHUTDOWN, NSSM_TAB_EXIT, NSSM_TAB_IO, NSSM_TAB_ROTATION, NSSM_TAB_ENVIRONMENT, NSSM_NUM_TABS };
  3. static HWND tablist[NSSM_NUM_TABS];
  4. static int selected_tab;
  5. int nssm_gui(int resource, TCHAR *name) {
  6. /* Create window */
  7. HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg);
  8. if (! dlg) {
  9. popup_message(MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));
  10. return 1;
  11. }
  12. /* Display the window */
  13. centre_window(dlg);
  14. ShowWindow(dlg, SW_SHOW);
  15. /* Set service name if given */
  16. if (name) {
  17. SetDlgItemText(dlg, IDC_NAME, name);
  18. /* No point making user click remove if the name is already entered */
  19. if (resource == IDD_REMOVE) {
  20. HWND button = GetDlgItem(dlg, IDC_REMOVE);
  21. if (button) {
  22. SendMessage(button, WM_LBUTTONDOWN, 0, 0);
  23. SendMessage(button, WM_LBUTTONUP, 0, 0);
  24. }
  25. }
  26. }
  27. /* Go! */
  28. MSG message;
  29. while (GetMessage(&message, 0, 0, 0)) {
  30. if (IsDialogMessage(dlg, &message)) continue;
  31. TranslateMessage(&message);
  32. DispatchMessage(&message);
  33. }
  34. return (int) message.wParam;
  35. }
  36. void centre_window(HWND window) {
  37. HWND desktop;
  38. RECT size, desktop_size;
  39. unsigned long x, y;
  40. if (! window) return;
  41. /* Find window size */
  42. if (! GetWindowRect(window, &size)) return;
  43. /* Find desktop window */
  44. desktop = GetDesktopWindow();
  45. if (! desktop) return;
  46. /* Find desktop window size */
  47. if (! GetWindowRect(desktop, &desktop_size)) return;
  48. /* Centre window */
  49. x = (desktop_size.right - size.right) / 2;
  50. y = (desktop_size.bottom - size.bottom) / 2;
  51. MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);
  52. }
  53. static inline void check_stop_method(nssm_service_t *service, unsigned long method, unsigned long control) {
  54. if (SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], control, BM_GETCHECK, 0, 0) & BST_CHECKED) return;
  55. service->stop_method &= ~method;
  56. }
  57. static inline void check_method_timeout(HWND tab, unsigned long control, unsigned long *timeout) {
  58. BOOL translated;
  59. unsigned long configured = GetDlgItemInt(tab, control, &translated, 0);
  60. if (translated) *timeout = configured;
  61. }
  62. static inline void check_io(TCHAR *name, TCHAR *buffer, unsigned long len, unsigned long control) {
  63. if (! SendMessage(GetDlgItem(tablist[NSSM_TAB_IO], control), WM_GETTEXTLENGTH, 0, 0)) return;
  64. if (GetDlgItemText(tablist[NSSM_TAB_IO], control, buffer, (int) len)) return;
  65. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, name);
  66. ZeroMemory(buffer, len * sizeof(TCHAR));
  67. }
  68. /* Install the service. */
  69. int install(HWND window) {
  70. if (! window) return 1;
  71. nssm_service_t *service = alloc_nssm_service();
  72. if (service) {
  73. set_nssm_service_defaults(service);
  74. /* Get service name. */
  75. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  76. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  77. cleanup_nssm_service(service);
  78. return 2;
  79. }
  80. /* Get executable name */
  81. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe, _countof(service->exe))) {
  82. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);
  83. return 3;
  84. }
  85. /* Get startup directory. */
  86. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir, _countof(service->dir))) {
  87. _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);
  88. strip_basename(service->dir);
  89. }
  90. /* Get flags. */
  91. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
  92. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags, _countof(service->flags))) {
  93. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);
  94. return 4;
  95. }
  96. }
  97. /* Get details. */
  98. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME), WM_GETTEXTLENGTH, 0, 0)) {
  99. if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME, service->displayname, _countof(service->displayname))) {
  100. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DISPLAYNAME);
  101. return 5;
  102. }
  103. }
  104. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION), WM_GETTEXTLENGTH, 0, 0)) {
  105. if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION, service->description, _countof(service->description))) {
  106. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DESCRIPTION);
  107. return 5;
  108. }
  109. }
  110. HWND combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  111. service->startup = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  112. if (service->startup == CB_ERR) service->startup = 0;
  113. /* Get stop method stuff. */
  114. check_stop_method(service, NSSM_STOP_METHOD_CONSOLE, IDC_METHOD_CONSOLE);
  115. check_stop_method(service, NSSM_STOP_METHOD_WINDOW, IDC_METHOD_WINDOW);
  116. check_stop_method(service, NSSM_STOP_METHOD_THREADS, IDC_METHOD_THREADS);
  117. check_stop_method(service, NSSM_STOP_METHOD_TERMINATE, IDC_METHOD_TERMINATE);
  118. check_method_timeout(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, &service->kill_console_delay);
  119. check_method_timeout(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, &service->kill_window_delay);
  120. check_method_timeout(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, &service->kill_threads_delay);
  121. /* Get exit action stuff. */
  122. check_method_timeout(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, &service->throttle_delay);
  123. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  124. service->default_exit_action = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  125. if (service->default_exit_action == CB_ERR) service->default_exit_action = 0;
  126. /* Get I/O stuff. */
  127. check_io(_T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN);
  128. check_io(_T("stdout"), service->stdout_path, _countof(service->stdout_path), IDC_STDOUT);
  129. check_io(_T("stderr"), service->stderr_path, _countof(service->stderr_path), IDC_STDERR);
  130. /* Override stdout and/or stderr. */
  131. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  132. if (service->stdout_path[0]) service->stdout_disposition = CREATE_ALWAYS;
  133. if (service->stderr_path[0]) service->stderr_disposition = CREATE_ALWAYS;
  134. }
  135. /* Get rotation stuff. */
  136. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  137. service->rotate_files = true;
  138. }
  139. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS_ENABLED, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  140. check_method_timeout(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds);
  141. }
  142. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW_ENABLED, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  143. check_method_timeout(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low);
  144. }
  145. /* Get environment. */
  146. unsigned long envlen = (unsigned long) SendMessage(GetDlgItem(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT), WM_GETTEXTLENGTH, 0, 0);
  147. if (envlen) {
  148. TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (envlen + 2) * sizeof(TCHAR));
  149. if (! env) {
  150. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  151. cleanup_nssm_service(service);
  152. return 5;
  153. }
  154. if (! GetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, env, envlen + 1)) {
  155. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  156. HeapFree(GetProcessHeap(), 0, env);
  157. cleanup_nssm_service(service);
  158. return 5;
  159. }
  160. /* Strip CR and replace LF with NULL. */
  161. unsigned long newlen = 0;
  162. unsigned long i, j;
  163. for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) newlen++;
  164. /* Must end with two NULLs. */
  165. newlen += 2;
  166. TCHAR *newenv = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR));
  167. if (! newenv) {
  168. HeapFree(GetProcessHeap(), 0, env);
  169. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  170. cleanup_nssm_service(service);
  171. return 5;
  172. }
  173. for (i = 0, j = 0; i < envlen; i++) {
  174. if (env[i] == _T('\r')) continue;
  175. if (env[i] == _T('\n')) newenv[j] = _T('\0');
  176. else newenv[j] = env[i];
  177. j++;
  178. }
  179. HeapFree(GetProcessHeap(), 0, env);
  180. env = newenv;
  181. envlen = newlen;
  182. /* Test the environment is valid. */
  183. if (test_environment(env)) {
  184. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  185. HeapFree(GetProcessHeap(), 0, env);
  186. cleanup_nssm_service(service);
  187. return 5;
  188. }
  189. if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  190. service->env = env;
  191. service->envlen = envlen;
  192. }
  193. else {
  194. service->env_extra = env;
  195. service->env_extralen = envlen;
  196. }
  197. }
  198. }
  199. /* See if it works. */
  200. switch (install_service(service)) {
  201. case 1:
  202. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("install()"));
  203. cleanup_nssm_service(service);
  204. return 1;
  205. case 2:
  206. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  207. cleanup_nssm_service(service);
  208. return 2;
  209. case 3:
  210. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  211. cleanup_nssm_service(service);
  212. return 3;
  213. case 4:
  214. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  215. cleanup_nssm_service(service);
  216. return 4;
  217. case 5:
  218. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
  219. cleanup_nssm_service(service);
  220. return 5;
  221. case 6:
  222. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
  223. cleanup_nssm_service(service);
  224. return 6;
  225. }
  226. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
  227. cleanup_nssm_service(service);
  228. return 0;
  229. }
  230. /* Remove the service */
  231. int remove(HWND window) {
  232. if (! window) return 1;
  233. /* See if it works */
  234. nssm_service_t *service = alloc_nssm_service();
  235. if (service) {
  236. /* Get service name */
  237. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  238. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  239. cleanup_nssm_service(service);
  240. return 2;
  241. }
  242. /* Confirm */
  243. if (popup_message(MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, service->name) != IDYES) {
  244. cleanup_nssm_service(service);
  245. return 0;
  246. }
  247. }
  248. switch (remove_service(service)) {
  249. case 1:
  250. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("remove()"));
  251. cleanup_nssm_service(service);
  252. return 1;
  253. case 2:
  254. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  255. cleanup_nssm_service(service);
  256. return 2;
  257. case 3:
  258. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
  259. return 3;
  260. cleanup_nssm_service(service);
  261. case 4:
  262. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
  263. cleanup_nssm_service(service);
  264. return 4;
  265. }
  266. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
  267. cleanup_nssm_service(service);
  268. return 0;
  269. }
  270. static TCHAR *browse_filter(int message) {
  271. switch (message) {
  272. case NSSM_GUI_BROWSE_FILTER_APPLICATIONS: return _T("*.exe;*.bat;*.cmd");
  273. case NSSM_GUI_BROWSE_FILTER_DIRECTORIES: return _T(".");
  274. case NSSM_GUI_BROWSE_FILTER_ALL_FILES: /* Fall through. */
  275. default: return _T("*.*");
  276. }
  277. }
  278. UINT_PTR CALLBACK browse_hook(HWND dlg, UINT message, WPARAM w, LPARAM l) {
  279. switch (message) {
  280. case WM_INITDIALOG:
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. /* Browse for application */
  286. void browse(HWND window, TCHAR *current, unsigned long flags, ...) {
  287. if (! window) return;
  288. va_list arg;
  289. size_t bufsize = 256;
  290. size_t len = bufsize;
  291. int i;
  292. OPENFILENAME ofn;
  293. ZeroMemory(&ofn, sizeof(ofn));
  294. ofn.lStructSize = sizeof(ofn);
  295. ofn.lpstrFilter = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, bufsize * sizeof(TCHAR));
  296. /* XXX: Escaping nulls with FormatMessage is tricky */
  297. if (ofn.lpstrFilter) {
  298. ZeroMemory((void *) ofn.lpstrFilter, bufsize);
  299. len = 0;
  300. /* "Applications" + NULL + "*.exe" + NULL */
  301. va_start(arg, flags);
  302. while (i = va_arg(arg, int)) {
  303. TCHAR *localised = message_string(i);
  304. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize, _TRUNCATE, localised);
  305. len += _tcslen(localised) + 1;
  306. LocalFree(localised);
  307. TCHAR *filter = browse_filter(i);
  308. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, _T("%s"), filter);
  309. len += _tcslen(filter) + 1;
  310. }
  311. va_end(arg);
  312. /* Remainder of the buffer is already zeroed */
  313. }
  314. ofn.lpstrFile = new TCHAR[MAX_PATH];
  315. if (flags & OFN_NOVALIDATE) {
  316. /* Directory hack. */
  317. _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T(":%s:"), message_string(NSSM_GUI_BROWSE_FILTER_DIRECTORIES));
  318. }
  319. else _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T("%s"), current);
  320. ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
  321. ofn.nMaxFile = MAX_PATH;
  322. ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | flags;
  323. if (GetOpenFileName(&ofn)) {
  324. /* Directory hack. */
  325. if (flags & OFN_NOVALIDATE) strip_basename(ofn.lpstrFile);
  326. SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
  327. }
  328. if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
  329. delete[] ofn.lpstrFile;
  330. }
  331. INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
  332. switch (message) {
  333. case WM_INITDIALOG:
  334. return 1;
  335. /* Button was pressed or control was controlled. */
  336. case WM_COMMAND:
  337. HWND dlg;
  338. TCHAR buffer[MAX_PATH];
  339. unsigned long state;
  340. switch (LOWORD(w)) {
  341. /* Browse for application. */
  342. case IDC_BROWSE:
  343. dlg = GetDlgItem(tab, IDC_PATH);
  344. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  345. browse(dlg, buffer, OFN_FILEMUSTEXIST, NSSM_GUI_BROWSE_FILTER_APPLICATIONS, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  346. /* Fill in startup directory if it wasn't already specified. */
  347. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  348. if (! buffer[0]) {
  349. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  350. strip_basename(buffer);
  351. SetDlgItemText(tab, IDC_DIR, buffer);
  352. }
  353. break;
  354. /* Browse for startup directory. */
  355. case IDC_BROWSE_DIR:
  356. dlg = GetDlgItem(tab, IDC_DIR);
  357. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  358. browse(dlg, buffer, OFN_NOVALIDATE, NSSM_GUI_BROWSE_FILTER_DIRECTORIES, 0);
  359. break;
  360. /* Browse for stdin. */
  361. case IDC_BROWSE_STDIN:
  362. dlg = GetDlgItem(tab, IDC_STDIN);
  363. GetDlgItemText(tab, IDC_STDIN, buffer, _countof(buffer));
  364. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  365. break;
  366. /* Browse for stdout. */
  367. case IDC_BROWSE_STDOUT:
  368. dlg = GetDlgItem(tab, IDC_STDOUT);
  369. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  370. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  371. /* Fill in stderr if it wasn't already specified. */
  372. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  373. if (! buffer[0]) {
  374. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  375. SetDlgItemText(tab, IDC_STDERR, buffer);
  376. }
  377. break;
  378. /* Browse for stderr. */
  379. case IDC_BROWSE_STDERR:
  380. dlg = GetDlgItem(tab, IDC_STDERR);
  381. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  382. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  383. break;
  384. /* Rotation. */
  385. case IDC_ROTATE:
  386. case IDC_ROTATE_SECONDS_ENABLED:
  387. case IDC_ROTATE_BYTES_LOW_ENABLED:
  388. if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) state = BST_CHECKED;
  389. else state = BST_UNCHECKED;
  390. SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_SETCHECK, state, 0);
  391. SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS_ENABLED, BM_SETCHECK, state, 0);
  392. SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW_ENABLED, BM_SETCHECK, state, 0);
  393. break;
  394. }
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. /* Install/remove dialogue callback */
  400. INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
  401. switch (message) {
  402. /* Creating the dialogue */
  403. case WM_INITDIALOG:
  404. SetFocus(GetDlgItem(window, IDC_NAME));
  405. HWND tabs;
  406. HWND combo;
  407. tabs = GetDlgItem(window, IDC_TAB1);
  408. if (! tabs) return 0;
  409. /* Set up tabs. */
  410. TCITEM tab;
  411. ZeroMemory(&tab, sizeof(tab));
  412. tab.mask = TCIF_TEXT;
  413. /* Application tab. */
  414. tab.pszText = message_string(NSSM_GUI_TAB_APPLICATION);
  415. tab.cchTextMax = (int) _tcslen(tab.pszText);
  416. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab);
  417. tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);
  418. ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW);
  419. /* Details tab. */
  420. tab.pszText = message_string(NSSM_GUI_TAB_DETAILS);
  421. tab.cchTextMax = (int) _tcslen(tab.pszText);
  422. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab);
  423. tablist[NSSM_TAB_DETAILS] = CreateDialog(0, MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);
  424. ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE);
  425. /* Set defaults. */
  426. combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  427. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_AUTOMATIC, (LPARAM) message_string(NSSM_GUI_STARTUP_AUTOMATIC));
  428. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DELAYED, (LPARAM) message_string(NSSM_GUI_STARTUP_DELAYED));
  429. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_MANUAL, (LPARAM) message_string(NSSM_GUI_STARTUP_MANUAL));
  430. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DISABLED, (LPARAM) message_string(NSSM_GUI_STARTUP_DISABLED));
  431. SendMessage(combo, CB_SETCURSEL, NSSM_STARTUP_AUTOMATIC, 0);
  432. /* Shutdown tab. */
  433. tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN);
  434. tab.cchTextMax = (int) _tcslen(tab.pszText);
  435. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab);
  436. tablist[NSSM_TAB_SHUTDOWN] = CreateDialog(0, MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);
  437. ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE);
  438. /* Set defaults. */
  439. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_CHECKED, 0);
  440. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, NSSM_KILL_CONSOLE_GRACE_PERIOD, 0);
  441. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_WINDOW, BM_SETCHECK, BST_CHECKED, 0);
  442. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, NSSM_KILL_WINDOW_GRACE_PERIOD, 0);
  443. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_THREADS, BM_SETCHECK, BST_CHECKED, 0);
  444. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, NSSM_KILL_THREADS_GRACE_PERIOD, 0);
  445. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_TERMINATE, BM_SETCHECK, BST_CHECKED, 0);
  446. /* Restart tab. */
  447. tab.pszText = message_string(NSSM_GUI_TAB_EXIT);
  448. tab.cchTextMax = (int) _tcslen(tab.pszText);
  449. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab);
  450. tablist[NSSM_TAB_EXIT] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);
  451. ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE);
  452. /* Set defaults. */
  453. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, NSSM_RESET_THROTTLE_RESTART, 0);
  454. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  455. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_RESTART, (LPARAM) message_string(NSSM_GUI_EXIT_RESTART));
  456. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_IGNORE, (LPARAM) message_string(NSSM_GUI_EXIT_IGNORE));
  457. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_REALLY, (LPARAM) message_string(NSSM_GUI_EXIT_REALLY));
  458. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_UNCLEAN, (LPARAM) message_string(NSSM_GUI_EXIT_UNCLEAN));
  459. SendMessage(combo, CB_SETCURSEL, NSSM_EXIT_RESTART, 0);
  460. /* I/O tab. */
  461. tab.pszText = message_string(NSSM_GUI_TAB_IO);
  462. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  463. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab);
  464. tablist[NSSM_TAB_IO] = CreateDialog(0, MAKEINTRESOURCE(IDD_IO), window, tab_dlg);
  465. ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE);
  466. /* Rotation tab. */
  467. tab.pszText = message_string(NSSM_GUI_TAB_ROTATION);
  468. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  469. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab);
  470. tablist[NSSM_TAB_ROTATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);
  471. ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);
  472. /* Set defaults. */
  473. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0);
  474. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0);
  475. /* Environment tab. */
  476. tab.pszText = message_string(NSSM_GUI_TAB_ENVIRONMENT);
  477. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  478. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab);
  479. tablist[NSSM_TAB_ENVIRONMENT] = CreateDialog(0, MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);
  480. ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);
  481. selected_tab = 0;
  482. return 1;
  483. /* Tab change. */
  484. case WM_NOTIFY:
  485. NMHDR *notification;
  486. notification = (NMHDR *) l;
  487. switch (notification->code) {
  488. case TCN_SELCHANGE:
  489. HWND tabs;
  490. int selection;
  491. tabs = GetDlgItem(window, IDC_TAB1);
  492. if (! tabs) return 0;
  493. selection = (int) SendMessage(tabs, TCM_GETCURSEL, 0, 0);
  494. if (selection != selected_tab) {
  495. ShowWindow(tablist[selected_tab], SW_HIDE);
  496. /*
  497. XXX: Sets focus to the service name which isn't ideal but is
  498. better than leaving it in another tab.
  499. */
  500. ShowWindow(tablist[selection], SW_SHOWDEFAULT);
  501. SetFocus(tablist[selection]);
  502. selected_tab = selection;
  503. }
  504. return 1;
  505. }
  506. return 0;
  507. /* Button was pressed or control was controlled */
  508. case WM_COMMAND:
  509. switch (LOWORD(w)) {
  510. /* OK button */
  511. case IDOK:
  512. if (! install(window)) PostQuitMessage(0);
  513. break;
  514. /* Cancel button */
  515. case IDCANCEL:
  516. DestroyWindow(window);
  517. break;
  518. /* Remove button */
  519. case IDC_REMOVE:
  520. if (! remove(window)) PostQuitMessage(0);
  521. break;
  522. }
  523. return 1;
  524. /* Window closing */
  525. case WM_CLOSE:
  526. DestroyWindow(window);
  527. return 0;
  528. case WM_DESTROY:
  529. PostQuitMessage(0);
  530. }
  531. return 0;
  532. }