summaryrefslogtreecommitdiffstats
path: root/src/nativewindow/native/win32/GDImisc.c
blob: 40f42f2854b07448448273dbfe6120fbd6fdb883 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <jni.h>
#include <stdlib.h>
#include <assert.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN

#include <wingdi.h>
#include <stddef.h>

#include <gluegen_stdint.h>

#include <stdio.h>

#include "NativewindowCommon.h"
#include "jogamp_nativewindow_windows_GDIUtil.h"

// #define VERBOSE_ON 1

#ifdef VERBOSE_ON
    #define DBG_PRINT(args...) fprintf(stderr, args);
#else
    #define DBG_PRINT(args...)
#endif

static const char * const ClazzNamePoint = "javax/media/nativewindow/util/Point";
static const char * const ClazzAnyCstrName = "<init>";
static const char * const ClazzNamePointCstrSignature = "(II)V";

static jclass pointClz = NULL;
static jmethodID pointCstr = NULL;

static DWORD threadid = 0;

typedef struct ThreadParam_s
{
  jlong jHInstance;
  const TCHAR* wndClassName;
  const TCHAR* wndName;
  jint x;
  jint y;
  jint width;
  jint height;
  volatile HWND *hWndPtr;
  volatile BOOL *threadReady;
} ThreadParam;

#define TM_OPENWIN WM_APP+1
#define TM_CLOSEWIN WM_APP+2
#define TM_STOP WM_APP+3

DWORD WINAPI ThreadFunc(LPVOID param)
{
 MSG msg;
 BOOL bRet;
 ThreadParam *startupThreadParam = (ThreadParam*)param;

  /* there can not be any messages for us now, as the creator waits for
     threadReady before continuing, but we must use this PeekMessage() to
     create the thread message queue */
  PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);

  /* now we can safely say: we have a qeue and are ready to receive messages */
  *(startupThreadParam->threadReady) = TRUE;

  while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    {
      if (bRet == -1)
	{
	  return 0;
	}
      else
	{
	  switch(msg.message)
	    {
	    case TM_OPENWIN:
	      {
		ThreadParam *tParam = (ThreadParam*)msg.wParam;
		HINSTANCE hInstance = (HINSTANCE) (intptr_t) tParam->jHInstance;
		DWORD dwExStyle;
		DWORD dwStyle;

		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		dwStyle = WS_OVERLAPPEDWINDOW;

		HWND hwnd = CreateWindowEx( dwExStyle,
					    tParam->wndClassName,
					    tParam->wndName,
				dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
			  tParam->x, tParam->y, tParam->width, tParam->height,
					    NULL, NULL, hInstance, NULL );

		*(tParam->hWndPtr) = hwnd;
		*(tParam->threadReady) = TRUE;
	      }
	      break;
	    case TM_CLOSEWIN:
	      {
		ThreadParam *tParam = (ThreadParam*)msg.wParam;
		HWND hwnd = *(tParam->hWndPtr);
		DestroyWindow(hwnd);
		*(tParam->threadReady) = TRUE;
	      }
	      break;
	    case TM_STOP:
	      return 0;
	      break;	    
	    default:
	      TranslateMessage(&msg); 
	      DispatchMessage(&msg); 
	      break;
	    }
	}
    }

 return 0;
} /* ThreadFunc */


HINSTANCE GetApplicationHandle() {
    return GetModuleHandle(NULL);
}

/*   Java->C glue code:
 *   Java package: jogamp.nativewindow.windows.GDIUtil
 *    Java method: boolean CreateWindowClass(long hInstance, java.lang.String clazzName, long wndProc)
 *     C function: BOOL CreateWindowClass(HANDLE hInstance, LPCSTR clazzName, HANDLE wndProc);
 */
