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