gui.cpp 24 KB

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