Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CGraphicsEnv.m
41152 views
/*1* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import "AWT_debug.h"2627#import "JNIUtilities.h"28#import "ThreadUtilities.h"2930#define MAX_DISPLAYS 643132/*33* Class: sun_awt_CGraphicsEnvironment34* Method: getDisplayIDs35* Signature: ()[I36*/37JNIEXPORT jintArray JNICALL38Java_sun_awt_CGraphicsEnvironment_getDisplayIDs39(JNIEnv *env, jclass class)40{41jintArray ret = NULL;4243JNI_COCOA_ENTER(env);4445/* Get the count */46CGDisplayCount displayCount;47if (CGGetOnlineDisplayList(MAX_DISPLAYS, NULL, &displayCount) != kCGErrorSuccess) {48JNU_ThrowInternalError(env, "CGGetOnlineDisplayList() failed to get display count");49return NULL;50}5152/* Allocate an array and get the size list of display Ids */53CGDirectDisplayID displays[MAX_DISPLAYS];54if (CGGetOnlineDisplayList(displayCount, displays, &displayCount) != kCGErrorSuccess) {55JNU_ThrowInternalError(env, "CGGetOnlineDisplayList() failed to get display list");56return NULL;57}5859CGDisplayCount i;60CGDisplayCount displayActiveCount = 0; //Active and sleeping.61for (i = 0; i < displayCount; ++i) {62if (CGDisplayMirrorsDisplay(displays[i]) == kCGNullDirectDisplay) {63++displayActiveCount;64} else {65displays[i] = kCGNullDirectDisplay;66}67}6869/* Allocate a java array for display identifiers */70ret = (*env)->NewIntArray(env, displayActiveCount);71CHECK_NULL_RETURN(ret, NULL);7273/* Initialize and return the backing int array */74assert(sizeof(jint) >= sizeof(CGDirectDisplayID));75jint *elems = (*env)->GetIntArrayElements(env, ret, 0);76CHECK_NULL_RETURN(elems, NULL);7778/* Filter out the mirrored displays */79for (i = 0; i < displayCount; ++i) {80if (displays[i] != kCGNullDirectDisplay) {81elems[--displayActiveCount] = displays[i];82}83}8485(*env)->ReleaseIntArrayElements(env, ret, elems, 0);8687JNI_COCOA_EXIT(env);8889return ret;90}9192/*93* Class: sun_awt_CGraphicsEnvironment94* Method: getMainDisplayID95* Signature: ()I96*/97JNIEXPORT jint JNICALL98Java_sun_awt_CGraphicsEnvironment_getMainDisplayID99(JNIEnv *env, jclass class)100{101return CGMainDisplayID();102}103104/*105* Post the display reconfiguration event.106*/107static void displaycb_handle108(CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo)109{110if (flags == kCGDisplayBeginConfigurationFlag) return;111112[ThreadUtilities performOnMainThreadWaiting:NO block:^() {113114JNIEnv *env = [ThreadUtilities getJNIEnvUncached];115jobject cgeRef = (jobject)userInfo;116117jobject graphicsEnv = (*env)->NewLocalRef(env, cgeRef);118if (graphicsEnv == NULL) return; // ref already GC'd119DECLARE_CLASS(jc_CGraphicsEnvironment, "sun/awt/CGraphicsEnvironment");120DECLARE_METHOD(jm_displayReconfiguration,121jc_CGraphicsEnvironment, "_displayReconfiguration","(IZ)V");122(*env)->CallVoidMethod(env, graphicsEnv, jm_displayReconfiguration,123(jint) display, (jboolean) flags & kCGDisplayRemoveFlag);124(*env)->DeleteLocalRef(env, graphicsEnv);125CHECK_EXCEPTION();126}];127}128129/*130* Class: sun_awt_CGraphicsEnvironment131* Method: registerDisplayReconfiguration132* Signature: ()J133*/134JNIEXPORT jlong JNICALL135Java_sun_awt_CGraphicsEnvironment_registerDisplayReconfiguration136(JNIEnv *env, jobject this)137{138jlong ret = 0L;139140JNI_COCOA_ENTER(env);141142jobject cgeRef = (*env)->NewWeakGlobalRef(env, this);143144/* Register the callback */145if (CGDisplayRegisterReconfigurationCallback(&displaycb_handle, cgeRef) != kCGErrorSuccess) {146JNU_ThrowInternalError(env, "CGDisplayRegisterReconfigurationCallback() failed");147return 0L;148}149150ret = ptr_to_jlong(cgeRef);151152JNI_COCOA_EXIT(env);153154return ret;155}156157/*158* Class: sun_awt_CGraphicsEnvironment159* Method: deregisterDisplayReconfiguration160* Signature: (J)V161*/162JNIEXPORT void JNICALL163Java_sun_awt_CGraphicsEnvironment_deregisterDisplayReconfiguration164(JNIEnv *env, jobject this, jlong p)165{166JNI_COCOA_ENTER(env);167168jobject cgeRef = (jobject)jlong_to_ptr(p);169if (!cgeRef) return;170171/* Remove the registration */172if (CGDisplayRemoveReconfigurationCallback(&displaycb_handle, cgeRef) != kCGErrorSuccess) {173JNU_ThrowInternalError(env, "CGDisplayRemoveReconfigurationCallback() failed, leaking the callback context!");174return;175}176177(*env)->DeleteWeakGlobalRef(env, cgeRef);178179JNI_COCOA_EXIT(env);180}181182183