JNIEXPORT jboolean JNICALL
Java_jogamp_nativewindow_windows_GDIUtil_CreateWindowClass
    (JNIEnv *env, jclass _unused, jlong jHInstance, jstring jClazzName, jlong wndProc) 
{
    HINSTANCE hInstance = (HINSTANCE) (intptr_t) jHInstance;
    const TCHAR* clazzName = NULL;
    WNDCLASS  wc;
    jboolean res;

#ifdef UNICODE
    clazzName = NewtCommon_GetNullTerminatedStringChars(env, jClazzName);
#else
    clazzName = (*env)->GetStringUTFChars(env, jClazzName, NULL);
#endif

    ZeroMemory( &wc, sizeof( wc ) );
    if( GetClassInfo( hInstance,  clazzName, &wc ) ) {
        // registered already
        res = JNI_TRUE;
    } else {      
        // register now
        ZeroMemory( &wc, sizeof( wc ) );
        wc.style = CS_HREDRAW | CS_VREDRAW ;
        wc.lpfnWndProc = (WNDPROC) (intptr_t) wndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = NULL;
        wc.hCursor = LoadCursor( NULL, IDC_ARROW);
        wc.hbrBackground = NULL; // no background paint - GetStockObject(BLACK_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = clazzName;
        res = ( 0 != RegisterClass( &wc ) ) ? JNI_TRUE : JNI_FALSE ;
    }

#ifdef UNICODE
    free((void*) clazzName);
#else
    (*env)->ReleaseStringUTFChars(env, jClazzName, clazzName);
#endif

    if(res == JNI_TRUE)
      {
	volatile BOOL threadReady = FALSE;
	ThreadParam tParam = {0};
	tParam.threadReady = &threadReady;

	CreateThread(NULL, 0, ThreadFunc, (LPVOID)&tParam, 0, &(threadid));
	if(threadid)
	  {
	    while(1)
	      {
		if(threadReady)
		  break;
	      }
	  }
	else
	  {
	    res = JNI_FALSE;
	  }
      }

    return res;
}

/*   Java->C glue code:
 *   Java package: jogamp.nativewindow.windows.GDIUtil
 *    Java method: boolean DestroyWindowClass(long hInstance, java.lang.String className)
 *     C function: BOOL DestroyWindowClass(HANDLE hInstance, LPCSTR className);
 */
JNIEXPORT jboolean JNICALL
Java_jogamp_nativewindow_windows_GDIUtil_DestroyWindowClass
    (JNIEnv *env, jclass _unused, jlong jHInstance, jstring jClazzName) 
{
    HINSTANCE hInstance = (HINSTANCE) (intptr_t) jHInstance;
    const TCHAR* clazzName = NULL;
    jboolean res;

#ifdef UNICODE
    clazzName = NewtCommon_GetNullTerminatedStringChars(env, jClazzName);
#else
    clazzName = (*env)->GetStringUTFChars(env, jClazzName, NULL);
#endif

    res = ( 0 != UnregisterClass( clazzName, hInstance ) ) ? JNI_TRUE : JNI_FALSE ;

#ifdef UNICODE
    free((void*) clazzName);
#else
    (*env)->ReleaseStringUTFChars(env, jClazzName, clazzName);
#endif

    PostThreadMessage(threadid, TM_STOP, 0, 0);

    return res;
}

/*   Java->C glue code:
 *   Java package: jogamp.nativewindow.windows.GDIUtil
 *    Java method: long CreateDummyWindowAndMessageLoop(long hInstance, java.lang.String className, java.lang.String windowName, int x, int y, int width, int height)
 *     C function: HANDLE CreateDummyWindowAndMessageLoop(HANDLE hInstance, LPCSTR className, LPCSTR windowName, int x, int y, int width, int height);
 */
