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