diff --git a/CMakeLists.txt b/CMakeLists.txt index 44f581c..de12770 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,13 @@ set(CMAKE_CXX_STANDARD 17) project(libnut) # Source -set(SOURCE_FILES "src/libnut.cc" "src/deadbeef_rand.c" "src/mouse.c" "src/keypress.c" "src/keycode.c" "src/screen.c" "src/screengrab.c" "src/snprintf.c" "src/MMBitmap.c") +set(SOURCE_FILES "src/libnut.cc" "src/deadbeef_rand.c" "src/mouse.c" "src/keypress.c" "src/keycode.c" "src/screen.cc" "src/screengrab.c" "src/snprintf.c" "src/MMBitmap.c") if (UNIX AND NOT APPLE) set(SOURCE_FILES "${SOURCE_FILES}" "src/xdisplay.c") +elseif (UNIX AND APPLE) + set(SOURCE_FILES "${SOURCE_FILES}" "src/window_macos.mm") +elseif (WIN32) + set(SOURCE_FILES "${SOURCE_FILES}" "src/window_win32.cc") endif() add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES}) @@ -20,9 +24,7 @@ set(INCLUDES "") if (UNIX AND APPLE) message(STATUS "macOS build") set(LIBS "${LIBS}" "-framework ApplicationServices") - set(LIBS "${LIBS}" "-framework Carbon") - set(LIBS "${LIBS}" "-framework CoreFoundation") - set(LIBS "${LIBS}" "-framework OpenGL") + set(LIBS "${LIBS}" "-framework Cocoa") elseif (WIN32) message(STATUS "Windows build") # Required for disabeling delayed loading of node libs when linking diff --git a/src/window_win32.cc b/src/window_win32.cc new file mode 100644 index 0000000..a1f0070 --- /dev/null +++ b/src/window_win32.cc @@ -0,0 +1,82 @@ +#ifndef UNICODE +#define UNICODE +#endif + +#include +#include +#include "window.h" + +#define ID_CLOSE_TIMER 1001 + +LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + +Window::Window(int x, int y, int width, int height) { + this->_x = x; + this->_y = y; + this->_width = width; + this->_height = height; +} + +void Window::show(int duration, float opacity) { + // Register the window class. + const wchar_t CLASS_NAME[] = L"Highlight Window Class"; + + WNDCLASS wc = { }; + + wc.lpfnWndProc = WindowProc; + wc.hInstance = NULL; + wc.lpszClassName = CLASS_NAME; + wc.hbrBackground = CreateSolidBrush(RGB(255, 0, 0)); + + RegisterClass(&wc); + + HWND hwnd = CreateWindowEx( + WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW, + CLASS_NAME, + 0, + WS_POPUP, + this->_x, + this->_y, + this->_width, + this->_height, + NULL, + NULL, + NULL, + NULL + ); + + SetLayeredWindowAttributes(hwnd, 0, 255 * opacity, LWA_ALPHA); + + if (hwnd == NULL) { + return; + } + + SetTimer(hwnd, ID_CLOSE_TIMER, 2000, NULL); + ShowWindow(hwnd, 1); + + MSG msg = { }; + while (GetMessage(&msg, NULL, 0, 0)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } +} + +LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + switch (uMsg) + { + case WM_DESTROY: + KillTimer(hwnd, ID_CLOSE_TIMER); + PostQuitMessage(0); + return 0; + case WM_TIMER: + switch(wParam) { + case ID_CLOSE_TIMER: + KillTimer(hwnd, ID_CLOSE_TIMER); + CloseWindow(hwnd); + PostQuitMessage(0); + return 0; + } + } + return DefWindowProc(hwnd, uMsg, wParam, lParam); +}