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 
 128         static JNF_CLASS_CACHE(jc_CGraphicsEnvironment, "sun/awt/CGraphicsEnvironment");
 129         static JNF_MEMBER_CACHE(jm_displayReconfiguration, jc_CGraphicsEnvironment, "_displayReconfiguration", "(J)V");
 130         JNFCallVoidMethod(env, graphicsEnv, jm_displayReconfiguration);
 131     });
 132 }
 133 
 134 /*
 135  * Class:     sun_awt_CGraphicsEnvironment
 136  * Method:    registerDisplayReconfiguration
 137  * Signature: ()J
 138  */
 139 JNIEXPORT jlong JNICALL
 140 Java_sun_awt_CGraphicsEnvironment_registerDisplayReconfiguration
 141 (JNIEnv *env, jobject this)
 142 {
 143     jlong ret = 0L;
 144 
 145 JNF_COCOA_ENTER(env);
 146 
 147     JNFWeakJObjectWrapper *wrapper = [JNFWeakJObjectWrapper wrapperWithJObject:this withEnv:env];
 148     CFRetain(wrapper); // pin from ObjC-GC
 149 
 150     /* Register the callback */
 151     if (CGDisplayRegisterReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {
 152         [JNFException raise:env
 153                          as:kInternalError
 154                      reason:"CGDisplayRegisterReconfigurationCallback() failed"];
 155         return 0L;
 156     }
 157 
 158     ret = ptr_to_jlong(wrapper);
 159 
 160 JNF_COCOA_EXIT(env);
 161 
 162     return ret;
 163 }
 164 
 165 /*
 166  * Class:     sun_awt_CGraphicsEnvironment
 167  * Method:    deregisterDisplayReconfiguration
 168  * Signature: (J)V
 169  */
 170 JNIEXPORT void JNICALL
 171 Java_sun_awt_CGraphicsEnvironment_deregisterDisplayReconfiguration
 172 (JNIEnv *env, jobject this, jlong p)
 173 {
 174 JNF_COCOA_ENTER(env);
 175 
 176     JNFWeakJObjectWrapper *wrapper = (JNFWeakJObjectWrapper *)jlong_to_ptr(p);
 177     if (!wrapper) return;
 178 
 179     /* Remove the registration */
 180     if (CGDisplayRemoveReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {
 181         [JNFException raise:env
 182                          as:kInternalError
 183                      reason:"CGDisplayRemoveReconfigurationCallback() failed, leaking the callback context!"];
 184         return;
 185     }
 186 
 187     [wrapper setJObject:NULL withEnv:env]; // more efficiant to pre-clear
 188 
 189     CFRelease(wrapper);
 190 
 191 JNF_COCOA_EXIT(env);
 192 }