1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
259 Java_sun_lwawt_macosx_CRobot_keyEvent
260 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
261 {
262 CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
263
264 CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
265 if (event != NULL) {
266 CGEventPost(kCGSessionEventTap, event);
267 CFRelease(event);
268 }
269 }
270
271 /*
272 * Class: sun_lwawt_macosx_CRobot
273 * Method: nativeGetScreenPixels
274 * Signature: (IIIII[I)V
275 */
276 JNIEXPORT void JNICALL
277 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
278 (JNIEnv *env, jobject peer,
279 jint x, jint y, jint width, jint height, jintArray pixels)
280 {
281 JNF_COCOA_ENTER(env);
282
283 jint picX = x;
284 jint picY = y;
285 jint picWidth = width;
286 jint picHeight = height;
287
288 CGRect screenRect = CGRectMake(picX, picY, picWidth, picHeight);
289 CGImageRef screenPixelsImage = CGWindowListCreateImage(screenRect,
290 kCGWindowListOptionOnScreenOnly,
291 kCGNullWindowID, kCGWindowImageDefault);
292
293 if (screenPixelsImage == NULL) {
294 return;
295 }
296
297 // get a pointer to the Java int array
298 void *jPixelData = (*env)->GetPrimitiveArrayCritical(env, pixels, 0);
299 CHECK_NULL(jPixelData);
300
301 // create a graphics context around the Java int array
302 CGColorSpaceRef picColorSpace = CGColorSpaceCreateWithName(
303 kCGColorSpaceGenericRGB);
304 CGContextRef jPicContextRef = CGBitmapContextCreate(
305 jPixelData,
306 picWidth, picHeight,
307 8, picWidth * sizeof(jint),
308 picColorSpace,
309 kCGBitmapByteOrder32Host |
310 kCGImageAlphaPremultipliedFirst);
311
|
1 /*
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
259 Java_sun_lwawt_macosx_CRobot_keyEvent
260 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
261 {
262 CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
263
264 CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
265 if (event != NULL) {
266 CGEventPost(kCGSessionEventTap, event);
267 CFRelease(event);
268 }
269 }
270
271 /*
272 * Class: sun_lwawt_macosx_CRobot
273 * Method: nativeGetScreenPixels
274 * Signature: (IIIII[I)V
275 */
276 JNIEXPORT void JNICALL
277 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
278 (JNIEnv *env, jobject peer,
279 jint x, jint y, jint width, jint height, jdouble scale, jintArray pixels)
280 {
281 JNF_COCOA_ENTER(env);
282
283 jint picX = x;
284 jint picY = y;
285 jint picWidth = width;
286 jint picHeight = height;
287
288 CGRect screenRect = CGRectMake(picX / scale, picY / scale,
289 picWidth / scale, picHeight / scale);
290 CGImageRef screenPixelsImage = CGWindowListCreateImage(screenRect,
291 kCGWindowListOptionOnScreenOnly,
292 kCGNullWindowID, kCGWindowImageBestResolution);
293
294 if (screenPixelsImage == NULL) {
295 return;
296 }
297
298 // get a pointer to the Java int array
299 void *jPixelData = (*env)->GetPrimitiveArrayCritical(env, pixels, 0);
300 CHECK_NULL(jPixelData);
301
302 // create a graphics context around the Java int array
303 CGColorSpaceRef picColorSpace = CGColorSpaceCreateWithName(
304 kCGColorSpaceGenericRGB);
305 CGContextRef jPicContextRef = CGBitmapContextCreate(
306 jPixelData,
307 picWidth, picHeight,
308 8, picWidth * sizeof(jint),
309 picColorSpace,
310 kCGBitmapByteOrder32Host |
311 kCGImageAlphaPremultipliedFirst);
312
|