Subversion Repositories AndroidProjects

Rev

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