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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
/*
* Motif Tools Library, Version 3.1
* $Id$
*
* Written by David Flanagan.
* Copyright (c) 1992-2001 by David Flanagan.
* All Rights Reserved. See the file COPYRIGHT for details.
* This is open source software. See the file LICENSE for details.
* There is no warranty for this software. See NO_WARRANTY for details.
*
* $Log$
* Revision 1.1.1.1 2001/07/18 11:06:01 root
* Initial checkin.
*
* Revision 1.1 2001/06/12 15:00:21 andre
* AA-2001-06-12-0: replaced Xmt212 by Xmt310
* (http://sourceforge.net/projects/motiftools) with
* our xmt212 patches applied
*
*
*/
#include <Xmt/Xmt.h>
#include <Xmt/ProceduresP.h>
#include <Xmt/Symbols.h>
#include <Xmt/ConvertersP.h>
#include <Xmt/Util.h>
#include <Xmt/Lexer.h>
#include <Xmt/QuarksP.h>
typedef struct {
String procedure_name;
XmtProcedureInfo *procedure;
String argument_strings[XmtMAX_PROCEDURE_ARGS];
XtPointer arguments[XmtMAX_PROCEDURE_ARGS];
XmtSymbol symbols[XmtMAX_PROCEDURE_ARGS];
Boolean error;
} CallInfo;
typedef struct {
CallInfo *calls;
int num_calls;
} Callback;
typedef void (*XmtProcedure0)(
#if NeedFunctionPrototypes
void
#endif
);
typedef void (*XmtProcedure1)(
#if NeedFunctionPrototypes
XtPointer
#endif
);
typedef void (*XmtProcedure2)(
#if NeedFunctionPrototypes
XtPointer, XtPointer
#endif
);
typedef void (*XmtProcedure3)(
#if NeedFunctionPrototypes
XtPointer, XtPointer, XtPointer
#endif
);
typedef void (*XmtProcedure4)(
#if NeedFunctionPrototypes
XtPointer, XtPointer,
XtPointer, XtPointer
#endif
);
typedef void (*XmtProcedure5)(
#if NeedFunctionPrototypes
XtPointer, XtPointer, XtPointer,
XtPointer, XtPointer
#endif
);
typedef void (*XmtProcedure6)(
#if NeedFunctionPrototypes
XtPointer, XtPointer, XtPointer,
XtPointer, XtPointer, XtPointer
#endif
);
typedef void (*XmtProcedure7)(
#if NeedFunctionPrototypes
XtPointer, XtPointer, XtPointer,
XtPointer, XtPointer, XtPointer,
XtPointer
#endif
);
typedef void (*XmtProcedure8)(
#if NeedFunctionPrototypes
XtPointer, XtPointer, XtPointer,
XtPointer, XtPointer, XtPointer,
XtPointer, XtPointer
#endif
);
#if NeedFunctionPrototypes
static void ExpectedWarning(String s)
#else
static void ExpectedWarning(s)
String s;
#endif
{
XmtWarningMsg("XmtConvertStringToCallback", "expected",
"%s expected.",
s);
}
#if NeedFunctionPrototypes
static Boolean ParseCall(XmtLexer l, CallInfo *ci)
#else
static Boolean ParseCall(l, ci)
XmtLexer l;
CallInfo *ci;
#endif
{
XmtLexerToken tok;
Cardinal num_args;
int i;
ci->procedure_name = NULL;
ci->procedure = NULL;
for(i=0; i < XmtMAX_PROCEDURE_ARGS; i++) {
ci->argument_strings[i] = NULL;
ci->arguments[i] = NULL;
ci->symbols[i] = NULL;
}
ci->error = False;
tok = XmtLexerGetToken(l);
if (tok != XmtLexerIdent) {
ExpectedWarning("procedure name");
goto error;
}
ci->procedure_name = XmtLexerStrValue(l);
XmtLexerConsumeToken(l);
if (XmtLexerGetToken(l) != XmtLexerLParen) {
ExpectedWarning("'('");
goto error;
}
XmtLexerGetArgList(l,ci->argument_strings,XmtMAX_PROCEDURE_ARGS,&num_args);
return True;
error:
/* free any strings allocated so far. XtFree is okay with NULL */
XtFree(ci->procedure_name);
ci->procedure_name = NULL;
for(i=0; i < XmtMAX_PROCEDURE_ARGS; i++) {
XtFree(ci->argument_strings[i]);
ci->argument_strings[i] = NULL;
}
return False;
}
#if NeedFunctionPrototypes
static Callback *StringToCallback(String str)
#else
static Callback *StringToCallback(str)
String str;
#endif
{
static XmtLexer l = NULL;
XmtLexerToken tok;
int num, max;
CallInfo *calls;
Callback *cb;
Boolean stat;
if (l == NULL)
l = XmtLexerCreate((String *)NULL, 0);
XmtLexerInit(l, str);
num = max = 0;
calls = NULL;
while(XmtLexerGetToken(l) != XmtLexerEndOfString) {
if (num == max) {
if (max == 0) max = 4; else max *= 2;
calls = (CallInfo*)XtRealloc((char*)calls, sizeof(CallInfo) * max);
}
/* parse a single procedure call */
stat = ParseCall(l, &calls[num]);
if (stat) num++;
else { /* on error read 'till next semicolon */
while(1) {
tok = XmtLexerGetToken(l);
if ((tok == XmtLexerSemicolon) ||
(tok == XmtLexerEndOfString)) break;
XmtLexerConsumeToken(l);
}
}
/* and discard any semicolons */
while (XmtLexerGetToken(l) == XmtLexerSemicolon)
XmtLexerConsumeToken(l);
}
if (num == 0) { /* if empty string or errors */
XtFree((char *)calls);
cb = NULL;
}
else {
calls = (CallInfo *)XtRealloc((char *)calls, sizeof(CallInfo)*num);
cb = XtNew(Callback);
cb->calls = calls;
cb->num_calls = num;
}
return cb;
}
#if NeedFunctionPrototypes
static void DestroyCallback(Callback *cb)
#else
static void DestroyCallback(cb)
Callback *cb;
#endif
{
int i,j;
CallInfo *info;
for(i=0; i < cb->num_calls; i++) {
info = &cb->calls[i];
XtFree(info->procedure_name);
for(j=0; j < XmtMAX_PROCEDURE_ARGS; j++)
XtFree(info->argument_strings[j]);
}
XtFree((char *) cb->calls);
XtFree((char *)cb);
}
#if NeedFunctionPrototypes
static void ConvertArg(Widget w, CallInfo *call, int i, int j)
#else
static void ConvertArg(w, call, i, j)
Widget w;
CallInfo *call;
int i;
int j;
#endif
{
XrmValue from, to;
Boolean stat;
XmtProcedureInfo *proc = call->procedure;
/*
* if the argument is a symbol name, bind it,
* check the type, and remember the symbol
*/
if (call->argument_strings[j][0] == '$') {
call->symbols[i] = XmtLookupSymbol(&call->argument_strings[j][1]);
if (!call->symbols[i]) {
XmtWarningMsg("XmtCallCallbacks", "symbol",
"argument %d of procedure %s.\n\tSymbol '%s' is undefined.",
j, call->procedure_name,
call->argument_strings[j]);
}
else {
/*
* check symbol type.
* A buffer symbol will match a string argument.
*/
XrmQuark symtype = XmtSymbolTypeQuark(call->symbols[i]);
XrmQuark argtype = (XrmQuark) proc->argument_types[i];
if ((symtype != argtype) &&
!((symtype == XmtQBuffer) && (argtype == XmtQString))) {
XmtWarningMsg("XmtCallCallbacks", "symbolType",
"type mismatch:\n\targument %d of procedure %s.\n\tSymbol '%s' is not of type %s",
j, call->procedure_name,
XmtSymbolName(call->symbols[i]),
XrmQuarkToString((XrmQuark)
proc->argument_types[i]));
call->symbols[i] = NULL;
}
}
if (!call->symbols[i]) call->error = True;
}
else {
/*
* if it is not a symbol, then if it is a string constant, we're done.
* otherwise, we've got to convert our string to the appropriate type.
*/
if ((XrmQuark)proc->argument_types[i] == XmtQString) {
call->arguments[i] = (XtPointer)call->argument_strings[j];
}
else {
from.addr = (XPointer) call->argument_strings[j];
from.size = strlen(from.addr)+1;
to.addr = (XPointer) NULL;
/* XtConvertAndStore() will ensure that converted
* values are freed when the widget w is destroyed.
*/
stat = XtConvertAndStore(w, XtRString, &from,
XrmQuarkToString((XrmQuark)proc->argument_types[i]),
&to);
if (!stat) {
XmtWarningMsg("XmtCallCallbacks", "typeMismatch",
"type mismatch: \n\targument %d of procedure %s.\n\tCan't convert \"%s\" to type %s",
j, call->procedure_name,
call->argument_strings[i],
XrmQuarkToString((XrmQuark)
proc->argument_types[i]));
call->error = True;
}
else {
/* XXX
* This code isn't as portable as it should be, but it
* does seem to work on 32-bit architectures, and 64-bit Alpha.
* I don't know if there are any architectures that have
* calling conventions unlike those assumed here.
*
* All functions registered with the callback converter
* must expect arguments of the same size as XtPointer.
* This means that chars and shorts must be "widened"
* to longs. Also, arguments of type double are not
* supported
*/
switch(to.size) {
case 1:
call->arguments[i] = (XtPointer)(long)*(char *)to.addr;
break;
case 2:
/* XXX signed vs. unsigned??? */
call->arguments[i] = (XtPointer)(long)*(short *)to.addr;
break;
case 4:
call->arguments[i] = (XtPointer)(long)*(int *)to.addr;
break;
default:
call->arguments[i] = (XtPointer)*(long *)to.addr;
}
}
}
}
}
#if NeedFunctionPrototypes
static void XmtCallCallback(Widget w, XtPointer tag, XtPointer data)
#else
static void XmtCallCallback(w, tag, data)
Widget w;
XtPointer tag;
XtPointer data;
#endif
{
Callback *cb = (Callback *) tag;
CallInfo *call;
XmtProcedureInfo *proc;
Boolean firsttime;
int n;
int i, j;
for(n=0; n < cb->num_calls; n++) {
call = &cb->calls[n];
if (call->error) {
XmtWarningMsg("XmtCallCallback", "badCall",
"not calling %s() because of previous errors.",
call->procedure_name);
continue;
}
/* if this is the 1st time, figure out the procedure info */
/* also set a flag for use below */
if (call->procedure == NULL) {
firsttime = True;
call->procedure = XmtLookupProcedure(call->procedure_name);
if (call->procedure == NULL) {
XmtWarningMsg("XmtCallCallback", "unknown",
"unknown procedure %s",
call->procedure_name);
continue;
}
}
else firsttime = False;
proc = call->procedure;
/* if this is the first time, check the # of arguments */
if (firsttime) {
for(i=0;
(i < XmtMAX_PROCEDURE_ARGS) && (call->argument_strings[i]);
i++);
if (i != proc->expected_args) {
XmtWarningMsg("XmtCallCallback", "wrongNum",
"procedure %s expects %d arguments.",
call->procedure_name, proc->expected_args);
call->error = True;
continue;
}
}
/* build up the array of arguments */
i = j = 0;
while((i < XmtMAX_PROCEDURE_ARGS) &&
(proc->argument_types[i] != (String)NULLQUARK)) {
if ((XrmQuark)proc->argument_types[i] == _XmtQCallbackWidget)
call->arguments[i] = (XtPointer) w;
else if ((XrmQuark)proc->argument_types[i] == _XmtQCallbackData)
call->arguments[i] = data;
else if((XrmQuark)proc->argument_types[i]==_XmtQCallbackAppContext)
call->arguments[i] =(XtPointer)XtWidgetToApplicationContext(w);
else if ((XrmQuark)proc->argument_types[i] == _XmtQCallbackWindow)
call->arguments[i] = (XtPointer) XtWindow(w);
else if ((XrmQuark)proc->argument_types[i] == _XmtQCallbackDisplay)
call->arguments[i] = (XtPointer) XtDisplay(w);
else if ((XrmQuark)proc->argument_types[i] == _XmtQCallbackUnused)
call->arguments[i] = NULL;
else {
/*
* if this is the first time this CallInfo has been called,
* we've got to convert the argument string to the expected
* type, or to a symbol of the expected type. If the argument
* is a constant, then we can use it for all subsequent calls.
* if the argument is a symbol, we save the bound symbol,
* and look up its value for each subsequent call.
* Note we convert argument_string[j] to argument[i].
*/
if (firsttime) {
ConvertArg(w, call, i, j);
}
/*
* if the arg is a symbol, get its value.
* Otherwise, its a constant, already converted, so do nothing.
*/
if (call->symbols[i]) {
XmtSymbolGetValue(call->symbols[i],
(XtArgVal *)&call->arguments[i]);
}
j++;
}
if (call->error) break;
i++;
}
if (call->error) continue;
/*
* Call the function.
* If you change XmtMAX_PROCEDURE_ARGS, change this code too.
* Note that all functions declared this way must expect
* all their arguments to be the same size as XtPointer.
* On most (all?) architectures it is safe to pass extra arguments
* to functions, so we could just do case 8: here in all cases.
* But this is a little cleaner when using a debugger, for example.
*/
switch(i) { /* number of arguments*/
case 0:
(*((XmtProcedure0)proc->function))();
break;
case 1:
(*((XmtProcedure1)proc->function))(call->arguments[0]);
break;
case 2:
(*((XmtProcedure2)proc->function))
(call->arguments[0], call->arguments[1]);
break;
case 3:
(*((XmtProcedure3)proc->function))
(call->arguments[0], call->arguments[1],
call->arguments[2]);
break;
case 4:
(*((XmtProcedure4)proc->function))
(call->arguments[0], call->arguments[1],
call->arguments[2], call->arguments[3]);
break;
case 5:
(*((XmtProcedure5)proc->function))
(call->arguments[0], call->arguments[1],
call->arguments[2], call->arguments[3],
call->arguments[4]);
break;
case 6:
(*((XmtProcedure6)proc->function))
(call->arguments[0], call->arguments[1],
call->arguments[2], call->arguments[3],
call->arguments[4], call->arguments[5]);
break;
case 7:
(*((XmtProcedure7)proc->function))
(call->arguments[0], call->arguments[1],
call->arguments[2], call->arguments[3],
call->arguments[4], call->arguments[5],
call->arguments[6]);
break;
case 8:
(*((XmtProcedure8)proc->function))
(call->arguments[0], call->arguments[1],
call->arguments[2], call->arguments[3],
call->arguments[4], call->arguments[5],
call->arguments[6], call->arguments[7]);
break;
}
}
}
/* ARGSUSED */
#if NeedFunctionPrototypes
Boolean XmtConvertStringToCallback(Display *dpy,
XrmValue *args, Cardinal *num_args,
XrmValue *from, XrmValue *to,
XtPointer *converter_data)
#else
Boolean XmtConvertStringToCallback(dpy, args, num_args, from, to,
converter_data)
Display *dpy;
XrmValue *args;
Cardinal *num_args;
XrmValue *from;
XrmValue *to;
XtPointer *converter_data;
#endif
{
Callback *callback;
XtCallbackList list;
callback = StringToCallback((char *)from->addr);
if (callback == NULL) { /* error, couldn't convert */
/* XXX print the string here, and mark where the error occured. */
return False;
}
else {
list = (XtCallbackList)XtCalloc(sizeof(XtCallbackRec), 2);
list[0].callback = (XtCallbackProc) XmtCallCallback;
list[0].closure = (XtPointer) callback;
done(XtCallbackList, list);
}
}
/* ARGSUSED */
#if NeedFunctionPrototypes
static void XmtDestroyCallback(XtAppContext app, XrmValue *to,
XtPointer converter_data,
XrmValue *args, Cardinal *num_args)
#else
static void XmtDestroyCallback(app, to, converter_data, args, num_args)
XtAppContext app;
XrmValue *to;
XtPointer converter_data;
XrmValue *args;
Cardinal *num_args;
#endif
{
XtCallbackList list = *(XtCallbackList *)to->addr;
DestroyCallback((Callback *)list[0].closure);
XtFree((char *)list);
}
#if NeedFunctionPrototypes
void XmtRegisterCallbackConverter(void)
#else
void XmtRegisterCallbackConverter()
#endif
{
static Boolean registered = False;
if (!registered) {
registered = True;
/*
* Note that the results of this converter cannot be cached, because
* the callback arguments need to be converted in the context of
* their particular widget and thus cannot be shared. It doesn't
* make much sense, however, to have lots of widgets with the same
* callbacks and arguments, however, so this is no great loss.
*/
XtSetTypeConverter(XtRString, XtRCallback,
XmtConvertStringToCallback, NULL, 0,
XtCacheNone | XtCacheRefCount,
XmtDestroyCallback);
/* ensure that the quarks we need will exist when the
* converter is called
*/
_XmtInitQuarks();
/*
* we also register the converter in variables declared in
* GlobalsI.h so that it can be looked up by the automatic
* widget creation routines. We don't want those routines to
* reference these directly because then they would be linked
* together.
*/
_XmtCallbackConverter = XmtConvertStringToCallback;
}
}
#if NeedFunctionPrototypes
Boolean XmtCallbackCheckList(XtCallbackList list, XmtProcedure proc)
#else
Boolean XmtCallbackCheckList(list, proc)
XtCallbackList list;
XmtProcedure proc;
#endif
{
Callback *cb;
int i;
/* for each item in the callback list */
for(; list->callback != NULL; list++) {
/* if the procedures match exactly */
if (list->callback == (XtCallbackProc) proc) return True;
/* else if it is the result of the converter */
else if (list->callback == (XtCallbackProc) XmtCallCallback) {
/* the client data is a list of calls */
cb = (Callback *) list->closure;
for (i=0; i < cb->num_calls; i++) {
/* if the callback has been called once and already converted*/
if (cb->calls[i].procedure) {
if (cb->calls[i].procedure->function == proc) return True;
}
/* otherwise, we've got to look up procedures by name */
else {
XmtProcedureInfo *procedure;
procedure =XmtLookupProcedure(cb->calls[i].procedure_name);
if (procedure && procedure->function == proc) return True;
}
}
}
}
return False;
}
|