gui.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "nssm.h"
  2. static void strip_basename(char *buffer) {
  3. size_t len = strlen(buffer);
  4. size_t i;
  5. for (i = len; i && buffer[i] != '\\' && buffer[i] != '/'; i--);
  6. /* X:\ is OK. */
  7. if (i && buffer[i-1] == ':') i++;
  8. buffer[i] = '\0';
  9. }
  10. int nssm_gui(int resource, char *name) {
  11. /* Create window */
  12. HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg);
  13. if (! dlg) {
  14. popup_message(MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));
  15. return 1;
  16. }
  17. /* Display the window */
  18. centre_window(dlg);
  19. ShowWindow(dlg, SW_SHOW);
  20. /* Set service name if given */
  21. if (name) {
  22. SetDlgItemText(dlg, IDC_NAME, name);
  23. /* No point making user click remove if the name is already entered */
  24. if (resource == IDD_REMOVE) {
  25. HWND button = GetDlgItem(dlg, IDC_REMOVE);
  26. if (button) {
  27. SendMessage(button, WM_LBUTTONDOWN, 0, 0);
  28. SendMessage(button, WM_LBUTTONUP, 0, 0);
  29. }
  30. }
  31. }
  32. /* Go! */
  33. MSG message;
  34. while (GetMessage(&message, 0, 0, 0)) {
  35. if (IsDialogMessage(dlg, &message)) continue;
  36. TranslateMessage(&message);
  37. DispatchMessage(&message);
  38. }
  39. return (int) message.wParam;
  40. }
  41. void centre_window(HWND window) {
  42. HWND desktop;
  43. RECT size, desktop_size;
  44. unsigned long x, y;
  45. if (! window) return;
  46. /* Find window size */
  47. if (! GetWindowRect(window, &size)) return;
  48. /* Find desktop window */
  49. desktop = GetDesktopWindow();
  50. if (! desktop) return;
  51. /* Find desktop window size */
  52. if (! GetWindowRect(desktop, &desktop_size)) return;
  53. /* Centre window */
  54. x = (desktop_size.right - size.right) / 2;
  55. y = (desktop_size.bottom - size.bottom) / 2;
  56. MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);
  57. }
  58. /* Install the service */
  59. int install(HWND window) {
  60. if (! window) return 1;
  61. nssm_service_t *service = alloc_nssm_service();
  62. if (service) {
  63. /* Get service name. */
  64. if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) {
  65. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  66. cleanup_nssm_service(service);
  67. return 2;
  68. }
  69. /* Get executable name */
  70. if (! GetDlgItemText(window, IDC_PATH, service->exe, sizeof(service->exe))) {
  71. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);
  72. return 3;
  73. }
  74. /* Get flags */
  75. if (SendMessage(GetDlgItem(window, IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
  76. if (! GetDlgItemText(window, IDC_FLAGS, service->flags, sizeof(service->flags))) {
  77. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);
  78. return 4;
  79. }
  80. }
  81. memmove(service->dir, service->exe, strlen(service->exe));
  82. strip_basename(service->dir);
  83. }
  84. /* See if it works. */
  85. switch (install_service(service)) {
  86. case 1:
  87. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "service", "install()");
  88. cleanup_nssm_service(service);
  89. return 1;
  90. case 2:
  91. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  92. cleanup_nssm_service(service);
  93. return 2;
  94. case 3:
  95. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  96. cleanup_nssm_service(service);
  97. return 3;
  98. case 4:
  99. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  100. cleanup_nssm_service(service);
  101. return 4;
  102. case 5:
  103. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
  104. cleanup_nssm_service(service);
  105. return 5;
  106. case 6:
  107. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
  108. cleanup_nssm_service(service);
  109. return 6;
  110. }
  111. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
  112. cleanup_nssm_service(service);
  113. return 0;
  114. }
  115. /* Remove the service */
  116. int remove(HWND window) {
  117. if (! window) return 1;
  118. /* See if it works */
  119. nssm_service_t *service = alloc_nssm_service();
  120. if (service) {
  121. /* Get service name */
  122. if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) {
  123. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  124. cleanup_nssm_service(service);
  125. return 2;
  126. }
  127. /* Confirm */
  128. if (popup_message(MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, service->name) != IDYES) {
  129. cleanup_nssm_service(service);
  130. return 0;
  131. }
  132. }
  133. switch (remove_service(service)) {
  134. case 1:
  135. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "service", "remove()");
  136. cleanup_nssm_service(service);
  137. return 1;
  138. case 2:
  139. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  140. cleanup_nssm_service(service);
  141. return 2;
  142. case 3:
  143. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
  144. return 3;
  145. cleanup_nssm_service(service);
  146. case 4:
  147. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
  148. cleanup_nssm_service(service);
  149. return 4;
  150. }
  151. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
  152. cleanup_nssm_service(service);
  153. return 0;
  154. }
  155. /* Browse for application */
  156. void browse(HWND window) {
  157. if (! window) return;
  158. size_t bufsize = 256;
  159. size_t len = bufsize;
  160. OPENFILENAME ofn;
  161. ZeroMemory(&ofn, sizeof(ofn));
  162. ofn.lStructSize = sizeof(ofn);
  163. ofn.lpstrFilter = (char *) HeapAlloc(GetProcessHeap(), 0, bufsize);
  164. /* XXX: Escaping nulls with FormatMessage is tricky */
  165. if (ofn.lpstrFilter) {
  166. ZeroMemory((void *) ofn.lpstrFilter, bufsize);
  167. char *localised = message_string(NSSM_GUI_BROWSE_FILTER_APPLICATIONS);
  168. _snprintf_s((char *) ofn.lpstrFilter, bufsize, _TRUNCATE, localised);
  169. /* "Applications" + NULL + "*.exe" + NULL */
  170. len = strlen(localised) + 1;
  171. LocalFree(localised);
  172. _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.exe");
  173. /* "All files" + NULL + "*.*" + NULL */
  174. len += 6;
  175. localised = message_string(NSSM_GUI_BROWSE_FILTER_ALL_FILES);
  176. _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, localised);
  177. len += strlen(localised) + 1;
  178. LocalFree(localised);
  179. _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.*");
  180. /* Remainder of the buffer is already zeroed */
  181. }
  182. ofn.lpstrFile = new char[MAX_PATH];
  183. ofn.lpstrFile[0] = '\0';
  184. ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
  185. ofn.nMaxFile = MAX_PATH;
  186. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
  187. if (GetOpenFileName(&ofn)) {
  188. SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
  189. }
  190. if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
  191. delete[] ofn.lpstrFile;
  192. }
  193. /* Install/remove dialogue callback */
  194. INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
  195. switch (message) {
  196. /* Creating the dialogue */
  197. case WM_INITDIALOG:
  198. return 1;
  199. /* Button was pressed or control was controlled */
  200. case WM_COMMAND:
  201. switch (LOWORD(w)) {
  202. /* OK button */
  203. case IDC_OK:
  204. if (! install(window)) PostQuitMessage(0);
  205. break;
  206. /* Cancel button */
  207. case IDCANCEL:
  208. DestroyWindow(window);
  209. break;
  210. /* Browse button */
  211. case IDC_BROWSE:
  212. browse(GetDlgItem(window, IDC_PATH));
  213. break;
  214. /* Remove button */
  215. case IDC_REMOVE:
  216. if (! remove(window)) PostQuitMessage(0);
  217. break;
  218. }
  219. return 1;
  220. /* Window closing */
  221. case WM_CLOSE:
  222. DestroyWindow(window);
  223. return 0;
  224. case WM_DESTROY:
  225. PostQuitMessage(0);
  226. }
  227. return 0;
  228. }