~ubuntu-branches/ubuntu/maverick/uim/maverick

« back to all changes in this revision

Viewing changes to emacs/uim-el-helper-agent.c

  • Committer: Bazaar Package Importer
  • Author(s): Masahito Omote
  • Date: 2006-11-23 15:10:53 UTC
  • mfrom: (3.1.8 edgy)
  • Revision ID: james.westby@ubuntu.com-20061123151053-q42sk1lvks41xpfx
Tags: 1:1.2.1-9
uim-gtk2.0.postinst: Don't call update-gtk-immodules on purge.
(closes: Bug#398530)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2006 uim Project http://uim.freedesktop.org/
 
3
 
 
4
  All rights reserved.
 
5
 
 
6
  Redistribution and use in source and binary forms, with or
 
7
  without modification, are permitted provided that the
 
8
  following conditions are met:
 
9
 
 
10
  1. Redistributions of source code must retain the above
 
11
     copyright notice, this list of conditions and the
 
12
     following disclaimer.
 
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
 
20
     permission.
 
21
 
 
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.
 
35
*/
 
36
 
 
37
 
 
38
#include <stdio.h>
 
39
#include <string.h>
 
40
#include <stdarg.h>
 
41
#include <stdlib.h>
 
42
#include <ctype.h>
 
43
#include <unistd.h>
 
44
#include <locale.h>
 
45
 
 
46
#include <sys/types.h>
 
47
#include <sys/select.h>
 
48
 
 
49
#include <uim/uim.h>
 
50
#include <uim/uim-helper.h>
 
51
 
 
52
#include "uim-el-helper-agent.h"
 
53
 
 
54
char* cmdbuf = NULL;
 
55
unsigned cmdbuf_len = 0;
 
56
 
 
57
int focused = 0;
 
58
 
 
59
static int
 
60
command_exists_in_cmdbuf()
 
61
{
 
62
  return (strchr(cmdbuf, '\n') != NULL);
 
63
}
 
64
 
 
65
 
 
66
static int
 
67
process_command()
 
68
{
 
69
  char *p;
 
70
  char *cmd;
 
71
  unsigned rest;
 
72
 
 
73
  debug_printf(DEBUG_NOTE, "process command\n");
 
74
  
 
75
  /* cmd always terminates with \n */
 
76
 
 
77
  p = strchr(cmdbuf, '\n');
 
78
  *p = '\0';
 
79
 
 
80
  /* send command to helper-server */
 
81
  cmd = helper_message_decode(cmdbuf);
 
82
  uim_helper_send_message(helper_fd, cmd);
 
83
 
 
84
  if (strcmp(cmd, "focus_in\n") == 0)
 
85
        focused = 1;
 
86
 
 
87
  free(cmd);
 
88
 
 
89
  rest = strlen(p + 1);
 
90
 
 
91
  if (rest > 0)
 
92
        memmove(cmdbuf, p + 1, rest);
 
93
  else
 
94
        cmdbuf[0] = '\0';
 
95
 
 
96
  return 1;
 
97
}
 
98
 
 
99
 
 
100
static void
 
101
process_message(char *msg)
 
102
{
 
103
  char *eol;
 
104
 
 
105
  debug_printf(DEBUG_NOTE, "process message from uim-helper-server\n");
 
106
  
 
107
  if (msg) {
 
108
        if ((eol = strchr(msg, '\n')) != NULL) {
 
109
          *eol = '\0';
 
110
        } else {
 
111
          free(msg);
 
112
          return;
 
113
        }
 
114
 
 
115
        if (strcmp("focus_in", msg) == 0) {
 
116
 
 
117
          if (focused)
 
118
                printf("focus_in\n");
 
119
 
 
120
          focused = 0;
 
121
 
 
122
        } else if (strcmp("prop_activate", msg) == 0) { 
 
123
 
 
124
          char *prop = eol + 1;
 
125
 
 
126
          if ((eol = strchr(prop, '\n')) != NULL) {
 
127
                *eol = '\0';
 
128
                printf("prop_activate %s\n", prop);
 
129
          }
 
130
 
 
131
        } else if (strcmp("prop_list_get", msg) == 0) { 
 
132
 
 
133
          printf("prop_list_get\n");
 
134
 
 
135
        } else if (strncmp(msg, "im_change_", 10) == 0) {
 
136
 
 
137
          char *imname = eol + 1;
 
138
 
 
139
          if ((eol = strchr(imname, '\n')) != NULL) {
 
140
                *eol = '\0';
 
141
                printf("%s %s\n", msg, imname);
 
142
          }
 
143
          
 
144
        } else if (strcmp("im_list_get", msg) == 0) {
 
145
 
 
146
          printf("im_list_get\n");
 
147
 
 
148
        } else if (strcmp("commit_string", msg) == 0) {
 
149
 
 
150
          char *rest, *charset = "UTF-8";
 
151
 
 
152
          debug_printf(DEBUG_NOTE, "commit string\n");
 
153
 
 
154
          rest = eol + 1;
 
155
 
 
156
          if ((eol = strchr(rest, '\n')) != NULL) {
 
157
                *eol = '\0';
 
158
                if (strncmp(rest, "charset=", 8) == 0) {
 
159
                  charset = rest + 8;
 
160
                  if ((eol = strchr(charset, ' ')) != NULL) {
 
161
                        *eol = '\0';
 
162
                        rest = eol + 1;
 
163
                  }
 
164
                }
 
165
                rest = helper_message_encode(rest);
 
166
                printf("commit_string %s %s\n", charset, rest);
 
167
                free(rest);
 
168
          }
 
169
 
 
170
        } else if (strcmp("prop_update_custom", msg) == 0) {
 
171
          
 
172
          char *val, *custom = eol + 1;
 
173
 
 
174
          if ((eol = strchr(custom, '\n')) != NULL) {
 
175
                *eol = '\0';
 
176
                val = eol + 1;
 
177
 
 
178
                if ((eol = strchr(val, '\n')) != NULL) {
 
179
                  *eol = '\0';
 
180
                  printf("prop_update_custom %s %s\n", custom, val);
 
181
                }
 
182
          }
 
183
 
 
184
        } else if (strcmp("custom_reload_notify", msg) == 0) {
 
185
 
 
186
          printf("custom_reload_notify\n");
 
187
 
 
188
        } else {
 
189
 
 
190
          debug_printf(DEBUG_NOTE, "other message %s\n", msg);
 
191
 
 
192
        }
 
193
 
 
194
        fflush(NULL);
 
195
        free(msg);
 
196
  }
 
197
}
 
198
 
 
199
 
 
200
static void
 
201
read_command()
 
202
{
 
203
  ssize_t len;
 
204
  char rbuf[DEFAULT_MESSAGE_SIZE];
 
205
 
 
206
  debug_printf(DEBUG_NOTE, "read command\n");
 
207
 
 
208
  do {
 
209
        len = read(STDIN_FILENO, rbuf, sizeof(rbuf) - 1);
 
210
        rbuf[len] = '\0';
 
211
 
 
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");
 
216
        }
 
217
 
 
218
        strcat(cmdbuf, rbuf);
 
219
 
 
220
  } while (!command_exists_in_cmdbuf());
 
221
 
 
222
}
 
