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 #include "glass_general.h"
  26 
  27 #include <jni.h>
  28 #include <gtk/gtk.h>
  29 
  30 char const * const GDK_WINDOW_DATA_CONTEXT = "glass_window_context";
  31 
  32 jclass jStringCls;
  33 jclass jByteBufferCls;
  34 jmethodID jByteBufferArray;
  35 jmethodID jByteBufferWrap;
  36 
  37 jclass jRunnableCls;
  38 jmethodID jRunnableRun;
  39 
  40 jclass jArrayListCls;
  41 jmethodID jArrayListInit;
  42 jmethodID jArrayListAdd;
  43 jmethodID jArrayListGetIdx;
  44 
  45 jmethodID jPixelsAttachData;
  46 
  47 jclass jGtkPixelsCls;
  48 jmethodID jGtkPixelsInit;
  49 
  50 jclass jScreenCls;
  51 jmethodID jScreenInit;
  52 jmethodID jScreenNotifySettingsChanged;
  53 
  54 jmethodID jViewNotifyResize;
  55 jmethodID jViewNotifyMouse;
  56 jmethodID jViewNotifyRepaint;
  57 jmethodID jViewNotifyKey;
  58 jmethodID jViewNotifyView;
  59 jmethodID jViewNotifyDragEnter;
  60 jmethodID jViewNotifyDragOver;
  61 jmethodID jViewNotifyDragDrop;
  62 jmethodID jViewNotifyDragLeave;
  63 jmethodID jViewNotifyScroll;
  64 jmethodID jViewNotifyInputMethod;
  65 jmethodID jViewNotifyInputMethodDraw;
  66 jmethodID jViewNotifyInputMethodCaret;
  67 jmethodID jViewNotifyPreeditMode;
  68 jmethodID jViewNotifyMenu;
  69 jfieldID  jViewPtr;
  70 
  71 jmethodID jWindowNotifyResize;
  72 jmethodID jWindowNotifyMove;
  73 jmethodID jWindowNotifyDestroy;
  74 jmethodID jWindowNotifyClose;
  75 jmethodID jWindowNotifyFocus;
  76 jmethodID jWindowNotifyFocusDisabled;
  77 jmethodID jWindowNotifyFocusUngrab;
  78 jmethodID jWindowNotifyMoveToAnotherScreen;
  79 jmethodID jWindowNotifyLevelChanged;
  80 jmethodID jWindowIsEnabled;
  81 jmethodID jWindowNotifyDelegatePtr;
  82 jfieldID jWindowPtr;
  83 jfieldID jCursorPtr;
  84 
  85 jmethodID jGtkWindowNotifyStateChanged;
  86 
  87 jmethodID jClipboardContentChanged;
  88 
  89 jmethodID jSizeInit;
  90 
  91 jmethodID jMapGet;
  92 jmethodID jMapKeySet;
  93 jmethodID jMapContainsKey;
  94 
  95 jclass jHashSetCls;
  96 jmethodID jHashSetInit;
  97 
  98 jmethodID jSetAdd;
  99 jmethodID jSetSize;
 100 jmethodID jSetToArray;
 101 
 102 jmethodID jIterableIterator;
 103 jmethodID jIteratorHasNext;
 104 jmethodID jIteratorNext;
 105 
 106 jclass jApplicationCls;
 107 jfieldID jApplicationDisplay;
 108 jfieldID jApplicationScreen;
 109 jfieldID jApplicationVisualID;
 110 jmethodID jApplicationReportException;
 111 jmethodID jApplicationGetApplication;
 112 jmethodID jApplicationGetName;
 113 
 114 void init_threads() {
 115 #if !GLIB_CHECK_VERSION(2,31,0)
 116     if (!g_thread_supported()) {
 117         g_thread_init(NULL);
 118     }
 119 #endif
 120     gdk_threads_init();
 121 }
 122 
 123 static jboolean displayValid = JNI_FALSE;
 124 
 125 jboolean
 126 is_display_valid() {
 127     return displayValid;
 128 }
 129 
 130 JNIEXPORT jint JNICALL
 131 JNI_OnLoad(JavaVM *jvm, void *reserved)
 132 {
 133     (void)reserved;
 134 
 135     JNIEnv *env;
 136     jclass clazz;
 137     Display* display;
 138 
 139     if (jvm->GetEnv((void **)&env, JNI_VERSION_1_6)) {
 140          return JNI_ERR; /* JNI version not supported */
 141      }
 142 
 143     clazz = env->FindClass("java/lang/String");
 144     if (env->ExceptionCheck()) return JNI_ERR;
 145     jStringCls = (jclass) env->NewGlobalRef(clazz);
 146 
 147     clazz = env->FindClass("java/nio/ByteBuffer");
 148     if (env->ExceptionCheck()) return JNI_ERR;
 149     jByteBufferCls = (jclass) env->NewGlobalRef(clazz);
 150     jByteBufferArray = env->GetMethodID(jByteBufferCls, "array", "()[B");
 151     if (env->ExceptionCheck()) return JNI_ERR;
 152     jByteBufferWrap = env->GetStaticMethodID(jByteBufferCls, "wrap", "([B)Ljava/nio/ByteBuffer;");
 153     if (env->ExceptionCheck()) return JNI_ERR;
 154 
 155     clazz = env->FindClass("java/lang/Runnable");
 156     if (env->ExceptionCheck()) return JNI_ERR;
 157 
 158     jRunnableRun = env->GetMethodID(clazz, "run", "()V");
 159     if (env->ExceptionCheck()) return JNI_ERR;
 160 
 161     clazz = env->FindClass("java/util/ArrayList");
 162     if (env->ExceptionCheck()) return JNI_ERR;
 163     jArrayListCls = (jclass) env->NewGlobalRef(clazz);
 164     jArrayListInit = env->GetMethodID(jArrayListCls, "<init>", "()V");
 165     if (env->ExceptionCheck()) return JNI_ERR;
 166     jArrayListAdd = env->GetMethodID(jArrayListCls, "add", "(Ljava/lang/Object;)Z");
 167     if (env->ExceptionCheck()) return JNI_ERR;
 168     jArrayListGetIdx = env->GetMethodID(jArrayListCls, "get", "(I)Ljava/lang/Object;");
 169     if (env->ExceptionCheck()) return JNI_ERR;
 170     clazz = env->FindClass("com/sun/glass/ui/Pixels");
 171     if (env->ExceptionCheck()) return JNI_ERR;
 172     jPixelsAttachData = env->GetMethodID(clazz, "attachData", "(J)V");
 173     if (env->ExceptionCheck()) return JNI_ERR;
 174 
 175     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkPixels");
 176     if (env->ExceptionCheck()) return JNI_ERR;
 177 
 178     jGtkPixelsCls = (jclass) env->NewGlobalRef(clazz);
 179     jGtkPixelsInit = env->GetMethodID(jGtkPixelsCls, "<init>", "(IILjava/nio/ByteBuffer;)V");
 180     if (env->ExceptionCheck()) return JNI_ERR;
 181 
 182     clazz = env->FindClass("com/sun/glass/ui/Screen");
 183     if (env->ExceptionCheck()) return JNI_ERR;
 184     jScreenCls = (jclass) env->NewGlobalRef(clazz);
 185     jScreenInit = env->GetMethodID(jScreenCls, "<init>", "(JIIIIIIIIIIIF)V");
 186     if (env->ExceptionCheck()) return JNI_ERR;
 187     jScreenNotifySettingsChanged = env->GetStaticMethodID(jScreenCls, "notifySettingsChanged", "()V");
 188     if (env->ExceptionCheck()) return JNI_ERR;
 189 
 190     clazz = env->FindClass("com/sun/glass/ui/View");
 191     if (env->ExceptionCheck()) return JNI_ERR;
 192     jViewNotifyResize = env->GetMethodID(clazz, "notifyResize", "(II)V");
 193     if (env->ExceptionCheck()) return JNI_ERR;
 194     jViewNotifyMouse = env->GetMethodID(clazz, "notifyMouse", "(IIIIIIIZZ)V");
 195     if (env->ExceptionCheck()) return JNI_ERR;
 196     jViewNotifyRepaint = env->GetMethodID(clazz, "notifyRepaint", "(IIII)V");
 197     if (env->ExceptionCheck()) return JNI_ERR;
 198     jViewNotifyKey = env->GetMethodID(clazz, "notifyKey", "(II[CI)V");
 199     if (env->ExceptionCheck()) return JNI_ERR;
 200     jViewNotifyView = env->GetMethodID(clazz, "notifyView", "(I)V");
 201     if (env->ExceptionCheck()) return JNI_ERR;
 202     jViewNotifyDragEnter = env->GetMethodID(clazz, "notifyDragEnter", "(IIIII)I");
 203     if (env->ExceptionCheck()) return JNI_ERR;
 204     jViewNotifyDragOver = env->GetMethodID(clazz, "notifyDragOver", "(IIIII)I");
 205     if (env->ExceptionCheck()) return JNI_ERR;
 206     jViewNotifyDragDrop = env->GetMethodID(clazz, "notifyDragDrop", "(IIIII)I");
 207     if (env->ExceptionCheck()) return JNI_ERR;
 208     jViewNotifyDragLeave = env->GetMethodID(clazz, "notifyDragLeave", "()V");
 209     if (env->ExceptionCheck()) return JNI_ERR;
 210     jViewNotifyScroll = env->GetMethodID(clazz, "notifyScroll", "(IIIIDDIIIIIDD)V");
 211     if (env->ExceptionCheck()) return JNI_ERR;
 212     jViewNotifyInputMethod = env->GetMethodID(clazz, "notifyInputMethod", "(Ljava/lang/String;[I[I[BIII)V");
 213     if (env->ExceptionCheck()) return JNI_ERR;
 214     jViewNotifyMenu = env->GetMethodID(clazz, "notifyMenu", "(IIIIZ)V");
 215     if (env->ExceptionCheck()) return JNI_ERR;
 216     jViewPtr = env->GetFieldID(clazz, "ptr", "J");
 217     if (env->ExceptionCheck()) return JNI_ERR;
 218 
 219     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkView");
 220     if (env->ExceptionCheck()) return JNI_ERR;
 221     jViewNotifyInputMethodDraw = env->GetMethodID(clazz, "notifyInputMethodDraw", "(Ljava/lang/String;III)V");
 222     if (env->ExceptionCheck()) return JNI_ERR;
 223     jViewNotifyInputMethodCaret = env->GetMethodID(clazz, "notifyInputMethodCaret", "(III)V");
 224     if (env->ExceptionCheck()) return JNI_ERR;
 225     jViewNotifyPreeditMode = env->GetMethodID(clazz, "notifyPreeditMode", "(Z)V");
 226     if (env->ExceptionCheck()) return JNI_ERR;
 227 
 228     clazz = env->FindClass("com/sun/glass/ui/Window");
 229     if (env->ExceptionCheck()) return JNI_ERR;
 230     jWindowNotifyResize = env->GetMethodID(clazz, "notifyResize", "(III)V");
 231     if (env->ExceptionCheck()) return JNI_ERR;
 232     jWindowNotifyMove = env->GetMethodID(clazz, "notifyMove", "(II)V");
 233     if (env->ExceptionCheck()) return JNI_ERR;
 234     jWindowNotifyDestroy = env->GetMethodID(clazz, "notifyDestroy", "()V");
 235     if (env->ExceptionCheck()) return JNI_ERR;
 236     jWindowNotifyClose = env->GetMethodID(clazz, "notifyClose", "()V");
 237     if (env->ExceptionCheck()) return JNI_ERR;
 238     jWindowNotifyFocus = env->GetMethodID(clazz, "notifyFocus", "(I)V");
 239     if (env->ExceptionCheck()) return JNI_ERR;
 240     jWindowNotifyFocusDisabled = env->GetMethodID(clazz, "notifyFocusDisabled", "()V");
 241     if (env->ExceptionCheck()) return JNI_ERR;
 242     jWindowNotifyFocusUngrab = env->GetMethodID(clazz, "notifyFocusUngrab", "()V");
 243     if (env->ExceptionCheck()) return JNI_ERR;
 244     jWindowNotifyMoveToAnotherScreen = env->GetMethodID(clazz, "notifyMoveToAnotherScreen", "(Lcom/sun/glass/ui/Screen;)V");
 245     if (env->ExceptionCheck()) return JNI_ERR;
 246     jWindowNotifyLevelChanged = env->GetMethodID(clazz, "notifyLevelChanged", "(I)V");
 247     if (env->ExceptionCheck()) return JNI_ERR;
 248     jWindowIsEnabled = env->GetMethodID(clazz, "isEnabled", "()Z");
 249     if (env->ExceptionCheck()) return JNI_ERR;
 250     jWindowNotifyDelegatePtr = env->GetMethodID(clazz, "notifyDelegatePtr", "(J)V");
 251     if (env->ExceptionCheck()) return JNI_ERR;
 252     jWindowPtr = env->GetFieldID(clazz, "ptr", "J");
 253     if (env->ExceptionCheck()) return JNI_ERR;
 254 
 255     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkWindow");
 256     if (env->ExceptionCheck()) return JNI_ERR;
 257     jGtkWindowNotifyStateChanged =
 258             env->GetMethodID(clazz, "notifyStateChanged", "(I)V");
 259     if (env->ExceptionCheck()) return JNI_ERR;
 260 
 261     clazz = env->FindClass("com/sun/glass/ui/Clipboard");
 262     if (env->ExceptionCheck()) return JNI_ERR;
 263     jClipboardContentChanged = env->GetMethodID(clazz, "contentChanged", "()V");
 264     if (env->ExceptionCheck()) return JNI_ERR;
 265 
 266     clazz = env->FindClass("com/sun/glass/ui/Cursor");
 267     if (env->ExceptionCheck()) return JNI_ERR;
 268     jCursorPtr = env->GetFieldID(clazz, "ptr", "J");
 269     if (env->ExceptionCheck()) return JNI_ERR;
 270 
 271     clazz = env->FindClass("com/sun/glass/ui/Size");
 272     if (env->ExceptionCheck()) return JNI_ERR;
 273     jSizeInit = env->GetMethodID(clazz, "<init>", "(II)V");
 274     if (env->ExceptionCheck()) return JNI_ERR;
 275 
 276     clazz = env->FindClass("java/util/Map");
 277     if (env->ExceptionCheck()) return JNI_ERR;
 278     jMapGet = env->GetMethodID(clazz, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
 279     if (env->ExceptionCheck()) return JNI_ERR;
 280     jMapKeySet = env->GetMethodID(clazz, "keySet", "()Ljava/util/Set;");
 281     if (env->ExceptionCheck()) return JNI_ERR;
 282     jMapContainsKey = env->GetMethodID(clazz, "containsKey", "(Ljava/lang/Object;)Z");
 283     if (env->ExceptionCheck()) return JNI_ERR;
 284 
 285     clazz = env->FindClass("java/util/HashSet");
 286     if (env->ExceptionCheck()) return JNI_ERR;
 287     jHashSetCls = (jclass) env->NewGlobalRef(clazz);
 288     jHashSetInit = env->GetMethodID(jHashSetCls, "<init>", "()V");
 289     if (env->ExceptionCheck()) return JNI_ERR;
 290 
 291     clazz = env->FindClass("java/util/Set");
 292     if (env->ExceptionCheck()) return JNI_ERR;
 293     jSetAdd = env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z");
 294     if (env->ExceptionCheck()) return JNI_ERR;
 295     jSetSize = env->GetMethodID(clazz, "size", "()I");
 296     if (env->ExceptionCheck()) return JNI_ERR;
 297     jSetToArray = env->GetMethodID(clazz, "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;");
 298     if (env->ExceptionCheck()) return JNI_ERR;
 299 
 300     clazz = env->FindClass("java/lang/Iterable");
 301     if (env->ExceptionCheck()) return JNI_ERR;
 302     jIterableIterator = env->GetMethodID(clazz, "iterator", "()Ljava/util/Iterator;");
 303     if (env->ExceptionCheck()) return JNI_ERR;
 304 
 305     clazz = env->FindClass("java/util/Iterator");
 306     if (env->ExceptionCheck()) return JNI_ERR;
 307     jIteratorHasNext = env->GetMethodID(clazz, "hasNext", "()Z");
 308     if (env->ExceptionCheck()) return JNI_ERR;
 309     jIteratorNext = env->GetMethodID(clazz, "next", "()Ljava/lang/Object;");
 310     if (env->ExceptionCheck()) return JNI_ERR;
 311 
 312     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkApplication");
 313     if (env->ExceptionCheck()) return JNI_ERR;
 314     jApplicationCls = (jclass) env->NewGlobalRef(clazz);
 315     jApplicationDisplay = env->GetStaticFieldID(jApplicationCls, "display", "J");
 316     if (env->ExceptionCheck()) return JNI_ERR;
 317     jApplicationScreen = env->GetStaticFieldID(jApplicationCls, "screen", "I");
 318     if (env->ExceptionCheck()) return JNI_ERR;
 319     jApplicationVisualID = env->GetStaticFieldID(jApplicationCls, "visualID", "J");
 320     if (env->ExceptionCheck()) return JNI_ERR;
 321     jApplicationReportException = env->GetStaticMethodID(
 322         jApplicationCls, "reportException", "(Ljava/lang/Throwable;)V");
 323     if (env->ExceptionCheck()) return JNI_ERR;
 324     jApplicationGetApplication = env->GetStaticMethodID(
 325         jApplicationCls, "GetApplication", "()Lcom/sun/glass/ui/Application;");
 326     if (env->ExceptionCheck()) return JNI_ERR;
 327     jApplicationGetName = env->GetMethodID(jApplicationCls, "getName", "()Ljava/lang/String;");
 328     if (env->ExceptionCheck()) return JNI_ERR;
 329 
 330     // Before doing anything with GTK we validate that the DISPLAY can be opened
 331     display = XOpenDisplay(NULL);
 332     if (display != NULL) {
 333         XCloseDisplay(display);
 334         displayValid = JNI_TRUE;
 335     } else {
 336         // Invalid DISPLAY, skip initialization
 337         return JNI_VERSION_1_6;
 338     }
 339 
 340     clazz = env->FindClass("sun/misc/GThreadHelper");    
 341     if (env->ExceptionCheck()) return JNI_ERR;
 342     if (clazz) {
 343         jmethodID mid_getAndSetInitializationNeededFlag = env->GetStaticMethodID(clazz, "getAndSetInitializationNeededFlag", "()Z");
 344         if (env->ExceptionCheck()) return JNI_ERR;
 345         jmethodID mid_lock = env->GetStaticMethodID(clazz, "lock", "()V");
 346         if (env->ExceptionCheck()) return JNI_ERR;
 347         jmethodID mid_unlock = env->GetStaticMethodID(clazz, "unlock", "()V");    
 348         if (env->ExceptionCheck()) return JNI_ERR;
 349         
 350         env->CallStaticVoidMethod(clazz, mid_lock);
 351 
 352         if (!env->CallStaticBooleanMethod(clazz, mid_getAndSetInitializationNeededFlag)) {
 353             init_threads();
 354         }
 355 
 356         env->CallStaticVoidMethod(clazz, mid_unlock);
 357     } else {
 358         env->ExceptionClear();
 359         init_threads();
 360     }
 361 
 362     gdk_threads_enter();
 363     gtk_init(NULL, NULL);
 364 
 365     return JNI_VERSION_1_6;
 366 }
 367 
 368 void
 369 glass_throw_exception(JNIEnv * env,
 370                       const char * exceptionClass,
 371                       const char * exceptionMessage) {
 372     jclass throwableClass = env->FindClass(exceptionClass);
 373     if (check_and_clear_exception(env)) return;
 374     env->ThrowNew(throwableClass, exceptionMessage);    
 375     check_and_clear_exception(env);
 376 }
 377 
 378 int
 379 glass_throw_oom(JNIEnv * env, const char * message) {
 380     glass_throw_exception(env, "java/lang/OutOfMemoryError", message);
 381     // must return a non-zero value, see HANDLE_MEM_ALLOC_ERROR
 382     return 1;
 383 }
 384 
 385 
 386 guint8* convert_BGRA_to_RGBA(const int* pixels, int stride, int height) {
 387   guint8* new_pixels = (guint8*) g_malloc(height * stride);
 388   int i = 0;
 389 
 390   for (i = 0; i < height * stride; i += 4) {
 391       new_pixels[i] = (guint8)(*pixels >> 16);
 392       new_pixels[i + 1] = (guint8)(*pixels >> 8);
 393       new_pixels[i + 2] = (guint8)(*pixels);
 394       new_pixels[i + 3] = (guint8)(*pixels >> 24);
 395       pixels++;
 396   }
 397 
 398   return new_pixels;
 399 }
 400 
 401 
 402 void dump_jstring_array(JNIEnv* env, jobjectArray arr) {
 403     if (arr == NULL) {
 404         LOG0("dump: Array is null\n")
 405         return;
 406     }
 407     jsize len = env->GetArrayLength(arr);
 408     LOG1("dump: length = %d\n", len)
 409     int i = 0;
 410     jboolean isCopy;
 411     for(i = 0; i < len; i++) {
 412         jstring jstr = (jstring) env->GetObjectArrayElement(arr, i);
 413         check_and_clear_exception(env);
 414         const char* str = env->GetStringUTFChars(jstr, &isCopy);
 415         LOG2("dump: s[%d]: %s\n", i, str)
 416     }
 417 }
 418 
 419 gboolean check_and_clear_exception(JNIEnv *env) {
 420     jthrowable t = env->ExceptionOccurred();
 421     if (t) {
 422         env->ExceptionClear();
 423         env->CallStaticVoidMethod(jApplicationCls, jApplicationReportException, t);
 424         return TRUE;
 425     }
 426     return FALSE;
 427 }
 428 
 429 // The returned string should be freed with g_free().
 430 gchar* get_application_name() {
 431     gchar* ret = NULL;
 432     
 433     jobject japp = mainEnv->CallStaticObjectMethod(jApplicationCls, jApplicationGetApplication);
 434     CHECK_JNI_EXCEPTION_RET(mainEnv, NULL);
 435     jstring jname = (jstring) mainEnv->CallObjectMethod(japp, jApplicationGetName);
 436     CHECK_JNI_EXCEPTION_RET(mainEnv, NULL);
 437     if (const gchar *name = mainEnv->GetStringUTFChars(jname, NULL)) {
 438         ret = g_strdup(name);
 439         mainEnv->ReleaseStringUTFChars(jname, name);
 440     }
 441     return ret;
 442 }
 443 
 444 gpointer glass_try_malloc_n(gsize m, gsize n, 
 445         gboolean zer0 /* initialized to 0 if true*/) {
 446     if (n > 0 && m > G_MAXSIZE / n) {
 447         return NULL;
 448     }
 449     return (zer0)
 450             ? g_try_malloc0(m * n)
 451             : g_try_malloc(m * n);
 452 }
 453 
 454 /*
 455  * Since we support glib 2.18 we can't use g_try_malloc_n and g_try_malloc0_n
 456  * which was introduced in 2.24. 
 457  * glass_try_malloc_n and glass_try_malloc0_n is replacement for those functions
 458  */
 459 gpointer glass_try_malloc0_n(gsize m, gsize n) {
 460     return glass_try_malloc_n(m, n, TRUE);
 461 }
 462 
 463 gpointer glass_try_malloc_n(gsize m, gsize n) {
 464     return glass_try_malloc_n(m, n, FALSE);
 465 }
 466 
 467 gsize get_files_count(gchar **uris) {
 468     if (!uris) {
 469         return 0;
 470     }
 471 
 472     guint size = g_strv_length(uris);
 473     guint files_cnt = 0;
 474 
 475     for (guint i = 0; i < size; ++i) {
 476         if (g_str_has_prefix(uris[i], FILE_PREFIX)) {
 477             files_cnt++;
 478         }
 479     }
 480     return files_cnt;
 481 }
 482 
 483 // Note: passed uris will be freed by this function
 484 jobject uris_to_java(JNIEnv *env, gchar **uris, gboolean files) {
 485     if (uris == NULL) {
 486         return NULL;
 487     }
 488 
 489     jobject result = NULL;
 490 
 491     guint size = g_strv_length(uris);
 492     guint files_cnt = get_files_count(uris);
 493 
 494     if (files) {
 495         if (files_cnt) {
 496             result = env->NewObjectArray(files_cnt, jStringCls, NULL);            
 497             check_and_clear_exception(env);
 498 
 499             for (gsize i = 0; i < size; ++i) {
 500                 if (g_str_has_prefix(uris[i], FILE_PREFIX)) {
 501                     gchar* path = g_filename_from_uri(uris[i], NULL, NULL);
 502                     jstring str = env->NewStringUTF(path);
 503                     check_and_clear_exception(env);
 504                     env->SetObjectArrayElement((jobjectArray) result, i, str);
 505                     check_and_clear_exception(env);
 506                     g_free(path);
 507                 }
 508             }
 509         }
 510     } else if (size - files_cnt) {
 511         GString* str = g_string_new(NULL); //http://www.ietf.org/rfc/rfc2483.txt
 512 
 513         for (guint i = 0; i < size; ++i) {
 514             if (!g_str_has_prefix(uris[i], FILE_PREFIX)
 515                     && !g_str_has_prefix(uris[i], URI_LIST_COMMENT_PREFIX)) {
 516                 g_string_append(str, uris[i]);
 517                 g_string_append(str, URI_LIST_LINE_BREAK);
 518             }
 519         }
 520 
 521         if (str->len > 2) {
 522             g_string_erase(str, str->len - 2, 2);
 523         }
 524 
 525         result = env->NewStringUTF(str->str);
 526         check_and_clear_exception(env);
 527 
 528         g_string_free(str, TRUE);
 529     }
 530     g_strfreev(uris);
 531     return result;
 532 }