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 <gdk/gdkx.h>
  34 #include <gtk/gtk.h>
  35 
  36 #if GTK_CHECK_VERSION(3, 0, 0)
  37 #define GLASS_GTK3
  38 #endif
  39 
  40 
  41 #define JLONG_TO_PTR(value) ((void*)(intptr_t)(value))
  42 #define PTR_TO_JLONG(value) ((jlong)(intptr_t)(value))
  43 
  44 #define FILE_PREFIX "file://"
  45 #define URI_LIST_COMMENT_PREFIX "#"
  46 #define URI_LIST_LINE_BREAK "\r\n"
  47 
  48 extern JNIEnv* mainEnv; // Use only with main loop thread!!!
  49 extern JavaVM* javaVM;
  50 
  51 #define GLASS_GDK_KEY_CONSTANT(key) (GDK_KEY_ ## key)
  52 
  53 #include <exception>
  54 
  55 struct jni_exception: public std::exception {
  56     jni_exception(jthrowable _th): throwable(_th), message() {
  57             jclass jc = mainEnv->FindClass("java/lang/Throwable");
  58             if (mainEnv->ExceptionOccurred()) {
  59                 mainEnv->ExceptionDescribe();
  60                 mainEnv->ExceptionClear();
  61             }
  62             jmethodID jmid = mainEnv->GetMethodID(jc, "getMessage", "()Ljava/lang/String;");
  63             if (mainEnv->ExceptionOccurred()) {
  64                 mainEnv->ExceptionDescribe();
  65                 mainEnv->ExceptionClear();
  66             }
  67             jmessage = (jstring)mainEnv->CallObjectMethod(throwable, jmid);
  68             message = jmessage == NULL ? "" : mainEnv->GetStringUTFChars(jmessage, NULL);
  69     }
  70     const char *what() const throw()
  71     {
  72         return message;
  73     }
  74     ~jni_exception() throw(){
  75         if (jmessage && message) {
  76             mainEnv->ReleaseStringUTFChars(jmessage, message);
  77         }
  78     }
  79 private:
  80     jthrowable throwable;
  81     const char *message;
  82     jstring jmessage;
  83 };
  84 
  85 #define EXCEPTION_OCCURED(env) (check_and_clear_exception(env))
  86 
  87 #define CHECK_JNI_EXCEPTION(env) \
  88         if (env->ExceptionCheck()) {\
  89             check_and_clear_exception(env);\
  90             return;\
  91         }
  92 
  93 #define CHECK_JNI_EXCEPTION_RET(env, ret) \
  94         if (env->ExceptionCheck()) {\
  95             check_and_clear_exception(env);\
  96             return ret;\
  97         }
  98 
  99 #define JNI_EXCEPTION_TO_CPP(env) \
 100         if (env->ExceptionCheck()) {\
 101             check_and_clear_exception(env);\
 102             throw jni_exception(env->ExceptionOccurred());\
 103         }
 104 
 105 #define HANDLE_MEM_ALLOC_ERROR(env, nativePtr, message) \
 106         ((nativePtr == NULL) && glass_throw_oom(env, message))
 107 
 108     gpointer glass_try_malloc0_n(gsize m, gsize n);
 109 
 110     gpointer glass_try_malloc_n(gsize m, gsize n);
 111 
 112     typedef struct {
 113         jobject runnable;
 114         int flag;
 115     } RunnableContext;
 116 
 117     extern char const * const GDK_WINDOW_DATA_CONTEXT;
 118 
 119     GdkCursor* get_native_cursor(int type);
 120 
 121     // JNI global references
 122     extern jclass jStringCls; // java.lang.String
 123 
 124     extern jclass jByteBufferCls; //java.nio.ByteBuffer
 125     extern jmethodID jByteBufferArray; //java.nio.ByteBuffer#array()[B
 126     extern jmethodID jByteBufferWrap; //java.nio.ByteBuffer#wrap([B)Ljava/nio/ByteBuffer;
 127 
 128     extern jclass jRunnableCls; // java.lang.Runnable
 129     extern jmethodID jRunnableRun; // java.lang.Runnable#run ()V
 130 
 131     extern jclass jArrayListCls; // java.util.ArrayList
 132     extern jmethodID jArrayListInit; // java.util.ArrayList#<init> ()V
 133     extern jmethodID jArrayListAdd; // java.util.ArrayList#add (Ljava/lang/Object;)Z
 134     extern jmethodID jArrayListGetIdx; //java.util.ArryList#get (I)Ljava/lang/Object;
 135 
 136     extern jmethodID jPixelsAttachData; // com.sun.class.ui.Pixels#attachData (J)V
 137     extern jclass jGtkPixelsCls; // com.sun.class.ui.gtk.GtkPixels
 138     extern jmethodID jGtkPixelsInit; // com.sun.class.ui.gtk.GtkPixels#<init> (IILjava/nio/ByteBuffer;)V
 139 
 140     extern jclass jScreenCls;   // com.sun.glass.ui.Screen
 141     extern jmethodID jScreenInit; // com.sun.glass.ui.Screen#<init> ()V
 142     extern jmethodID jScreenNotifySettingsChanged; // com.sun.glass.ui.Screen#notifySettingsChanged ()V
 143     extern jmethodID jScreenGetScreenForLocation; //com.sun.glass.ui.Screen#getScreenForLocation(JJ)Lcom.sun.glass.ui.Screen;
 144     extern jmethodID jScreenGetNativeScreen; //com.sun.glass.ui.Screen#getNativeScreen()J
 145 
 146     extern jmethodID jViewNotifyResize; // com.sun.glass.ui.View#notifyResize (II)V
 147     extern jmethodID jViewNotifyMouse; // com.sun.glass.ui.View#notifyMouse (IIIIIIIZZ)V
 148     extern jmethodID jViewNotifyRepaint; // com.sun.glass.ui.View#notifyRepaint (IIII)V
 149     extern jmethodID jViewNotifyKey; // com.sun.glass.ui.View#notifyKey (II[CI)V
 150     extern jmethodID jViewNotifyView; //com.sun.glass.ui.View#notifyView (I)V
 151     extern jmethodID jViewNotifyDragEnter; //com.sun.glass.ui.View#notifyDragEnter (IIIII)I
 152     extern jmethodID jViewNotifyDragOver; //com.sun.glass.ui.View#notifyDragOver (IIIII)I
 153     extern jmethodID jViewNotifyDragDrop; //com.sun.glass.ui.View#notifyDragDrop (IIIII)I
 154     extern jmethodID jViewNotifyDragLeave; //com.sun.glass.ui.View#notifyDragLeave ()V
 155     extern jmethodID jViewNotifyScroll; //com.sun.glass.ui.View#notifyScroll (IIIIDDIIIIIDD)V
 156     extern jmethodID jViewNotifyInputMethod; //com.sun.glass.ui.View#notifyInputMethod (Ljava/lang/String;[I[I[BIII)V
 157     extern jmethodID jViewNotifyInputMethodDraw; //com.sun.glass.ui.gtk.GtkView#notifyInputMethodDraw (Ljava/lang/String;III[B)V
 158     extern jmethodID jViewNotifyInputMethodCaret; //com.sun.glass.ui.gtk.GtkView#notifyInputMethodCaret (III)V
 159     extern jmethodID jViewNotifyPreeditMode; //com.sun.glass.ui.gtk.GtkView#notifyPreeditMode (Z)V
 160     extern jmethodID jViewNotifyMenu; //com.sun.glass.ui.View#notifyMenu (IIIIZ)V
 161     extern jfieldID  jViewPtr; //com.sun.glass.ui.View.ptr
 162 
 163     extern jmethodID jWindowNotifyResize; // com.sun.glass.ui.Window#notifyResize (III)V
 164     extern jmethodID jWindowNotifyMove; // com.sun.glass.ui.Window#notifyMove (II)V
 165     extern jmethodID jWindowNotifyDestroy; // com.sun.glass.ui.Window#notifyDestroy ()V
 166     extern jmethodID jWindowNotifyClose; // com.sun.glass.ui.Window#notifyClose ()V
 167     extern jmethodID jWindowNotifyFocus; // com.sun.glass.ui.Window#notifyFocus (I)V
 168     extern jmethodID jWindowNotifyFocusDisabled; // com.sun.glass.ui.Window#notifyFocusDisabled ()V
 169     extern jmethodID jWindowNotifyFocusUngrab; // com.sun.glass.ui.Window#notifyFocusUngrab ()V
 170     extern jmethodID jWindowNotifyMoveToAnotherScreen; // com.sun.glass.ui.Window#notifyMoveToAnotherScreen (Lcom/sun/glass/ui/Screen;)V
 171     extern jmethodID jWindowNotifyDelegatePtr; //com.sun.glass.ui.Window#notifyDelegatePtr (J)V
 172     extern jmethodID jWindowNotifyLevelChanged; //com.sun.glass.ui.Window#notifyLevelChanged (I)V
 173 
 174     extern jmethodID jWindowIsEnabled; // com.sun.glass.ui.Window#isEnabled ()Z
 175     extern jfieldID jWindowPtr; // com.sun.glass.ui.Window#ptr
 176     extern jfieldID jCursorPtr; // com.sun.glass.ui.Cursor#ptr
 177 
 178     extern jmethodID jGtkWindowNotifyStateChanged; // com.sun.glass.ui.GtkWindow#notifyStateChanged (I)V
 179 
 180     extern jmethodID jClipboardContentChanged; // com.sun.glass.ui.Clipboard#contentChanged ()V
 181 
 182     extern jmethodID jSizeInit; // com.sun.class.ui.Size#<init> ()V
 183 
 184     extern jmethodID jMapGet; // java.util.Map#get(Ljava/lang/Object;)Ljava/lang/Object;
 185     extern jmethodID jMapKeySet; // java.util.Map#keySet()Ljava/util/Set;
 186     extern jmethodID jMapContainsKey; // java.util.Map#containsKey(Ljava/lang/Object;)Z
 187 
 188     extern jclass jHashSetCls; // java.util.HashSet
 189     extern jmethodID jHashSetInit; // java.util.HashSet#<init> ()V
 190 
 191     extern jmethodID jSetAdd; //java.util.Set#add (Ljava/lang/Object;)Z
 192     extern jmethodID jSetSize; //java.util.Set#size ()I
 193     extern jmethodID jSetToArray; //java.util.Set#toArray ([Ljava/lang/Object;)[Ljava/lang/Object;
 194 
 195     extern jmethodID jIterableIterator; // java.lang.Iterable#iterator()Ljava/util/Iterator;
 196     extern jmethodID jIteratorHasNext; // java.util.Iterator#hasNext()Z;
 197     extern jmethodID jIteratorNext; // java.util.Iterator#next()Ljava/lang/Object;
 198 
 199     extern jclass jApplicationCls; //com.sun.glass.ui.gtk.GtkApplication
 200     extern jfieldID jApplicationDisplay; //com.sun.glass.ui.gtk.GtkApplication#display
 201     extern jfieldID jApplicationScreen; //com.sun.glass.ui.gtk.GtkApplication#screen
 202     extern jfieldID jApplicationVisualID; //com.sun.glass.ui.gtk.GtkApplication#visualID
 203     extern jmethodID jApplicationReportException; // reportException(Ljava/lang/Throwable;)V
 204     extern jmethodID jApplicationGetApplication; // GetApplication()()Lcom/sun/glass/ui/Application;
 205     extern jmethodID jApplicationGetName; // getName()Ljava/lang/String;
 206 
 207 #ifdef VERBOSE
 208 #define LOG0(msg) {printf(msg);fflush(stdout);}
 209 #define LOG1(msg, param) {printf(msg, param);fflush(stdout);}
 210 #define LOG2(msg, param1, param2) {printf(msg, param1, param2);fflush(stdout);}
 211 #define LOG3(msg, param1, param2, param3) {printf(msg, param1, param2, param3);fflush(stdout);}
 212 #define LOG4(msg, param1, param2, param3, param4) {printf(msg, param1, param2, param3, param4);fflush(stdout);}
 213 #define LOG5(msg, param1, param2, param3, param4, param5) {printf(msg, param1, param2, param3, param4, param5);fflush(stdout);}
 214 
 215 #define LOG_STRING_ARRAY(env, array) dump_jstring_array(env, array);
 216 
 217 #define ERROR0(msg) {fprintf(stderr, msg);fflush(stderr);}
 218 #define ERROR1(msg, param) {fprintf(stderr, msg, param);fflush(stderr);}
 219 #define ERROR2(msg, param1, param2) {fprintf(stderr, msg, param1, param2);fflush(stderr);}
 220 #define ERROR3(msg, param1, param2, param3) {fprintf(stderr, msg, param1, param2, param3);fflush(stderr);}
 221 #define ERROR4(msg, param1, param2, param3, param4) {fprintf(stderr, msg, param1, param2, param3, param4);fflush(stderr);}
 222 #else
 223 #define LOG0(msg)
 224 #define LOG1(msg, param)
 225 #define LOG2(msg, param1, param2)
 226 #define LOG3(msg, param1, param2, param3)
 227 #define LOG4(msg, param1, param2, param3, param4)
 228 #define LOG5(msg, param1, param2, param3, param4, param5)
 229 
 230 #define LOG_STRING_ARRAY(env, array)
 231 
 232 #define ERROR0(msg)
 233 #define ERROR1(msg, param)
 234 #define ERROR2(msg, param1, param2)
 235 #define ERROR3(msg, param1, param2, param3)
 236 #define ERROR4(msg, param1, param2, param3, param4)
 237 #endif
 238 
 239 #define LOG_EXCEPTION(env) check_and_clear_exception(env);
 240 
 241     gchar* get_application_name();
 242     void glass_throw_exception(JNIEnv * env,
 243             const char * exceptionClass,
 244             const char * exceptionMessage);
 245     int glass_throw_oom(JNIEnv * env, const char * exceptionMessage);
 246     void dump_jstring_array(JNIEnv*, jobjectArray);
 247 
 248     guint8* convert_BGRA_to_RGBA(const int* pixels, int stride, int height);
 249 
 250     gboolean check_and_clear_exception(JNIEnv *env);
 251 
 252     jboolean is_display_valid();
 253 
 254     gsize get_files_count(gchar **uris);
 255 
 256     jobject uris_to_java(JNIEnv *env, gchar **uris, gboolean files);
 257 
 258 
 259 #ifdef __cplusplus
 260 extern "C" {
 261 #endif
 262 
 263 extern jboolean gtk_verbose;
 264 
 265 void
 266 glass_widget_set_visual (GtkWidget *widget, GdkVisual *visual);
 267 
 268 gint
 269 glass_gdk_visual_get_depth (GdkVisual * visual);
 270 
 271 GdkScreen *
 272 glass_gdk_window_get_screen(GdkWindow * gdkWindow);
 273 
 274 gboolean
 275 glass_gdk_mouse_devices_grab(GdkWindow * gdkWindow);
 276 
 277 gboolean
 278 glass_gdk_mouse_devices_grab_with_cursor(GdkWindow * gdkWindow, GdkCursor *cursor, gboolean owner_events);
 279 
 280 void
 281 glass_gdk_mouse_devices_ungrab();
 282 
 283 void
 284 glass_gdk_master_pointer_grab(GdkWindow *window, GdkCursor *cursor);
 285 
 286 void
 287 glass_gdk_master_pointer_ungrab();
 288 
 289 void
 290 glass_gdk_master_pointer_get_position(gint *x, gint *y);
 291 
 292 gboolean
 293 glass_gdk_device_is_grabbed(GdkDevice *device);
 294 
 295 void
 296 glass_gdk_device_ungrab(GdkDevice *device);
 297 
 298 GdkWindow *
 299 glass_gdk_device_get_window_at_position(
 300                GdkDevice *device, gint *x, gint *y);
 301 
 302 void
 303 glass_gtk_configure_transparency_and_realize(GtkWidget *window,
 304                                                   gboolean transparent);
 305 
 306 const guchar *
 307 glass_gtk_selection_data_get_data_with_length(
 308         GtkSelectionData * selectionData,
 309         gint * length);
 310 
 311 void
 312 glass_gtk_window_configure_from_visual(GtkWidget *widget, GdkVisual *visual);
 313 
 314 void
 315 glass_gdk_window_get_size(GdkWindow *window, gint *w, gint *h);
 316 
 317 void
 318 glass_gdk_display_get_pointer(GdkDisplay* display, gint* x, gint *y);
 319 
 320 void
 321 glass_gdk_x11_display_set_window_scale(GdkDisplay *display, gint scale);
 322 
 323 gboolean
 324 glass_configure_window_transparency(GtkWidget *window, gboolean transparent);
 325 
 326 GdkPixbuf *
 327 glass_pixbuf_from_window(GdkWindow *window,
 328     gint srcx, gint srcy,
 329     gint width, gint height);
 330 
 331 void
 332 glass_window_apply_shape_mask(GdkWindow *window,
 333     void* data, uint width, uint height);
 334 
 335 void
 336 glass_window_reset_input_shape_mask(GdkWindow *window);
 337 
 338 GdkWindow *
 339 glass_gdk_drag_context_get_dest_window (GdkDragContext * context);
 340 
 341 guint
 342 glass_settings_get_guint_opt (const gchar *schema_name,
 343                     const gchar *key_name,
 344                     int defval);
 345 
 346 #ifdef __cplusplus
 347 }
 348 #endif
 349 
 350 #endif        /* GLASS_GENERAL_H */