JNIEXPORT jlong JNICALL
Java_jogamp_nativewindow_windows_GDIUtil_CreateDummyWindowAndMessageLoop
    (JNIEnv *env, jclass _unused, jlong jHInstance, jstring jWndClassName, jstring jWndName, jint x, jint y, jint width, jint height) 
{
  volatile HWND hWnd = 0;
  volatile BOOL threadReady = FALSE;
  ThreadParam tParam = {0};

  tParam.jHInstance = jHInstance;
  tParam.x = x;
  tParam.y = y;
  tParam.width = width;
  tParam.height = height;
  tParam.hWndPtr = &hWnd;
  tParam.threadReady = &threadReady;

#ifdef UNICODE
    tParam.wndClassName = NewtCommon_GetNullTerminatedStringChars(env, jWndClassName);
    tParam.wndName = NewtCommon_GetNullTerminatedStringChars(env, jWndName);
#else
    tParam.wndClassName = (*env)->GetStringUTFChars(env, jWndClassName, NULL);
    tParam.wndName = (*env)->GetStringUTFChars(env, jWndName, NULL);
#endif

  PostThreadMessage(threadid, TM_OPENWIN, (WPARAM)&tParam, 0);

  while(1)
    {
      if(threadReady)
	break;
    }

#ifdef UNICODE
    free((void*) tParam.wndClassName);
    free((void*) tParam.wndName);
#else
    (*env)->ReleaseStringUTFChars(env, jWndClassName, tParam.wndClassName);
    (*env)->ReleaseStringUTFChars(env, jWndName, tParam.wndName);
#endif

  return (jlong) (intptr_t) hWnd;
}

/*   Java->C glue code:
 *   Java package: jogamp.nativewindow.windows.GDIUtil
 *    Java method: long CreateDummyWindow0(long hInstance, java.lang.String className, java.lang.String windowName, int x, int y, int width, int height)
 *     C function: HANDLE CreateDummyWindow0(HANDLE hInstance, LPCSTR className, LPCSTR windowName, int x, int y, int width, int height);
 */
JNIEXPORT jlong JNICALL
Java_jogamp_nativewindow_windows_GDIUtil_CreateDummyWindow0
    (JNIEnv *env, jclass _unused, jlong jHInstance, jstring jWndClassName, jstring jWndName, jint x, jint y, jint width, jint height) 
{
    HINSTANCE hInstance = (HINSTANCE) (intptr_t) jHInstance;
    const TCHAR* wndClassName = NULL;
    const TCHAR* wndName = NULL;
    DWORD     dwExStyle;
    DWORD     dwStyle;
    HWND      hWnd;

#ifdef UNICODE
    wndClassName = NewtCommon_GetNullTerminatedStringChars(env, jWndClassName);
    wndName = NewtCommon_GetNullTerminatedStringChars(env, jWndName);
#else
    wndClassName = (*env)->GetStringUTFChars(env, jWndClassName, NULL);
    wndName = (*env)->GetStringUTFChars(env, jWndName, NULL);
#endif

    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    dwStyle = WS_OVERLAPPEDWINDOW;

    hWnd = CreateWindowEx( dwExStyle,
                           wndClassName,
                           wndName,
                           dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
                           x, y, width, height,
                           NULL, NULL, hInstance, NULL );

#ifdef UNICODE
    free((void*) wndClassName);
    free((void*) wndName);
#else
    (*env)->ReleaseStringUTFChars(env, jWndClassName, wndClassName);
    (*env)->ReleaseStringUTFChars(env, jWndName, wndName);
#endif

    return (jlong) (intptr_t) hWnd;
}


