comparison android/dw.cpp @ 2572:bbe693293be5

Android: New color handling system, save the color in the C API and pass it into the Java/Kotlin API directly so the threads won't contaminate.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 21 May 2021 00:39:13 +0000
parents e34b627b2491
children 6fdab466d7a2
comparison
equal deleted inserted replaced
2571:e34b627b2491 2572:bbe693293be5
49 static char _dw_app_name[_DW_APP_ID_SIZE+1]= {0}; 49 static char _dw_app_name[_DW_APP_ID_SIZE+1]= {0};
50 static char _dw_exec_dir[MAX_PATH+1] = {0}; 50 static char _dw_exec_dir[MAX_PATH+1] = {0};
51 static int _dw_android_api = 0; 51 static int _dw_android_api = 0;
52 52
53 static pthread_key_t _dw_env_key; 53 static pthread_key_t _dw_env_key;
54 static pthread_key_t _dw_fgcolor_key;
55 static pthread_key_t _dw_bgcolor_key;
54 static HEV _dw_main_event; 56 static HEV _dw_main_event;
55 static JavaVM *_dw_jvm; 57 static JavaVM *_dw_jvm;
56 static jobject _dw_obj; 58 static jobject _dw_obj;
57 static jobject _dw_class_loader; 59 static jobject _dw_class_loader;
58 static jmethodID _dw_class_method; 60 static jmethodID _dw_class_method;
171 _dw_obj = env->NewGlobalRef(obj); 173 _dw_obj = env->NewGlobalRef(obj);
172 174
173 /* Save the JNIEnv for the main thread */ 175 /* Save the JNIEnv for the main thread */
174 pthread_key_create(&_dw_env_key, nullptr); 176 pthread_key_create(&_dw_env_key, nullptr);
175 pthread_setspecific(_dw_env_key, env); 177 pthread_setspecific(_dw_env_key, env);
178 pthread_key_create(&_dw_fgcolor_key, nullptr);
179 pthread_setspecific(_dw_fgcolor_key, nullptr);
180 pthread_key_create(&_dw_bgcolor_key, nullptr);
181 pthread_setspecific(_dw_bgcolor_key, nullptr);
176 182
177 /* Create the dwmain event */ 183 /* Create the dwmain event */
178 _dw_main_event = dw_event_new(); 184 _dw_main_event = dw_event_new();
179 } 185 }
180 186
2653 * green: green value. 2659 * green: green value.
2654 * blue: blue value. 2660 * blue: blue value.
2655 */ 2661 */
2656 void API dw_color_foreground_set(unsigned long value) 2662 void API dw_color_foreground_set(unsigned long value)
2657 { 2663 {
2658 JNIEnv *env; 2664 pthread_setspecific(_dw_fgcolor_key, (void *)_dw_get_color(value));
2659
2660 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2661 {
2662 unsigned long color = _dw_get_color(value);
2663
2664 // First get the class that contains the method you need to call
2665 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2666 // Get the method that you want to call
2667 jmethodID colorSet = env->GetMethodID(clazz, "colorSet",
2668 "(IIII)V");
2669 // Call the method on the object
2670 env->CallVoidMethod(_dw_obj, colorSet, 0, (jint)DW_RED_VALUE(color), (jint)DW_GREEN_VALUE(color), (jint)DW_BLUE_VALUE(color));
2671 _dw_jni_check_exception(env);
2672 }
2673 } 2665 }
2674 2666
2675 /* Sets the current background drawing color. 2667 /* Sets the current background drawing color.
2676 * Parameters: 2668 * Parameters:
2677 * red: red value. 2669 * red: red value.
2678 * green: green value. 2670 * green: green value.
2679 * blue: blue value. 2671 * blue: blue value.
2680 */ 2672 */
2681 void API dw_color_background_set(unsigned long value) 2673 void API dw_color_background_set(unsigned long value)
2682 { 2674 {
2683 JNIEnv *env; 2675 pthread_setspecific(_dw_bgcolor_key, (void *)_dw_get_color(value));
2684
2685 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2686 {
2687 unsigned long color = _dw_get_color(value);
2688
2689 // First get the class that contains the method you need to call
2690 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2691 // Get the method that you want to call
2692 jmethodID bgColorSet = env->GetMethodID(clazz, "bgColorSet",
2693 "(IIII)V");
2694 // Call the method on the object
2695 env->CallVoidMethod(_dw_obj, bgColorSet, 0, (jint)DW_RED_VALUE(color), (jint)DW_GREEN_VALUE(color), (jint)DW_BLUE_VALUE(color));
2696 _dw_jni_check_exception(env);
2697 }
2698 } 2676 }
2699 2677
2700 /* Allows the user to choose a color using the system's color chooser dialog. 2678 /* Allows the user to choose a color using the system's color chooser dialog.
2701 * Parameters: 2679 * Parameters:
2702 * value: current color 2680 * value: current color
2719 { 2697 {
2720 JNIEnv *env; 2698 JNIEnv *env;
2721 2699
2722 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 2700 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2723 { 2701 {
2702 unsigned long fgcolor = (unsigned long)pthread_getspecific(_dw_fgcolor_key);
2703 unsigned long bgcolor = (unsigned long)pthread_getspecific(_dw_bgcolor_key);
2704
2724 // First get the class that contains the method you need to call 2705 // First get the class that contains the method you need to call
2725 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 2706 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2726 // Get the method that you want to call 2707 // Get the method that you want to call
2727 jmethodID drawPoint = env->GetMethodID(clazz, "drawPoint", 2708 jmethodID drawPoint = env->GetMethodID(clazz, "drawPoint",
2728 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;II)V"); 2709 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IIII)V");
2729 // Call the method on the object 2710 // Call the method on the object
2730 env->CallVoidMethod(_dw_obj, drawPoint, handle, pixmap ? pixmap->bitmap : nullptr, x, y); 2711 env->CallVoidMethod(_dw_obj, drawPoint, handle, pixmap ? pixmap->bitmap : nullptr, x, y, (jint)fgcolor, (jint)bgcolor);
2731 _dw_jni_check_exception(env); 2712 _dw_jni_check_exception(env);
2732 } 2713 }
2733 } 2714 }
2734 2715
2735 /* Draw a line on a window (preferably a render window). 2716 /* Draw a line on a window (preferably a render window).
2745 { 2726 {
2746 JNIEnv *env; 2727 JNIEnv *env;
2747 2728
2748 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 2729 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2749 { 2730 {
2731 unsigned long fgcolor = (unsigned long)pthread_getspecific(_dw_fgcolor_key);
2732 unsigned long bgcolor = (unsigned long)pthread_getspecific(_dw_bgcolor_key);
2733
2750 // First get the class that contains the method you need to call 2734 // First get the class that contains the method you need to call
2751 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 2735 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2752 // Get the method that you want to call 2736 // Get the method that you want to call
2753 jmethodID drawLine = env->GetMethodID(clazz, "drawLine", 2737 jmethodID drawLine = env->GetMethodID(clazz, "drawLine",
2754 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IIII)V"); 2738 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IIIIII)V");
2755 // Call the method on the object 2739 // Call the method on the object
2756 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, x1, y1, x2, y2); 2740 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, x1, y1, x2, y2, (jint)fgcolor, (jint)bgcolor);
2757 _dw_jni_check_exception(env); 2741 _dw_jni_check_exception(env);
2758 } 2742 }
2759 } 2743 }
2760 2744
2761 /* Draw text on a window (preferably a render window). 2745 /* Draw text on a window (preferably a render window).
2770 { 2754 {
2771 JNIEnv *env; 2755 JNIEnv *env;
2772 2756
2773 if((handle || pixmap) && text && (env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 2757 if((handle || pixmap) && text && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2774 { 2758 {
2759 unsigned long fgcolor = (unsigned long)pthread_getspecific(_dw_fgcolor_key);
2760 unsigned long bgcolor = (unsigned long)pthread_getspecific(_dw_bgcolor_key);
2761
2775 // Construct the string 2762 // Construct the string
2776 jstring jstr = env->NewStringUTF(text); 2763 jstring jstr = env->NewStringUTF(text);
2777 // First get the class that contains the method you need to call 2764 // First get the class that contains the method you need to call
2778 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 2765 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2779 // Get the method that you want to call 2766 // Get the method that you want to call
2780 jmethodID drawLine = env->GetMethodID(clazz, "drawText", 2767 jmethodID drawLine = env->GetMethodID(clazz, "drawText",
2781 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IILjava/lang/String;Landroid/graphics/Typeface;ILandroid/view/View;)V"); 2768 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IILjava/lang/String;Landroid/graphics/Typeface;ILandroid/view/View;II)V");
2782 // Call the method on the object 2769 // Call the method on the object
2783 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, x, y, jstr, 2770 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, x, y, jstr,
2784 pixmap ? pixmap->typeface : nullptr, pixmap ? pixmap->fontsize : 0, pixmap ? pixmap->handle : nullptr); 2771 pixmap ? pixmap->typeface : nullptr, pixmap ? pixmap->fontsize : 0,
2772 pixmap ? pixmap->handle : nullptr, (jint)fgcolor, (jint)bgcolor);
2785 _dw_jni_check_exception(env); 2773 _dw_jni_check_exception(env);
2786 } 2774 }
2787 } 2775 }
2788 2776
2789 /* Query the width and height of a text string. 2777 /* Query the width and height of a text string.
2838 jintArray jx = env->NewIntArray(npoints); 2826 jintArray jx = env->NewIntArray(npoints);
2839 jintArray jy = env->NewIntArray(npoints); 2827 jintArray jy = env->NewIntArray(npoints);
2840 2828
2841 if(jx && jy) 2829 if(jx && jy)
2842 { 2830 {
2831 unsigned long fgcolor = (unsigned long)pthread_getspecific(_dw_fgcolor_key);
2832 unsigned long bgcolor = (unsigned long)pthread_getspecific(_dw_bgcolor_key);
2833
2843 // Construct the integer arrays 2834 // Construct the integer arrays
2844 env->SetIntArrayRegion(jx, 0, npoints, x); 2835 env->SetIntArrayRegion(jx, 0, npoints, x);
2845 env->SetIntArrayRegion(jy, 0, npoints, y); 2836 env->SetIntArrayRegion(jy, 0, npoints, y);
2846 // First get the class that contains the method you need to call 2837 // First get the class that contains the method you need to call
2847 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 2838 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2848 // Get the method that you want to call 2839 // Get the method that you want to call
2849 jmethodID drawPolygon = env->GetMethodID(clazz, "drawPolygon", 2840 jmethodID drawPolygon = env->GetMethodID(clazz, "drawPolygon",
2850 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;II[I[I)V"); 2841 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;II[I[III)V");
2851 // Call the method on the object 2842 // Call the method on the object
2852 env->CallVoidMethod(_dw_obj, drawPolygon, handle, pixmap ? pixmap->bitmap : nullptr, flags, npoints, jx, jy); 2843 env->CallVoidMethod(_dw_obj, drawPolygon, handle, pixmap ? pixmap->bitmap : nullptr, flags, npoints, jx, jy, (jint)fgcolor, (jint)bgcolor);
2853 _dw_jni_check_exception(env); 2844 _dw_jni_check_exception(env);
2854 } 2845 }
2855 } 2846 }
2856 } 2847 }
2857 2848
2869 { 2860 {
2870 JNIEnv *env; 2861 JNIEnv *env;
2871 2862
2872 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 2863 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2873 { 2864 {
2865 unsigned long fgcolor = (unsigned long)pthread_getspecific(_dw_fgcolor_key);
2866 unsigned long bgcolor = (unsigned long)pthread_getspecific(_dw_bgcolor_key);
2867
2874 // First get the class that contains the method you need to call 2868 // First get the class that contains the method you need to call
2875 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 2869 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2876 // Get the method that you want to call 2870 // Get the method that you want to call
2877 jmethodID drawLine = env->GetMethodID(clazz, "drawRect", 2871 jmethodID drawLine = env->GetMethodID(clazz, "drawRect",
2878 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IIII)V"); 2872 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IIIIII)V");
2879 // Call the method on the object 2873 // Call the method on the object
2880 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, x, y, width, height); 2874 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, x, y, width, height, (jint)fgcolor, (jint)bgcolor);
2881 _dw_jni_check_exception(env); 2875 _dw_jni_check_exception(env);
2882 } 2876 }
2883 } 2877 }
2884 2878
2885 /* Draw an arc on a window (preferably a render window). 2879 /* Draw an arc on a window (preferably a render window).
2899 { 2893 {
2900 JNIEnv *env; 2894 JNIEnv *env;
2901 2895
2902 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 2896 if((handle || pixmap) && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2903 { 2897 {
2898 unsigned long fgcolor = (unsigned long)pthread_getspecific(_dw_fgcolor_key);
2899 unsigned long bgcolor = (unsigned long)pthread_getspecific(_dw_bgcolor_key);
2900
2904 // First get the class that contains the method you need to call 2901 // First get the class that contains the method you need to call
2905 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 2902 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2906 // Get the method that you want to call 2903 // Get the method that you want to call
2907 jmethodID drawLine = env->GetMethodID(clazz, "drawArc", 2904 jmethodID drawLine = env->GetMethodID(clazz, "drawArc",
2908 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IIIIIII)V"); 2905 "(Lorg/dbsoft/dwindows/DWRender;Landroid/graphics/Bitmap;IIIIIIIII)V");
2909 // Call the method on the object 2906 // Call the method on the object
2910 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, flags, xorigin, yorigin, x1, y1, x2, y2); 2907 env->CallVoidMethod(_dw_obj, drawLine, handle, pixmap ? pixmap->bitmap : nullptr, flags, xorigin, yorigin, x1, y1, x2, y2, (jint)fgcolor, (jint)bgcolor);
2911 _dw_jni_check_exception(env); 2908 _dw_jni_check_exception(env);
2912 } 2909 }
2913 } 2910 }
2914 2911
2915 /* 2912 /*
6577 { 6574 {
6578 JNIEnv *env; 6575 JNIEnv *env;
6579 6576
6580 _dw_jvm->AttachCurrentThread(&env, nullptr); 6577 _dw_jvm->AttachCurrentThread(&env, nullptr);
6581 pthread_setspecific(_dw_env_key, env); 6578 pthread_setspecific(_dw_env_key, env);
6579 pthread_setspecific(_dw_fgcolor_key, nullptr);
6580 pthread_setspecific(_dw_bgcolor_key, nullptr);
6582 } 6581 }
6583 6582
6584 /* 6583 /*
6585 * Generally an internal function called from a terminating 6584 * Generally an internal function called from a terminating
6586 * thread to cleanup the Dynamic Windows environment for the thread. 6585 * thread to cleanup the Dynamic Windows environment for the thread.