223
 
 
224
 
 
225
static void
 
226
wait_data_arrival(fd_set *rfds)
 
227
{
 
228
  int fdmax = STDIN_FILENO;
 
229
 
 
230
  if (helper_fd > 0) {
 
231
        FD_ZERO(rfds);
 
232
        FD_SET(helper_fd, rfds);
 
233
        if (helper_fd > fdmax) fdmax = helper_fd;
 
234
  }
 
235
 
 
236
  FD_SET(STDIN_FILENO, rfds);
 
237
 
 
238
  if (select(helper_fd + 1, rfds, NULL, NULL, NULL) < 0)
 
239
        debug_printf(DEBUG_ERROR, "select error\n");
 
240
 
 
241
}
 
242
 
 
243
 
 
244
 
 
245
void
 
246
cleanup(void)
 
247
{
 
248
  uim_quit();
 
249
}
 
250
 
 
251
 
 
252
int
 
253
main(int argc, char *argv[])
 
254
{
 
255
  int opt;
 
256
 
 
257
  while ((opt = getopt(argc, argv, "d")) != -1) {
 
258
        switch (opt) {
 
259
        case 'd':
 
260
          debug_level ++;
 
261
          break;
 
262
        }
 
263
  }
 
264
 
 
265
  if (debug_level == 0) fclose(stderr);
 
266
 
 
267
  if (uim_init() < 0) {
 
268
        debug_printf(DEBUG_ERROR, "uim_init failed\n");
 
269
        return -1;
 
270
  }
 
271
 
 
272
  atexit(cleanup);
 
273
 
 
274
  a_printf("OK\n");
 
275
 
 
276
  cmdbuf_len = DEFAULT_MESSAGE_SIZE;
 
277
  cmdbuf = (char *)malloc(cmdbuf_len);
 
278
  cmdbuf[0] = '\0';
 
279
 
 
280
  while (1) {
 
281
        char *msg;
 
282
        fd_set rfds;
 
283
 
 
284
        check_helper_connection();
 
285
 
 
286
        wait_data_arrival(&rfds);
 
287
 
 
288
        debug_printf(DEBUG_NOTE, "data arrive\n");
 
289
 
 
290
        if (FD_ISSET(STDIN_FILENO, &rfds)) {
 
291
          read_command();
 
292
        }
 
293
 
 
294
        if (FD_ISSET(helper_fd, &rfds)) {
 
295
          /* read message from helper */
 
296
          uim_helper_read_proc(helper_fd);
 
297
        }
 
298
 
 
299
        while (command_exists_in_cmdbuf())
 
300
          process_command();
 
301
 
 
302
        while ((msg = uim_helper_get_message())) {
 
303
          process_message(msg);
 
304
        }
 
305
        fflush(NULL);
 
306
  }
 
307
 
 
308
  uim_quit();
 
309
  return 0;
 
310
}
 
311