/*
 * Class:     jogamp_nativewindow_windows_GDIUtil
 * Method:    initIDs0
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_jogamp_nativewindow_windows_GDIUtil_initIDs0
  (JNIEnv *env, jclass clazz)
{
    if(NativewindowCommon_init(env)) {
        jclass c = (*env)->FindClass(env, ClazzNamePoint);
        if(NULL==c) {
            NativewindowCommon_FatalError(env, "FatalError jogamp_nativewindow_windows_GDIUtil: can't find %s", ClazzNamePoint);
        }
        pointClz = (jclass)(*env)->NewGlobalRef(env, c);
        (*env)->DeleteLocalRef(env, c);
        if(NULL==pointClz) {
            NativewindowCommon_FatalError(env, "FatalError jogamp_nativewindow_windows_GDIUtil: can't use %s", ClazzNamePoint);
        }
        pointCstr = (*env)->GetMethodID(env, pointClz, ClazzAnyCstrName, ClazzNamePointCstrSignature);
        if(NULL==pointCstr) {
            NativewindowCommon_FatalError(env, "FatalError jogamp_nativewindow_windows_GDIUtil: can't fetch %s.%s %s",
                ClazzNamePoint, ClazzAnyCstrName, ClazzNamePointCstrSignature);
        }
    }
    return JNI_TRUE;
}

LRESULT CALLBACK DummyWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

/*
 * Class:     jogamp_nativewindow_windows_GDIUtil
 * Method:    getDummyWndProc0
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_jogamp_nativewindow_windows_GDIUtil_getDummyWndProc0
  (JNIEnv *env, jclass clazz)
{
  return (jlong) (intptr_t) DummyWndProc;
}

/*
 * Class:     jogamp_nativewindow_windows_GDIUtil
 * Method:    GetRelativeLocation0
 * Signature: (JJII)Ljavax/media/nativewindow/util/Point;
 */
JNIEXPORT jobject JNICALL Java_jogamp_nativewindow_windows_GDIUtil_GetRelativeLocation0
  (JNIEnv *env, jclass unused, jlong jsrc_win, jlong jdest_win, jint src_x, jint src_y)
{
    HWND src_win = (HWND) (intptr_t) jsrc_win;
    HWND dest_win = (HWND) (intptr_t) jdest_win;
    POINT dest = { src_x, src_y } ;
    int res;

    res = MapWindowPoints(src_win, dest_win, &dest, 1);

    DBG_PRINT("*** WindowsWindow: getRelativeLocation0: %p %d/%d -> %p %d/%d - ok: %d\n",
        (void*)src_win, src_x, src_y, (void*)dest_win, (int)dest.x, (int)dest.y, res);

    return (*env)->NewObject(env, pointClz, pointCstr, (jint)dest.x, (jint)dest.y);
}

/*
 * Class:     jogamp_nativewindow_windows_GDIUtil
 * Method:    IsChild0
 */
JNIEXPORT jboolean JNICALL Java_jogamp_nativewindow_windows_GDIUtil_IsChild0
  (JNIEnv *env, jclass unused, jlong jwin)
{
    HWND hwnd = (HWND) (intptr_t) jwin;
    LONG style = GetWindowLong(hwnd, GWL_STYLE);
    BOOL bIsChild = 0 != (style & WS_CHILD) ;
    return bIsChild ? JNI_TRUE : JNI_FALSE;
}

/*
 * Class:     jogamp_nativewindow_windows_GDIUtil
 * Method:    IsUndecorated0
 */
JNIEXPORT jboolean JNICALL Java_jogamp_nativewindow_windows_GDIUtil_IsUndecorated0
  (JNIEnv *env, jclass unused, jlong jwin)
{
    HWND hwnd = (HWND) (intptr_t) jwin;
    LONG style = GetWindowLong(hwnd, GWL_STYLE);
    BOOL bIsUndecorated = 0 != (style & (WS_CHILD|WS_POPUP)) ;
    return bIsUndecorated ? JNI_TRUE : JNI_FALSE;
}

/*
 * Class:     jogamp_nativewindow_windows_GDIUtil
 * Method:    SendCloseMessage
 */
JNIEXPORT jboolean JNICALL Java_jogamp_nativewindow_windows_GDIUtil_SendCloseMessage
(JNIEnv *env, jclass unused, jlong jwin)
{
  ThreadParam tParam = {0};
  volatile HWND hwnd = (HWND) (intptr_t) jwin;
  volatile BOOL threadReady = FALSE;

  tParam.hWndPtr = &(hwnd);
  tParam.threadReady = &(threadReady);

  PostThreadMessage(threadid, TM_CLOSEWIN, (WPARAM)&tParam, 0);

  while(1)
    {
      if(threadReady)
	break;
    }

  return JNI_TRUE;
}