Skip to content

Feature/3/highlight #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ 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")
if (UNIX AND NOT APPLE)
set(SOURCE_FILES "${SOURCE_FILES}" "src/xdisplay.c")
set(SOURCE_FILES "${SOURCE_FILES}" "src/xdisplay.c" "src/highlightwindow_linux.c")
elseif (UNIX AND APPLE)
set(SOURCE_FILES "${SOURCE_FILES}" "src/highlightwindow_macos.m")
elseif (WIN32)
set(SOURCE_FILES "${SOURCE_FILES}" "src/highlightwindow_win32.c")
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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Bitmap {

export interface Screen {
capture(x?: number, y?: number, width?: number, height?: number): Bitmap;
highlight(x: number, y: number, width: number, height: number, duration: number, opacity: number): void;
}

export function setKeyboardDelay(ms: number): void;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function bitmap(width, height, byteWidth, bitsPerPixel, bytesPerPixel, image) {
};
}

module.exports.screen.highlight = libnut.highlight;

module.exports.screen.capture = function(x, y, width, height) {
//If coords have been passed, use them.
if (
Expand Down
127 changes: 55 additions & 72 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libnut",
"version": "1.0.2",
"version": "1.1.0",
"description": "libnut is an N-API module for desktop automation with node",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/highlightwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#ifndef HIGHLIGHT_WINDOW_H
#define HIGHLIGHT_WINDOW_H

void showHighlightWindow(int x, int y, int width, int height, int duration, float opacity);

#endif
39 changes: 39 additions & 0 deletions src/highlightwindow_linux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "highlightwindow.h"

void showHighlightWindow(int x, int y, int width, int height, int duration, float opacity)
{
Display *d = XOpenDisplay(NULL);
Window root = DefaultRootWindow(d);
int default_screen = XDefaultScreen(d);

XSetWindowAttributes attrs;
attrs.override_redirect = True;

XVisualInfo vinfo;
if (!XMatchVisualInfo(d, DefaultScreen(d), 32, TrueColor, &vinfo)) {
return;
}
attrs.colormap = XCreateColormap(d, root, vinfo.visual, AllocNone);
int colorValue = 255 * opacity;
attrs.background_pixel = (colorValue << 24 | colorValue << 16);
attrs.border_pixel = 0;

Window overlay = XCreateWindow(
d, root,
x, y, width, height, 0,
vinfo.depth, InputOutput,
vinfo.visual,
CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel, &attrs
);

XMapWindow(d, overlay);

XFlush(d);

sleep(duration);

XUnmapWindow(d, overlay);
XCloseDisplay(d);
}
27 changes: 27 additions & 0 deletions src/highlightwindow_macos.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "highlightwindow.h"
#import <Cocoa/Cocoa.h>

void showHighlightWindow(int x, int y, int width, int height, int duration, float opacity) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];

NSRect mainScreenFrameRect = [[NSScreen mainScreen] frame];
CGFloat screenHeight = mainScreenFrameRect.size.height;
NSRect frame = NSMakeRect(x, screenHeight - y - height, width, height);
NSUInteger styleMask = NSWindowStyleMaskBorderless;
NSWindow * window = [[[NSWindow alloc] initWithContentRect:frame styleMask:styleMask backing:NSBackingStoreBuffered defer:NO] autorelease];
[window setBackgroundColor:[NSColor redColor]];
[window setAlphaValue:opacity];
[window orderFrontRegardless];

[NSTimer scheduledTimerWithTimeInterval: duration
repeats: NO
block: ^(NSTimer * timer){
[NSApp stop:0];
NSRect f = [window frame];
f.size = CGSizeMake(0, 0);
[window setFrame: f display: YES animate: NO];
}];
[NSApp run];
[pool release];
}
Loading