1 /*
   2  * Copyright (c) 2011, 2014, 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 #ifndef GLASS_GENERAL_H
  26 #define        GLASS_GENERAL_H
  27 
  28 #include <jni.h>
  29 
  30 
  31 #include <stdint.h>
  32 #include <X11/Xlib.h>
  33 #include <gdk/gdk.h>
  34 #include <gtk/gtk.h>
  35 
  36 #define JLONG_TO_PTR(value) ((void*)(intptr_t)(value))
  37 #define PTR_TO_JLONG(value) ((jlong)(intptr_t)(value))
  38 
  39 #define FILE_PREFIX "file://"
  40 #define URI_LIST_COMMENT_PREFIX "#"
  41 #define URI_LIST_LINE_BREAK "\r\n"
  42 
  43 extern JNIEnv* mainEnv; // Use only with main loop thread!!!
  44 
  45 #include <exception>
  46 
  47 struct jni_exception: public std::exception {
  48     jni_exception(jthrowable _th): throwable(_th), message() {
  49             jclass jc = mainEnv->FindClass("java/lang/Throwable");
  50             if (mainEnv->ExceptionOccurred()) {
  51                 mainEnv->ExceptionDescribe();
  52                 mainEnv->ExceptionClear();
  53             }
  54             jmethodID jmid = mainEnv->GetMethodID(jc, "getMessage", "()Ljava/lang/String;");
  55             if (mainEnv->ExceptionOccurred()) {
  56                 mainEnv->ExceptionDescribe();
  57                 mainEnv->ExceptionClear();
  58             }
  59             jmessage = (jstring)mainEnv->CallObjectMethod(throwable, jmid);
  60             message = jmessage == NULL ? "" : mainEnv->GetStringUTFChars(jmessage, NULL);
  61     }
  62     const char *what() const throw()
  63     {
  64         return message;
  65     }
  66     ~jni_exception() throw(){
  67         if (jmessage && message) {
  68             mainEnv->ReleaseStringUTFChars(jmessage, message);
  69         }
  70     }
  71 private:
  72     jthrowable throwable;
  73     const char *message;
  74     jstring jmessage;
  75 };
  76 
  77 #define EXCEPTION_OCCURED(env) (check_and_clear_exception(env))
  78 
  79 #define CHECK_JNI_EXCEPTION(env) \
  80         if (env->ExceptionCheck()) {\
  81             check_and_clear_exception(env);\
  82             return;\
  83         }
  84 
  85 #define CHECK_JNI_EXCEPTION_RET(env, ret) \
  86         if (env->ExceptionCheck()) {\
  87             check_and_clear_exception(env);\
  88             return ret;\
  89         }
  90 
  91 #define JNI_EXCEPTION_TO_CPP(env) \
  92         if (env->ExceptionCheck()) {\
  93             check_and_clear_exception(env);\
  94             throw jni_exception(env->ExceptionOccurred());\
  95         }
  96 
  97 #define HANDLE_MEM_ALLOC_ERROR(env, nativePtr, message) \
  98         ((nativePtr == NULL) && glass_throw_oom(env, message))
  99 
 100     gpointer glass_try_malloc0_n(gsize m, gsize n);
 101 
 102     gpointer glass_try_malloc_n(gsize m, gsize n);
 103 
 104     typedef struct {
 105         jobject runnable;
 106         int flag;
 107     } RunnableContext;
 108 
 109     extern char const * const GDK_WINDOW_DATA_CONTEXT;
 110 
 111     GdkCursor* get_native_cursor(int type);
 112 
 113     // JNI global references
 114     extern jclass jStringCls; // java.lang.String
 115 
 116     extern jclass jByteBufferCls; //java.nio.ByteBuffer
 117     extern jmethodID jByteBufferArray; //java.nio.ByteBuffer#array()[B
 118     extern jmethodID jByteBufferWrap; //java.nio.ByteBuffer#wrap([B)Ljava/nio/ByteBuffer;
 119 
 120     extern jclass jRunnableCls; // java.lang.Runnable
 121     extern jmethodID jRunnableRun; // java.lang.Runnable#run ()V
 122 
 123     extern jclass jArrayListCls; // java.util.ArrayList
 124     extern jmethodID jArrayListInit; // java.util.ArrayList#<init> ()V
 125     extern jmethodID jArrayListAdd; // java.util.ArrayList#add (Ljava/lang/Object;)Z
 126     extern jmethodID jArrayListGetIdx; //java.util.ArryList#get (I)Ljava/lang/Object;
 127 
 128     extern jmethodID jPixelsAttachData; // com.sun.class.ui.Pixels#attachData (J)V
 129     extern jclass jGtkPixelsCls; // com.sun.class.ui.gtk.GtkPixels
 130     extern jmethodID jGtkPixelsInit; // com.sun.class.ui.gtk.GtkPixels#<init> (IILjava/nio/ByteBuffer;)V
 131 
 132     extern jclass jScreenCls;   // com.sun.glass.ui.Screen
 133     extern jmethodID jScreenInit; // com.sun.glass.ui.Screen#<init> ()V
 134     extern jmethodID jScreenNotifySettingsChanged; // com.sun.glass.ui.Screen#notifySettingsChanged ()V
 135     extern jmethodID jScreenGetScreenForLocation; //com.sun.glass.ui.Screen#getScreenForLocation(JJ)Lcom.sun.glass.ui.Screen;
 136     extern jmethodID jScreenGetNativeScreen; //com.sun.glass.ui.Screen#getNativeScreen()J
 137 
 138     extern jmethodID jViewNotifyResize; // com.sun.glass.ui.View#notifyResize (II)V
 139     extern jmethodID jViewNotifyMouse; // com.sun.glass.ui.View#notifyMouse (IIIIIIIZZ)V
 140     extern jmethodID jViewNotifyRepaint; // com.sun.glass.ui.View#notifyRepaint (IIII)V
 141     extern jmethodID jViewNotifyKey; // com.sun.glass.ui.View#notifyKey (II[CI)V
 142     extern jmethodID jViewNotifyView; //com.sun.glass.ui.View#notifyView (I)V
 143     extern jmethodID jViewNotifyDragEnter; //com.sun.glass.ui.View#notifyDragEnter (IIIII)I
 144     extern jmethodID jViewNotifyDragOver; //com.sun.glass.ui.View#notifyDragOver (IIIII)I
 145     extern jmethodID jViewNotifyDragDrop; //com.sun.glass.ui.View#notifyDragDrop (IIIII)I
 146     extern jmethodID jViewNotifyDragLeave; //com.sun.glass.ui.View#notifyDragLeave ()V
 147     extern jmethodID jViewNotifyScroll; //com.sun.glass.ui.View#notifyScroll (IIIIDDIIIIIDD)V
 148     extern jmethodID jViewNotifyInputMethod; //com.sun.glass.ui.View#notifyInputMethod (Ljava/lang/String;[I[I[BIII)V
 149     extern jmethodID jViewNotifyInputMethodDraw; //com.sun.glass.ui.gtk.GtkView#notifyInputMethodDraw (Ljava/lang/String;III[B)V
 150     extern jmethodID jViewNotifyInputMethodCaret; //com.sun.glass.ui.gtk.GtkView#notifyInputMethodCaret (III)V
 151     extern jmethodID jViewNotifyPreeditMode; //com.sun.glass.ui.gtk.GtkView#notifyPreeditMode (Z)V
 152     extern jmethodID jViewNotifyMenu; //com.sun.glass.ui.View#notifyMenu (IIIIZ)V
 153     extern jfieldID  jViewPtr; //com.sun.glass.ui.View.ptr
 154 
 155     extern jmethodID jWindowNotifyResize; // com.sun.glass.ui.Window#notifyResize (III)V
 156     extern jmethodID jWindowNotifyMove; // com.sun.glass.ui.Window#notifyMove (II)V
 157     extern jmethodID jWindowNotifyDestroy; // com.sun.glass.ui.Window#notifyDestroy ()V
 158     extern jmethodID jWindowNotifyClose; // com.sun.glass.ui.Window#notifyClose ()V
 159     extern jmethodID jWindowNotifyFocus; // com.sun.glass.ui.Window#notifyFocus (I)V
 160     extern jmethodID jWindowNotifyFocusDisabled; // com.sun.glass.ui.Window#notifyFocusDisabled ()V
 161     extern jmethodID jWindowNotifyFocusUngrab; // com.sun.glass.ui.Window#notifyFocusUngrab ()V
 162     extern jmethodID jWindowNotifyMoveToAnotherScreen; // com.sun.glass.ui.Window#notifyMoveToAnotherScreen (Lcom/sun/glass/ui/Screen;)V
 163     extern jmethodID jWindowNotifyDelegatePtr; //com.sun.glass.ui.Window#notifyDelegatePtr (J)V
 164     extern jmethodID jWindowNotifyLevelChanged; //com.sun.glass.ui.Window#notifyLevelChanged (I)V
 165 
 166     extern jmethodID jWindowIsEnabled; // com.sun.glass.ui.Window#isEnabled ()Z
 167     extern jfieldID jWindowPtr; // com.sun.glass.ui.Window#ptr
 168     extern jfieldID jCursorPtr; // com.sun.glass.ui.Cursor#ptr
 169 
 170     extern jmethodID jGtkWindowNotifyStateChanged; // com.sun.glass.ui.GtkWindow#notifyStateChanged (I)V
 171 
 172     extern jmethodID jClipboardContentChanged; // com.sun.glass.ui.Clipboard#contentChanged ()V
 173 
 174     extern jmethodID jSizeInit; // com.sun.class.ui.Size#<init> ()V
 175 
 176     extern jmethodID jMapGet; // java.util.Map#get(Ljava/lang/Object;)Ljava/lang/Object;
 177     extern jmethodID jMapKeySet; // java.util.Map#keySet()Ljava/util/Set;
 178     extern jmethodID jMapContainsKey; // java.util.Map#containsKey(Ljava/lang/Object;)Z
 179 
 180     extern jclass jHashSetCls; // java.util.HashSet
 181     extern jmethodID jHashSetInit; // java.util.HashSet#<init> ()V
 182 
 183     extern jmethodID jSetAdd; //java.util.Set#add (Ljava/lang/Object;)Z
 184     extern jmethodID jSetSize; //java.util.Set#size ()I
 185     extern jmethodID jSetToArray; //java.util.Set#toArray ([Ljava/lang/Object;)[Ljava/lang/Object;
 186 
 187     extern jmethodID jIterableIterator; // java.lang.Iterable#iterator()Ljava/util/Iterator;
 188     extern jmethodID jIteratorHasNext; // java.util.Iterator#hasNext()Z;
 189     extern jmethodID jIteratorNext; // java.util.Iterator#next()Ljava/lang/Object;
 190 
 191     extern jclass jApplicationCls; //com.sun.glass.ui.gtk.GtkApplication
 192     extern jfieldID jApplicationDisplay; //com.sun.glass.ui.gtk.GtkApplication#display
 193     extern jfieldID jApplicationScreen; //com.sun.glass.ui.gtk.GtkApplication#screen
 194     extern jfieldID jApplicationVisualID; //com.sun.glass.ui.gtk.GtkApplication#visualID
 195     extern jmethodID jApplicationReportException; // reportException(Ljava/lang/Throwable;)V
 196     extern jmethodID jApplicationGetApplication; // GetApplication()()Lcom/sun/glass/ui/Application;
 197     extern jmethodID jApplicationGetName; // getName()Ljava/lang/String;
 198 
 199 #ifdef VERBOSE
 200 #define LOG0(msg) {printf(msg);fflush(stdout);}
 201 #define LOG1(msg, param) {printf(msg, param);fflush(stdout);}
 202 #define LOG2(msg, param1, param2) {printf(msg, param1, param2);fflush(stdout);}
 203 #define LOG3(msg, param1, param2, param3) {printf(msg, param1, param2, param3);fflush(stdout);}
 204 #define LOG4(msg, param1, param2, param3, param4) {printf(msg, param1, param2, param3, param4);fflush(stdout);}
 205 #define LOG5(msg, param1, param2, param3, param4, param5) {printf(msg, param1, param2, param3, param4, param5);fflush(stdout);}
 206 
 207 #define LOG_STRING_ARRAY(env, array) dump_jstring_array(env, array);
 208 
 209 #define ERROR0(msg) {fprintf(stderr, msg);fflush(stderr);}
 210 #define ERROR1(msg, param) {fprintf(stderr, msg, param);fflush(stderr);}
 211 #define ERROR2(msg, param1, param2) {fprintf(stderr, msg, param1, param2);fflush(stderr);}
 212 #define ERROR3(msg, param1, param2, param3) {fprintf(stderr, msg, param1, param2, param3);fflush(stderr);}
 213 #define ERROR4(msg, param1, param2, param3, param4) {fprintf(stderr, msg, param1, param2, param3, param4);fflush(stderr);}
 214 #else
 215 #define LOG0(msg)
 216 #define LOG1(msg, param)
 217 #define LOG2(msg, param1, param2)
 218 #define LOG3(msg, param1, param2, param3)
 219 #define LOG4(msg, param1, param2, param3, param4)
 220 #define LOG5(msg, param1, param2, param3, param4, param5)
 221 
 222 #define LOG_STRING_ARRAY(env, array)
 223 
 224 #define ERROR0(msg)
 225 #define ERROR1(msg, param)
 226 #define ERROR2(msg, param1, param2)
 227 #define ERROR3(msg, param1, param2, param3)
 228 #define ERROR4(msg, param1, param2, param3, param4)
 229 #endif
 230 
 231 #define LOG_EXCEPTION(env) check_and_clear_exception(env);
 232 
 233     gchar* get_application_name();
 234     void glass_throw_exception(JNIEnv * env,
 235             const char * exceptionClass,
 236             const char * exceptionMessage);
 237     int glass_throw_oom(JNIEnv * env, const char * exceptionMessage);
 238     void dump_jstring_array(JNIEnv*, jobjectArray);
 239 
 240     guint8* convert_BGRA_to_RGBA(const int* pixels, int stride, int height);
 241 
 242     gboolean check_and_clear_exception(JNIEnv *env);
 243 
 244     jboolean is_display_valid();
 245 
 246     gsize get_files_count(gchar **uris);
 247 
 248     jobject uris_to_java(JNIEnv *env, gchar **uris, gboolean files);
 249 
 250 #endif        /* GLASS_GENERAL_H */
 251