1 /*
   2  * Copyright (c) 2010, 2013, 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 #include "GlassView.h"
  27 #include "GlassViewGL.h"
  28 
  29 
  30 static inline UIView<GlassView>* getGlassView(JNIEnv *env, jlong jPtr)
  31 {
  32     if (jPtr != 0L)
  33     {
  34         return (UIView<GlassView>*)jlong_to_ptr(jPtr);
  35     }
  36     else
  37     {
  38         return nil;
  39     }
  40 }
  41 
  42 // to be called on main thread
  43 jlong Do_com_sun_glass_ui_ios_IosView__1create(JNIEnv *env, jobject jView, jobject jCapabilities)
  44 {
  45     UIView<GlassView> *view;
  46     
  47     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  48     
  49     {
  50         view = [[GlassViewGL alloc] initWithFrame:CGRectMake(0,0,1,1) withJview:jView withJproperties:jCapabilities];
  51         (*env)->SetLongField(env, jView, (*env)->GetFieldID(env, mat_jViewClass, "nativePtr", "J"), ptr_to_jlong(view));
  52     }
  53     [pool drain];
  54     
  55     GLASS_CHECK_EXCEPTION(env);
  56     
  57     return ptr_to_jlong(view);
  58 }
  59 
  60 // to be called on main thread
  61 void Do_com_sun_glass_ui_ios_IosView__1close(JNIEnv *env, jlong jPtr)
  62 {
  63     UIView<GlassView> *view = getGlassView(env, jPtr);
  64     if (view != nil) {
  65         [view removeFromSuperview];
  66         [view release];
  67     }
  68 }
  69 
  70 
  71 @interface GlassViewDispatcher : NSObject
  72 {
  73 @public
  74     jobject jView;
  75     jobject jCapabilities;
  76     jlong   jPtr;
  77     jlong   jlongReturn;
  78 }
  79 @end
  80 
  81 
  82 
  83 @implementation GlassViewDispatcher
  84 
  85 - (void) _1create
  86 {
  87     self->jlongReturn = Do_com_sun_glass_ui_ios_IosView__1create(jEnv, self->jView, self->jCapabilities);
  88 }
  89 
  90 
  91 - (void)Do_com_sun_glass_ui_ios_IosView__1close
  92 {
  93     GET_MAIN_JENV;
  94     Do_com_sun_glass_ui_ios_IosView__1close(env, self->jPtr);
  95 }
  96 
  97 @end
  98 
  99 
 100 
 101 /*
 102  * Class:     com_sun_glass_ui_View
 103  * Method:    _create
 104  * Signature: (Ljava/util/Map;)J
 105  */
 106 JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_ios_IosView__1create
 107 (JNIEnv *env, jobject jview, jobject jCapabilities) {
 108     
 109     jlong value;
 110     
 111     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 112     GLASS_POOL_ENTER;
 113     {
 114         GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1create");
 115         jobject jViewRef = (*env)->NewGlobalRef(env, jview);
 116         jobject jCapabilitiesRef = (*env)->NewGlobalRef(env, jCapabilities);
 117         {
 118             if ([[NSThread currentThread] isMainThread] == YES)
 119             {
 120                 // Create GlassViewGL
 121                 return Do_com_sun_glass_ui_ios_IosView__1create(env, jview, jCapabilities);
 122             }
 123             else
 124             {
 125                 GlassViewDispatcher *dispatcher = [[GlassViewDispatcher alloc] autorelease];
 126                 dispatcher->jView = jViewRef;
 127                 dispatcher->jCapabilities = jCapabilitiesRef;
 128                 // Create GlassViewGL on main thread
 129                 [dispatcher performSelectorOnMainThread:@selector(_1create) withObject:dispatcher waitUntilDone:YES]; // block and wait for the return value
 130                 value = dispatcher->jlongReturn;
 131             }
 132         }
 133         (*env)->DeleteGlobalRef(env, jCapabilitiesRef);
 134         (*env)->DeleteGlobalRef(env, jViewRef);
 135     }
 136     GLASS_POOL_EXIT;
 137     GLASS_CHECK_EXCEPTION(env);
 138     return value;
 139 }
 140 
 141 
 142 /*
 143  * Class:     com_sun_glass_ui_ios_IosView
 144  * Method:    _getNativeView
 145  * Signature: (J)J
 146  */
 147 JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_ios_IosView__1getNativeView
 148 (JNIEnv *env, jobject jview, jlong ptr) {
 149     // On iOS the native jPtr is the UIView* instance itself
 150     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1getNativeView - returns ptr");
 151     return ptr;
 152 }
 153 
 154 
 155 /*
 156  * Class:     com_sun_glass_ui_ios_IosView
 157  * Method:    _getNativeFrameBuffer
 158  * Signature: (J)I
 159  */
 160 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_ios_IosView__1getNativeFrameBuffer
 161 (JNIEnv *env, jobject jView, jlong ptr)
 162 {
 163 
 164     jint framebuffer = 0;
 165 
 166     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 167     GLASS_POOL_ENTER;
 168     {
 169         // get FBO associated with given GlassViewGL
 170         UIView<GlassView> *view = getGlassView(env, ptr);
 171         if (view) {
 172             framebuffer = ((GlassViewGL*)view)->frameBuffer;
 173         }
 174     }
 175     GLASS_POOL_EXIT;
 176     GLASS_CHECK_EXCEPTION(env);
 177 
 178     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView_1getNativeFrameBuffer() returns %d",framebuffer);
 179 
 180     return framebuffer;
 181 }
 182 
 183 
 184 /*
 185  * Class:     com_sun_glass_ui_ios_IosView
 186  * Method:    _getX
 187  * Signature: (J)I
 188  */
 189 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_ios_IosView__1getX
 190 (JNIEnv *env, jobject jview, jlong ptr) {
 191     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1getX - returns 0");
 192         
 193     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 194     GLASS_CHECK_EXCEPTION(env);
 195     
 196     return 0; //on iOS Windows content rect equals it's frame, thus View's origin equals Window's origin
 197 }
 198 
 199 
 200 /*
 201  * Class:     com_sun_glass_ui_ios_IosView
 202  * Method:    _getY
 203  * Signature: (J)I
 204  */
 205 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_ios_IosView__1getY
 206 (JNIEnv *env, jobject jview, jlong ptr) {
 207     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1getY - returning 0");
 208     
 209     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 210     GLASS_CHECK_EXCEPTION(env);
 211     
 212     return 0; //on iOS Windows content rect equals it's frame, thus View's origin equals Window's origin
 213 }
 214 
 215 
 216 /*
 217  * Class:     com_sun_glass_ui_ios_IosView
 218  * Method:    _close
 219  * Signature: (J)Z
 220  */
 221 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_ios_IosView__1close
 222 (JNIEnv *env, jclass jview, jlong ptr) {
 223     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1close called.");
 224    
 225     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 226     GLASS_POOL_ENTER;
 227     {
 228         if ([[NSThread currentThread] isMainThread] == YES)
 229         {
 230             Do_com_sun_glass_ui_ios_IosView__1close(env, ptr);
 231         }
 232         else
 233         {
 234             GlassViewDispatcher *dispatcher = [[GlassViewDispatcher alloc] autorelease];
 235             dispatcher->jPtr = ptr;
 236             [dispatcher performSelectorOnMainThread:@selector(Do_com_sun_glass_ui_ios_IosView__1close) withObject:dispatcher waitUntilDone:YES];
 237         }
 238     }
 239     GLASS_POOL_EXIT;
 240     GLASS_CHECK_EXCEPTION(env);
 241     
 242     return JNI_TRUE;
 243 }
 244 
 245 
 246 /*
 247  * Class:     com_sun_glass_ui_ios_IosView
 248  * Method:    _begin
 249  * Signature: (J)V
 250  */
 251 JNIEXPORT void JNICALL Java_com_sun_glass_ui_ios_IosView__1begin
 252 (JNIEnv *env, jobject jview, jlong ptr) {
 253     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1begin");
 254     
 255     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 256     UIView<GlassView> *view = getGlassView(env, ptr);
 257     GLASS_POOL_PUSH; // it will be popped by "_end"
 258     {
 259         // Prepare GlassView for redraw
 260         [view retain];
 261         [view begin];
 262     }
 263 }
 264 
 265 
 266 /*
 267  * Class:     com_sun_glass_ui_ios_IosView
 268  * Method:    _end
 269  * Signature: (J)V
 270  */
 271 JNIEXPORT void JNICALL Java_com_sun_glass_ui_ios_IosView__1end
 272 (JNIEnv *env, jobject jview, jlong ptr) {
 273     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1end");
 274     
 275     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 276     UIView<GlassView> *view = getGlassView(env, ptr);
 277     {
 278         [view end];
 279         [view release];
 280         // GlassView was redrawn
 281     }
 282     GLASS_POOL_POP;
 283 }
 284 
 285 
 286 /*
 287  * Class:     com_sun_glass_ui_ios_IosView
 288  * Method:    _scheduleRepaint
 289  * Signature: (J)V
 290  */
 291 JNIEXPORT void JNICALL Java_com_sun_glass_ui_ios_IosView__1scheduleRepaint
 292 (JNIEnv *env, jobject jview, jlong ptr) {
 293     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1scheduleRepaint");
 294     
 295     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 296     GLASS_POOL_ENTER;
 297     {
 298         UIView<GlassView> *view = getGlassView(env, ptr);
 299         [view setNeedsDisplay];
 300     }
 301     GLASS_POOL_EXIT;
 302     GLASS_CHECK_EXCEPTION(env);
 303 }
 304 
 305 
 306 /*
 307  * Class:     com_sun_glass_ui_ios_IosView
 308  * Method:    _setParent
 309  * Signature: (JJ)V
 310  */
 311 JNIEXPORT void JNICALL Java_com_sun_glass_ui_ios_IosView__1setParent
 312 (JNIEnv *env, jobject jView, jlong jPtr, jlong parentPtr)
 313 {
 314     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1setParent. Code template");
 315     
 316     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 317 }
 318 
 319 
 320 /*
 321  * Class:     com_sun_glass_ui_ios_IosView
 322  * Method:    _enterFullscreen
 323  * Signature: (ZZZ)V
 324  */
 325 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_ios_IosView__1enterFullscreen
 326 (JNIEnv *env, jobject jView, jlong jPtr, jboolean jAnimate, jboolean jKeepRatio, jboolean jHideCursor)
 327 {
 328     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1enterFullscreen. Code template.");
 329     
 330     return JNI_FALSE;
 331 }
 332 
 333 
 334 /*
 335  * Class:     com_sun_glass_ui_ios_IosView
 336  * Method:    _exitFullscreen
 337  * Signature: (Z)V
 338  */
 339 JNIEXPORT void JNICALL Java_com_sun_glass_ui_ios_IosView__1exitFullscreen
 340 (JNIEnv *env, jobject jView, jlong jPtr, jboolean jAnimate)
 341 {
 342     GLASS_LOG("Java_com_sun_glass_ui_ios_IosView__1exitFullscreen. Code template.");
 343 }