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