Added websocket server for supporting legacy Web IMEs
[platform/core/uifw/libscl-core.git] / src / legacy_support / web_helper_agent.cpp
1 /*
2  * Copyright (c) 2012 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
18 #include <stdio.h>
19 #include <dlog.h>
20
21 #include "web_helper_agent.h"
22
23 #include "websocket.h"
24 #include "sclcoreimpl.h"
25
26 using namespace scl;
27
28 typedef struct {
29     int major_version;
30     WEB_HELPER_AGENT_TYPE agent_type;
31 } WEB_HELPER_AGENT_TYPES_FOR_VERSIONS;
32
33 static WEB_HELPER_AGENT_TYPES_FOR_VERSIONS web_helper_agent_types_for_versions[] = {
34     {1, WEB_HELPER_AGENT_WEBSOCKET}, /* Major version 1 indicates that it uses websocket for communication */
35 };
36
37 static int WEB_HELPER_AGENT_TYPES_FOR_VERSIONS_NUM = \
38     sizeof(web_helper_agent_types_for_versions) / sizeof(WEB_HELPER_AGENT_TYPES_FOR_VERSIONS);
39
40 WEB_HELPER_AGENT_TYPE CWebHelperAgent::get_web_helper_agent_type_from_major_version(int version)
41 {
42     for (int loop = 0;loop < WEB_HELPER_AGENT_TYPES_FOR_VERSIONS_NUM;loop++) {
43         if (web_helper_agent_types_for_versions[loop].major_version == version) {
44             return web_helper_agent_types_for_versions[loop].agent_type;
45         }
46     }
47     return WEB_HELPER_AGENT_UNKNOWN;
48 }
49
50 CWebHelperAgent* CWebHelperAgent::create_web_helper_agent(WEB_HELPER_AGENT_TYPE type)
51 {
52     CWebHelperAgent *ret = NULL;
53     if (type == WEB_HELPER_AGENT_WEBSOCKET) {
54         ret = new CWebHelperAgentWebSocket;
55     }
56     return ret;
57 }
58
59 void CWebHelperAgent::destroy_web_helper_agent(CWebHelperAgent* agent)
60 {
61     if (agent) delete agent;
62 }
63
64 CWebHelperAgent::CWebHelperAgent()
65 {
66 }
67
68 CWebHelperAgent::~CWebHelperAgent()
69 {
70
71 }
72
73 bool CWebHelperAgent::init()
74 {
75     return true;
76 }
77
78 bool CWebHelperAgent::exit()
79 {
80     return true;
81 }
82
83 void CWebHelperAgent::signal(int sig)
84 {
85
86 }
87
88 void CWebHelperAgent::log(const char *str)
89 {
90     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
91     if (impl) {
92         //impl->logise_log(str);
93     }
94 }
95
96 void CWebHelperAgent::commit_string(const char *str)
97 {
98     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
99     if (impl) {
100         impl->commit_string(-1, "", str);
101     }
102 }
103
104 void CWebHelperAgent::update_preedit_string(const char *str)
105 {
106     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
107     if (impl) {
108         impl->update_preedit_string(-1, "", str);
109     }
110 }
111
112 void CWebHelperAgent::send_key_event(unsigned int key, unsigned int key_mask)
113 {
114     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
115     if (impl) {
116         impl->send_key_event(-1, "", key, key_mask);
117     }
118 }
119
120 void CWebHelperAgent::forward_key_event(unsigned int key)
121 {
122     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
123     if (impl) {
124         impl->forward_key_event(-1, "", key, scim::SCIM_KEY_NullMask);
125     }
126 }
127
128 void CWebHelperAgent::set_keyboard_sizes(int portrait_width, int portrait_height, int landscape_width, int landscape_height)
129 {
130     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
131     if (impl) {
132         SclSize portrait, landscape;
133         portrait.width = portrait_width;
134         portrait.height = portrait_height;
135         landscape.width = landscape_width;
136         landscape.height = landscape_height;
137         impl->set_keyboard_size_hints(portrait, landscape);
138     }
139 }
140
141 void CWebHelperAgent::set_selection(int start_index, int end_index)
142 {
143     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
144     if (impl) {
145         impl->set_selection(start_index, end_index);
146     }
147 }
148
149 void CWebHelperAgent::get_selection()
150 {
151     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
152     if (impl) {
153         //impl->get_selection(-1, "", );
154     }
155 }
156
157 void CWebHelperAgent::get_surrounding_text(int maxlen_before, int maxlen_after)
158 {
159     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
160     if (impl) {
161         impl->get_surrounding_text("", maxlen_before, maxlen_after);
162     }
163 }
164
165 void CWebHelperAgent::delete_surrounding_text(int offset, int len)
166 {
167     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
168     if (impl) {
169         impl->delete_surrounding_text(offset, len);
170     }
171 }
172
173 std::string CMagicKeyManager::get_magic_key()
174 {
175     static std::string current_magic_key;
176
177     /* If we don't have magic key generated yet */
178     if (current_magic_key.length() != MAGIC_KEY_LENGTH) {
179         char magic_key[MAGIC_KEY_LENGTH + 1];
180         /* We are going to generate a magic key that contains ascii characters in the range of '0' to 'z' */
181         const char magic_key_range_lower = '0';
182         const char magic_key_range_upper = 'Z';
183
184         srand(time(NULL));
185         for(int loop = 0;loop < MAGIC_KEY_LENGTH;loop++) {
186             magic_key[loop] = (rand() % (magic_key_range_upper - magic_key_range_lower)) + magic_key_range_lower;
187         }
188         magic_key[MAGIC_KEY_LENGTH] = '\0';
189
190         current_magic_key = magic_key;
191     }
192
193     return current_magic_key;
194 }