summaryrefslogtreecommitdiffstats
path: root/src/newt/native/IntelGDL.c
blob: 44eccbc796a30fdc5a6e4ddf7bf782485531288c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 */

#include <inttypes.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "com_sun_javafx_newt_intel_gdl_Display.h"
#include "com_sun_javafx_newt_intel_gdl_Screen.h"
#include "com_sun_javafx_newt_intel_gdl_Window.h"

#include "EventListener.h"
#include "MouseEvent.h"
#include "KeyEvent.h"

#include <gdl.h>
#include <gdl_version.h>

#define VERBOSE_ON 1

#ifdef VERBOSE_ON
    #define DBG_PRINT(...) fprintf(stdout, "*** INTEL-GDL: " __VA_ARGS__)
#else
    #define DBG_PRINT(...)
#endif

static jmethodID screenCreatedID = NULL;
static jmethodID updateSizeID = NULL;

#define NUM_PLANES 5
static jobject newtWindows[NUM_PLANES] = { NULL, NULL, NULL, NULL, NULL } ;
static gdl_plane_id_t planes[NUM_PLANES]  = { GDL_PLANE_ID_UPP_A, GDL_PLANE_ID_UPP_B, GDL_PLANE_ID_UPP_C, GDL_PLANE_ID_UPP_D, GDL_PLANE_ID_UPP_E };

static int getWindowIdx(jobject win) {
    int i;
    for(i=0; i<NUM_PLANES && newtWindows[i]!=win; i++) ;
    return (i<NUM_PLANES)?i:-1;
}
static int getPlaneIdx(gdl_plane_id_t plane) {
    int i;
    for(i=0; i<NUM_PLANES && planes[i]!=plane; i++) ;
    return (i<NUM_PLANES)?i:-1;
}

static jobject getNewtWindow(gdl_plane_id_t plane) {
    int idx = getPlaneIdx(plane);
    if(idx>0) {
        return newtWindows[idx];
    }
    return NULL;
}
static gdl_plane_id_t getPlane(jobject win) {
    int idx = getWindowIdx(win);
    if(idx>0) {
        return planes[idx];
    }
    return GDL_PLANE_ID_UNDEFINED;
}

static gdl_plane_id_t allocPlane(JNIEnv *env, jobject newtWindow) {
    int i = getWindowIdx(NULL);
    if (i<NUM_PLANES) {
        newtWindows[i] = (*env)->NewGlobalRef(env, newtWindow);
        return planes[i];
    }
    return GDL_PLANE_ID_UNDEFINED;
}
static void freePlane(JNIEnv *env, gdl_plane_id_t plane) {
    int i = getPlaneIdx(plane);
    if (i<NUM_PLANES) {
        if(NULL!=newtWindows[i]) {
            (*env)->DeleteGlobalRef(env, newtWindows[i]);
            newtWindows[i] = NULL;;
        }
    }
}

static void JNI_ThrowNew(JNIEnv *env, const char *throwable, const char* message) {
    jclass throwableClass = (*env)->FindClass(env, throwable);
    if (throwableClass == NULL) {
        (*env)->FatalError(env, "Failed to load throwable class");
    }

    if ((*env)->ThrowNew(env, throwableClass, message) != 0) {
        (*env)->FatalError(env, "Failed to throw throwable");
    }
}


/**
 * Display
 */

JNIEXPORT void JNICALL Java_com_sun_javafx_newt_intel_gdl_Display_DispatchMessages
  (JNIEnv *env, jobject obj, jlong displayHandle, jobject focusedWindow)
{
    // FIXME: n/a
    (void) env;
    (void) obj;
    (void) displayHandle;
    /**
    gdl_driver_info_t * p_driver_info = (gdl_driver_info_t *) (intptr_t) displayHandle;
    jobject newtWin = getNewtWindow(plane);
    if(NULL!=newtWin) {
        // here we can dispatch messages .. etc
    } */
}

JNIEXPORT jlong JNICALL Java_com_sun_javafx_newt_intel_gdl_Display_CreateDisplay
  (JNIEnv *env, jobject obj)
{
    gdl_ret_t retval;
    gdl_driver_info_t * p_driver_info = NULL;

    (void) env;
    (void) obj;
    
    DBG_PRINT("[CreateDisplay]\n");

    retval = gdl_init(0);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_init");
        return (jlong)0;
    }

    p_driver_info = calloc(sizeof(gdl_driver_info_t), 1);
    retval = gdl_get_driver_info(p_driver_info);
    if (retval != GDL_SUCCESS) {
        free(p_driver_info);
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_get_driver_info");
        return (jlong)0;
    }
    DBG_PRINT("[gdl_get_driver_info: major %d minor %d vers %d build %d flags %x name %s size %d avail %d]\n",
              p_driver_info->header_version_major, p_driver_info->header_version_minor,
              p_driver_info->gdl_version, p_driver_info->build_tag, p_driver_info->flags,
              p_driver_info->name, p_driver_info->mem_size, p_driver_info->mem_avail);


    return (jlong) (intptr_t) p_driver_info;
}

JNIEXPORT void JNICALL Java_com_sun_javafx_newt_intel_gdl_Display_DestroyDisplay
  (JNIEnv *env, jobject obj, jlong displayHandle)
{
    gdl_driver_info_t * p_driver_info = (gdl_driver_info_t *) (intptr_t) displayHandle;
    (void) env;
    (void) obj;

    if(NULL!=p_driver_info) {
        gdl_close();
        free(p_driver_info);
    }

    DBG_PRINT("[DestroyDisplay] X\n");
}

