2
Copyright (c) 2006 uim Project http://uim.freedesktop.org/
6
Redistribution and use in source and binary forms, with or
7
without modification, are permitted provided that the
8
following conditions are met:
10
1. Redistributions of source code must retain the above
11
copyright notice, this list of conditions and the
13
2. Redistributions in binary form must reproduce the above
14
copyright notice, this list of conditions and the
15
following disclaimer in the documentation and/or other
16
materials provided with the distribution.
17
3. Neither the name of authors nor the names of its
18
contributors may be used to endorse or promote products
19
derived from this software without specific prior written
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
#include <sys/types.h>
47
#include <sys/select.h>
50
#include <uim/uim-helper.h>
52
#include "uim-el-helper-agent.h"
55
unsigned cmdbuf_len = 0;
60
command_exists_in_cmdbuf()
62
return (strchr(cmdbuf, '\n') != NULL);
73
debug_printf(DEBUG_NOTE, "process command\n");
75
/* cmd always terminates with \n */
77
p = strchr(cmdbuf, '\n');
80
/* send command to helper-server */
81
cmd = helper_message_decode(cmdbuf);
82
uim_helper_send_message(helper_fd, cmd);
84
if (strcmp(cmd, "focus_in\n") == 0)
92
memmove(cmdbuf, p + 1, rest);
101
process_message(char *msg)
105
debug_printf(DEBUG_NOTE, "process message from uim-helper-server\n");
108
if ((eol = strchr(msg, '\n')) != NULL) {
115
if (strcmp("focus_in", msg) == 0) {
118
printf("focus_in\n");
122
} else if (strcmp("prop_activate", msg) == 0) {
124
char *prop = eol + 1;
126
if ((eol = strchr(prop, '\n')) != NULL) {
128
printf("prop_activate %s\n", prop);
131
} else if (strcmp("prop_list_get", msg) == 0) {
133
printf("prop_list_get\n");
135
} else if (strncmp(msg, "im_change_", 10) == 0) {
137
char *imname = eol + 1;
139
if ((eol = strchr(imname, '\n')) != NULL) {
141
printf("%s %s\n", msg, imname);
144
} else if (strcmp("im_list_get", msg) == 0) {
146
printf("im_list_get\n");
148
} else if (strcmp("commit_string", msg) == 0) {
150
char *rest, *charset = "UTF-8";
152
debug_printf(DEBUG_NOTE, "commit string\n");
156
if ((eol = strchr(rest, '\n')) != NULL) {
158
if (strncmp(rest, "charset=", 8) == 0) {
160
if ((eol = strchr(charset, ' ')) != NULL) {
165
rest = helper_message_encode(rest);
166
printf("commit_string %s %s\n", charset, rest);
170
} else if (strcmp("prop_update_custom", msg) == 0) {
172
char *val, *custom = eol + 1;
174
if ((eol = strchr(custom, '\n')) != NULL) {
178
if ((eol = strchr(val, '\n')) != NULL) {
180
printf("prop_update_custom %s %s\n", custom, val);
184
} else if (strcmp("custom_reload_notify", msg) == 0) {
186
printf("custom_reload_notify\n");
190
debug_printf(DEBUG_NOTE, "other message %s\n", msg);
204
char rbuf[DEFAULT_MESSAGE_SIZE];
206
debug_printf(DEBUG_NOTE, "read command\n");
209
len = read(STDIN_FILENO, rbuf, sizeof(rbuf) - 1);
212
if (strlen(cmdbuf) + len + 1 > cmdbuf_len) {
213
cmdbuf_len += DEFAULT_MESSAGE_SIZE;
214
cmdbuf = (char *)realloc(cmdbuf, cmdbuf_len);
215
debug_printf(DEBUG_NOTE, "cmdbuf has extended\n");
218
strcat(cmdbuf, rbuf);
220
} while (!command_exists_in_cmdbuf());
226
wait_data_arrival(fd_set *rfds)
228
int fdmax = STDIN_FILENO;
232
FD_SET(helper_fd, rfds);
233
if (helper_fd > fdmax) fdmax = helper_fd;
236
FD_SET(STDIN_FILENO, rfds);
238
if (select(helper_fd + 1, rfds, NULL, NULL, NULL) < 0)
239
debug_printf(DEBUG_ERROR, "select error\n");
253
main(int argc, char *argv[])
257
while ((opt = getopt(argc, argv, "d")) != -1) {
265
if (debug_level == 0) fclose(stderr);
267
if (uim_init() < 0) {
268
debug_printf(DEBUG_ERROR, "uim_init failed\n");
276
cmdbuf_len = DEFAULT_MESSAGE_SIZE;
277
cmdbuf = (char *)malloc(cmdbuf_len);
284
check_helper_connection();
286
wait_data_arrival(&rfds);
288
debug_printf(DEBUG_NOTE, "data arrive\n");
290
if (FD_ISSET(STDIN_FILENO, &rfds)) {
294
if (FD_ISSET(helper_fd, &rfds)) {
295
/* read message from helper */
296
uim_helper_read_proc(helper_fd);
299
while (command_exists_in_cmdbuf())
302
while ((msg = uim_helper_get_message())) {
303
process_message(msg);