Rotate files while the service is running.
[nssm.git] / gui.cpp
diff --git a/gui.cpp b/gui.cpp
index 7697e26..c09b363 100644 (file)
--- a/gui.cpp
+++ b/gui.cpp
@@ -67,6 +67,7 @@ int nssm_gui(int resource, nssm_service_t *service) {
 \r
     /* Set existing details. */\r
     HWND combo;\r
+    HWND list;\r
 \r
     /* Application tab. */\r
     if (service->native) SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->image);\r
@@ -101,6 +102,21 @@ int nssm_gui(int resource, nssm_service_t *service) {
       SendMessage(combo, CB_SETCURSEL, priority, 0);\r
     }\r
 \r
+    if (service->affinity) {\r
+      list = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY);\r
+      SendDlgItemMessage(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY_ALL, BM_SETCHECK, BST_UNCHECKED, 0);\r
+      EnableWindow(GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY), 1);\r
+\r
+      DWORD_PTR affinity, system_affinity;\r
+      if (GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) {\r
+        if ((service->affinity & (__int64) system_affinity) != service->affinity) popup_message(dlg, MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_AFFINITY);\r
+      }\r
+\r
+      for (int i = 0; i < num_cpus(); i++) {\r
+        if (! (service->affinity & (1LL << (__int64) i))) SendMessage(list, LB_SETSEL, 0, i);\r
+      }\r
+    }\r
+\r
     /* Shutdown tab. */\r
     if (! (service->stop_method & NSSM_STOP_METHOD_CONSOLE)) {\r
       SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_UNCHECKED, 0);\r
@@ -125,6 +141,7 @@ int nssm_gui(int resource, nssm_service_t *service) {
     SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, service->throttle_delay, 0);\r
     combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);\r
     SendMessage(combo, CB_SETCURSEL, service->default_exit_action, 0);\r
+    SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_RESTART_DELAY, service->restart_delay, 0);\r
 \r
     /* I/O tab. */\r
     SetDlgItemText(tablist[NSSM_TAB_IO], IDC_STDIN, service->stdin_path);\r
@@ -135,9 +152,11 @@ int nssm_gui(int resource, nssm_service_t *service) {
     if (service->stdout_disposition == CREATE_ALWAYS) SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_SETCHECK, BST_CHECKED, 0);\r
     if (service->rotate_files) {\r
       SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_SETCHECK, BST_CHECKED, 0);\r
+      EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE), 1);\r
       EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), 1);\r
       EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), 1);\r
     }\r
+    if (service->rotate_stdout_online || service->rotate_stderr_online) SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE, BM_SETCHECK, BST_CHECKED, 0);\r
     SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, service->rotate_seconds, 0);\r
     if (! service->rotate_bytes_high) SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, service->rotate_bytes_low, 0);\r
 \r
@@ -230,7 +249,12 @@ static inline void set_logon_enabled(unsigned char enabled) {
   EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), enabled);\r
 }\r
 \r
+static inline void set_affinity_enabled(unsigned char enabled) {\r
+  EnableWindow(GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY), enabled);\r
+}\r
+\r
 static inline void set_rotation_enabled(unsigned char enabled) {\r
+  EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE), enabled);\r
   EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), enabled);\r
   EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), enabled);\r
 }\r
@@ -438,6 +462,22 @@ int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service
   combo = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_PRIORITY);\r
   service->priority = priority_index_to_constant((unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0));\r
 \r
+  service->affinity = 0LL;\r
+  if (! (SendDlgItemMessage(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY_ALL, BM_GETCHECK, 0, 0) & BST_CHECKED)) {\r
+    HWND list = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY);\r
+    int selected = (int) SendMessage(list, LB_GETSELCOUNT, 0, 0);\r
+    int count = (int) SendMessage(list, LB_GETCOUNT, 0, 0);\r
+    if (! selected) {\r
+      popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_WARN_AFFINITY_NONE);\r
+      return 5;\r
+    }\r
+    else if (selected < count) {\r
+      for (int i = 0; i < count; i++) {\r
+        if (SendMessage(list, LB_GETSEL, i, 0)) service->affinity |= (1LL << (__int64) i);\r
+      }\r
+    }\r
+  }\r
+\r
   /* Get stop method stuff. */\r
   check_stop_method(service, NSSM_STOP_METHOD_CONSOLE, IDC_METHOD_CONSOLE);\r
   check_stop_method(service, NSSM_STOP_METHOD_WINDOW, IDC_METHOD_WINDOW);\r
