amigalib.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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-2003 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 DoMethodA, DoSuperMethodA, CoerceMethodA and SetSuperAttrsA.
  15. I've translated those from amigae. I'm not sure that they are
  16. correct but it's a start. Now you can try to make some tests
  17. with mui.
  18. 30 Jul 2000.
  19. Added stuff for commodities.
  20. FreeIEvents
  21. CxCustom
  22. CxDebug
  23. CxFilter
  24. CxSender
  25. CxSignal
  26. CxTranslate
  27. 19 Aug 2000.
  28. Rewrote Createport and DeletePort.
  29. 06 Sep 2000.
  30. Added two printf, one with pchar and one with string.
  31. They use array of const so this unit compiles with
  32. mode objfpc.
  33. 05 Nov 2002.
  34. Added the define use_amiga_smartlink
  35. 13 Jan 2003.
  36. [email protected]
  37. }
  38. {$mode objfpc}
  39. {$I useamigasmartlink.inc}
  40. {$ifdef use_amiga_smartlink}
  41. {$smartlink on}
  42. {$endif use_amiga_smartlink}
  43. unit amigalib;
  44. INTERFACE
  45. uses exec,intuition,utility,commodities,inputevent,amigados;
  46. {* Exec support functions from amiga.lib *}
  47. procedure BeginIO (ioRequest: pIORequest);
  48. function CreateExtIO (port: pMsgPort; size: Longint): pIORequest;
  49. procedure DeleteExtIO (ioReq: pIORequest);
  50. function CreateStdIO (port: pMsgPort): pIOStdReq;
  51. procedure DeleteStdIO (ioReq: pIOStdReq);
  52. function CreatePort (name: PChar; pri: longint): pMsgPort;
  53. procedure DeletePort (port: pMsgPort);
  54. function CreateTask (name: STRPTR; pri: longint;
  55. initPC : Pointer;
  56. stackSize : ULONG): pTask;
  57. procedure DeleteTask (task: pTask);
  58. procedure NewList (list: pList);
  59. {* Commodities support functions from amiga.lib *}
  60. procedure FreeIEvents (events: pInputEvent);
  61. function CxCustom
  62. (action: pointer;
  63. id: longint): pCxObj;
  64. function CxDebug (id: long): pCxObj;
  65. function CxFilter (d: STRPTR): pCxObj;
  66. function CxSender
  67. (port: pMsgPort;
  68. id: longint): pCxObj;
  69. function CxSignal
  70. (task: pTask;
  71. sig: byte): pCxObj;
  72. function CxTranslate (ie: pInputEvent): pCxObj;
  73. function DoMethodA(obj : pObject_; msg : APTR): ulong;
  74. function DoSuperMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong;
  75. function CoerceMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong;
  76. function SetSuperAttrsA(cl : pIClass; obj: pObject_; msg : APTR): ulong;
  77. {
  78. NAME
  79. printf - print a formatted output line to the standard output.
  80. SYNOPSIS
  81. printf(formatstring [,value [,values] ] );
  82. FUNCTION
  83. Format the output in accordance with specifications in the format
  84. string.
  85. INPUTS
  86. formatString - a C-language-like NULL-terminated format string,
  87. with the following supported % options:
  88. %[flags][width][.limit][length]type
  89. $ - must follow the arg_pos value, if specified
  90. flags - only one allowed. '-' specifies left justification.
  91. width - field width. If the first character is a '0', the
  92. field is padded with leading 0s.
  93. . - must precede the field width value, if specified
  94. limit - maximum number of characters to output from a string.
  95. (only valid for %s or %b).
  96. length - size of input data defaults to word (16-bit) for types c,
  97. d, u and x, 'l' changes this to long (32-bit).
  98. type - supported types are:
  99. b - BSTR, data is 32-bit BPTR to byte count followed
  100. by a byte string. A NULL BPTR is treated as an
  101. empty string. (V36)
  102. d - signed decimal
  103. u - unsigned decimal
  104. x - hexadecimal with hex digits in uppercase
  105. X - hexadecimal with hex digits in lowercase
  106. s - string, a 32-bit pointer to a NULL-terminated
  107. byte string. A NULL pointer is treated
  108. as an empty string.
  109. c - character
  110. value(s) - numeric variables or addresses of null-terminated strings
  111. to be added to the format information.
  112. NOTE
  113. The global "_stdout" must be defined, and contain a pointer to
  114. a legal AmigaDOS file handle. Using the standard Amiga startup
  115. module sets this up. In other cases you will need to define
  116. stdout, and assign it to some reasonable value (like what the
  117. dos.library/Output() call returns). This code would set it up:
  118. ULONG stdout;
  119. stdout=Output();
  120. BUGS
  121. This function will crash if the resulting stream after
  122. parameter substitution is longer than 140 bytes.
  123. }
  124. procedure printf(Fmtstr : pchar; Args : array of const);
  125. procedure printf(Fmtstr : string; Args : array of const);
  126. IMPLEMENTATION
  127. uses pastoc;
  128. {* Exec support functions from amiga.lib *}
  129. procedure BeginIO (ioRequest: pIORequest);
  130. begin
  131. asm
  132. move.l a6,-(a7)
  133. move.l ioRequest,a1 ; get IO Request
  134. move.l 20(a1),a6 ; extract Device ptr
  135. jsr -30(a6) ; call BEGINIO directly
  136. move.l (a7)+,a6
  137. end;
  138. end;
  139. function CreateExtIO (port: pMsgPort; size: Longint): pIORequest;
  140. var
  141. IOReq: pIORequest;
  142. begin
  143. IOReq := NIL;
  144. if port <> NIL then
  145. begin
  146. IOReq := AllocMem(size, MEMF_CLEAR or MEMF_PUBLIC);
  147. if IOReq <> NIL then
  148. begin
  149. IOReq^.io_Message.mn_Node.ln_Type := NT_REPLYMSG;
  150. IOReq^.io_Message.mn_Length := size;
  151. IOReq^.io_Message.mn_ReplyPort := port;
  152. end;
  153. end;
  154. CreateExtIO := IOReq;
  155. end;
  156. procedure DeleteExtIO (ioReq: pIORequest);
  157. begin
  158. if ioReq <> NIL then
  159. begin
  160. ioReq^.io_Message.mn_Node.ln_Type := $FF;
  161. ioReq^.io_Message.mn_ReplyPort := pMsgPort(-1);
  162. ioReq^.io_Device := pDevice(-1);
  163. ExecFreeMem(ioReq, ioReq^.io_Message.mn_Length);
  164. end
  165. end;
  166. function CreateStdIO (port: pMsgPort): pIOStdReq;
  167. begin
  168. CreateStdIO := pIOStdReq(CreateExtIO(port, sizeof(tIOStdReq)))
  169. end;
  170. procedure DeleteStdIO (ioReq: pIOStdReq);
  171. begin
  172. DeleteExtIO(pIORequest(ioReq))
  173. end;
  174. function Createport(name : PChar; pri : longint): pMsgPort;
  175. var
  176. sigbit : Byte;
  177. port : pMsgPort;
  178. begin
  179. sigbit := AllocSignal(-1);
  180. if sigbit = -1 then CreatePort := nil;
  181. port := Allocmem(sizeof(tMsgPort),MEMF_CLEAR or MEMF_PUBLIC);
  182. if port = nil then begin
  183. FreeSignal(sigbit);
  184. CreatePort := nil;
  185. end;
  186. with port^ do begin
  187. if assigned(name) then
  188. mp_Node.ln_Name := name
  189. else mp_Node.ln_Name := nil;
  190. mp_Node.ln_Pri := pri;
  191. mp_Node.ln_Type := NT_MsgPort;
  192. mp_Flags := PA_Signal;
  193. mp_SigBit := sigbit;
  194. mp_SigTask := FindTask(nil);
  195. end;
  196. if assigned(name) then AddPort(port)
  197. else NewList(addr(port^.mp_MsgList));
  198. CreatePort := port;
  199. end;
  200. procedure DeletePort (port: pMsgPort);
  201. begin
  202. if port <> NIL then
  203. begin
  204. if port^.mp_Node.ln_Name <> NIL then
  205. RemPort(port);
  206. port^.mp_Node.ln_Type := $FF;
  207. port^.mp_MsgList.lh_Head := pNode(-1);
  208. FreeSignal(port^.mp_SigBit);
  209. ExecFreeMem(port, sizeof(tMsgPort));
  210. end;
  211. end;
  212. function CreateTask (name: STRPTR; pri: longint;
  213. initPC: pointer; stackSize: ULONG): pTask;
  214. var
  215. memlist : pMemList;
  216. task : pTask;
  217. totalsize : Longint;
  218. begin
  219. task := NIL;
  220. stackSize := (stackSize + 3) and not 3;
  221. totalsize := sizeof(tMemList) + sizeof(tTask) + stackSize;
  222. memlist := AllocMem(totalsize, MEMF_PUBLIC + MEMF_CLEAR);
  223. if memlist <> NIL then begin
  224. memlist^.ml_NumEntries := 1;
  225. memlist^.ml_ME[0].me_Un.meu_Addr := Pointer(memlist + 1);
  226. memlist^.ml_ME[0].me_Length := totalsize - sizeof(tMemList);
  227. task := pTask(memlist + sizeof(tMemList) + stackSize);
  228. task^.tc_Node.ln_Pri := pri;
  229. task^.tc_Node.ln_Type := NT_TASK;
  230. task^.tc_Node.ln_Name := name;
  231. task^.tc_SPLower := Pointer(memlist + sizeof(tMemList));
  232. task^.tc_SPUpper := Pointer(task^.tc_SPLower + stackSize);
  233. task^.tc_SPReg := task^.tc_SPUpper;
  234. NewList(@task^.tc_MemEntry);
  235. AddTail(@task^.tc_MemEntry,@memlist^.ml_Node);
  236. AddTask(task,initPC,NIL)
  237. end;
  238. CreateTask := task;
  239. end;
  240. procedure DeleteTask (task: pTask);
  241. begin
  242. RemTask(task)
  243. end;
  244. procedure NewList (list: pList);
  245. begin
  246. with list^ do
  247. begin
  248. lh_Head := pNode(@lh_Tail);
  249. lh_Tail := NIL;
  250. lh_TailPred := pNode(@lh_Head)
  251. end
  252. end;
  253. procedure FreeIEvents (events: pInputEvent);
  254. begin
  255. while events <> NIL do
  256. begin
  257. FreeMem (events, sizeof (tInputEvent));
  258. events := events^.ie_NextEvent
  259. end
  260. end;
  261. function CxCustom
  262. (action: pointer;
  263. id: longint): pCxObj;
  264. begin
  265. CxCustom := CreateCxObj(CX_CUSTOM, longint(action), id)
  266. end;
  267. function CxDebug (id: long): pCxObj;
  268. begin
  269. CxDebug := CreateCxObj(CX_DEBUG, id, 0)
  270. end;
  271. function CxFilter (d: STRPTR): pCxObj;
  272. begin
  273. CxFilter := CreateCxObj(CX_FILTER, longint(d), 0)
  274. end;
  275. function CxSender
  276. (port: pMsgPort;
  277. id: longint): pCxObj;
  278. begin
  279. CxSender := CreateCxObj(CX_SEND, longint(port), id)
  280. end;
  281. function CxSignal
  282. (task: pTask;
  283. sig: byte): pCxObj;
  284. begin
  285. CxSignal:= CreateCxObj(CX_SIGNAL, longint(task), sig)
  286. end;
  287. function CxTranslate (ie: pInputEvent): pCxObj;
  288. begin
  289. CxTranslate := CreateCxObj(CX_TRANSLATE, longint(ie), 0)
  290. end;
  291. function DoMethodA(obj : pObject_; msg : APTR): ulong;
  292. var
  293. o : p_Object;
  294. begin
  295. if assigned(obj) then begin
  296. o := p_Object(obj);
  297. DoMethodA := CallHookPkt(@o^.o_Class^.cl_Dispatcher, obj,msg);
  298. end else DoMethodA := 0;
  299. end;
  300. function DoSuperMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong;
  301. begin
  302. if assigned(obj) and assigned(cl) then
  303. DoSuperMethodA := CallHookPkt(@cl^.cl_Super^.cl_Dispatcher,obj,msg)
  304. else DoSuperMethodA := 0;
  305. end;
  306. function CoerceMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong;
  307. begin
  308. if assigned(cl) and assigned(obj) then
  309. CoerceMethodA := CallHookPkt(@cl^.cl_Dispatcher,obj,msg)
  310. else CoerceMethodA := 0;
  311. end;
  312. function SetSuperAttrsA(cl : pIClass; obj: pObject_; msg : APTR): ulong;
  313. var
  314. arr : array[0..2] of longint;
  315. begin
  316. arr[0] := OM_SET;
  317. arr[1] := longint(msg);
  318. arr[2] := 0;
  319. SetSuperAttrsA := DoSuperMethodA(cl, obj, @arr);
  320. end;
  321. var
  322. argarray : array [0..20] of longint;
  323. function gettheconst(args : array of const): pointer;
  324. var
  325. i : longint;
  326. begin
  327. for i := 0 to High(args) do begin
  328. case args[i].vtype of
  329. vtinteger : argarray[i] := longint(args[i].vinteger);
  330. vtpchar : argarray[i] := longint(args[i].vpchar);
  331. vtchar : argarray[i] := longint(args[i].vchar);
  332. vtpointer : argarray[i] := longint(args[i].vpointer);
  333. vtstring : argarray[i] := longint(pas2c(args[i].vstring^));
  334. end;
  335. end;
  336. gettheconst := @argarray;
  337. end;
  338. procedure printf(Fmtstr : pchar; Args : array of const);
  339. begin
  340. VPrintf(Fmtstr,gettheconst(Args));
  341. end;
  342. procedure printf(Fmtstr : string; Args : array of const);
  343. begin
  344. VPrintf(pas2c(Fmtstr) ,gettheconst(Args));
  345. end;
  346. end.