Skip to content
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

Bugfix/nt 133/meta keys #182

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 5 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
matrix:
os: [ ubuntu-20.04, macos-11, windows-2019 ]
node: [ 18 ]
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
runs-on: ${{matrix.os}}
steps:
- name: Set up Git repository
Expand All @@ -20,6 +22,7 @@ jobs:
uses: actions/setup-node@v4.0.2
with:
node-version: ${{matrix.node}}
registry-url: https://registry.npmjs.org
- name: Configure Linux environment
if: ${{matrix.os == 'ubuntu-20.04'}}
run: sudo apt-get install -y cmake libx11-dev zlib1g-dev libpng-dev libxtst-dev build-essential
Expand All @@ -28,12 +31,12 @@ jobs:
- name: Build
run: npm run build:release
- name: Run tests
uses: GabrielBB/xvfb-action@v1
uses: coactions/setup-xvfb@v1
with:
working-directory: ./test/
run: npm cit
- name: Run window tests
uses: GabrielBB/xvfb-action@v1
uses: coactions/setup-xvfb@v1
with:
working-directory: ./test/window-integration-tests
run: npm cit
7 changes: 5 additions & 2 deletions .github/workflows/snapshot_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
matrix:
os: [ ubuntu-20.04, macos-11, windows-2019 ]
node: [ 18 ]
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
runs-on: ${{matrix.os}}
steps:
- name: Set up Git repository
Expand All @@ -24,6 +26,7 @@ jobs:
uses: actions/setup-node@v4.0.2
with:
node-version: ${{matrix.node}}
registry-url: https://registry.npmjs.org
- name: Configure Linux environment
if: ${{matrix.os == 'ubuntu-20.04'}}
run: sudo apt-get install -y cmake libx11-dev zlib1g-dev libpng-dev libxtst-dev build-essential
Expand All @@ -32,12 +35,12 @@ jobs:
- name: Build
run: npm run build:release
- name: Run tests
uses: GabrielBB/xvfb-action@v1
uses: coactions/setup-xvfb@v1
with:
working-directory: ./test/
run: npm cit
- name: Run window tests
uses: GabrielBB/xvfb-action@v1
uses: coactions/setup-xvfb@v1
with:
working-directory: ./test/window-integration-tests
run: npm cit
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/tagged_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ jobs:
os: [ ubuntu-20.04, macos-11, windows-2019 ]
node: [ 18 ]
runs-on: ${{matrix.os}}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
steps:
- name: Set up Git repository
uses: actions/checkout@v4.1.1
- name: Set up node
uses: actions/setup-node@v4.0.2
with:
node-version: ${{matrix.node}}
registry-url: https://registry.npmjs.org
- name: Configure Linux environment
if: ${{matrix.os == 'ubuntu-20.04'}}
run: sudo apt-get install -y cmake libx11-dev zlib1g-dev libpng-dev libxtst-dev build-essential
Expand All @@ -26,12 +29,12 @@ jobs:
- name: Build
run: npm run build:release
- name: Run tests
uses: GabrielBB/xvfb-action@v1
uses: coactions/setup-xvfb@v1
with:
working-directory: ./test/
run: npm cit
- name: Run window tests
uses: GabrielBB/xvfb-action@v1
uses: coactions/setup-xvfb@v1
with:
working-directory: ./test/window-integration-tests
run: npm cit
Expand Down
81 changes: 25 additions & 56 deletions src/macos/screengrab.m
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
#include "../screengrab.h"
#include "../endian.h"
#include <stdlib.h> /* malloc() */
#include <stdio.h> /* printf() */

#include <ApplicationServices/ApplicationServices.h>
#import <Cocoa/Cocoa.h>

static double getPixelDensity() {
@autoreleasepool
{
NSScreen * mainScreen = [NSScreen
mainScreen];
if (mainScreen) {
return mainScreen.backingScaleFactor;
} else {
return 1.0;
}
}
}

MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) {

CGDirectDisplayID displayID = CGMainDisplayID();
Expand All @@ -33,56 +21,37 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) {

if (!image) { return NULL; }

CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
CGDataProviderRef provider = CGImageGetDataProvider(image);

CFDataRef imageData = CGDataProviderCopyData(provider);

if (!imageData) { return NULL; }

long bufferSize = CFDataGetLength(imageData);
size_t bytesPerPixel = (size_t) (CGImageGetBitsPerPixel(image) / 8);
double pixelDensity = getPixelDensity();
long expectedBufferSize = rect.size.width * pixelDensity * rect.size.height * pixelDensity * bytesPerPixel;

if (expectedBufferSize < bufferSize) {
size_t reportedByteWidth = CGImageGetBytesPerRow(image);
size_t expectedByteWidth = expectedBufferSize / (rect.size.height * pixelDensity);

uint8_t *buffer = malloc(expectedBufferSize);
size_t bitsPerPixel = CGImageGetBitsPerPixel(image);
size_t bytesPerPixel = bitsPerPixel / 8;
size_t bytesPerRow = CGImageGetBytesPerRow(image);
size_t actualBytesPerRow = width * bytesPerPixel;
const UInt8 *dataPointer = CFDataGetBytePtr(imageData);

const uint8_t *dataPointer = CFDataGetBytePtr(imageData);
size_t parts = bufferSize / reportedByteWidth;
uint8_t *imageDataWithoutPadding = malloc(height * actualBytesPerRow);

for (size_t idx = 0; idx < parts - 1; ++idx) {
memcpy(buffer + (idx * expectedByteWidth),
dataPointer + (idx * reportedByteWidth),
expectedByteWidth
);
}

MMBitmapRef bitmap = createMMBitmap(buffer,
rect.size.width * pixelDensity,
rect.size.height * pixelDensity,
expectedByteWidth,
CGImageGetBitsPerPixel(image),
CGImageGetBitsPerPixel(image) / 8);

CFRelease(imageData);
CGImageRelease(image);

return bitmap;
} else {
uint8_t *buffer = malloc(bufferSize);
CFDataGetBytes(imageData, CFRangeMake(0, bufferSize), buffer);
MMBitmapRef bitmap = createMMBitmap(buffer,
CGImageGetWidth(image),
CGImageGetHeight(image),
CGImageGetBytesPerRow(image),
CGImageGetBitsPerPixel(image),
CGImageGetBitsPerPixel(image) / 8);
for (size_t y = 0; y < height; ++y) {
const UInt8 *rowPtr = dataPointer + y * bytesPerRow;
memcpy(imageDataWithoutPadding + y * actualBytesPerRow, rowPtr, actualBytesPerRow);
}

CFRelease(imageData);
MMBitmapRef bitmap = createMMBitmap(imageDataWithoutPadding,
width,
height,
actualBytesPerRow,
bitsPerPixel,
bytesPerPixel);

CGImageRelease(image);
CFRelease(imageData);
CGImageRelease(image);

return bitmap;
}
return bitmap;
}
4 changes: 2 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ int CheckKeyFlags(std::string &flagString, MMKeyFlags *flags) {
if (flagString == "alt" || flagString == "right_alt") {
*flags = MOD_ALT;
#if defined(IS_MACOSX)
} else if (flagString == "command" || flagString == "cmd" || flagString == "right_cmd") {
} else if (flagString == "meta" || flagString == "right_meta" || flagString == "cmd" || flagString == "right_cmd") {
*flags = MOD_META;
#else
} else if (flagString == "command" || flagString == "win" || flagString == "right_win") {
} else if (flagString == "meta" || flagString == "right_meta" || flagString == "win" || flagString == "right_win") {
*flags = MOD_META;
#endif
} else if (flagString == "control" || flagString == "right_control") {
Expand Down
1 change: 1 addition & 0 deletions test/window-integration-tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe("focusWindow", () => {
win.focus();
});

await sleep(3000);
const result = libnut.focusWindow(openWindowHandle);

// THEN
Expand Down
Loading