Skip to content

Commit a8f66af

Browse files
committed
(#3) Updated linked frameworks on macOS and added window implementation to sources on macOS
1 parent 6dda547 commit a8f66af

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ set(CMAKE_CXX_STANDARD 17)
44
project(libnut)
55

66
# Source
7-
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")
7+
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")
88
if (UNIX AND NOT APPLE)
99
set(SOURCE_FILES "${SOURCE_FILES}" "src/xdisplay.c")
10+
elseif (UNIX AND APPLE)
11+
set(SOURCE_FILES "${SOURCE_FILES}" "src/window_macos.mm")
12+
elseif (WIN32)
13+
set(SOURCE_FILES "${SOURCE_FILES}" "src/window_win32.cc")
1014
endif()
1115
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
1216

@@ -20,9 +24,7 @@ set(INCLUDES "")
2024
if (UNIX AND APPLE)
2125
message(STATUS "macOS build")
2226
set(LIBS "${LIBS}" "-framework ApplicationServices")
23-
set(LIBS "${LIBS}" "-framework Carbon")
24-
set(LIBS "${LIBS}" "-framework CoreFoundation")
25-
set(LIBS "${LIBS}" "-framework OpenGL")
27+
set(LIBS "${LIBS}" "-framework Cocoa")
2628
elseif (WIN32)
2729
message(STATUS "Windows build")
2830
# Required for disabeling delayed loading of node libs when linking

src/window_win32.cc

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#ifndef UNICODE
2+
#define UNICODE
3+
#endif
4+
5+
#include <windows.h>
6+
#include <cstdio>
7+
#include "window.h"
8+
9+
#define ID_CLOSE_TIMER 1001
10+
11+
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
12+
13+
Window::Window(int x, int y, int width, int height) {
14+
this->_x = x;
15+
this->_y = y;
16+
this->_width = width;
17+
this->_height = height;
18+
}
19+
20+
void Window::show(int duration, float opacity) {
21+
// Register the window class.
22+
const wchar_t CLASS_NAME[] = L"Highlight Window Class";
23+
24+
WNDCLASS wc = { };
25+
26+
wc.lpfnWndProc = WindowProc;
27+
wc.hInstance = NULL;
28+
wc.lpszClassName = CLASS_NAME;
29+
wc.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));
30+
31+
RegisterClass(&wc);
32+
33+
HWND hwnd = CreateWindowEx(
34+
WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW,
35+
CLASS_NAME,
36+
0,
37+
WS_POPUP,
38+
this->_x,
39+
this->_y,
40+
this->_width,
41+
this->_height,
42+
NULL,
43+
NULL,
44+
NULL,
45+
NULL
46+
);
47+
48+
SetLayeredWindowAttributes(hwnd, 0, 255 * opacity, LWA_ALPHA);
49+
50+
if (hwnd == NULL) {
51+
return;
52+
}
53+
54+
SetTimer(hwnd, ID_CLOSE_TIMER, 2000, NULL);
55+
ShowWindow(hwnd, 1);
56+
57+
MSG msg = { };
58+
while (GetMessage(&msg, NULL, 0, 0)) {
59+
TranslateMessage(&msg);
60+
DispatchMessage(&msg);
61+
}
62+
}
63+
64+
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
65+
{
66+
switch (uMsg)
67+
{
68+
case WM_DESTROY:
69+
KillTimer(hwnd, ID_CLOSE_TIMER);
70+
PostQuitMessage(0);
71+
return 0;
72+
case WM_TIMER:
73+
switch(wParam) {
74+
case ID_CLOSE_TIMER:
75+
KillTimer(hwnd, ID_CLOSE_TIMER);
76+
CloseWindow(hwnd);
77+
PostQuitMessage(0);
78+
return 0;
79+
}
80+
}
81+
return DefWindowProc(hwnd, uMsg, wParam, lParam);
82+
}

0 commit comments

Comments
 (0)