2
0

commodities.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 1998-2002 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. History:
  14. Added overlay functions for Pchar->Strings, functions
  15. and procedures.
  16. 14 Jul 2000.
  17. Added the defines use_amiga_smartlink and
  18. use_auto_openlib. Implemented autoopening
  19. of the library.
  20. 13 Jan 2003.
  21. changed integer > smallint,
  22. cardinal > longword.
  23. 09 Feb 2003.
  24. [email protected]
  25. }
  26. {$I useamigasmartlink.inc}
  27. {$ifdef use_amiga_smartlink}
  28. {$smartlink on}
  29. {$endif use_amiga_smartlink}
  30. unit commodities;
  31. INTERFACE
  32. uses exec, inputevent, keymap;
  33. { **************
  34. * Broker stuff
  35. **************}
  36. CONST
  37. { buffer sizes }
  38. CBD_NAMELEN = 24;
  39. CBD_TITLELEN = 40;
  40. CBD_DESCRLEN = 40;
  41. { CxBroker errors }
  42. CBERR_OK = 0; { No error }
  43. CBERR_SYSERR = 1; { System error , no memory, etc }
  44. CBERR_DUP = 2; { uniqueness violation }
  45. CBERR_VERSION = 3; { didn't understand nb_VERSION }
  46. NB_VERSION = 5; { Version of NewBroker structure }
  47. Type
  48. pNewBroker = ^tNewBroker;
  49. tNewBroker = record
  50. nb_Version : Shortint; { set to NB_VERSION }
  51. nb_Name,
  52. nb_Title,
  53. nb_Descr : STRPTR;
  54. nb_Unique,
  55. nb_Flags : smallint;
  56. nb_Pri : Shortint;
  57. { new in V5 }
  58. nb_Port : pMsgPort;
  59. nb_ReservedChannel : smallint; { plans for later port sharing }
  60. END;
  61. CONST
  62. { Flags for nb_Unique }
  63. NBU_DUPLICATE = 0;
  64. NBU_UNIQUE = 1; { will not allow duplicates }
  65. NBU_NOTIFY = 2; { sends CXM_UNIQUE to existing broker }
  66. { Flags for nb_Flags }
  67. COF_SHOW_HIDE = 4;
  68. { *******
  69. * cxusr
  70. *******}
  71. { * Fake data types for system private objects }
  72. Type
  73. CxObj = Longint;
  74. pCxObj = ^CxObj;
  75. CxMsg = Longint;
  76. pCXMsg = ^CxMsg;
  77. CONST
  78. { ******************************}
  79. { * Commodities Object Types *}
  80. { ******************************}
  81. CX_INVALID = 0; { not a valid object (probably null) }
  82. CX_FILTER = 1; { input event messages only }
  83. CX_TYPEFILTER = 2; { filter on message type }
  84. CX_SEND = 3; { sends a message }
  85. CX_SIGNAL = 4; { sends a signal }
  86. CX_TRANSLATE = 5; { translates IE into chain }
  87. CX_BROKER = 6; { application representative }
  88. CX_DEBUG = 7; { dumps kprintf to serial port }
  89. CX_CUSTOM = 8; { application provids function }
  90. CX_ZERO = 9; { system terminator node }
  91. { ***************}
  92. { * CxMsg types *}
  93. { ***************}
  94. CXM_UNIQUE = 16; { sent down broker by CxBroker() }
  95. { Obsolete: subsumed by CXM_COMMAND (below) }
  96. { Messages of this type rattle around the Commodities input network.
  97. * They will be sent to you by a Sender object, and passed to you
  98. * as a synchronous function call by a Custom object.
  99. *
  100. * The message port or function entry point is stored in the object,
  101. * and the ID field of the message will be set to what you arrange
  102. * issuing object.
  103. *
  104. * The Data field will point to the input event triggering the
  105. * message.
  106. }
  107. CXM_IEVENT = 32;
  108. { These messages are sent to a port attached to your Broker.
  109. * They are sent to you when the controller program wants your
  110. * program to do something. The ID field identifies the command.
  111. *
  112. * The Data field will be used later.
  113. }
  114. CXM_COMMAND = 64;
  115. { ID values }
  116. CXCMD_DISABLE = (15); { please disable yourself }
  117. CXCMD_ENABLE = (17); { please enable yourself }
  118. CXCMD_APPEAR = (19); { open your window, if you can }
  119. CXCMD_DISAPPEAR = (21); { go dormant }
  120. CXCMD_KILL = (23); { go away for good }
  121. CXCMD_UNIQUE = (25); { someone tried to create a broker
  122. * with your name. Suggest you Appear.
  123. }
  124. CXCMD_LIST_CHG = (27); { Used by Exchange program. Someone }
  125. { has changed the broker list }
  126. { return values for BrokerCommand(): }
  127. CMDE_OK = (0);
  128. CMDE_NOBROKER = (-1);
  129. CMDE_NOPORT = (-2);
  130. CMDE_NOMEM = (-3);
  131. { IMPORTANT NOTE: for V5:
  132. * Only CXM_IEVENT messages are passed through the input network.
  133. *
  134. * Other types of messages are sent to an optional port in your broker.
  135. *
  136. * This means that you must test the message type in your message handling,
  137. * if input messages and command messages come to the same port.
  138. *
  139. * Older programs have no broker port, so processing loops which
  140. * make assumptions about type won't encounter the new message types.
  141. *
  142. * The TypeFilter CxObject is hereby obsolete.
  143. *
  144. * It is less convenient for the application, but eliminates testing
  145. * for type of input messages.
  146. }
  147. { ********************************************************}
  148. { * CxObj Error Flags (return values from CxObjError()) *}
  149. { ********************************************************}
  150. COERR_ISNULL = 1; { you called CxError(NULL) }
  151. COERR_NULLATTACH = 2; { someone attached NULL to my list }
  152. COERR_BADFILTER = 4; { a bad filter description was given }
  153. COERR_BADTYPE = 8; { unmatched type-specific operation }
  154. { ****************************}
  155. { Input Expression structure }
  156. { ****************************}
  157. IX_VERSION = 2;
  158. Type
  159. pInputXpression = ^tInputXpression;
  160. tInputXpression = record
  161. ix_Version, { must be set to IX_VERSION }
  162. ix_Class : Byte; { class must match exactly }
  163. ix_Code : Word; { Bits that we want }
  164. ix_CodeMask : Word; { Set bits here to indicate }
  165. { which bits in ix_Code are }
  166. { don't care bits. }
  167. ix_Qualifier: Word; { Bits that we want }
  168. ix_QualMask : Word; { Set bits here to indicate }
  169. { which bits in ix_Qualifier }
  170. { are don't care bits }
  171. ix_QualSame : Word; { synonyms in qualifier }
  172. END;
  173. IX = tInputXpression;
  174. pIX = ^IX;
  175. CONST
  176. { QualSame identifiers }
  177. IXSYM_SHIFT = 1; { left- and right- shift are equivalent }
  178. IXSYM_CAPS = 2; { either shift or caps lock are equivalent }
  179. IXSYM_ALT = 4; { left- and right- alt are equivalent }
  180. { corresponding QualSame masks }
  181. IXSYM_SHIFTMASK = (IEQUALIFIER_LSHIFT + IEQUALIFIER_RSHIFT);
  182. IXSYM_CAPSMASK = (IXSYM_SHIFTMASK + IEQUALIFIER_CAPSLOCK);
  183. IXSYM_ALTMASK = (IEQUALIFIER_LALT + IEQUALIFIER_RALT);
  184. IX_NORMALQUALS = $7FFF; { for QualMask field: avoid RELATIVEMOUSE }
  185. VAR CxBase : pLibrary;
  186. const
  187. COMMODITIESNAME : PChar = 'commodities.library';
  188. FUNCTION ActivateCxObj(co : pCxObj; tru : LONGINT) : LONGINT;
  189. PROCEDURE AddIEvents(events : pInputEvent);
  190. PROCEDURE AttachCxObj(headObj : pCxObj; co : pCxObj);
  191. PROCEDURE ClearCxObjError(co : pCxObj);
  192. FUNCTION CreateCxObj(typ : ULONG; arg1 : LONGINT; arg2 : LONGINT): pCxObj;
  193. FUNCTION CxBroker(nb : pNewBroker; error : pCxObj) : pCxObj;
  194. FUNCTION CxMsgData(cxm : pCxMsg) : POINTER;
  195. FUNCTION CxMsgID(cxm : pCxMsg) : LONGINT;
  196. FUNCTION CxMsgType(cxm : pCxMsg) : ULONG;
  197. FUNCTION CxObjError(co : pCxObj) : LONGINT;
  198. FUNCTION CxObjType(co : pCxObj) : ULONG;
  199. PROCEDURE DeleteCxObj(co : pCxObj);
  200. PROCEDURE DeleteCxObjAll(co : pCxObj);
  201. PROCEDURE DisposeCxMsg(cxm : pCxMsg);
  202. PROCEDURE DivertCxMsg(cxm : pCxMsg; headObj : pCxObj; returnObj : pCxObj);
  203. PROCEDURE EnqueueCxObj(headObj : pCxObj; co : pCxObj);
  204. PROCEDURE InsertCxObj(headObj : pCxObj; co : pCxObj; pred : pCxObj);
  205. FUNCTION InvertKeyMap(ansiCode : ULONG; event : pInputEvent; km : pKeyMap) : BOOLEAN;
  206. FUNCTION MatchIX(event : pInputEvent; ix : pInputXpression) : BOOLEAN;
  207. FUNCTION ParseIX(description : pCHAR; ix : pInputXpression) : LONGINT;
  208. PROCEDURE RemoveCxObj(co : pCxObj);
  209. PROCEDURE RouteCxMsg(cxm : pCxMsg; co : pCxObj);
  210. FUNCTION SetCxObjPri(co : pCxObj; pri : LONGINT) : LONGINT;
  211. PROCEDURE SetFilter(filter : pCxObj; text : pCHAR);
  212. PROCEDURE SetFilterIX(filter : pCxObj; ix : pInputXpression);
  213. PROCEDURE SetTranslate(translator : pCxObj; events : pInputEvent);
  214. { overlay functions}
  215. FUNCTION ParseIX(description : string; ix : pInputXpression) : LONGINT;
  216. PROCEDURE SetFilter(filter : pCxObj; text : string);
  217. IMPLEMENTATION
  218. uses pastoc,msgbox;
  219. FUNCTION ActivateCxObj(co : pCxObj; tru : LONGINT) : LONGINT;
  220. BEGIN
  221. ASM
  222. MOVE.L A6,-(A7)
  223. MOVEA.L co,A0
  224. MOVE.L tru,D0
  225. MOVEA.L CxBase,A6
  226. JSR -042(A6)
  227. MOVEA.L (A7)+,A6
  228. MOVE.L D0,@RESULT
  229. END;
  230. END;
  231. PROCEDURE AddIEvents(events : pInputEvent);
  232. BEGIN
  233. ASM
  234. MOVE.L A6,-(A7)
  235. MOVEA.L events,A0
  236. MOVEA.L CxBase,A6
  237. JSR -180(A6)
  238. MOVEA.L (A7)+,A6
  239. END;
  240. END;
  241. PROCEDURE AttachCxObj(headObj : pCxObj; co : pCxObj);
  242. BEGIN
  243. ASM
  244. MOVE.L A6,-(A7)
  245. MOVEA.L headObj,A0
  246. MOVEA.L co,A1
  247. MOVEA.L CxBase,A6
  248. JSR -084(A6)
  249. MOVEA.L (A7)+,A6
  250. END;
  251. END;
  252. PROCEDURE ClearCxObjError(co : pCxObj);
  253. BEGIN
  254. ASM
  255. MOVE.L A6,-(A7)
  256. MOVEA.L co,A0
  257. MOVEA.L CxBase,A6
  258. JSR -072(A6)
  259. MOVEA.L (A7)+,A6
  260. END;
  261. END;
  262. FUNCTION CreateCxObj(typ : ULONG; arg1 : LONGINT; arg2 : LONGINT) : pCxObj;
  263. BEGIN
  264. ASM
  265. MOVE.L A6,-(A7)
  266. MOVE.L typ,D0
  267. MOVEA.L arg1,A0
  268. MOVEA.L arg2,A1
  269. MOVEA.L CxBase,A6
  270. JSR -030(A6)
  271. MOVEA.L (A7)+,A6
  272. MOVE.L D0,@RESULT
  273. END;
  274. END;
  275. FUNCTION CxBroker(nb : pNewBroker; error : pCxObj) : pCxObj;
  276. BEGIN
  277. ASM
  278. MOVE.L A6,-(A7)
  279. MOVEA.L nb,A0
  280. MOVE.L error,D0
  281. MOVEA.L CxBase,A6
  282. JSR -036(A6)
  283. MOVEA.L (A7)+,A6
  284. MOVE.L D0,@RESULT
  285. END;
  286. END;
  287. FUNCTION CxMsgData(cxm : pCxMsg) : POINTER;
  288. BEGIN
  289. ASM
  290. MOVE.L A6,-(A7)
  291. MOVEA.L cxm,A0
  292. MOVEA.L CxBase,A6
  293. JSR -144(A6)
  294. MOVEA.L (A7)+,A6
  295. MOVE.L D0,@RESULT
  296. END;
  297. END;
  298. FUNCTION CxMsgID(cxm : pCxMsg) : LONGINT;
  299. BEGIN
  300. ASM
  301. MOVE.L A6,-(A7)
  302. MOVEA.L cxm,A0
  303. MOVEA.L CxBase,A6
  304. JSR -150(A6)
  305. MOVEA.L (A7)+,A6
  306. MOVE.L D0,@RESULT
  307. END;
  308. END;
  309. FUNCTION CxMsgType(cxm : pCxMsg) : ULONG;
  310. BEGIN
  311. ASM
  312. MOVE.L A6,-(A7)
  313. MOVEA.L cxm,A0
  314. MOVEA.L CxBase,A6
  315. JSR -138(A6)
  316. MOVEA.L (A7)+,A6
  317. MOVE.L D0,@RESULT
  318. END;
  319. END;
  320. FUNCTION CxObjError(co : pCxObj) : LONGINT;
  321. BEGIN
  322. ASM
  323. MOVE.L A6,-(A7)
  324. MOVEA.L co,A0
  325. MOVEA.L CxBase,A6
  326. JSR -066(A6)
  327. MOVEA.L (A7)+,A6
  328. MOVE.L D0,@RESULT
  329. END;
  330. END;
  331. FUNCTION CxObjType(co : pCxObj) : ULONG;
  332. BEGIN
  333. ASM
  334. MOVE.L A6,-(A7)
  335. MOVEA.L co,A0
  336. MOVEA.L CxBase,A6
  337. JSR -060(A6)
  338. MOVEA.L (A7)+,A6
  339. MOVE.L D0,@RESULT
  340. END;
  341. END;
  342. PROCEDURE DeleteCxObj(co : pCxObj);
  343. BEGIN
  344. ASM
  345. MOVE.L A6,-(A7)
  346. MOVEA.L co,A0
  347. MOVEA.L CxBase,A6
  348. JSR -048(A6)
  349. MOVEA.L (A7)+,A6
  350. END;
  351. END;
  352. PROCEDURE DeleteCxObjAll(co : pCxObj);
  353. BEGIN
  354. ASM
  355. MOVE.L A6,-(A7)
  356. MOVEA.L co,A0
  357. MOVEA.L CxBase,A6
  358. JSR -054(A6)
  359. MOVEA.L (A7)+,A6
  360. END;
  361. END;
  362. PROCEDURE DisposeCxMsg(cxm : pCxMsg);
  363. BEGIN
  364. ASM
  365. MOVE.L A6,-(A7)
  366. MOVEA.L cxm,A0
  367. MOVEA.L CxBase,A6
  368. JSR -168(A6)
  369. MOVEA.L (A7)+,A6
  370. END;
  371. END;
  372. PROCEDURE DivertCxMsg(cxm : pCxMsg; headObj : pCxObj; returnObj : pCxObj);
  373. BEGIN
  374. ASM
  375. MOVE.L A6,-(A7)
  376. MOVEA.L cxm,A0
  377. MOVEA.L headObj,A1
  378. MOVEA.L returnObj,A2
  379. MOVEA.L CxBase,A6
  380. JSR -156(A6)
  381. MOVEA.L (A7)+,A6
  382. END;
  383. END;
  384. PROCEDURE EnqueueCxObj(headObj : pCxObj; co : pCxObj);
  385. BEGIN
  386. ASM
  387. MOVE.L A6,-(A7)
  388. MOVEA.L headObj,A0
  389. MOVEA.L co,A1
  390. MOVEA.L CxBase,A6
  391. JSR -090(A6)
  392. MOVEA.L (A7)+,A6
  393. END;
  394. END;
  395. PROCEDURE InsertCxObj(headObj : pCxObj; co : pCxObj; pred : pCxObj);
  396. BEGIN
  397. ASM
  398. MOVE.L A6,-(A7)
  399. MOVEA.L headObj,A0
  400. MOVEA.L co,A1
  401. MOVEA.L pred,A2
  402. MOVEA.L CxBase,A6
  403. JSR -096(A6)
  404. MOVEA.L (A7)+,A6
  405. END;
  406. END;
  407. FUNCTION InvertKeyMap(ansiCode : ULONG; event : pInputEvent; km : pKeyMap) : BOOLEAN;
  408. BEGIN
  409. ASM
  410. MOVE.L A6,-(A7)
  411. MOVE.L ansiCode,D0
  412. MOVEA.L event,A0
  413. MOVEA.L km,A1
  414. MOVEA.L CxBase,A6
  415. JSR -174(A6)
  416. MOVEA.L (A7)+,A6
  417. TST.W D0
  418. BEQ.B @end
  419. MOVEQ #1,D0
  420. @end: MOVE.B D0,@RESULT
  421. END;
  422. END;
  423. FUNCTION MatchIX(event : pInputEvent; ix : pInputXpression) : BOOLEAN;
  424. BEGIN
  425. ASM
  426. MOVE.L A6,-(A7)
  427. MOVEA.L event,A0
  428. MOVEA.L ix,A1
  429. MOVEA.L CxBase,A6
  430. JSR -204(A6)
  431. MOVEA.L (A7)+,A6
  432. TST.W D0
  433. BEQ.B @end
  434. MOVEQ #1,D0
  435. @end: MOVE.B D0,@RESULT
  436. END;
  437. END;
  438. FUNCTION ParseIX(description : pCHAR; ix : pInputXpression) : LONGINT;
  439. BEGIN
  440. ASM
  441. MOVE.L A6,-(A7)
  442. MOVEA.L description,A0
  443. MOVEA.L ix,A1
  444. MOVEA.L CxBase,A6
  445. JSR -132(A6)
  446. MOVEA.L (A7)+,A6
  447. MOVE.L D0,@RESULT
  448. END;
  449. END;
  450. PROCEDURE RemoveCxObj(co : pCxObj);
  451. BEGIN
  452. ASM
  453. MOVE.L A6,-(A7)
  454. MOVEA.L co,A0
  455. MOVEA.L CxBase,A6
  456. JSR -102(A6)
  457. MOVEA.L (A7)+,A6
  458. END;
  459. END;
  460. PROCEDURE RouteCxMsg(cxm : pCxMsg; co : pCxObj);
  461. BEGIN
  462. ASM
  463. MOVE.L A6,-(A7)
  464. MOVEA.L cxm,A0
  465. MOVEA.L co,A1
  466. MOVEA.L CxBase,A6
  467. JSR -162(A6)
  468. MOVEA.L (A7)+,A6
  469. END;
  470. END;
  471. FUNCTION SetCxObjPri(co : pCxObj; pri : LONGINT) : LONGINT;
  472. BEGIN
  473. ASM
  474. MOVE.L A6,-(A7)
  475. MOVEA.L co,A0
  476. MOVE.L pri,D0
  477. MOVEA.L CxBase,A6
  478. JSR -078(A6)
  479. MOVEA.L (A7)+,A6
  480. MOVE.L D0,@RESULT
  481. END;
  482. END;
  483. PROCEDURE SetFilter(filter : pCxObj; text : pCHAR);
  484. BEGIN
  485. ASM
  486. MOVE.L A6,-(A7)
  487. MOVEA.L filter,A0
  488. MOVEA.L text,A1
  489. MOVEA.L CxBase,A6
  490. JSR -120(A6)
  491. MOVEA.L (A7)+,A6
  492. END;
  493. END;
  494. PROCEDURE SetFilterIX(filter : pCxObj; ix : pInputXpression);
  495. BEGIN
  496. ASM
  497. MOVE.L A6,-(A7)
  498. MOVEA.L filter,A0
  499. MOVEA.L ix,A1
  500. MOVEA.L CxBase,A6
  501. JSR -126(A6)
  502. MOVEA.L (A7)+,A6
  503. END;
  504. END;
  505. PROCEDURE SetTranslate(translator : pCxObj; events : pInputEvent);
  506. BEGIN
  507. ASM
  508. MOVE.L A6,-(A7)
  509. MOVEA.L translator,A0
  510. MOVEA.L events,A1
  511. MOVEA.L CxBase,A6
  512. JSR -114(A6)
  513. MOVEA.L (A7)+,A6
  514. END;
  515. END;
  516. FUNCTION ParseIX(description : string; ix : pInputXpression) : LONGINT;
  517. begin
  518. ParseIX := ParseIX(pas2c(description),ix);
  519. end;
  520. PROCEDURE SetFilter(filter : pCxObj; text : string);
  521. begin
  522. SetFilter(filter,pas2c(text));
  523. end;
  524. {$I useautoopenlib.inc}
  525. {$ifdef use_auto_openlib}
  526. {$Info Compiling autoopening of commodities.library}
  527. var
  528. commodities_exit : Pointer;
  529. procedure ClosecommoditiesLibrary;
  530. begin
  531. ExitProc := commodities_exit;
  532. if CxBase <> nil then begin
  533. CloseLibrary(CxBase);
  534. CxBase := nil;
  535. end;
  536. end;
  537. const
  538. { Change VERSION and LIBVERSION to proper values }
  539. VERSION : string[2] = '0';
  540. LIBVERSION : longword = 0;
  541. begin
  542. CxBase := nil;
  543. CxBase := OpenLibrary(COMMODITIESNAME,LIBVERSION);
  544. if CxBase <> nil then begin
  545. commodities_exit := ExitProc;
  546. ExitProc := @ClosecommoditiesLibrary
  547. end else begin
  548. MessageBox('FPC Pascal Error',
  549. 'Can''t open commodities.library version ' + VERSION + #10 +
  550. 'Deallocating resources and closing down',
  551. 'Oops');
  552. halt(20);
  553. end;
  554. {$else}
  555. {$Warning No autoopening of commodities.library compiled}
  556. {$Info Make sure you open commodities.library yourself}
  557. {$endif use_auto_openlib}
  558. END. (* UNIT COMMODITIES *)