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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/******************************************************************
Copyright 1994, 1995 by Sun Microsystems, Inc.
Copyright 1993, 1994 by Hewlett-Packard Company
Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
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 Sun Microsystems, Inc.
and Hewlett-Packard not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
Sun Microsystems, Inc. and Hewlett-Packard make no representations about
the suitability of this software for any purpose. It is provided "as is"
without express or implied warranty.
SUN MICROSYSTEMS INC. AND HEWLETT-PACKARD COMPANY DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
SUN MICROSYSTEMS, INC. AND HEWLETT-PACKARD COMPANY BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Author: Hidetoshi Tajima(tajima@Eng.Sun.COM) Sun Microsystems, Inc.
This version tidied and debugged by Steve Underwood May 1999
******************************************************************/
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include "FrameMgr.h"
#include "IMdkit.h"
#include "Xi18n.h"
#include "Xi18nX.h"
#include "XimFunc.h"
extern Xi18nClient *_Xi18nFindClient(Xi18n, CARD16);
extern Xi18nClient *_Xi18nNewClient(Xi18n);
extern void _Xi18nDeleteClient(Xi18n, CARD16);
static Bool WaitXConnectMessage(Display*, Window,
XEvent*, XPointer);
static Bool WaitXIMProtocol(Display*, Window, XEvent*, XPointer);
static XClient *NewXClient (Xi18n i18n_core, Window new_client)
{
Display *dpy = i18n_core->address.dpy;
Xi18nClient *client = _Xi18nNewClient (i18n_core);
XClient *x_client;
x_client = (XClient *) malloc (sizeof (XClient));
x_client->client_win = new_client;
x_client->accept_win = XCreateSimpleWindow (dpy,
DefaultRootWindow(dpy),
0,
0,
1,
1,
1,
0,
0);
client->trans_rec = x_client;
return ((XClient *) x_client);
}
static unsigned char *ReadXIMMessage (XIMS ims,
XClientMessageEvent *ev,
int *connect_id)
{
Xi18n i18n_core = ims->protocol;
Xi18nClient *client = i18n_core->address.clients;
XClient *x_client = NULL;
FrameMgr fm;
extern XimFrameRec packet_header_fr[];
unsigned char *p = NULL;
unsigned char *p1;
while (client != NULL) {
x_client = (XClient *) client->trans_rec;
if (x_client->accept_win == ev->window) {
*connect_id = client->connect_id;
break;
}
client = client->next;
}
if (ev->format == 8) {
/* ClientMessage only */
XimProtoHdr *hdr = (XimProtoHdr *) ev->data.b;
unsigned char *rec = (unsigned char *) (hdr + 1);
register int total_size;
CARD8 major_opcode;
CARD8 minor_opcode;
CARD16 length;
extern int _Xi18nNeedSwap (Xi18n, CARD16);
if (client->byte_order == '?')
{
if (hdr->major_opcode != XIM_CONNECT)
return (unsigned char *) NULL; /* can do nothing */
client->byte_order = (CARD8) rec[0];
}
fm = FrameMgrInit (packet_header_fr,
(char *) hdr,
_Xi18nNeedSwap (i18n_core, *connect_id));
total_size = FrameMgrGetTotalSize (fm);
/* get data */
FrameMgrGetToken (fm, major_opcode);
FrameMgrGetToken (fm, minor_opcode);
FrameMgrGetToken (fm, length);
FrameMgrFree (fm);
if ((p = (unsigned char *) malloc (total_size + length * 4)) == NULL)
return (unsigned char *) NULL;
p1 = p;
memmove (p1, &major_opcode, sizeof (CARD8));
p1 += sizeof (CARD8);
memmove (p1, &minor_opcode, sizeof (CARD8));
p1 += sizeof (CARD8);
memmove (p1, &length, sizeof (CARD16));
p1 += sizeof (CARD16);
memmove (p1, rec, length * 4);
}
else if (ev->format == 32) {
/* ClientMessage and WindowProperty */
unsigned long length = (unsigned long) ev->data.l[0];
Atom atom = (Atom) ev->data.l[1];
int return_code;
Atom actual_type_ret;
int actual_format_ret;
unsigned long bytes_after_ret;
unsigned char *prop;
unsigned long nitems;
return_code = XGetWindowProperty (i18n_core->address.dpy,
x_client->accept_win,
atom,
0L,
length,
True,
AnyPropertyType,
&actual_type_ret,
&actual_format_ret,
&nitems,
&bytes_after_ret,
&prop);
if (return_code != Success || actual_format_ret == 0 || nitems == 0) {
if (return_code == Success)
XFree (prop);
return (unsigned char *) NULL;
}
if (length != nitems)
length = nitems;
if (actual_format_ret == 16)
length *= 2;
else if (actual_format_ret == 32)
length *= 4;
/* if hit, it might be an error */
if ((p = (unsigned char *) malloc (length)) == NULL)
return (unsigned char *) NULL;
memmove (p, prop, length);
XFree (prop);
}
return (unsigned char *) p;
}
static void ReadXConnectMessage (XIMS ims, XClientMessageEvent *ev)
{
Xi18n i18n_core = ims->protocol;
XSpecRec *spec = (XSpecRec *) i18n_core->address.connect_addr;
XEvent event;
Display *dpy = i18n_core->address.dpy;
Window new_client = ev->data.l[0];
CARD32 major_version = ev->data.l[1];
CARD32 minor_version = ev->data.l[2];
XClient *x_client = NewXClient (i18n_core, new_client);
if (ev->window != i18n_core->address.im_window)
return; /* incorrect connection request */
/*endif*/
if (major_version != 0 || minor_version != 0)
{
major_version =
minor_version = 0;
/* Only supporting only-CM & Property-with-CM method */
}
/*endif*/
_XRegisterFilterByType (dpy,
x_client->accept_win,
ClientMessage,
ClientMessage,
WaitXIMProtocol,
(XPointer)ims);
event.xclient.type = ClientMessage;
event.xclient.display = dpy;
event.xclient.window = new_client;
event.xclient.message_type = spec->connect_request;
event.xclient.format = 32;
event.xclient.data.l[0] = x_client->accept_win;
event.xclient.data.l[1] = major_version;
event.xclient.data.l[2] = minor_version;
event.xclient.data.l[3] = XCM_DATA_LIMIT;
XSendEvent (dpy,
new_client,
False,
NoEventMask,
&event);
XFlush (dpy);
}
static Bool Xi18nXBegin (XIMS ims)
{
Xi18n i18n_core = ims->protocol;
Display *dpy = i18n_core->address.dpy;
XSpecRec *spec = (XSpecRec *) i18n_core->address.connect_addr;
spec->xim_request = XInternAtom (i18n_core->address.dpy,
_XIM_PROTOCOL,
False);
spec->connect_request = XInternAtom (i18n_core->address.dpy,
_XIM_XCONNECT,
False);
_XRegisterFilterByType (dpy,
i18n_core->address.im_window,
ClientMessage,
ClientMessage,
WaitXConnectMessage,
(XPointer)ims);
return True;
}
static Bool Xi18nXEnd(XIMS ims)
{
Xi18n i18n_core = ims->protocol;
Display *dpy = i18n_core->address.dpy;
_XUnregisterFilter (dpy,
i18n_core->address.im_window,
WaitXConnectMessage,
(XPointer)ims);
return True;
}
static char *MakeNewAtom (CARD16 connect_id, char *atomName)
{
static int sequence = 0;
sprintf (atomName,
"_server%d_%d",
connect_id,
((sequence > 20) ? (sequence = 0) : sequence++));
return atomName;
}
static Bool Xi18nXSend (XIMS ims,
CARD16 connect_id,
unsigned char *reply,
long length)
{
Xi18n i18n_core = ims->protocol;
Xi18nClient *client = _Xi18nFindClient (i18n_core, connect_id);
XSpecRec *spec = (XSpecRec *) i18n_core->address.connect_addr;
XClient *x_client = (XClient *) client->trans_rec;
XEvent event;
event.type = ClientMessage;
event.xclient.window = x_client->client_win;
event.xclient.message_type = spec->xim_request;
if (length > XCM_DATA_LIMIT)
{
Atom atom;
char atomName[16];
Atom actual_type_ret;
int actual_format_ret;
int return_code;
unsigned long nitems_ret;
unsigned long bytes_after_ret;
unsigned char *win_data;
event.xclient.format = 32;
atom = XInternAtom (i18n_core->address.dpy,
MakeNewAtom (connect_id, atomName),
False);
return_code = XGetWindowProperty (i18n_core->address.dpy,
x_client->client_win,
atom,
0L,
10000L,
False,
XA_STRING,
&actual_type_ret,
&actual_format_ret,
&nitems_ret,
&bytes_after_ret,
&win_data);
if (return_code != Success)
return False;
/*endif*/
if (win_data)
XFree ((char *) win_data);
/*endif*/
XChangeProperty (i18n_core->address.dpy,
x_client->client_win,
atom,
XA_STRING,
8,
PropModeAppend,
(unsigned char *) reply,
length);
event.xclient.data.l[0] = length;
event.xclient.data.l[1] = atom;
}
else
{
unsigned char buffer[XCM_DATA_LIMIT];
int i;
event.xclient.format = 8;
/* Clear unused field with NULL */
memmove(buffer, reply, length);
for (i = length; i < XCM_DATA_LIMIT; i++)
buffer[i] = (char) 0;
/*endfor*/
length = XCM_DATA_LIMIT;
memmove (event.xclient.data.b, buffer, length);
}
XSendEvent (i18n_core->address.dpy,
x_client->client_win,
False,
NoEventMask,
&event);
XFlush (i18n_core->address.dpy);
return True;
}
static Bool CheckCMEvent (Display *display, XEvent *event, XPointer xi18n_core)
{
Xi18n i18n_core = (Xi18n) ((void *) xi18n_core);
XSpecRec *spec = (XSpecRec *) i18n_core->address.connect_addr;
if ((event->type == ClientMessage)
&&
(event->xclient.message_type == spec->xim_request))
{
return True;
}
/*endif*/
return False;
}
static Bool Xi18nXWait (XIMS ims,
CARD16 connect_id,
CARD8 major_opcode,
CARD8 minor_opcode)
{
Xi18n i18n_core = ims->protocol;
XEvent event;
Xi18nClient *client = _Xi18nFindClient (i18n_core, connect_id);
XClient *x_client = (XClient *) client->trans_rec;
for (;;)
{
unsigned char *packet;
XimProtoHdr *hdr;
int connect_id_ret;
XIfEvent (i18n_core->address.dpy,
&event,
CheckCMEvent,
(XPointer) i18n_core);
if (event.xclient.window == x_client->accept_win)
{
if ((packet = ReadXIMMessage (ims,
(XClientMessageEvent *) & event,
&connect_id_ret))
== (unsigned char*) NULL)
{
return False;
}
/*endif*/
hdr = (XimProtoHdr *)packet;
if ((hdr->major_opcode == major_opcode)
&&
(hdr->minor_opcode == minor_opcode))
{
return True;
}
else if (hdr->major_opcode == XIM_ERROR)
{
return False;
}
/*endif*/
}
/*endif*/
}
/*endfor*/
}
static Bool Xi18nXDisconnect (XIMS ims, CARD16 connect_id)
{
Xi18n i18n_core = ims->protocol;
Display *dpy = i18n_core->address.dpy;
Xi18nClient *client = _Xi18nFindClient (i18n_core, connect_id);
XClient *x_client = (XClient *) client->trans_rec;
XDestroyWindow (dpy, x_client->accept_win);
_XUnregisterFilter (dpy,
x_client->accept_win,
WaitXIMProtocol,
(XPointer)ims);
XFree (x_client);
_Xi18nDeleteClient (i18n_core, connect_id);
return True;
}
Bool _Xi18nCheckXAddress (Xi18n i18n_core,
TransportSW *transSW,
char *address)
{
XSpecRec *spec;
if (!(spec = (XSpecRec *) malloc (sizeof (XSpecRec))))
return False;
/*endif*/
i18n_core->address.connect_addr = (XSpecRec *) spec;
i18n_core->methods.begin = Xi18nXBegin;
i18n_core->methods.end = Xi18nXEnd;
i18n_core->methods.send = Xi18nXSend;
i18n_core->methods.wait = Xi18nXWait;
i18n_core->methods.disconnect = Xi18nXDisconnect;
return True;
}
static Bool WaitXConnectMessage (Display *dpy,
Window win,
XEvent *ev,
XPointer client_data)
{
XIMS ims = (XIMS)client_data;
Xi18n i18n_core = ims->protocol;
XSpecRec *spec = (XSpecRec *) i18n_core->address.connect_addr;
if (((XClientMessageEvent *) ev)->message_type
== spec->connect_request)
{
ReadXConnectMessage (ims, (XClientMessageEvent *) ev);
return True;
}
/*endif*/
return False;
}
static Bool WaitXIMProtocol (Display *dpy,
Window win,
XEvent *ev,
XPointer client_data)
{
extern void _Xi18nMessageHandler (XIMS, CARD16, unsigned char *, Bool *);
XIMS ims = (XIMS) client_data;
Xi18n i18n_core = ims->protocol;
XSpecRec *spec = (XSpecRec *) i18n_core->address.connect_addr;
Bool delete = True;
unsigned char *packet;
int connect_id;
if (((XClientMessageEvent *) ev)->message_type
== spec->xim_request)
{
if ((packet = ReadXIMMessage (ims,
(XClientMessageEvent *) ev,
&connect_id))
== (unsigned char *) NULL)
{
return False;
}
/*endif*/
_Xi18nMessageHandler (ims, connect_id, packet, &delete);
if (delete == True)
XFree (packet);
/*endif*/
return True;
}
/*endif*/
return False;
}
|