Skip to content

Commit

Permalink
(#3) Updated linked frameworks on macOS and added window implementati…
Browse files Browse the repository at this point in the history
…on to sources on macOS
  • Loading branch information
s1hofmann committed May 3, 2020
1 parent 6dda547 commit a8f66af
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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
Expand Down
82 changes: 82 additions & 0 deletions src/window_win32.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <cstdio>
#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);
}

0 comments on commit a8f66af

Please sign in to comment.