From 74d4b8e0b92cd9dc2f7055cb7296ce441c15269f Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 9 Mar 2016 14:41:14 -0500 Subject: [PATCH 1/4] invoke win32app functions from main --- Daemon.h | 11 +- DaemonWin32.cpp | 62 +++-------- Makefile | 4 +- Win32/Win32App.cpp | 267 +++++++++++++++++++++++---------------------- Win32/Win32App.h | 9 ++ 5 files changed, 167 insertions(+), 186 deletions(-) diff --git a/Daemon.h b/Daemon.h index 4f31b2dc..efbd5df4 100644 --- a/Daemon.h +++ b/Daemon.h @@ -24,14 +24,14 @@ namespace i2p bool isLogging; bool isDaemon; - + bool running; protected: Daemon_Singleton(); virtual ~Daemon_Singleton(); - bool IsService () const; + bool IsService () const; // d-pointer for httpServer, httpProxy, etc. class Daemon_Singleton_Private; @@ -48,9 +48,10 @@ namespace i2p return instance; } - virtual bool init(int argc, char* argv[]); - virtual bool start(); - virtual bool stop(); + bool init(int argc, char* argv[]); + bool start(); + bool stop(); + void run (); }; #else class DaemonLinux : public Daemon_Singleton diff --git a/DaemonWin32.cpp b/DaemonWin32.cpp index bf5f938c..e5de9c9a 100644 --- a/DaemonWin32.cpp +++ b/DaemonWin32.cpp @@ -5,7 +5,7 @@ #ifdef _WIN32 -#include "./Win32/Win32Service.h" +#include "Win32/Win32App.h" namespace i2p { @@ -16,61 +16,21 @@ namespace i2p setlocale(LC_CTYPE, ""); SetConsoleCP(1251); SetConsoleOutputCP(1251); - setlocale(LC_ALL, "Russian"); + setlocale(LC_ALL, "Russian"); + return Daemon_Singleton::init(argc, argv); + } - if (!Daemon_Singleton::init(argc, argv)) return false; - if (I2PService::isService()) - isDaemon = 1; - else - isDaemon = 0; - - std::string serviceControl; i2p::config::GetOption("svcctl", serviceControl); - if (serviceControl == "install") - { - LogPrint(eLogInfo, "WinSVC: installing ", SERVICE_NAME, " as service"); - InstallService( - SERVICE_NAME, // Name of service - SERVICE_DISPLAY_NAME, // Name to display - SERVICE_START_TYPE, // Service start type - SERVICE_DEPENDENCIES, // Dependencies - SERVICE_ACCOUNT, // Service running account - SERVICE_PASSWORD // Password of the account - ); - exit(0); - } - else if (serviceControl == "remove") - { - LogPrint(eLogInfo, "WinSVC: uninstalling ", SERVICE_NAME, " service"); - UninstallService(SERVICE_NAME); - exit(0); - } - - if (isDaemon == 1) - { - LogPrint(eLogDebug, "Daemon: running as service"); - I2PService service(SERVICE_NAME); - if (!I2PService::Run(service)) - { - LogPrint(eLogError, "Daemon: Service failed to run w/err 0x%08lx\n", GetLastError()); - exit(EXIT_FAILURE); - } - exit(EXIT_SUCCESS); - } - else - LogPrint(eLogDebug, "Daemon: running as user"); - - return true; - } bool DaemonWin32::start() { setlocale(LC_CTYPE, ""); SetConsoleCP(1251); SetConsoleOutputCP(1251); setlocale(LC_ALL, "Russian"); + if (!i2p::win32::StartWin32App ()) return false; bool ret = Daemon_Singleton::start(); if (ret && IsLogToFile ()) - { + { // TODO: find out where this garbage to console comes from SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE); SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE); @@ -79,9 +39,15 @@ namespace i2p } bool DaemonWin32::stop() - { + { + i2p::win32::StopWin32App (); return Daemon_Singleton::stop(); - } + } + + void DaemonWin32::run () + { + i2p::win32::RunWin32App (); + } } } diff --git a/Makefile b/Makefile index 2a0baaba..8f1847f3 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ else ifeq ($(UNAME),Linux) DAEMON_SRC += DaemonLinux.cpp include Makefile.linux else # win32 mingw - DAEMON_SRC += DaemonWin32.cpp Win32/Win32Service.cpp Win32/Win32App.cpp + DAEMON_SRC += DaemonWin32.cpp Win32/Win32App.cpp include Makefile.mingw endif @@ -52,7 +52,7 @@ obj/%.o: %.cpp # '-' is 'ignore if missing' on first run -include $(DEPS) -DAEMON_OBJS += $(patsubst %.cpp,obj/%.o,$(DAEMON_SRC)) +DAEMON_OBJS += $(patsubst %.cpp,obj/%.o,$(DAEMON_SRC)) $(I2PD): $(DAEMON_OBJS) $(ARLIB) $(ARLIB_CLIENT) $(CXX) -o $@ $^ $(LDLIBS) $(LDFLAGS) diff --git a/Win32/Win32App.cpp b/Win32/Win32App.cpp index c356aec5..c997b266 100644 --- a/Win32/Win32App.cpp +++ b/Win32/Win32App.cpp @@ -11,147 +11,152 @@ #define ID_TRAY_ICON 2050 #define WM_TRAYICON (WM_USER + 1) -void ShowPopupMenu (HWND hWnd, POINT *curpos, int wDefaultItem) +namespace i2p { - HMENU hPopup = CreatePopupMenu(); - InsertMenu (hPopup, 0, MF_BYPOSITION | MF_STRING, ID_ABOUT, "About..."); - InsertMenu (hPopup, 1, MF_BYPOSITION | MF_STRING, ID_EXIT , "Exit"); - SetMenuDefaultItem (hPopup, ID_ABOUT, FALSE); - SetFocus (hWnd); - SendMessage (hWnd, WM_INITMENUPOPUP, (WPARAM)hPopup, 0); - - POINT p; - if (!curpos) +namespace win32 +{ + static void ShowPopupMenu (HWND hWnd, POINT *curpos, int wDefaultItem) { - GetCursorPos (&p); - curpos = &p; + HMENU hPopup = CreatePopupMenu(); + InsertMenu (hPopup, 0, MF_BYPOSITION | MF_STRING, ID_ABOUT, "About..."); + InsertMenu (hPopup, 1, MF_BYPOSITION | MF_STRING, ID_EXIT , "Exit"); + SetMenuDefaultItem (hPopup, ID_ABOUT, FALSE); + SetFocus (hWnd); + SendMessage (hWnd, WM_INITMENUPOPUP, (WPARAM)hPopup, 0); + + POINT p; + if (!curpos) + { + GetCursorPos (&p); + curpos = &p; + } + + WORD cmd = TrackPopupMenu (hPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, curpos->x, curpos->y, 0, hWnd, NULL); + SendMessage (hWnd, WM_COMMAND, cmd, 0); + + DestroyMenu(hPopup); } - WORD cmd = TrackPopupMenu (hPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, curpos->x, curpos->y, 0, hWnd, NULL); - SendMessage (hWnd, WM_COMMAND, cmd, 0); - - DestroyMenu(hPopup); -} - -void AddTrayIcon (HWND hWnd) -{ - NOTIFYICONDATA nid; - memset(&nid, 0, sizeof(nid)); - nid.cbSize = sizeof(nid); - nid.hWnd = hWnd; - nid.uID = ID_TRAY_ICON; - nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - nid.uCallbackMessage = WM_TRAYICON; - nid.hIcon = LoadIcon (GetModuleHandle(NULL), MAKEINTRESOURCE (IDI_ICON1)); - strcpy (nid.szTip, "i2pd"); - Shell_NotifyIcon(NIM_ADD, &nid ); -} - -void RemoveTrayIcon (HWND hWnd) -{ - NOTIFYICONDATA nid; - nid.hWnd = hWnd; - nid.uID = ID_TRAY_ICON; - Shell_NotifyIcon (NIM_DELETE, &nid); -} - -static LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -{ - switch (uMsg) + static void AddTrayIcon (HWND hWnd) { - case WM_CREATE: + NOTIFYICONDATA nid; + memset(&nid, 0, sizeof(nid)); + nid.cbSize = sizeof(nid); + nid.hWnd = hWnd; + nid.uID = ID_TRAY_ICON; + nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; + nid.uCallbackMessage = WM_TRAYICON; + nid.hIcon = LoadIcon (GetModuleHandle(NULL), MAKEINTRESOURCE (IDI_ICON1)); + strcpy (nid.szTip, "i2pd"); + Shell_NotifyIcon(NIM_ADD, &nid ); + } + + static void RemoveTrayIcon (HWND hWnd) + { + NOTIFYICONDATA nid; + nid.hWnd = hWnd; + nid.uID = ID_TRAY_ICON; + Shell_NotifyIcon (NIM_DELETE, &nid); + } + + static LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) + { + switch (uMsg) { - AddTrayIcon (hWnd); - break; - } - case WM_CLOSE: - { - RemoveTrayIcon (hWnd); - PostQuitMessage (0); - break; - } - case WM_COMMAND: - { - switch (LOWORD(wParam)) + case WM_CREATE: { - case ID_ABOUT: - { - MessageBox( hWnd, TEXT("i2pd"), TEXT("About"), MB_ICONINFORMATION | MB_OK ); - return 0; - } - case ID_EXIT: - { - PostMessage (hWnd, WM_CLOSE, 0, 0); - return 0; - } + AddTrayIcon (hWnd); + break; } - break; - } - case WM_TRAYICON: - { - SetForegroundWindow (hWnd); - switch (lParam) + case WM_CLOSE: { - case WM_RBUTTONUP: - { - SetForegroundWindow (hWnd); - ShowPopupMenu(hWnd, NULL, -1); - PostMessage (hWnd, WM_APP + 1, 0, 0); - break; - } + RemoveTrayIcon (hWnd); + PostQuitMessage (0); + break; + } + case WM_COMMAND: + { + switch (LOWORD(wParam)) + { + case ID_ABOUT: + { + MessageBox( hWnd, TEXT("i2pd"), TEXT("About"), MB_ICONINFORMATION | MB_OK ); + return 0; + } + case ID_EXIT: + { + PostMessage (hWnd, WM_CLOSE, 0, 0); + return 0; + } + } + break; + } + case WM_TRAYICON: + { + SetForegroundWindow (hWnd); + switch (lParam) + { + case WM_RBUTTONUP: + { + SetForegroundWindow (hWnd); + ShowPopupMenu(hWnd, NULL, -1); + PostMessage (hWnd, WM_APP + 1, 0, 0); + break; + } + } + break; } - break; } + return DefWindowProc( hWnd, uMsg, wParam, lParam); + } + + bool StartWin32App () + { + if (FindWindow (I2PD_WIN32_CLASSNAME, TEXT("i2pd"))) + { + MessageBox(NULL, TEXT("I2Pd is running already"), TEXT("Warning"), MB_OK); + return false; + } + // register main window + auto hInst = GetModuleHandle(NULL); + WNDCLASSEX wclx; + memset (&wclx, 0, sizeof(wclx)); + wclx.cbSize = sizeof(wclx); + wclx.style = 0; + wclx.lpfnWndProc = WndProc; + wclx.cbClsExtra = 0; + wclx.cbWndExtra = 0; + wclx.hInstance = hInst; + wclx.hIcon = LoadIcon (hInst, IDI_APPLICATION); + wclx.hIconSm = LoadIcon (hInst, IDI_APPLICATION); + wclx.hCursor = LoadCursor (NULL, IDC_ARROW); + wclx.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); + wclx.lpszMenuName = NULL; + wclx.lpszClassName = I2PD_WIN32_CLASSNAME; + RegisterClassEx (&wclx); + // create new window + if (!CreateWindow(I2PD_WIN32_CLASSNAME, TEXT("i2pd"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 150, NULL, NULL, hInst, NULL)) + { + MessageBox(NULL, "Failed to create main window", TEXT("Warning!"), MB_ICONERROR | MB_OK | MB_TOPMOST); + return false; + } + return true; + } + + int RunWin32App () + { + MSG msg; + while (GetMessage (&msg, NULL, 0, 0 )) + { + TranslateMessage (&msg); + DispatchMessage (&msg); + } + return msg.wParam; + } + + void StopWin32App () + { + UnregisterClass (I2PD_WIN32_CLASSNAME, GetModuleHandle(NULL)); } - return DefWindowProc( hWnd, uMsg, wParam, lParam); } - - int WINAPI WinMain (HINSTANCE hInst, HINSTANCE prev, LPSTR cmdline, int show) - { - // check if tunning already - if (FindWindow (I2PD_WIN32_CLASSNAME, TEXT("i2pd"))) - { - MessageBox(NULL, TEXT("I2Pd is running already"), TEXT("Warning"), MB_OK); - return 0; - } - // register main window - WNDCLASSEX wclx; - memset (&wclx, 0, sizeof(wclx)); - wclx.cbSize = sizeof(wclx); - wclx.style = 0; - wclx.lpfnWndProc = WndProc; - wclx.cbClsExtra = 0; - wclx.cbWndExtra = 0; - wclx.hInstance = hInst; - wclx.hIcon = LoadIcon (hInst, IDI_APPLICATION); - wclx.hIconSm = LoadIcon (hInst, IDI_APPLICATION); - wclx.hCursor = LoadCursor (NULL, IDC_ARROW); - wclx.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); - wclx.lpszMenuName = NULL; - wclx.lpszClassName = I2PD_WIN32_CLASSNAME; - RegisterClassEx (&wclx); - // create new window - if (!CreateWindow(I2PD_WIN32_CLASSNAME, TEXT("i2pd"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 150, NULL, NULL, hInst, NULL)) - { - MessageBox(NULL, "Failed to create main window", TEXT("Warning!"), MB_ICONERROR | MB_OK | MB_TOPMOST); - return 1; - } - - /* // init - char * argv[] = { (char *)"i2pd" }; - Daemon.init(sizeof (argv)/sizeof (argv[0]), argv); - // start - Daemon.start ();*/ - // main loop - MSG msg; - while (GetMessage (&msg, NULL, 0, 0 )) - { - TranslateMessage (&msg); - DispatchMessage (&msg); - } - /* // atop - Daemon.stop ();*/ - // terminate - UnregisterClass (I2PD_WIN32_CLASSNAME, hInst); - return msg.wParam; - } +} diff --git a/Win32/Win32App.h b/Win32/Win32App.h index 8b14ae1b..7d35ec1e 100644 --- a/Win32/Win32App.h +++ b/Win32/Win32App.h @@ -3,4 +3,13 @@ #define I2PD_WIN32_CLASSNAME "i2pd main window" +namespace i2p +{ +namespace win32 +{ + bool StartWin32App (); + void StopWin32App (); + int RunWin32App (); +} +} #endif // WIN32APP_H__ From 38b6c12153d9473dbf4ba87eed48f19d2075dbf9 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 10 Mar 2016 12:05:28 -0500 Subject: [PATCH 2/4] fixed bug with missed data directory --- FS.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FS.cpp b/FS.cpp index 5cb64dae..e84ff1d4 100644 --- a/FS.cpp +++ b/FS.cpp @@ -68,10 +68,10 @@ namespace fs { } bool Init() { - if (boost::filesystem::exists(dataDir)) + if (!boost::filesystem::exists(dataDir)) boost::filesystem::create_directory(dataDir); std::string destinations = DataDirPath("destinations"); - if (boost::filesystem::exists(destinations)) + if (!boost::filesystem::exists(destinations)) boost::filesystem::create_directory(destinations); return true; From 0493a321d294a3225e680a782dbf6b5f15d70417 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 10 Mar 2016 12:23:17 -0500 Subject: [PATCH 3/4] oveeride --log for windows --- Config.h | 6 +++--- DaemonWin32.cpp | 4 +++- Makefile.mingw | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Config.h b/Config.h index 07d7ccb7..d79a9c47 100644 --- a/Config.h +++ b/Config.h @@ -30,7 +30,7 @@ namespace config { /** * @brief Parse cmdline parameters, and show help if requested * @param argc Cmdline arguments count, should be passed from main(). - * @param argv Cmdline parameters array, should be passed from main() + * @param argv Cmdline parameters array, should be passed from main() * * If --help is given in parameters, shows it's list with description * terminates the program with exitcode 0. @@ -90,10 +90,10 @@ namespace config { bool SetOption(const char *name, const T& value) { if (!m_Options.count(name)) return false; - m_Options[name] = value; + m_Options.at(name).value() = value; notify(m_Options); return true; - } + } /** * @brief Check is value explicitly given or default diff --git a/DaemonWin32.cpp b/DaemonWin32.cpp index e5de9c9a..02c602f0 100644 --- a/DaemonWin32.cpp +++ b/DaemonWin32.cpp @@ -27,7 +27,9 @@ namespace i2p SetConsoleOutputCP(1251); setlocale(LC_ALL, "Russian"); if (!i2p::win32::StartWin32App ()) return false; - + + // override log + i2p::config::SetOption("log", std::string ("file")); bool ret = Daemon_Singleton::start(); if (ret && IsLogToFile ()) { diff --git a/Makefile.mingw b/Makefile.mingw index ba741918..d752e48c 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -1,6 +1,6 @@ CXX = g++ WINDRES = windres -CXXFLAGS = -O2 -D_MT -DWIN32 -D_WINDOWS -DWIN32_LEAN_AND_MEAN +CXXFLAGS = -D_MT -DWIN32 -D_WINDOWS -DWIN32_LEAN_AND_MEAN NEEDED_CXXFLAGS = -std=c++11 BOOST_SUFFIX = -mt INCFLAGS = -I/usr/include/ -I/usr/local/include/ From 0e5b32ef13f4678de4d3246aa3dee807e7a840f7 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 10 Mar 2016 13:34:16 -0500 Subject: [PATCH 4/4] 2.5.1 --- version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.h b/version.h index 0550dcc9..244096b7 100644 --- a/version.h +++ b/version.h @@ -8,7 +8,7 @@ #define I2PD_VERSION_MAJOR 2 #define I2PD_VERSION_MINOR 5 -#define I2PD_VERSION_MICRO 0 +#define I2PD_VERSION_MICRO 1 #define I2PD_VERSION_PATCH 0 #define I2PD_VERSION MAKE_VERSION(I2PD_VERSION_MAJOR, I2PD_VERSION_MINOR, I2PD_VERSION_MICRO) #define VERSION I2PD_VERSION