| 15747 |
anikendra |
1 |
/*
|
|
|
2 |
* Copyright (c) 2013 BlackBerry Limited
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#include <string>
|
|
|
18 |
#include <sstream>
|
|
|
19 |
#include <json/reader.h>
|
|
|
20 |
#include <json/writer.h>
|
|
|
21 |
#include <pthread.h>
|
|
|
22 |
#include "keyboard_ndk.hpp"
|
|
|
23 |
#include "keyboard_js.hpp"
|
|
|
24 |
#include <QtCore>
|
|
|
25 |
namespace webworks {
|
|
|
26 |
|
|
|
27 |
Keyboard_NDK::Keyboard_NDK(Keyboard_JS *parent):
|
|
|
28 |
m_pParent(parent),
|
|
|
29 |
keyboardProperty(50),
|
|
|
30 |
keyboardThreadCount(1),
|
|
|
31 |
threadHalt(true),
|
|
|
32 |
m_thread(0) {
|
|
|
33 |
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
|
|
34 |
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
35 |
bps_initialize();
|
|
|
36 |
|
|
|
37 |
virtualkeyboard_request_events(0);
|
|
|
38 |
|
|
|
39 |
virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_EMAIL,VIRTUALKEYBOARD_ENTER_DEFAULT);
|
|
|
40 |
|
|
|
41 |
m_pParent->getLog()->info("Keyboard Created");
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
Keyboard_NDK::~Keyboard_NDK() {
|
|
|
48 |
//bps_shutdown();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
// Loops and runs the callback method
|
|
|
53 |
void* KeyboardThread(void* parent) {
|
|
|
54 |
Keyboard_NDK *pParent = static_cast<Keyboard_NDK *>(parent);
|
|
|
55 |
|
|
|
56 |
sleep(1);
|
|
|
57 |
|
|
|
58 |
// 1. Start the library
|
|
|
59 |
bps_initialize();
|
|
|
60 |
|
|
|
61 |
// 2. Request events to flow into the event queue
|
|
|
62 |
virtualkeyboard_request_events(0);
|
|
|
63 |
|
|
|
64 |
sleep(3);
|
|
|
65 |
// 3. Use any service at any time
|
|
|
66 |
//virtualkeyboard_show(); // Show the virtual keyboard
|
|
|
67 |
|
|
|
68 |
// 4. Listen for events
|
|
|
69 |
for (;;) {
|
|
|
70 |
// get an event
|
|
|
71 |
bps_event_t *event;
|
|
|
72 |
bps_get_event(&event, -1); // blocking
|
|
|
73 |
|
|
|
74 |
// handle the event
|
|
|
75 |
pParent->event(event);
|
|
|
76 |
}
|
|
|
77 |
return NULL;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// Starts the thread and returns a message on status
|
|
|
81 |
std::string Keyboard_NDK::keyboardStartThread() {
|
|
|
82 |
m_pParent->NotifyEvent("Teste");
|
|
|
83 |
if (!m_thread) {
|
|
|
84 |
m_pParent->NotifyEvent("Teste");
|
|
|
85 |
int rc;
|
|
|
86 |
rc = pthread_mutex_lock(&mutex);
|
|
|
87 |
threadHalt = false;
|
|
|
88 |
rc = pthread_cond_signal(&cond);
|
|
|
89 |
rc = pthread_mutex_unlock(&mutex);
|
|
|
90 |
|
|
|
91 |
pthread_attr_t thread_attr;
|
|
|
92 |
pthread_attr_init(&thread_attr);
|
|
|
93 |
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
|
|
|
94 |
|
|
|
95 |
pthread_create(&m_thread, &thread_attr, KeyboardThread,
|
|
|
96 |
static_cast<void *>(this));
|
|
|
97 |
pthread_attr_destroy(&thread_attr);
|
|
|
98 |
//threadCallbackId = callbackId;
|
|
|
99 |
m_pParent->getLog()->info("Thread Started");
|
|
|
100 |
return "Thread Started";
|
|
|
101 |
} else {
|
|
|
102 |
m_pParent->getLog()->warn("Thread Started but already running");
|
|
|
103 |
return "Thread Running";
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
void Keyboard_NDK::event(bps_event_t *event) {
|
|
|
109 |
Json::FastWriter writer;
|
|
|
110 |
Json::Value root;
|
|
|
111 |
root["threadCount"] = "10";
|
|
|
112 |
int domain = bps_event_get_domain(event);
|
|
|
113 |
if (domain == virtualkeyboard_get_domain()) {
|
|
|
114 |
int code = bps_event_get_code(event);
|
|
|
115 |
int a;
|
|
|
116 |
std::string str;
|
|
|
117 |
std::string eventString;
|
|
|
118 |
std::ostringstream strs;
|
|
|
119 |
switch(code) {
|
|
|
120 |
case VIRTUALKEYBOARD_EVENT_VISIBLE:
|
|
|
121 |
eventString = "native.keyboardshow";
|
|
|
122 |
eventString.append(" ");
|
|
|
123 |
virtualkeyboard_get_height(&a) ;
|
|
|
124 |
strs << a;
|
|
|
125 |
str = strs.str();
|
|
|
126 |
eventString.append("{\"keyboardHeight\":\""+str+"\"}");
|
|
|
127 |
m_pParent->NotifyEvent(eventString);
|
|
|
128 |
|
|
|
129 |
break;
|
|
|
130 |
case VIRTUALKEYBOARD_EVENT_HIDDEN:
|
|
|
131 |
|
|
|
132 |
m_pParent->NotifyEvent("native.keyboardhide");
|
|
|
133 |
break;
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
}
|
|
|
138 |
void Keyboard_NDK::callKeyboardEmail(){
|
|
|
139 |
virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_EMAIL,VIRTUALKEYBOARD_ENTER_SEND);
|
|
|
140 |
virtualkeyboard_show();
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
void Keyboard_NDK::callKeyboardNumber(){
|
|
|
144 |
|
|
|
145 |
virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_NUMBER,VIRTUALKEYBOARD_ENTER_SEND);
|
|
|
146 |
virtualkeyboard_show();
|
|
|
147 |
}
|
|
|
148 |
void Keyboard_NDK::cancelKeyboard(){
|
|
|
149 |
virtualkeyboard_hide();
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
}
|