Subversion Repositories AndroidProjects

Rev

Rev 562 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
562 chris 1
/*
2
 * com_gebauz_Bauzoid_platform_windows_Win7TouchInput.cpp
3
 *
4
 *  Created on: 19.04.2013
5
 *      Author: chiu
6
 */
7
 
8
#include <iostream>
9
#include <Windows.h>
10
#include <windowsx.h>
11
 
12
#include "com_gebauz_Bauzoid_platform_windows_Win7TouchInput.h"
13
 
14
using namespace std;
15
 
16
#define WM_REGISTERTOUCH (WM_USER + 0x0001)
17
 
18
WNDPROC wpOrigEditProc = NULL;
19
JNIEnv* jniEnv = NULL;
20
jobject jInputObject = NULL;
21
 
22
 
563 chris 23
void SetIntField(jobject obj, char* fieldName, int value)
562 chris 24
{
563 chris 25
        jclass cls = jniEnv->GetObjectClass(obj);
26
        jfieldID fid = jniEnv->GetFieldID(cls, fieldName, "I");
27
        jniEnv->SetIntField(obj, fid, value);
28
 
29
        /*jfieldID fieldId = env->GetFieldID(cls, fieldName, "I");
562 chris 30
        if (fieldId == NULL)
31
        {
32
                cout << "Field '" << fieldName << "' not found!" << endl;
33
                return;
34
        }
563 chris 35
        env->SetIntField(obj, fieldId, value);*/
562 chris 36
}
37
 
38
void SendTouchInput(TOUCHINPUT& input)
39
{
40
        cout << "[C++] ID: " << input.dwID << " (" << input.x << ", " << input.y << ") - " << input.dwFlags << endl;
41
 
42
        if ((jniEnv == NULL) || (jInputObject == NULL))
43
                return;
44
 
45
        // construct a Java WindowsTouchInfo object and send it to Win7TouchInput
46
 
47
        // find the class
48
        jclass touchInfoClass = jniEnv->FindClass("com/gebauz/Bauzoid/platform/windows/Win7TouchInput$WindowsTouchInfo");
49
        if (touchInfoClass == NULL)
50
        {
51
                cout << "WindowsTouchInfo class not found" << endl;
52
                return;
53
        }
54
 
563 chris 55
        //cout << "1" << endl;
56
 
562 chris 57
        // get constructor
58
        jmethodID methodConstructor = jniEnv->GetMethodID(touchInfoClass, "<init>", "()V");
59
        if (methodConstructor == NULL)
60
        {
61
                cout << "WindowsTouchInfo constructor not found" << endl;
62
                return;
63
        }
64
 
563 chris 65
        //cout << "2" << endl;
66
 
562 chris 67
        // invoke constructor
68
        jobject touchInfoInstance = jniEnv->NewObject(touchInfoClass, methodConstructor);
69
        if (touchInfoInstance == NULL)
70
        {
71
                cout << "Could not create WindowsTouchInfo instance!" << endl;
72
                return;
73
        }
74
 
563 chris 75
        //cout << "3" << endl;
76
 
562 chris 77
        // set fields
563 chris 78
        SetIntField(touchInfoInstance, "x", input.x);
79
        SetIntField(touchInfoInstance, "y", input.y);
80
        SetIntField(touchInfoInstance, "id", input.dwID);
81
        SetIntField(touchInfoInstance, "actionFlags", input.dwFlags);
562 chris 82
 
563 chris 83
        //cout << "4" << endl;
84
 
85
        /*jclass cls = jniEnv->GetObjectClass(touchInfoInstance);
86
        jfieldID fid = jniEnv->GetFieldID(cls, "id", "I");
87
        jniEnv->SetIntField(touchInfoInstance, fid, 1337);*/
88
 
562 chris 89
        // invoke method that takes WindowsTouchInfo as parameter
90
        jmethodID methodOnTouch = jniEnv->GetMethodID(jniEnv->GetObjectClass(jInputObject), "onTouch", "(Lcom/gebauz/Bauzoid/platform/windows/Win7TouchInput$WindowsTouchInfo;)V");
563 chris 91
        if (methodOnTouch == NULL)
562 chris 92
        {
93
                cout << "Win7TouchInput.onTouch() method not found!" << endl;
94
                return;
95
        }
96
        jniEnv->CallVoidMethod(jInputObject, methodOnTouch, touchInfoInstance);
563 chris 97
 
98
        /*
99
        jmethodID methodTest = jniEnv->GetMethodID(jniEnv->GetObjectClass(jInputObject), "test", "(I)V");
100
        if (methodTest == NULL)
101
        {
102
                cout << "Win7TouchInput.onTouch() method not found!" << endl;
103
                return;
104
        }
105
        jniEnv->CallVoidMethod(jInputObject, methodTest, 1337);*/
106
 
107
        //jniEnv->DeleteLocalRef(touchInfoInstance);
108
 
109
        //cout << "6" << endl;
562 chris 110
}
111
 
