aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/windows/src/native/net_java_games_input_DummyWindow.c
blob: 539e45c8d8cb91d7746eb46b1ca6e53ea869a000 (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
/*
 * %W% %E%
 *
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

#include <windows.h>
#include <jni.h>
#include "net_java_games_input_DummyWindow.h"
#include "util.h"

static const TCHAR* DUMMY_WINDOW_NAME = "JInputControllerWindow";

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

static BOOL RegisterDummyWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)DummyWndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)NULL;
	wcex.lpszClassName	= DUMMY_WINDOW_NAME;
	wcex.hIconSm		= NULL;
	return RegisterClassEx(&wcex);
}

JNIEXPORT jlong JNICALL Java_net_java_games_input_DummyWindow_createWindow(JNIEnv *env, jclass unused) {
    HINSTANCE hInst = GetModuleHandle(NULL);
	HWND hwndDummy;
	WNDCLASSEX class_info;
	class_info.cbSize = sizeof(WNDCLASSEX);
	class_info.cbClsExtra = 0;
	class_info.cbWndExtra = 0;
	
	if (!GetClassInfoEx(hInst, DUMMY_WINDOW_NAME, &class_info)) {
		// Register the dummy input window
		if (!RegisterDummyWindow(hInst)) {
			throwIOException(env, "Failed to register window class (%d)\n", GetLastError());
			return 0;
		}
	}

    // Create the dummy input window
    hwndDummy = CreateWindow(DUMMY_WINDOW_NAME, NULL,
        WS_POPUP | WS_ICONIC,
        0, 0, 0, 0, NULL, NULL, hInst, NULL);
    if (hwndDummy == NULL) {
		throwIOException(env, "Failed to create window (%d)\n", GetLastError());
        return 0;
    }
	return (jlong)hwndDummy;
}

JNIEXPORT void JNICALL Java_net_java_games_input_DummyWindow_nDestroy(JNIEnv *env, jclass unused, jlong hwnd_address) {
	HWND hwndDummy = (HWND)(INT_PTR)hwnd_address;
	BOOL result = DestroyWindow(hwndDummy);
	if (!result) {
		throwIOException(env, "Failed to destroy window (%d)\n", GetLastError());
	}
}