-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
243 lines (213 loc) · 6.4 KB
/
main.cpp
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
/**
* @file main.cpp
*
* This file contains the support code at the C++ level for an HTML5/JS
* application that can access device services from JavaScript.
*
* You don't need to change anything in this code file unless you
* wish to add support for functions not available out-of-the box
* in wormhole.js.
*
* When reading the code below, it is good to know that there are
* two message formats: JSON and string streams. String streams are
* generally faster. See comments in the code below for further details.
* PhoneGap uses JSON messages, NativeUI uses string streams.
*/
#include <Wormhole/WebAppMoblet.h>
#include <Wormhole/MessageProtocol.h>
#include <Wormhole/MessageStream.h>
#include <Wormhole/Libs/JSONMessage.h>
#include <Wormhole/Libs/PhoneGap/PhoneGapMessageHandler.h>
#include <Wormhole/Libs/JSNativeUI/NativeUIMessageHandler.h>
#include <Wormhole/Libs/JSNativeUI/ResourceMessageHandler.h>
#include "MAHeaders.h"
// Namespaces we want to access.
using namespace MAUtil; // Class Moblet
using namespace NativeUI; // WebView widget.
using namespace Wormhole; // Wormhole library.
/**
* The application class.
*/
class MyMoblet : public WebAppMoblet
{
public:
MyMoblet() :
mPhoneGapMessageHandler(getWebView()),
mNativeUIMessageHandler(getWebView()),
mResourceMessageHandler(getWebView())
{
// Extract files in LocalFiles folder to the device.
extractFileSystem();
// Enable message sending from JavaScript to C++.
enableWebViewMessages();
// Show the WebView that contains the HTML/CSS UI
// and the JavaScript code.
getWebView()->setVisible(true);
// The page in the "LocalFiles" folder to
// show when the application starts.
showPage("index.html");
// Send the Device Screen size to JavaScript.
MAExtent scrSize = maGetScrSize();
int width = EXTENT_X(scrSize);
int height = EXTENT_Y(scrSize);
char buf[512];
sprintf(
buf,
"{mosyncScreenWidth=%d; mosyncScreenHeight = %d;}",
width,
height);
callJS(buf);
// Set the beep sound. This is defined in the
// Resources/Resources.lst file. You can change
// this by changing the sound file in that folder.
mPhoneGapMessageHandler.setBeepSound(BEEP_WAV);
// Initialize PhoneGap.
mPhoneGapMessageHandler.initializePhoneGap();
}
virtual ~MyMoblet()
{
// Add cleanup code as needed.
}
/**
* This method is called when a key is pressed.
* Forwards the event to PhoneGapMessageHandler.
*/
void keyPressEvent(int keyCode, int nativeCode)
{
// Forward to PhoneGap MessageHandler.
mPhoneGapMessageHandler.processKeyEvent(keyCode, nativeCode);
}
/**
* This method handles messages sent from the WebView.
*
* Note that the data object will be valid only during
* the life-time of the call of this method, then it
* will be deallocated.
*
* @param webView The WebView that sent the message.
* @param urlData Data object that holds message content.
*/
void handleWebViewMessage(WebView* webView, MAHandle data)
{
// Uncomment to print message data for debugging.
// You need to build the project in debug mode for
// the log output to be displayed.
//printMessage(data);
// Check the message protocol.
MessageProtocol protocol(data);
if (protocol.isMessageStreamJSON())
{
handleMessageStreamJSON(webView, data);
}
else if (protocol.isMessageStream())
{
handleMessageStream(webView, data);
}
else
{
lprintfln("@@@ MOSYNC: Undefined message protocol");
}
}
/**
* Handles JSON messages. This is used by PhoneGap.
*
* You can send your own messages from JavaScript and handle them here.
*
* @param webView A pointer to the web view posting this message.
* @param data The raw encoded JSON message array.
*/
void handleMessageStreamJSON(WebView* webView, MAHandle data)
{
// Create the message object. This parses the message data.
// The message object contains one or more messages.
JSONMessage message(webView, data);
// Loop through messages.
while (message.next())
{
// This detects the PhoneGap protocol.
if (message.is("PhoneGap"))
{
mPhoneGapMessageHandler.handlePhoneGapMessage(message);
}
// Here you can add your own message handing as needed.
}
}
/**
* Handles string stream messages (generally faster than JSON messages).
* This is used by the JavaScript NativeUI system.
*
* You can send your own messages from JavaScript and handle them here.
*
* @param webView A pointer to the web view posting this message.
* @param data The raw encoded stream of string messages.
*/
void handleMessageStream(WebView* webView, MAHandle data)
{
// Create a message stream object. This parses the message data.
// The message object contains one or more strings.
MessageStream stream(webView, data);
// Pointer to a string in the message stream.
const char* p;
// Process messages while there are strings left in the stream.
while (p = stream.getNext())
{
if (0 == strcmp(p, "NativeUI"))
{
//Forward NativeUI messages to the respective message handler
mNativeUIMessageHandler.handleMessage(stream);
}
else if (0 == strcmp(p, "Resource"))
{
//Forward Resource messages to the respective message handler
mResourceMessageHandler.handleMessage(stream);
}
else if (0 == strcmp(p, "close"))
{
// Close the application (calls method in class Moblet).
close();
}
// Here you can add your own message handing as needed.
}
}
/**
* For debugging.
*/
void printMessage(MAHandle dataHandle)
{
// Get length of the data, it is not zero terminated.
int dataSize = maGetDataSize(dataHandle);
// Allocate buffer for string data.
char* stringData = (char*) malloc(dataSize + 1);
// Get the data.
maReadData(dataHandle, stringData, 0, dataSize);
// Zero terminate.
stringData[dataSize] = 0;
// Print unparsed message data.
maWriteLog("@@@ MOSYNC Message:", 19);
maWriteLog(stringData, dataSize);
free(stringData);
}
private:
/**
* Handler for PhoneGap messages.
*/
PhoneGapMessageHandler mPhoneGapMessageHandler;
/**
* Handler for NativeUI messages
*/
NativeUIMessageHandler mNativeUIMessageHandler;
/**
* Handler for resource messages used for NativeUI
*/
ResourceMessageHandler mResourceMessageHandler;
};
/**
* Main function that is called when the program starts.
* Here an instance of the MyMoblet class is created and
* the program enters the main event loop.
*/
extern "C" int MAMain()
{
Moblet::run(new MyMoblet());
return 0;
}