1 /* Part of XPCE --- The SWI-Prolog GUI toolkit 2 3 Author: Jan Wielemaker and Anjo Anjewierden 4 E-mail: jan@swi.psy.uva.nl 5 WWW: http://www.swi.psy.uva.nl/projects/xpce/ 6 Copyright (c) 1985-2002, University of Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #ifndef _XPCE_INTERFACE_H_INCLUDED 36 #define _XPCE_INTERFACE_H_INCLUDED 37 38 39 /******************************** 40 * VERSIONS * 41 ********************************/ 42 43 #ifndef PCE_VERSION 44 #define PCE_VERSION "6.6.66, July 2009" 45 #endif 46 47 #ifndef OS_VERSION 48 #define OS_VERSION "unknown" 49 #endif 50 51 #ifndef PCE_MACHINE 52 #define PCE_MACHINE "unknown" 53 #endif 54 55 56 /******************************* 57 * WIN32 DLL * 58 *******************************/ 59 60 #ifndef __pce_export 61 #if defined(PCE_INCLUDED) && (defined(WIN32) || defined(__CYWIN32__)) 62 #define __pce_export __declspec(dllexport) 63 #else 64 #define __pce_export extern 65 #endif /*PCE_INCLUDED*/ 66 #endif /*__pce_export*/ 67 68 69 /******************************** 70 * PRIMITIVES * 71 ********************************/ 72 73 #ifndef PCE_INCLUDED 74 typedef void * PceObject; /* PCE's view of an object */ 75 typedef void * PceName; /* PCE's view of a name */ 76 typedef void * PceMethod; /* PCE's view of a method */ 77 typedef void * PceType; /* PCE's view of a type */ 78 typedef void * PceClass; /* PCE's view of a class */ 79 typedef void * PceHostData; /* PCE's view of a host data handle */ 80 81 #define INT_MASK_SHIFT 1 82 #define PCE_MAX_INT ((intptr_t)(((uintptr_t)1<<(sizeof(void*)*8 - INT_MASK_SHIFT-1))-1)) 83 #define PCE_MIN_INT (-(PCE_MAX_INT-1)) 84 85 typedef long AnswerMark; /* Mark on AnswerStack */ 86 87 __pce_export void _markAnswerStack(AnswerMark *); 88 __pce_export void _rewindAnswerStack(AnswerMark *, PceObject); 89 90 #define markAnswerStack(mark) _markAnswerStack(&(mark)) 91 #define rewindAnswerStack(mark, obj) _rewindAnswerStack(&(mark), obj) 92 93 #ifdef __GNUC__ 94 #define ArgVector(name, size) PceObject name[size] 95 #else 96 #define ArgVector(name, size) PceObject *name = \ 97 (PceObject *) alloca(size*sizeof(PceObject)) 98 #endif 99 typedef struct pceITFSymbol *PceITFSymbol; 100 101 #else /*PCE_INCLUDED*/ 102 103 #define PceObject Any 104 #define PceName Name 105 #define PceMethod Method 106 #define PceType Type 107 #define PceClass Class 108 #define PceHostData HostData 109 110 #define PCE_MAX_HOSTHANDLES 10 111 112 GLOBAL HashTable ObjectToITFTable; 113 GLOBAL HashTable NameToITFTable; 114 GLOBAL HashTable HandleToITFTables[PCE_MAX_HOSTHANDLES]; 115 #endif /*PCE_INCLUDED*/ 116 117 118 119 120 /******************************** 121 * INTERFACE TABLE * 122 ********************************/ 123 124 typedef void * hostHandle; /* Handle of host-language */ 125 126 struct pceITFSymbol 127 { PceObject object; /* global object associated */ 128 PceName name; /* Pce name associated */ 129 #if __GNUC__ && !__STRICT_ANSI__ 130 hostHandle handle[0]; /* Alien handles to operate on */ 131 #else 132 hostHandle handle[1]; /* Alien handles to operate on */ 133 #endif 134 }; 135 136 137 __pce_export PceITFSymbol pceLookupHandle(int, hostHandle); 138 __pce_export void pceRegisterName(int, hostHandle, PceName); 139 __pce_export void pceRegisterAssoc(int, hostHandle, PceObject); 140 __pce_export int pceHostHandles(int); 141 142 __pce_export PceITFSymbol getITFSymbolName(PceName); 143 144 /******************************** 145 * CONSTANTS * 146 ********************************/ 147 148 149 #define PCE_FAIL 0 150 #define PCE_SUCCEED 1 151 152 153 /******************************** 154 * PCE --> C * 155 ********************************/ 156 157 typedef union 158 { char character; /* Value is a C char */ 159 intptr_t integer; /* Value of PCE int or reference */ 160 double real; /* Value of PCE real */ 161 char * string; /* Value is a C char * */ 162 void * pointer; /* Anonymous pointer */ 163 PceITFSymbol itf_symbol; /* Interface symbol */ 164 } PceCValue; 165 166 167 #define PCE_INTEGER 1 168 #define PCE_NAME 2 169 #define PCE_REFERENCE 3 170 #define PCE_ASSOC 4 171 #define PCE_REAL 5 172 #define PCE_HOSTDATA 6 173 174 #define PCE_NO_POINTER ((void *) ~0L) 175 176 #define PCE_ANSWER 0x1 /* CtoHostData() flags */ 177 178 __pce_export int pceToC(PceObject datum, PceCValue *rval); 179 __pce_export int pceToCReference(PceObject datum, PceCValue *rval); 180 __pce_export char * pceCharArrayToCA(PceObject datum, size_t *len); 181 __pce_export wchar_t * pceCharArrayToCW(PceObject datum, size_t *len); 182 __pce_export int pceIsString(PceObject datum); 183 __pce_export void * pcePointerToC(PceObject datum); 184 __pce_export PceHostData CtoHostData(PceClass class, void *handle, int flags); 185 __pce_export void * getHostDataHandle(PceHostData hd); 186 __pce_export void makeAnyHostData(PceHostData hd); 187 __pce_export void setHostDataHandle(PceHostData hd, void *handle); 188 __pce_export int freeHostData(PceHostData hd); 189 190 191 /******************************** 192 * VMI * 193 ********************************/ 194 195 __pce_export PceObject pceNew (PceName classname, 196 PceObject assoc, 197 int argc, PceObject *argv); 198 __pce_export int pceSend(PceObject receiver, 199 PceName classname, 200 PceName selector, 201 int argc, PceObject * argv); 202 __pce_export PceObject pceGet (PceObject receiver, 203 PceName classname, 204 PceName selector, 205 int argc, PceObject * argv); 206 207 __pce_export PceClass nameToExistingClass(PceName name); 208 209 /******************************* 210 * DIRECT GOAL-INTERFACE * 211 *******************************/ 212 213 #define PCE_GOAL_DIRECT_ARGS 4 /* # in-line optimised arguments */ 214 215 /* Goal errno values */ 216 #define PCE_ERR_OK 0 217 #define PCE_ERR_NO_BEHAVIOUR 1 218 #define PCE_ERR_ARGTYPE 2 219 #define PCE_ERR_TOO_MANY_ARGS 3 220 #define PCE_ERR_ANONARG_AFTER_NAMED 4 221 #define PCE_ERR_NO_NAMED_ARGUMENT 5 222 #define PCE_ERR_MISSING_ARGUMENT 6 223 #define PCE_ERR_CODE_AS_GETMETHOD 7 224 #define PCE_ERR_PERMISSION 8 225 #define PCE_ERR_FUNCTION_FAILED 9 226 #define PCE_ERR_ERROR 10 /* id, vector(...args...) */ 227 #define PCE_ERR_RETTYPE 11 228 #define PCE_ERR_USER_BASE 1000 /* base for user (host) errors */ 229 230 #define PCE_GF_CATCHALL 0x001 /* <->catch_all implemeted */ 231 #define PCE_GF_SEND 0x002 /* a send operation */ 232 #define PCE_GF_GET 0x004 /* a get operation */ 233 #define PCE_GF_EXCEPTION 0x008 /* an error occurred */ 234 #define PCE_GF_HOST 0x010 /* Implemented by the host */ 235 #define PCE_GF_ALLOCATED 0x020 /* g->argv is allocated */ 236 #define PCE_GF_VA_ALLOCATED 0x040 /* g->va_argv is allocated */ 237 #define PCE_GF_CATCH 0x080 /* Catch exceptions */ 238 #define PCE_GF_THROW 0x100 /* Ok, here is one :-) */ 239 #define PCE_GF_HOSTARGS 0x200 /* Arguments are not in argv */ 240 241 typedef struct _pce_goal 242 { PceObject implementation; /* implementation of the method */ 243 PceObject receiver; /* Receiver */ 244 PceClass class; /* Class used for resolve action */ 245 246 struct _pce_goal *parent; /* Parent goal (if any) */ 247 248 int argc; /* # arguments */ 249 PceObject *argv; /* argument vector */ 250 int va_argc; /* Vararg # arguments (-1: none) */ 251 PceObject *va_argv; /* Vararg argument vector */ 252 253 int argn; /* Current argument */ 254 PceName selector; 255 PceType *types; 256 int flags; 257 int errcode; /* Error code */ 258 void * host_closure; /* Arbitrary host-handle */ 259 PceObject errc1; /* Error context #1 */ 260 PceObject errc2; /* Error context #2 */ 261 PceObject rval; /* get-goal return-value */ 262 PceType va_type; /* type for varargs */ 263 PceType return_type; /* return-type (get-methods) */ 264 int va_allocated; 265 PceObject _av[PCE_GOAL_DIRECT_ARGS]; 266 } pce_goal, *PceGoal; 267 268 __pce_export int pcePushArgument(PceGoal g, PceObject argument); 269 __pce_export int pceResolveImplementation(PceGoal goal); 270 __pce_export void pceInitArgumentsGoal(PceGoal goal); 271 __pce_export void pceVaAddArgGoal(PceGoal goal, PceObject value); 272 __pce_export int pcePushNamedArgument(PceGoal goal, 273 PceName name, PceObject arg); 274 __pce_export int pceGetArgumentTypeGoal(PceGoal goal, PceName name, 275 PceType *type, int *i); 276 __pce_export int pceSetErrorGoal(PceGoal goal, int err, ...); 277 __pce_export int pceExecuteGoal(PceGoal goal); 278 __pce_export void pceFreeGoal(PceGoal goal); 279 __pce_export void pcePushGoal(PceGoal goal); 280 __pce_export void pceTraceBack(PceGoal from, int depth); 281 __pce_export void pceReportErrorGoal(PceGoal goal); 282 __pce_export void pcePrintEnterGoal(PceGoal goal); 283 __pce_export void pcePrintReturnGoal(PceGoal goal, int rval); 284 /* Type logic */ 285 __pce_export int pceIncludesType(PceType t, PceType super); 286 __pce_export int pceIncludesHostDataType(PceType t, PceClass hdclass); 287 __pce_export PceObject pceCheckType(PceGoal g, PceType t, PceObject in); 288 __pce_export int pceCheckIntType(PceType t, long val); 289 __pce_export int pceCheckNameType(PceType t, const char *s); 290 __pce_export int pceCheckFloatType(PceType t, double f); 291 292 __pce_export int pceEnumElements(PceObject collection, 293 int (*enumfunc)(PceObject element, 294 void *closure), 295 void *closure); 296 297 298 /******************************* 299 * MULTI-THREADING SUPPORT * 300 *******************************/ 301 302 #define LOCK_PCE 0 303 304 __pce_export int pceMTinit(void); 305 __pce_export void pceMTLock(int lock); 306 __pce_export void pceMTUnlock(int lock); 307 __pce_export int pceMTTryLock(int lock); 308 __pce_export int pceMTdetach(void); 309 #ifdef __WINDOWS__ 310 __pce_export unsigned setPceThread(unsigned id); 311 #endif 312 313 /******************************** 314 * PCE CALLING C * 315 ********************************/ 316 317 #define HOST_QUERY 0 /* execute interactive query */ 318 #define HOST_TRACE 1 /* start debugger on host */ 319 #define HOST_BACKTRACE 2 /* give stack trace on host */ 320 #define HOST_HALT 3 /* exit host */ 321 #define HOST_BREAK 4 /* start interactive session */ 322 /*#define HOST_SYMBOLFILE 5*/ /* This is deleted */ 323 #define HOST_ABORT 6 /* abort, return to toplevel */ 324 #define HOST_SIGNAL 7 /* signal() replacement */ 325 #define HOST_RECOVER_FROM_FATAL_ERROR 9 /* Error: don't return */ 326 #define HOST_ATEXIT 10 /* Callback on exit */ 327 #define HOST_CONSOLE 11 /* Win32: query HWND of console */ 328 #define HOST_CHECK_INTERRUPT 12 /* Win32: periodic check for ^C */ 329 #define HOST_ENCODING 13 /* Query default encoding of the host */ 330 331 #define PCE_METHOD_INFO_HANDLE_ONLY 0x01 332 #define PCE_METHOD_INFO_TRACE_ENTER 0x02 333 #define PCE_METHOD_INFO_TRACE_EXIT 0x04 334 #define PCE_METHOD_INFO_TRACE_FAIL 0x08 335 #define PCE_METHOD_INFO_TRACE 0x0E 336 #define PCE_METHOD_INFO_BREAK_ENTER 0x10 337 #define PCE_METHOD_INFO_BREAK_EXIT 0x20 338 #define PCE_METHOD_INFO_BREAK_FAIL 0x40 339 #define PCE_METHOD_INFO_BREAK 0x70 340 341 typedef struct 342 { void * handle; /* anonymous data-handle */ 343 PceName name; /* name of the method */ 344 PceName context; /* Name of the context-class */ 345 int flags; /* Additional info */ 346 int argc; /* Argument-count */ 347 PceType *types; /* Type-vector */ 348 } pce_method_info; 349 350 __pce_export int pceGetMethodInfo(PceMethod m, pce_method_info *info); 351 352 typedef struct 353 { int (*hostSend) (PceObject, PceName, int, PceObject *); 354 PceObject (*hostGet) (PceObject, PceName, int, PceObject *); 355 int (*hostCall) (PceGoal goal); 356 int (*hostQuery) (int, PceCValue *); 357 int (*hostActionv) (int, va_list args); 358 void (*vCprintf) (const char *fmt, va_list args); 359 int (*Cputchar) (int); 360 void (*Cflush) (void); 361 char * (*Cgetline) (char *line, int size); 362 void * (*malloc) (size_t size); 363 void * (*realloc) (void *ptr, size_t size); 364 void (*free) (void *ptr); 365 #ifdef SIO_MAGIC /* defined from <SWI-Stream.h> */ 366 IOSTREAM* (*rc_open) (const char *name, const char *class, 367 const char *mode); 368 #else 369 void * rc_open; 370 #endif 371 PceObject (*getHostContext)(PceObject host); 372 PceObject (*setHostContext)(PceObject context); 373 PceObject (*translate) (PceObject handle, PceObject type); 374 int (*writeGoalArgs) (PceGoal g); 375 void * pad17; 376 void * pad18; 377 void * pad19; 378 void * pad20; 379 void * pad21; 380 void * pad22; 381 void * pad23; 382 } pce_callback_functions; 383 384 __pce_export void pceRegisterCallbacks(pce_callback_functions *funcs); 385 386 typedef struct 387 { void * (*call) (void *impl, void *type); 388 void (*exit) (void *node); 389 void *handle; /* context (type) handle */ 390 } pce_profile_hooks; 391 392 __pce_export int pceSetProfileHooks(pce_profile_hooks *hooks); 393 394 395 /******************************** 396 * INITIALISATION * 397 ********************************/ 398 399 __pce_export int pceInitialise(int handles, const char *home, const char *appdata, 400 int argc, char **argv); 401 402 403 /******************************** 404 * C --> PCE * 405 ********************************/ 406 407 __pce_export PceObject cToPceName_nA(const char *, size_t len); 408 __pce_export PceObject cToPceName_nW(const wchar_t *, size_t len); 409 __pce_export PceObject cToPceInteger(intptr_t); 410 __pce_export PceObject cToPceReal(double); 411 __pce_export PceObject cToPceStringA(PceName assoc, 412 const char *text, size_t len, 413 int translate); 414 __pce_export PceObject cToPceStringW(PceName assoc, 415 const wchar_t *text, size_t len, 416 int translate); 417 __pce_export PceObject cToPceAssoc(const char *); 418 __pce_export PceObject cToPceReference(uintptr_t); 419 __pce_export PceObject cToPcePointer(void *ptr); 420 __pce_export int pceLock(PceObject); 421 422 __pce_export PceObject cToPceTmpCharArray(const char *text); 423 __pce_export void donePceTmpCharArray(PceObject); 424 425 __pce_export int pceExistsReference(uintptr_t ref); 426 __pce_export char * pcePPReference(PceObject ref); 427 __pce_export int pceExistsAssoc(PceName assoc); 428 __pce_export PceObject pceObjectFromName(PceName assoc); 429 430 __pce_export int pceInstanceOf(PceObject obj, PceObject class); 431 432 /******************************** 433 * EVENTS * 434 ********************************/ 435 436 #define PCE_DISPATCH_INPUT (0) 437 #define PCE_DISPATCH_TIMEOUT (1) 438 439 __pce_export int pceDispatch(int fd, int msecs); 440 __pce_export void pceRedraw(int); 441 /* context is an XtAppContext pointer */ 442 __pce_export void * pceXtAppContext(void * context); 443 444 445 /******************************** 446 * DEBUGGER INTERFACE * 447 ********************************/ 448 449 #define PCE_EXEC_SERVICE 0 /* `service' call-back */ 450 #define PCE_EXEC_USER 1 /* application call-back */ 451 452 __pce_export int pceExecuteMode(void); 453 __pce_export void pceReset(void); 454 __pce_export void pceTrace(int); /* 1: trace; 0: notrace */ 455 __pce_export void pceWriteCurrentGoal(void); /* dump top stack */ 456 457 458 /* XPCE console interaction */ 459 __pce_export void Cprintf(const char *fmt, ...); 460 __pce_export void Cvprintf(const char *fmt, va_list args); 461 __pce_export int Cputchar(int chr); 462 __pce_export void Cflush(void); 463 __pce_export char * Cgetline(char *line, int size); 464 465 #ifdef PCE_INCLUDED 466 int Cputstr(PceString s); 467 #endif 468 469 470 /******************************* 471 * MEMORY ALLOCATION * 472 *******************************/ 473 474 #ifndef PCE_INCLUDED 475 __pce_export void * pceMalloc(size_t size); 476 __pce_export void * pceRealloc(void *ptr, size_t size); 477 __pce_export void pceFree(void *ptr); 478 #endif 479 480 __pce_export void * pceAlloc(int bytes); 481 __pce_export void pceUnAlloc(int bytes, void *p); 482 483 484 /******************************* 485 * OBJECTS * 486 *******************************/ 487 488 489 __pce_export PceClass pceClassOfObject(PceObject obj); 490 __pce_export int pceReferencesOfObject(PceObject obj); 491 __pce_export int pceFreeObject(PceObject obj); 492 493 /******************************* 494 * METHODS * 495 *******************************/ 496 497 __pce_export void pceSendMethod(PceClass class, 498 const char *name, 499 const char *group, 500 int argc, 501 ...); 502 __pce_export void pceGetMethod(PceClass class, 503 const char *name, 504 const char *group, 505 const char *rtype, 506 int argc, 507 ...); 508 509 /******************************* 510 * STREAM INTERFACE * 511 *******************************/ 512 513 #define PCE_OPEN_MAX 64 /* statically allocated max open */ 514 515 __pce_export int pceOpen(PceObject obj, int flags, void *encoding); 516 __pce_export int pceClose(int handle); 517 __pce_export ssize_t pceWrite(int handle, const char *buf, size_t size); 518 __pce_export ssize_t pceRead(int handle, char *buf, size_t size); 519 __pce_export long pceSeek(int handle, long offset, int whence); 520 __pce_export int pceControl(int handle, int cmd, void *closure); 521 __pce_export const char *pceOsError(); 522 523 /* flags for pceOpen() (or'ed) */ 524 #define PCE_RDONLY 0x1 525 #define PCE_WRONLY 0x2 526 #define PCE_RDWR 0x3 /* == PCE_RDONLY|PCE_WRONLY */ 527 #define PCE_APPEND 0x4 528 #define PCE_TRUNC 0x8 529 530 #define PCE_SEEK_SET 0 531 #define PCE_SEEK_CUR 1 532 #define PCE_SEEK_END 2 533 534 #define PCE_SETENCODING 1 /* pceControl() commands */ 535 536 /******************************* 537 * MISC * 538 *******************************/ 539 540 __pce_export void * pcePostScriptStream(void); 541 542 543 /******************************* 544 * CALL-BACK * 545 *******************************/ 546 547 #ifdef PCE_INCLUDED 548 extern pce_callback_functions TheCallbackFunctions; 549 550 #define hostCall(info) \ 551 (*TheCallbackFunctions.hostCall)(info) 552 #define pceMalloc(n) \ 553 (*TheCallbackFunctions.malloc)((n)) 554 #define pceRealloc(ptr, n) \ 555 (*TheCallbackFunctions.realloc)((ptr), (n)) 556 #define pceFree(ptr) \ 557 (*TheCallbackFunctions.free)((ptr)) 558 559 int hostSend(PceObject host, PceName selector, 560 int argc, PceObject argv []); 561 PceObject hostGet(PceObject host, PceName selector, 562 int argc, PceObject argv []); 563 int hostQuery(int what, PceCValue *value); 564 int hostAction(int what, ...); 565 566 extern pce_profile_hooks PceProfile; 567 #endif /*PCE_INCLUDED*/ 568 569 #endif /*_XPCE_INTERFACE_H_INCLUDED*/ 570