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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
#ifndef lint
static char *rcsid = "$Id: imic.c,v 1.10 1999/01/13 08:42:04 ishisone Exp $";
#endif
/*
* Copyright (c) 1994 Software Research Associates, Inc.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Software Research Associates not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. Software Research
* Associates makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied
* warranty.
*
* Author: Makoto Ishisone, Software Research Associates, Inc., Japan
*/
#include "im.h"
#define IMHASHVAL(imp) ((imp)->id % IM_HASH_SIZE)
#define ICHASHVAL(icp) ((icp)->id % IC_HASH_SIZE)
static IMIM *id2IM _Pt_((IMConnection *conn, unsigned int id));
static IMIC *id2IC _Pt_((IMIM *imp, unsigned int id));
static unsigned int newIMID _Pt_((IMConnection *conn));
static unsigned int newICID _Pt_((IMIM *imp));
static void registerIM _Pt_((IMIM *imp));
static void registerIC _Pt_((IMIC *icp));
static int unregisterIM _Pt_((IMIM *imp));
static int unregisterIC _Pt_((IMIC *icp));
static int removeIM _Pt_((IMIM *imp));
static int removeIC _Pt_((IMIC *icp));
/*- id2IM: input-method ID to IM converter -*/
static IMIM *
id2IM(conn, id)
IMConnection *conn;
unsigned int id;
{
IMIM **imHash = IMIMHash(conn->proto_widget);
IMIM *imp;
imp = imHash[id % IM_HASH_SIZE];
while (imp != NULL) {
if (imp->id == id) return imp;
imp = imp->hash_next;
}
return NULL;
}
/*- id2IC: input-context ID to IC converter -*/
static IMIC *
id2IC(imp, id)
IMIM *imp;
unsigned int id;
{
IMIC **icHash = IMICHash(imp->connection->proto_widget);
IMIC *icp;
icp = icHash[id % IC_HASH_SIZE];
while (icp != NULL) {
if (icp->id == id) return icp;
icp = icp->hash_next;
}
return NULL;
}
/*- newIMID: return unused input-method ID -*/
static unsigned int
newIMID(conn)
IMConnection *conn;
{
Widget w = conn->proto_widget;
unsigned int id, id_start;
id = id_start = IMNextIMID(w);
do {
if (id2IM(conn, id) == NULL) return id; /* unused ID */
} while ((id = IMNextIMID(w)) != id_start);
return 0;
}
/*- newICID: return unused input-context ID -*/
static unsigned int
newICID(imp)
IMIM *imp;
{
Widget w = imp->connection->proto_widget;
unsigned int id, id_start;
id = id_start = IMNextICID(w);
do {
if (id2IC(imp, id) == NULL) return id; /* unused ID */
} while ((id = IMNextICID(w)) != id_start);
return 0;
}
/*- registerIM: register IM to hash table -*/
static void
registerIM(imp)
IMIM *imp;
{
IMIM **imHash = IMIMHash(imp->connection->proto_widget);
imp->hash_next = imHash[IMHASHVAL(imp)];
imHash[IMHASHVAL(imp)] = imp;
}
/*- registerIC: register IC to hash table -*/
static void
registerIC(icp)
IMIC *icp;
{
IMIC **icHash = IMICHash(icp->im->connection->proto_widget);
icp->hash_next = icHash[ICHASHVAL(icp)];
icHash[ICHASHVAL(icp)] = icp;
}
/*- unregisterIM: remove IM from hash table -*/
static int
unregisterIM(imp)
IMIM *imp;
{
IMIM **imHash = IMIMHash(imp->connection->proto_widget);
IMIM *p, *q;
p = imHash[IMHASHVAL(imp)];
q = NULL;
while (p != NULL && p != imp) {
q = p;
p = p->hash_next;
}
if (p == NULL) return 0;
if (q != NULL) {
q->hash_next = p->hash_next;
} else {
imHash[IMHASHVAL(imp)] = p->hash_next;
}
return 1;
}
/*- unregisterIC: remove IC from hash table -*/
static int
unregisterIC(icp)
IMIC *icp;
{
IMIC **icHash = IMICHash(icp->im->connection->proto_widget);
IMIC *p, *q;
p = icHash[ICHASHVAL(icp)];
q = NULL;
while (p != NULL && p != icp) {
q = p;
p = p->hash_next;
}
if (p == NULL) return 0;
if (q != NULL) {
q->hash_next = p->hash_next;
} else {
icHash[ICHASHVAL(icp)] = p->hash_next;
}
return 1;
}
/* removeIM: remove IM from IM list which connection holds */
static int
removeIM(imp)
IMIM *imp;
{
IMConnection *conn = imp->connection;
IMIM *p, *q;
p = conn->im_list;
q = NULL;
while (p != NULL) {
if (p == imp) break;
q = p;
p = p->next;
}
if (p == NULL) return 0;
if (q == NULL) {
conn->im_list = p->next;
} else {
q->next = p->next;
}
return 1;
}
/* removeIC: remove IC from IC list which IM holds */
static int
removeIC(icp)
IMIC *icp;
{
IMIM *imp = icp->im;
IMIC *p, *q;
p = imp->ic_list;
q = NULL;
while (p != NULL) {
if (p == icp) break;
q = p;
p = p->next;
}
if (p == NULL) return 0;
if (q == NULL) {
imp->ic_list = p->next;
} else {
q->next = p->next;
}
return 1;
}
/*
* Public functions
*/
IMIM *
IMGetIM(conn, arglen)
IMConnection *conn;
int arglen;
{
unsigned int id;
IMIM *imp;
/* Check argument length */
if (arglen < 2) {
IMSendError(conn, IMBadSomething, 0, 0, "input-method ID expected");
return NULL;
}
id = IMGetC16(conn, 0);
imp = id2IM(conn, id);
if (imp != NULL && imp->connection == conn) return imp;
IMSendError(conn, IMBadSomething, 0, 0, "invalid input-method ID");
return NULL;
}
IMIC *
IMGetIC(conn, arglen)
IMConnection *conn;
int arglen;
{
unsigned int imid, icid;
IMIM *imp;
IMIC *icp;
/* Check argument length */
if (arglen < 4) {
IMSendError(conn, IMBadSomething, 0, 0, "input-method ID expected");
return NULL;
} else if (arglen < 4) {
IMSendError(conn, IMBadSomething, 0, 0, "input-context ID expected");
return NULL;
}
imid = IMGetC16(conn, 0);
icid = IMGetC16(conn, 2);
if ((imp = id2IM(conn, imid)) == NULL || imp->connection != conn) {
IMSendError(conn, IMBadSomething, 0, 0, "invalid input-method ID");
return NULL;
}
if ((icp = id2IC(imp, icid)) == NULL || icp->im != imp) {
IMSendError(conn, IMBadSomething, 0, 0, "invalid input-context ID");
return NULL;
}
return icp;
}
IMIM *
IMCreateIM(conn, converter)
IMConnection *conn;
IMConverter *converter;
{
IMIM *imp;
imp = XtNew(IMIM);
imp->id = newIMID(conn);
imp->connection = conn;
imp->converter = converter;
imp->mask = 0;
imp->ic_list = NULL;
registerIM(imp);
imp->next = conn->im_list;
conn->im_list = imp;
return imp;
}
IMIC *
IMCreateIC(imp)
IMIM *imp;
{
IMIC *icp;
icp = XtNew(IMIC);
icp->id = newICID(imp);
/*
* Initialize data
*/
icp->im = imp;
icp->conversion = NULL;
icp->state = 0;
icp->pending_events = NULL;
icp->style = IMSTYLE_SEPARATE;
icp->common_attr.set_mask = icp->common_attr.change_mask = 0;
icp->preedit_attr.set_mask = icp->preedit_attr.change_mask = 0;
icp->status_attr.set_mask = icp->status_attr.change_mask = 0;
icp->fonts = NULL;
icp->num_fonts = 0;
icp->status_fonts = NULL;
icp->num_status_fonts = 0;
icp->resetting = False;
icp->in_preedit = False;
icp->in_status = False;
registerIC(icp);
icp->next = imp->ic_list;
imp->ic_list = icp;
return icp;
}
void
IMDestroyIM(imp)
IMIM *imp;
{
IMIC *icp = imp->ic_list;
IMIC *icp_next;
/*
* Destroy all the ICs belonging to this IM.
*/
while (icp != NULL) {
icp_next = icp->next;
IMDestroyIC(icp);
icp = icp_next;
}
(void)unregisterIM(imp);
(void)removeIM(imp);
XtFree((char *)imp);
}
void
IMDestroyIC(icp)
IMIC *icp;
{
IMPendingEvent *pending;
/*
* Stop conversion.
*/
if (icp->state & IC_CONVERTING) {
IMStopConversion(icp);
}
/*
* Free pending event queue.
*/
pending = icp->pending_events;
while (pending != NULL) {
IMPendingEvent *next = pending->next;
XtFree((char *)pending);
pending = next;
}
/*
* Free IC attributes.
*/
IMFreeICAttributes(icp);
(void)unregisterIC(icp);
(void)removeIC(icp);
XtFree((char *)icp);
}
|