1 /*
   2  * Copyright (c) 2011, 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
  23  * questions.
  24  */
  25 
  26 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  27 
  28 #import "AWTSurfaceLayers.h"
  29 
  30 #import "jni_util.h"
  31 
  32 JNIEXPORT JAWT_DrawingSurfaceInfo* JNICALL awt_DrawingSurface_GetDrawingSurfaceInfo
  33 (JAWT_DrawingSurface* ds)
  34 {
  35     JAWT_DrawingSurfaceInfo* dsi = (JAWT_DrawingSurfaceInfo*)malloc(sizeof(JAWT_DrawingSurfaceInfo));
  36 
  37     JNIEnv *env = ds->env;
  38     jobject target = ds->target;
  39 
  40     static JNF_CLASS_CACHE(jc_Component, "java/awt/Component");
  41     static JNF_MEMBER_CACHE(jf_peer, jc_Component, "peer", "Ljava/awt/peer/ComponentPeer;");
  42     jobject peer = JNFGetObjectField(env, target, jf_peer);
  43 
  44     static JNF_CLASS_CACHE(jc_ComponentPeer, "sun/lwawt/LWComponentPeer");
  45     static JNF_MEMBER_CACHE(jf_platformComponent, jc_ComponentPeer,
  46                             "platformComponent", "Lsun/lwawt/PlatformComponent;");
  47     jobject platformComponent = JNFGetObjectField(env, peer, jf_platformComponent);
  48 
  49     static JNF_CLASS_CACHE(jc_PlatformComponent, "sun/lwawt/macosx/CPlatformComponent");
  50     static JNF_MEMBER_CACHE(jm_getPointer, jc_PlatformComponent, "getPointer", "()J");
  51     AWTSurfaceLayers *surfaceLayers = jlong_to_ptr(JNFCallLongMethod(env, platformComponent, jm_getPointer));
  52     // REMIND: assert(surfaceLayers)
  53 
  54     dsi->platformInfo = surfaceLayers;
  55     dsi->ds = ds;
  56 
  57     static JNF_MEMBER_CACHE(jf_x, jc_Component, "x", "I");
  58     static JNF_MEMBER_CACHE(jf_y, jc_Component, "y", "I");
  59     static JNF_MEMBER_CACHE(jf_width, jc_Component, "width", "I");
  60     static JNF_MEMBER_CACHE(jf_height, jc_Component, "height", "I");
  61 
  62     dsi->bounds.x = JNFGetIntField(env, target, jf_x);
  63     dsi->bounds.y = JNFGetIntField(env, target, jf_y);
  64     dsi->bounds.width = JNFGetIntField(env, target, jf_width);
  65     dsi->bounds.height = JNFGetIntField(env, target, jf_height);
  66 
  67     dsi->clipSize = 1;
  68     dsi->clip = &(dsi->bounds);
  69 
  70     return dsi;
  71 }
  72 
  73 JNIEXPORT jint JNICALL awt_DrawingSurface_Lock
  74 (JAWT_DrawingSurface* ds)
  75 {
  76     // TODO: implement
  77     return 0;
  78 }
  79 
  80 JNIEXPORT void JNICALL awt_DrawingSurface_Unlock
  81 (JAWT_DrawingSurface* ds)
  82 {
  83     // TODO: implement
  84 }
  85 
  86 JNIEXPORT void JNICALL awt_DrawingSurface_FreeDrawingSurfaceInfo
  87 (JAWT_DrawingSurfaceInfo* dsi)
  88 {
  89     free(dsi);
  90 }
  91 
  92 JNIEXPORT JAWT_DrawingSurface* JNICALL awt_GetDrawingSurface
  93 (JNIEnv* env, jobject target)
  94 {
  95     JAWT_DrawingSurface* ds = (JAWT_DrawingSurface*)malloc(sizeof(JAWT_DrawingSurface));
  96 
  97     // TODO: "target instanceof" check
  98 
  99     ds->env = env;
 100     ds->target = (*env)->NewGlobalRef(env, target);
 101     ds->Lock = awt_DrawingSurface_Lock;
 102     ds->GetDrawingSurfaceInfo = awt_DrawingSurface_GetDrawingSurfaceInfo;
 103     ds->FreeDrawingSurfaceInfo = awt_DrawingSurface_FreeDrawingSurfaceInfo;
 104     ds->Unlock = awt_DrawingSurface_Unlock;
 105 
 106     return ds;
 107 }
 108 
 109 JNIEXPORT void JNICALL awt_FreeDrawingSurface
 110 (JAWT_DrawingSurface* ds)
 111 {
 112     JNIEnv *env = ds->env;
 113     (*env)->DeleteGlobalRef(env, ds->target);
 114     free(ds);
 115 }
 116 
 117 JNIEXPORT void JNICALL awt_Lock
 118 (JNIEnv* env)
 119 {
 120     // TODO: implement
 121 }
 122 
 123 JNIEXPORT void JNICALL awt_Unlock
 124 (JNIEnv* env)
 125 {
 126     // TODO: implement
 127 }
 128 
 129 JNIEXPORT jobject JNICALL awt_GetComponent
 130 (JNIEnv* env, void* platformInfo)
 131 {
 132     // TODO: implement
 133     return NULL;
 134 }
 135 
 136 // EmbeddedFrame support
 137 
 138 static char *const embeddedClassName = "sun/lwawt/macosx/CViewEmbeddedFrame";
 139 
 140 JNIEXPORT jobject JNICALL awt_CreateEmbeddedFrame
 141 (JNIEnv* env, void* platformInfo)
 142 {
 143     static jmethodID mid = NULL;
 144     static jclass cls;
 145     if (mid == NULL) {
 146         cls = (*env)->FindClass(env, embeddedClassName);
 147         CHECK_NULL_RETURN(cls, NULL);
 148         mid = (*env)->GetMethodID(env, cls, "<init>", "(J)V");
 149         CHECK_NULL_RETURN(mid, NULL);
 150     }
 151     return (*env)->NewObject(env, cls, mid, platformInfo);
 152 }
 153 
 154 JNIEXPORT void JNICALL awt_SetBounds
 155 (JNIEnv *env, jobject embeddedFrame, jint x, jint y, jint w, jint h)
 156 {
 157     static jmethodID mid = NULL;
 158     if (mid == NULL) {
 159         jclass cls = (*env)->FindClass(env, embeddedClassName);
 160         CHECK_NULL(cls);
 161         mid = (*env)->GetMethodID(env, cls, "setBoundsPrivate", "(IIII)V");
 162         CHECK_NULL(mid);
 163     }
 164     (*env)->CallVoidMethod(env, embeddedFrame, mid, x, y, w, h);
 165 }
 166 
 167 JNIEXPORT void JNICALL awt_SynthesizeWindowActivation
 168 (JNIEnv *env, jobject embeddedFrame, jboolean doActivate)
 169 {
 170     static jmethodID mid = NULL;
 171     if (mid == NULL) {
 172         jclass cls = (*env)->FindClass(env, embeddedClassName);
 173         CHECK_NULL(cls);
 174         mid = (*env)->GetMethodID(env, cls, "synthesizeWindowActivation", "(Z)V");
 175         CHECK_NULL(mid);
 176     }
 177     (*env)->CallVoidMethod(env, embeddedFrame, mid, doActivate);
 178 }