Skip to content

Commit 0f81e2f

Browse files
authored
Merge pull request #5 from nut-tree/feature/3/highlight
Feature/3/highlight
2 parents 5ae5a57 + d9c1e75 commit 0f81e2f

12 files changed

+246
-77
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ project(libnut)
66
# Source
77
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")
88
if (UNIX AND NOT APPLE)
9-
set(SOURCE_FILES "${SOURCE_FILES}" "src/xdisplay.c")
9+
set(SOURCE_FILES "${SOURCE_FILES}" "src/xdisplay.c" "src/highlightwindow_linux.c")
10+
elseif (UNIX AND APPLE)
11+
set(SOURCE_FILES "${SOURCE_FILES}" "src/highlightwindow_macos.m")
12+
elseif (WIN32)
13+
set(SOURCE_FILES "${SOURCE_FILES}" "src/highlightwindow_win32.c")
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

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface Bitmap {
1010

1111
export interface Screen {
1212
capture(x?: number, y?: number, width?: number, height?: number): Bitmap;
13+
highlight(x: number, y: number, width: number, height: number, duration: number, opacity: number): void;
1314
}
1415

1516
export function setKeyboardDelay(ms: number): void;

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function bitmap(width, height, byteWidth, bitsPerPixel, bytesPerPixel, image) {
1717
};
1818
}
1919

20+
module.exports.screen.highlight = libnut.highlight;
21+
2022
module.exports.screen.capture = function(x, y, width, height) {
2123
//If coords have been passed, use them.
2224
if (

package-lock.json

Lines changed: 55 additions & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "libnut",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "libnut is an N-API module for desktop automation with node",
55
"main": "index.js",
66
"typings": "index.d.ts",

src/highlightwindow.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#ifndef HIGHLIGHT_WINDOW_H
3+
#define HIGHLIGHT_WINDOW_H
4+
5+
void showHighlightWindow(int x, int y, int width, int height, int duration, float opacity);
6+
7+
#endif

src/highlightwindow_linux.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <X11/Xlib.h>
2+
#include <X11/Xutil.h>
3+
#include "highlightwindow.h"
4+
5+
void showHighlightWindow(int x, int y, int width, int height, int duration, float opacity)
6+
{
7+
Display *d = XOpenDisplay(NULL);
8+
Window root = DefaultRootWindow(d);
9+
int default_screen = XDefaultScreen(d);
10+
11+
XSetWindowAttributes attrs;
12+
attrs.override_redirect = True;
13+
14+
XVisualInfo vinfo;
15+
if (!XMatchVisualInfo(d, DefaultScreen(d), 32, TrueColor, &vinfo)) {
16+
return;
17+
}
18+
attrs.colormap = XCreateColormap(d, root, vinfo.visual, AllocNone);
19+
int colorValue = 255 * opacity;
20+
attrs.background_pixel = (colorValue << 24 | colorValue << 16);
21+
attrs.border_pixel = 0;
22+
23+
Window overlay = XCreateWindow(
24+
d, root,
25+
x, y, width, height, 0,
26+
vinfo.depth, InputOutput,
27+
vinfo.visual,
28+
CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel, &attrs
29+
);
30+
31+
XMapWindow(d, overlay);
32+
33+
XFlush(d);
34+
35+
sleep(duration);
36+
37+
XUnmapWindow(d, overlay);
38+
XCloseDisplay(d);
39+
}

src/highlightwindow_macos.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "highlightwindow.h"
2+
#import <Cocoa/Cocoa.h>
3+
4+
void showHighlightWindow(int x, int y, int width, int height, int duration, float opacity) {
5+
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
6+
[NSApplication sharedApplication];
7+
8+
NSRect mainScreenFrameRect = [[NSScreen mainScreen] frame];
9+
CGFloat screenHeight = mainScreenFrameRect.size.height;
10+
NSRect frame = NSMakeRect(x, screenHeight - y - height, width, height);
11+
NSUInteger styleMask = NSWindowStyleMaskBorderless;
12+
NSWindow * window = [[[NSWindow alloc] initWithContentRect:frame styleMask:styleMask backing:NSBackingStoreBuffered defer:NO] autorelease];
13+
[window setBackgroundColor:[NSColor redColor]];
14+
[window setAlphaValue:opacity];
15+
[window orderFrontRegardless];
16+
17+
[NSTimer scheduledTimerWithTimeInterval: duration
18+
repeats: NO
19+
block: ^(NSTimer * timer){
20+
[NSApp stop:0];
21+
NSRect f = [window frame];
22+
f.size = CGSizeMake(0, 0);
23+
[window setFrame: f display: YES animate: NO];
24+
}];
25+
[NSApp run];
26+
[pool release];
27+
}

0 commit comments

Comments
 (0)