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