@@ -452,6 +492,7 @@ int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service
   combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);\r
   service->default_exit_action = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);\r
   if (service->default_exit_action == CB_ERR) service->default_exit_action = 0;\r
+  check_number(tablist[NSSM_TAB_EXIT], IDC_RESTART_DELAY, &service->restart_delay);\r
 \r
   /* Get I/O stuff. */\r
   check_io(window, _T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN);\r
@@ -467,6 +508,7 @@ int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service
   /* Get rotation stuff. */\r
   if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {\r
     service->rotate_files = true;\r
+    if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE, BM_GETCHECK, 0, 0) & BST_CHECKED) service->rotate_stdout_online = service->rotate_stderr_online = true;
     check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds);\r
     check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low);\r
   }\r
@@ -766,6 +808,13 @@ INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
           set_logon_enabled(1);\r
           break;\r
 \r
+        /* Affinity. */\r
+        case IDC_AFFINITY_ALL:\r
+          if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 0;\r
+          else enabled = 1;\r
+          set_affinity_enabled(enabled);\r
+          break;\r
+\r
         /* Shutdown methods. */\r
         case IDC_METHOD_CONSOLE:\r
           set_timeout_enabled(LOWORD(w), IDC_KILL_CONSOLE);\r
@@ -832,6 +881,8 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
 \r
       HWND tabs;\r
       HWND combo;\r
+      HWND list;\r
+      int i, n;\r
       tabs = GetDlgItem(window, IDC_TAB1);\r
       if (! tabs) return 0;\r
 \r
@@ -901,6 +952,37 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       SendMessage(combo, CB_INSERTSTRING, NSSM_IDLE_PRIORITY, (LPARAM) message_string(NSSM_GUI_IDLE_PRIORITY_CLASS));\r
       SendMessage(combo, CB_SETCURSEL, NSSM_NORMAL_PRIORITY, 0);\r
 \r
+      list = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY);\r
+      n = num_cpus();\r
+      SendMessage(list, LB_SETCOLUMNWIDTH, 16, 0);\r
+      for (i = 0; i < n; i++) {\r
+        TCHAR buffer[3];\r
+        _sntprintf_s(buffer, _countof(buffer), _TRUNCATE, _T("%d"), i);\r
+        SendMessage(list, LB_ADDSTRING, 0, (LPARAM) buffer);\r
+      }\r
+\r
+      /*\r
+        Size to fit.\r
+        The box is high enough for four rows.  It is wide enough for eight\r
+        columns without scrolling.  With scrollbars it shrinks to two rows.\r
+        Note that the above only holds if we set the column width BEFORE
+        adding the strings.
+      */\r
+      if (n < 32) {\r
+        int columns = (n - 1) / 4;\r
+        RECT rect;\r
+        GetWindowRect(list, &rect);\r
+        int width = rect.right - rect.left;
+        width -= (7 - columns) * 16;\r
+        int height = rect.bottom - rect.top;\r
+        if (n < 4) height -= (int) SendMessage(list, LB_GETITEMHEIGHT, 0, 0) * (4 - n);
+        SetWindowPos(list, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOOWNERZORDER);\r
+      }\r
+      SendMessage(list, LB_SELITEMRANGE, 1, MAKELPARAM(0, n));\r
+\r
+      SendDlgItemMessage(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY_ALL, BM_SETCHECK, BST_CHECKED, 0);\r
+      set_affinity_enabled(0);\r
+\r
       /* Shutdown tab. */\r
       tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText);\r
@@ -932,6 +1014,7 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_REALLY, (LPARAM) message_string(NSSM_GUI_EXIT_REALLY));\r
       SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_UNCLEAN, (LPARAM) message_string(NSSM_GUI_EXIT_UNCLEAN));\r
       SendMessage(combo, CB_SETCURSEL, NSSM_EXIT_RESTART, 0);\r
+      SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_RESTART_DELAY, 0, 0);\r
 \r
       /* I/O tab. */\r
       tab.pszText = message_string(NSSM_GUI_TAB_IO);\r
@@ -948,6 +1031,7 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);\r
 \r
       /* Set defaults. */\r
+      SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE, BM_SETCHECK, BST_UNCHECKED, 0);\r
       SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0);\r
       SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0);\r
       set_rotation_enabled(0);\r