1 /*
   2  * Copyright (c) 2011, 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
  23  * questions.
  24  */
  25 
  26 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  27 
  28 #import "LWCToolkit.h"
  29 #import "AWT_debug.h"
  30 
  31 
  32 /*
  33  * Class:     sun_awt_CGraphicsEnvironment
  34  * Method:    initCocoa
  35  * Signature: ()V
  36  */
  37 JNIEXPORT void JNICALL
  38 Java_sun_awt_CGraphicsEnvironment_initCocoa
  39 (JNIEnv *env, jclass self)
  40 {
  41 JNF_COCOA_ENTER(env);
  42 
  43     // Inform Cocoa that we're multi-threaded.
  44     // Creating a short-lived NSThread is the recommended way of doing so.
  45     [NSThread detachNewThreadSelector:@selector(self) toTarget:[NSObject class] withObject:nil];
  46 
  47 JNF_COCOA_EXIT(env);
  48 }
  49 
  50 #define MAX_DISPLAYS 64
  51 
  52 /*
  53  * Class:     sun_awt_CGraphicsEnvironment
  54  * Method:    getDisplayIDs
  55  * Signature: ()[I
  56  */
  57 JNIEXPORT jintArray JNICALL
  58 Java_sun_awt_CGraphicsEnvironment_getDisplayIDs
  59 (JNIEnv *env, jclass class)
  60 {
  61     jintArray ret = NULL;
  62 
  63 JNF_COCOA_ENTER(env);
  64 
  65     /* Get the count */
  66     CGDisplayCount displayCount;
  67     if (CGGetActiveDisplayList(MAX_DISPLAYS, NULL, &displayCount) != kCGErrorSuccess) {
  68         [JNFException raise:env
  69                          as:kInternalError
  70                      reason:"CGGetOnlineDisplayList() failed to get display count"];
  71         return NULL;
  72     }
  73 
  74     /* Allocate an array and get the size list of display Ids */
  75     CGDirectDisplayID displays[MAX_DISPLAYS];
  76     if (CGGetActiveDisplayList(displayCount, displays, &displayCount) != kCGErrorSuccess) {
  77         [JNFException raise:env
  78                          as:kInternalError
  79                      reason:"CGGetOnlineDisplayList() failed to get display list"];
  80         return NULL;
  81     }
  82 
  83     /* Allocate a java array for display identifiers */
  84     ret = JNFNewIntArray(env, displayCount);
  85 
  86     /* Initialize and return the backing int array */
  87     assert(sizeof(jint) >= sizeof(CGDirectDisplayID));
  88     jint *elems = (*env)->GetIntArrayElements(env, ret, 0);
  89 
  90     CGDisplayCount i;
  91     for (i = 0; i < displayCount; i++) {
  92         elems[i] = displays[i];
  93     }
  94 
  95     (*env)->ReleaseIntArrayElements(env, ret, elems, 0);
  96 
  97 JNF_COCOA_EXIT(env);
  98 
  99     return ret;
 100 }
 101 
 102 /*
 103  * Class:     sun_awt_CGraphicsEnvironment
 104  * Method:    getMainDisplayID
 105  * Signature: ()I
 106  */
 107 JNIEXPORT jint JNICALL
 108 Java_sun_awt_CGraphicsEnvironment_getMainDisplayID
 109 (JNIEnv *env, jclass class)
 110 {
 111     return CGMainDisplayID();
 112 }
 113 
 114 /*
 115  * Post the display reconfiguration event.
 116  */
 117 static void displaycb_handle
 118 (CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo)
 119 {
 120     if (flags == kCGDisplayBeginConfigurationFlag) return;
 121 
 122     JNFPerformEnvBlock(JNFThreadDetachImmediately, ^(JNIEnv *env) {
 123         JNFWeakJObjectWrapper *wrapper = (JNFWeakJObjectWrapper *)userInfo;
 124 
 125         jobject graphicsEnv = [wrapper jObjectWithEnv:env];
 126         if (graphicsEnv == NULL) return; // ref already GC'd
 127         static JNF_CLASS_CACHE(jc_CGraphicsEnvironment, "sun/awt/CGraphicsEnvironment");
 128         static JNF_MEMBER_CACHE(jm_displayReconfiguration, jc_CGraphicsEnvironment, "_displayReconfiguration", "(IZ)V");
 129         JNFCallVoidMethod(env, graphicsEnv, jm_displayReconfiguration,
 130                             (jint) display, 
 131                             (jboolean) flags & kCGDisplayRemoveFlag);
 132     });
 133 }
 134 
 135 /*
 136  * Class:     sun_awt_CGraphicsEnvironment
 137  * Method:    registerDisplayReconfiguration
 138  * Signature: ()J
 139  */
 140 JNIEXPORT jlong JNICALL
 141 Java_sun_awt_CGraphicsEnvironment_registerDisplayReconfiguration
 142 (JNIEnv *env, jobject this)
 143 {
 144     jlong ret = 0L;
 145 
 146 JNF_COCOA_ENTER(env);
 147 
 148     JNFWeakJObjectWrapper *wrapper = [JNFWeakJObjectWrapper wrapperWithJObject:this withEnv:env];
 149     CFRetain(wrapper); // pin from ObjC-GC
 150 
 151     /* Register the callback */
 152     if (CGDisplayRegisterReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {
 153         [JNFException raise:env
 154                          as:kInternalError
 155                      reason:"CGDisplayRegisterReconfigurationCallback() failed"];
 156         return 0L;
 157     }
 158 
 159     ret = ptr_to_jlong(wrapper);
 160 
 161 JNF_COCOA_EXIT(env);
 162 
 163     return ret;
 164 }
 165 
 166 /*
 167  * Class:     sun_awt_CGraphicsEnvironment
 168  * Method:    deregisterDisplayReconfiguration
 169  * Signature: (J)V
 170  */
 171 JNIEXPORT void JNICALL
 172 Java_sun_awt_CGraphicsEnvironment_deregisterDisplayReconfiguration
 173 (JNIEnv *env, jobject this, jlong p)
 174 {
 175 JNF_COCOA_ENTER(env);
 176 
 177     JNFWeakJObjectWrapper *wrapper = (JNFWeakJObjectWrapper *)jlong_to_ptr(p);
 178     if (!wrapper) return;
 179 
 180     /* Remove the registration */
 181     if (CGDisplayRemoveReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {
 182         [JNFException raise:env
 183                          as:kInternalError
 184                      reason:"CGDisplayRemoveReconfigurationCallback() failed, leaking the callback context!"];
 185         return;
 186     }
 187 
 188     [wrapper setJObject:NULL withEnv:env]; // more efficiant to pre-clear
 189 
 190     CFRelease(wrapper);
 191 
 192 JNF_COCOA_EXIT(env);
 193 }