From 3f404905d2a2b06039949357903c258cdd1568a1 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 21 May 2019 17:52:58 +1000 Subject: [PATCH] [c-host] added tray icon and context menu --- VERSION | 2 +- c-host/platform/Windows/icon.ico | Bin 0 -> 1150 bytes c-host/platform/Windows/resource.rc | 1 + c-host/platform/Windows/src/platform.c | 69 ++++++++++++++++++++++--- c-host/platform/Windows/src/platform.h | 1 + 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 c-host/platform/Windows/icon.ico diff --git a/VERSION b/VERSION index c8e9f65c..41f0adb5 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -a12-202-g77f942711a+1 \ No newline at end of file +a12-203-g67595d6deb+1 \ No newline at end of file diff --git a/c-host/platform/Windows/icon.ico b/c-host/platform/Windows/icon.ico new file mode 100644 index 0000000000000000000000000000000000000000..ffc30db8c8e369565fb9a8f6fb015ec2ee724029 GIT binary patch literal 1150 zcmbVMYflqV5WNKi1QQz@koY7qF+|0H!Nk&*Qb>hbfl`4KC=y5rjnAM#upov{w54FF zfKeVI2w0&7yDF`K*h?!9|v&N(x?O6lm*>lNMel`l=HT%}Y24cele-F=#r zqBBGG?vQ7?1=Gw(8mVSP3{{xUcVp_BOUCr|dQ9X#N`7xY?(cIT#f16dc5HixvFZ7M zt>z*8Y5j;Lb2kF1rc=cj&9owV(TRooorLotn)evttnyP~H7z5`2-!OR^+}AFqX(;WHj(2vG)5@hA|5lAaVr%sMElp9P55c?QM^9H zs(Y|(eJ0xW`oMZ8FV{+5#x5GW5XvY+Kv#;<3@hgDwqcQcU``z4%Z&zbowXQ&lv2zV zdPOhuja;Y_ET1!;J(#`I3f2&FyutU9jsqIv?tW~y4GV{8o)c47T?nO@iRYg$)krOV zJ$#LgMnASZL*O3K@(0gYh@)40=KM3aTCmwPC_Tj5m}|sPEj&2J{JnPJ^$$-xSu6Kk z!u=jgrf%_$b&TkY#KR)>OEmc9yEv|&xc3uJ{8Go6+bzVU8OyfkV66B`vG(TOzYdWW t?pgMEqj5lVa80=eNt%!N{x}}%4QJWFzD-`Kqn@lh;J21n`w#r@yaU;h{I~!B literal 0 HcmV?d00001 diff --git a/c-host/platform/Windows/resource.rc b/c-host/platform/Windows/resource.rc index 8af0ed12..8c557b1d 100644 --- a/c-host/platform/Windows/resource.rc +++ b/c-host/platform/Windows/resource.rc @@ -1,3 +1,4 @@ #include "winuser.h" CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "app.manifest" +IDI_APPLICATION ICON "icon.ico" diff --git a/c-host/platform/Windows/src/platform.c b/c-host/platform/Windows/src/platform.c index 40c4e2b1..9565e644 100644 --- a/c-host/platform/Windows/src/platform.c +++ b/c-host/platform/Windows/src/platform.c @@ -31,8 +31,13 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "windows/debug.h" #include "ivshmem/Public.h" +#define ID_MENU_OPEN_LOG 3000 +#define ID_MENU_EXIT 3001 + struct AppState { + HINSTANCE hInst; + int argc; char ** argv; @@ -41,6 +46,7 @@ struct AppState bool shmemOwned; IVSHMEM_MMAP shmemMap; HWND messageWnd; + HMENU trayMenu; }; static struct AppState app = @@ -65,11 +71,6 @@ LRESULT CALLBACK DummyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { - case WM_CLOSE: - mouseHook_remove(); - DestroyWindow(hwnd); - break; - case WM_DESTROY: PostQuitMessage(0); break; @@ -80,6 +81,36 @@ LRESULT CALLBACK DummyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) return cf->fn(cf->wParam, cf->lParam); } + case WM_TRAYICON: + { + if (lParam == WM_RBUTTONDOWN) + { + POINT curPoint; + GetCursorPos(&curPoint); + SetForegroundWindow(hwnd); + UINT clicked = TrackPopupMenu( + app.trayMenu, + TPM_RETURNCMD | TPM_NONOTIFY, + curPoint.x, + curPoint.y, + 0, + hwnd, + NULL + ); + + if (clicked == ID_MENU_EXIT ) app_quit(); + else if (clicked == ID_MENU_OPEN_LOG) + { + const char * logFile = option_get_string("os", "logFile"); + if (strcmp(logFile, "stderr") == 0) + DEBUG_INFO("Ignoring request to open the logFile, logging to stderr"); + else + ShellExecute(NULL, NULL, logFile, NULL, NULL, SW_SHOWNORMAL); + } + } + break; + } + default: return DefWindowProc(hwnd, msg, wParam, lParam); } @@ -88,8 +119,23 @@ LRESULT CALLBACK DummyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) static int appThread(void * opaque) { + // register our TrayIcon + NOTIFYICONDATA iconData = + { + .cbSize = sizeof(NOTIFYICONDATA), + .hWnd = app.messageWnd, + .uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP, + .uCallbackMessage = WM_TRAYICON, + .szTip = "Looking Glass (host)" + }; + iconData.hIcon = LoadIcon(app.hInst, IDI_APPLICATION); + Shell_NotifyIcon(NIM_ADD, &iconData); + int result = app_main(app.argc, app.argv); - SendMessage(app.messageWnd, WM_CLOSE, 0, 0); + + Shell_NotifyIcon(NIM_DELETE, &iconData); + mouseHook_remove(); + SendMessage(app.messageWnd, WM_DESTROY, 0, 0); return result; } @@ -112,6 +158,7 @@ static BOOL WINAPI CtrlHandler(DWORD dwCtrlType) int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int result = 0; + app.hInst = hInstance; char tempPath[MAX_PATH+1]; GetTempPathA(sizeof(tempPath), tempPath); @@ -164,6 +211,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine wx.lpfnWndProc = DummyWndProc; wx.hInstance = hInstance; wx.lpszClassName = "DUMMY_CLASS"; + wx.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); + wx.hCursor = LoadCursor(NULL, IDC_ARROW); + wx.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE; if (!RegisterClassEx(&wx)) { DEBUG_ERROR("Failed to register message window class"); @@ -172,6 +223,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } app.messageWnd = CreateWindowEx(0, "DUMMY_CLASS", "DUMMY_NAME", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); + app.trayMenu = CreatePopupMenu(); + AppendMenu(app.trayMenu, MF_STRING , ID_MENU_OPEN_LOG, "Open Log File"); + AppendMenu(app.trayMenu, MF_SEPARATOR, 0 , NULL ); + AppendMenu(app.trayMenu, MF_STRING , ID_MENU_EXIT , "Exit" ); + // create the application thread osThreadHandle * thread; if (!os_createThread("appThread", appThread, NULL, &thread)) @@ -202,6 +258,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } shutdown: + DestroyMenu(app.trayMenu); app_quit(); if (!os_joinThread(thread, &result)) { diff --git a/c-host/platform/Windows/src/platform.h b/c-host/platform/Windows/src/platform.h index 4d14b224..296a1bdb 100644 --- a/c-host/platform/Windows/src/platform.h +++ b/c-host/platform/Windows/src/platform.h @@ -20,6 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #define WM_CALL_FUNCTION (WM_USER+1) +#define WM_TRAYICON (WM_USER+2) typedef LRESULT (*CallFunction)(WPARAM wParam, LPARAM lParam); struct MSG_CALL_FUNCTION