112
void HandleTouch(HWND hwnd, WPARAM wParam, LPARAM lParam)
113
{
114
        UINT cInputs = LOWORD(wParam);
115
        PTOUCHINPUT pInputs = new TOUCHINPUT[cInputs];
116
        if (NULL != pInputs)
117
        {
118
                if (GetTouchInputInfo((HTOUCHINPUT)lParam,
119
                                                          cInputs,
120
                                                          pInputs,
121
                                                          sizeof(TOUCHINPUT)))
122
                {
123
                        // process pInputs
124
                        /*
125
                        wchar_t text[200];
126
                        wsprintf(text, L"WM_TOUCH %d", cInputs);
127
 
128
                        ::SetWindowText(hwnd, text);*/
129
 
563 chris 130
                        for (UINT i = 0; i < cInputs; i++)
562 chris 131
                        {
132
                                SendTouchInput(pInputs[i]);
133
                        }
134
 
563 chris 135
                        /*if (jniEnv != NULL)
136
                        {
137
                                jclass thisClass = jniEnv->GetObjectClass(jInputObject);
138
                                jmethodID midTest = jniEnv->GetMethodID(thisClass, "test", "()V");
139
                                if (NULL == midTest)
140
                                {
141
                                        cout << "test method not found" << endl;
142
                                }
143
                                else
144
                                {
145
                                        jniEnv->CallVoidMethod(jInputObject, midTest);
146
                                        cout << "Called back!!!" << endl;
147
                                }
148
                        }*/
149
 
150
 
562 chris 151
                        if (!CloseTouchInputHandle((HTOUCHINPUT)lParam))
152
                        {
153
                                // error handling
154
                        }
155
                }
156
                else
157
                {
158
                        // GetLastError() and error handling
159
                }
160
                delete [] pInputs;
161
        }
162
        else
163
        {
164
                // error handling, presumably out of memory
165
        }
166
}
167
 
168
// Subclass procedure 
169
LRESULT APIENTRY WindowSubClassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
170
{
171
        switch (uMsg)
172
        {
173
        case WM_REGISTERTOUCH:
174
                {
175
                        if (!RegisterTouchWindow(hwnd, TWF_WANTPALM))
176
                        {
177
                                cout << "error registering touch window: " << GetLastError() << endl;
178
                                break;
179
                        }
180
                        cout << "Registered touch" << endl;
181
 
563 chris 182
                        /*if (jniEnv != NULL)
562 chris 183
                        {
184
                                jclass thisClass = jniEnv->GetObjectClass(jInputObject);
185
                                jmethodID midTest = jniEnv->GetMethodID(thisClass, "test", "()V");
186
                                if (NULL == midTest)
187
                                {
188
                                        cout << "test method not found" << endl;
189
                                        break;
190
                                }
191
                                jniEnv->CallVoidMethod(jInputObject, midTest);
192
                                cout << "Called back!!!" << endl;
193
                        }*/
194
                }
195
                break;
196
        case WM_TOUCH:
197
                HandleTouch(hwnd, wParam, lParam);
198
                break;
199
        }
200
 
201
        return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
202
}
203
 
204
// JNI exported method
205
JNIEXPORT void JNICALL Java_com_gebauz_Bauzoid_platform_windows_Win7TouchInput_initTouch(JNIEnv* env, jobject jthis, jlong jhwnd)
206
{
207
        HWND hwnd = (HWND)jhwnd;
208
        jniEnv = env;
563 chris 209
        jInputObject = env->NewGlobalRef(jthis);
562 chris 210
 
211
        wpOrigEditProc = (WNDPROC)::SetWindowLong(hwnd, GWL_WNDPROC, (LONG)WindowSubClassProc);
212
        ::SendMessage(hwnd, WM_REGISTERTOUCH, 0, 0);
213
 
214
        cout << "Subclassed window" << endl;
215
}