/**
 * Screen
 */

JNIEXPORT jboolean JNICALL Java_com_sun_javafx_newt_intel_gdl_Screen_initIDs
  (JNIEnv *env, jclass clazz)
{
    screenCreatedID = (*env)->GetMethodID(env, clazz, "screenCreated", "(II)V");
    if (screenCreatedID == NULL) {
        DBG_PRINT("initIDs failed\n" );
        return JNI_FALSE;
    }
    DBG_PRINT("initIDs ok\n" );
    return JNI_TRUE;
}

JNIEXPORT void JNICALL Java_com_sun_javafx_newt_intel_gdl_Screen_GetScreenInfo
  (JNIEnv *env, jobject obj, jlong displayHandle, jint idx)
{
    gdl_driver_info_t * p_driver_info = (gdl_driver_info_t *) (intptr_t) displayHandle;
    gdl_display_info_t display_info;
    gdl_display_id_t id;
    gdl_ret_t retval;

    switch(idx) {
        case 1:     
            id = GDL_DISPLAY_ID_1; 
            break;
        default: 
            id = GDL_DISPLAY_ID_0;
    }

    retval = gdl_get_display_info(id, &display_info);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_get_display_info");
        return;
    }

    DBG_PRINT("[gdl_get_display_info: width %d height %d]\n",
              display_info.tvmode.width, display_info.tvmode.height);

    (*env)->CallVoidMethod(env, obj, screenCreatedID, (jint)display_info.tvmode.width, (jint)display_info.tvmode.height);
}

/**
 * Window
 */

JNIEXPORT jboolean JNICALL Java_com_sun_javafx_newt_intel_gdl_Window_initIDs
  (JNIEnv *env, jclass clazz)
{
    updateSizeID = (*env)->GetMethodID(env, clazz, "updateSize", "(II)V");
    if (updateSizeID == NULL) {
        DBG_PRINT("initIDs failed\n" );
        return JNI_FALSE;
    }
    DBG_PRINT("initIDs ok\n" );
    return JNI_TRUE;
}

JNIEXPORT jlong JNICALL Java_com_sun_javafx_newt_intel_gdl_Window_CreateSurface
  (JNIEnv *env, jobject obj, jlong displayHandle, jint width, jint height) {

    gdl_driver_info_t * p_driver_info = (gdl_driver_info_t *) (intptr_t) displayHandle;
    gdl_ret_t retval;
    gdl_pixel_format_t pixelFormat = GDL_PF_ARGB_32;
    gdl_color_space_t colorSpace = GDL_COLOR_SPACE_RGB;
    gdl_rectangle_t srcRect, dstRect;

    (void) env;
    (void) obj;
    
    gdl_plane_id_t plane = allocPlane(env, obj);
    if(plane == GDL_PLANE_ID_UNDEFINED) {
        DBG_PRINT("CreateSurface failed, couldn't alloc plane\n" );
        return 0;
    }

    DBG_PRINT("[CreateSurface: width %d height %d plane %d]\n", width, height, plane);

    srcRect.origin.x = 0;
    srcRect.origin.y = 0;
    srcRect.width = width;
    srcRect.height = height;

    dstRect.origin.x = 0;
    dstRect.origin.y = 0;
    dstRect.width = width;
    dstRect.height = height;

    retval = gdl_plane_reset(plane);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_plane_reset");
        freePlane(env, plane);
        return (jlong)0;
    }

    retval = gdl_plane_config_begin(plane);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_plane_config_begin");
        freePlane(env, plane);
        return (jlong)0;
    }

    retval = gdl_plane_set_attr(GDL_PLANE_SRC_COLOR_SPACE, &colorSpace);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_plane_set_attr color space");
        freePlane(env, plane);
        return (jlong)0;
    }

    retval = gdl_plane_set_attr(GDL_PLANE_PIXEL_FORMAT, &pixelFormat);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_plane_set_attr  pixel format");
        freePlane(env, plane);
        return (jlong)0;
    }

    retval = gdl_plane_set_attr(GDL_PLANE_DST_RECT, &dstRect);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_plane_set_attr dstRect");
        freePlane(env, plane);
        return (jlong)0;
    }

    retval = gdl_plane_set_attr(GDL_PLANE_SRC_RECT, &srcRect);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_plane_set_attr srcRect");
        freePlane(env, plane);
        return (jlong)0;
    }

    retval = gdl_plane_config_end(GDL_FALSE);
    if (retval != GDL_SUCCESS) {
        JNI_ThrowNew(env, "java/lang/IllegalStateException", "gdl_plane_config_end");
        freePlane(env, plane);
        return (jlong)0;
    }

    (*env)->CallVoidMethod(env, obj, updateSizeID, (jint)width, (jint)height);

    DBG_PRINT("[CreateSurface] returning plane %d\n", plane);
    
    return (jlong) (intptr_t) plane;
}

JNIEXPORT void JNICALL Java_com_sun_javafx_newt_intel_gdl_Window_CloseSurface
  (JNIEnv *env, jobject obj, jlong display, jlong surface)
{
    gdl_plane_id_t plane = (gdl_plane_id_t) (intptr_t) surface ;
    freePlane(env, plane);
    
    DBG_PRINT("[CloseSurface] plane %d\n", plane);
}