amigalib.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. {$INLINE ON}
  39. {$mode objfpc}
  40. unit amigalib
  41. deprecated 'Unit will be removed. Functions are moved to exec, intuition, utility and commodities unit.';
  42. INTERFACE
  43. uses exec,intuition,utility,commodities,inputevent,amigados;
  44. // moved to exec, use them from there
  45. {* Exec support functions from amiga.lib *}
  46. procedure BeginIO (ioRequest: pIORequest); inline;
  47. function CreateExtIO (port: pMsgPort; size: Longint): pIORequest; inline;
  48. procedure DeleteExtIO (ioReq: pIORequest); inline;
  49. function CreateStdIO (port: pMsgPort): pIOStdReq; inline;
  50. procedure DeleteStdIO (ioReq: pIOStdReq); inline;
  51. function CreatePort (name: PChar; pri: longint): pMsgPort; inline;
  52. procedure DeletePort (port: pMsgPort); inline;
  53. function CreateTask (name: STRPTR; pri: longint;
  54. initPC : Pointer;
  55. stackSize : ULONG): pTask; inline;
  56. procedure DeleteTask (task: pTask); inline;
  57. procedure NewList (list: pList); inline;
  58. // moved to commodities, use them from there
  59. {* Commodities support functions from amiga.lib *}
  60. procedure FreeIEvents (events: pInputEvent); inline;
  61. function CxCustom
  62. (action: pointer;
  63. id: longint): pCxObj; inline;
  64. function CxDebug (id: long): pCxObj; inline;
  65. function CxFilter (d: STRPTR): pCxObj; inline;
  66. function CxSender
  67. (port: pMsgPort;
  68. id: longint): pCxObj; inline;
  69. function CxSignal
  70. (task: pTask;
  71. sig: byte): pCxObj; inline;
  72. function CxTranslate (ie: pInputEvent): pCxObj; inline;
  73. // moved to intuition, use them from there
  74. function DoMethodA(obj : pObject_; msg : APTR): ulong; inline;
  75. function DoSuperMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong; inline;
  76. function CoerceMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong; inline;
  77. function SetSuperAttrsA(cl : pIClass; obj: pObject_; msg : APTR): ulong; inline;
  78. function DoMethod(obj: PObject_; Params: array of DWord): LongWord; inline;
  79. // moved to utility, use them from there
  80. procedure HookEntry;
  81. procedure HookEntryPas;
  82. {
  83. NAME
  84. printf - print a formatted output line to the standard output.
  85. SYNOPSIS
  86. printf(formatstring [,value [,values] ] );
  87. FUNCTION
  88. Format the output in accordance with specifications in the format
  89. string.
  90. INPUTS
  91. formatString - a C-language-like NULL-terminated format string,
  92. with the following supported % options:
  93. %[flags][width][.limit][length]type
  94. $ - must follow the arg_pos value, if specified
  95. flags - only one allowed. '-' specifies left justification.
  96. width - field width. If the first character is a '0', the
  97. field is padded with leading 0s.
  98. . - must precede the field width value, if specified
  99. limit - maximum number of characters to output from a string.
  100. (only valid for %s or %b).
  101. length - size of input data defaults to word (16-bit) for types c,
  102. d, u and x, 'l' changes this to long (32-bit).
  103. type - supported types are:
  104. b - BSTR, data is 32-bit BPTR to byte count followed
  105. by a byte string. A NULL BPTR is treated as an
  106. empty string. (V36)
  107. d - signed decimal
  108. u - unsigned decimal
  109. x - hexadecimal with hex digits in uppercase
  110. X - hexadecimal with hex digits in lowercase
  111. s - string, a 32-bit pointer to a NULL-terminated
  112. byte string. A NULL pointer is treated
  113. as an empty string.
  114. c - character
  115. value(s) - numeric variables or addresses of null-terminated strings
  116. to be added to the format information.
  117. NOTE
  118. The global "_stdout" must be defined, and contain a pointer to
  119. a legal AmigaDOS file handle. Using the standard Amiga startup
  120. module sets this up. In other cases you will need to define
  121. stdout, and assign it to some reasonable value (like what the
  122. dos.library/Output() call returns). This code would set it up:
  123. ULONG stdout;
  124. stdout=Output();
  125. BUGS
  126. This function will crash if the resulting stream after
  127. parameter substitution is longer than 140 bytes.
  128. }
  129. procedure printf(Fmtstr : pchar; const Args : array of const);
  130. procedure printf(Fmtstr : string; const Args : array of const);
  131. IMPLEMENTATION
  132. {* Exec support functions from amiga.lib *}
  133. procedure BeginIO (ioRequest: pIORequest); inline;
  134. begin
  135. Exec.BeginIO(ioRequest);
  136. end;
  137. function CreateExtIO (port: pMsgPort; size: Longint): pIORequest; inline;
  138. begin
  139. CreateExtIO := Exec.CreateExtIO(port, size);
  140. end;
  141. procedure DeleteExtIO (ioReq: pIORequest); inline;
  142. begin
  143. Exec.DeleteExtIO(ioReq);
  144. end;
  145. function CreateStdIO (port: pMsgPort): pIOStdReq; inline;
  146. begin
  147. CreateStdIO := Exec.CreateStdIO(port)
  148. end;
  149. procedure DeleteStdIO (ioReq: pIOStdReq); inline;
  150. begin
  151. Exec.DeleteStdIO(ioReq)
  152. end;
  153. function Createport(name : PChar; pri : longint): pMsgPort; inline;
  154. begin
  155. Createport := Exec.Createport(name, pri);
  156. end;
  157. procedure DeletePort (port: pMsgPort); inline;
  158. begin
  159. Exec.DeletePort(port);
  160. end;
  161. function CreateTask (name: STRPTR; pri: longint; initPC: pointer; stackSize: ULONG): pTask; inline;
  162. begin
  163. CreateTask := Exec.CreateTask(name, pri, initPC, stacksize);
  164. end;
  165. procedure DeleteTask (task: pTask); inline;
  166. begin
  167. Exec.DeleteTask(task)
  168. end;
  169. procedure NewList (list: pList); inline;
  170. begin
  171. Exec.NewList(list);
  172. end;
  173. procedure FreeIEvents (events: pInputEvent); inline;
  174. begin
  175. Commodities.FreeIEvents(events);
  176. end;
  177. function CxCustom(action: pointer; id: longint): pCxObj; inline;
  178. begin
  179. CxCustom := Commodities.CxCustom(action, id)
  180. end;
  181. function CxDebug(id: long): pCxObj; inline;
  182. begin
  183. CxDebug := Commodities.CxDebug(id)
  184. end;
  185. function CxFilter(d: STRPTR): pCxObj; inline;
  186. begin
  187. CxFilter := Commodities.CxFilter(d);
  188. end;
  189. function CxSender(port: pMsgPort; id: longint): pCxObj; inline;
  190. begin
  191. CxSender := Commodities.CxSender(port, id)
  192. end;
  193. function CxSignal(task: pTask; sig: byte): pCxObj; inline;
  194. begin
  195. CxSignal:= Commodities.CxSignal(task, sig)
  196. end;
  197. function CxTranslate (ie: pInputEvent): pCxObj;
  198. begin
  199. CxTranslate := Commodities.CxTranslate(ie)
  200. end;
  201. function DoMethodA(obj : pObject_; msg : APTR): ulong; inline;
  202. begin
  203. DoMethodA := Intuition.DoMethodA(obj, msg);
  204. end;
  205. function DoMethod(obj: PObject_; Params: array of DWord): LongWord; inline;
  206. begin
  207. DoMethod := Intuition.DoMethodA(obj, @Params);
  208. end;
  209. function DoSuperMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong; inline;
  210. begin
  211. DoSuperMethodA := Intuition.DoSuperMethodA(cl, obj, msg);
  212. end;
  213. function CoerceMethodA(cl : pIClass; obj : pObject_; msg : APTR): ulong; inline;
  214. begin
  215. CoerceMethodA := Intuition.CoerceMethodA(cl, obj, msg);
  216. end;
  217. function SetSuperAttrsA(cl : pIClass; obj: pObject_; msg : APTR): ulong; inline;
  218. begin
  219. SetSuperAttrsA := Intuition.SetSuperAttrsA(cl, obj, msg);
  220. end;
  221. { Do *NOT* change this to nostackframe! }
  222. { The compiler will build a stackframe with link/unlk. So that will actually correct
  223. the stackpointer for both Pascal/StdCall and Cdecl functions, so the stackpointer
  224. will be correct on exit. It also needs no manual RTS. The argument push order is
  225. also correct for both. (KB) }
  226. procedure HookEntry; assembler;
  227. asm
  228. move.l a1,-(a7) // Msg
  229. move.l a2,-(a7) // Obj
  230. move.l a0,-(a7) // PHook
  231. move.l 12(a0),a0 // h_SubEntry = Offset 12
  232. jsr (a0) // Call the SubEntry
  233. end;
  234. { This is to be used with when the subentry function uses FPC's register calling
  235. convention, also see the comments above HookEntry. It is advised to actually
  236. declare Hook functions with cdecl instead of using this function, especially
  237. when writing code which is platform independent. (KB) }
  238. procedure HookEntryPas; assembler;
  239. asm
  240. move.l a2,-(a7)
  241. move.l a1,-(a7) // Msg
  242. move.l a2,a1 // Obj
  243. // PHook is in a0 already
  244. move.l 12(a0),a2 // h_SubEntry = Offset 12
  245. jsr (a2) // Call the SubEntry
  246. move.l (a7)+,a2
  247. end;
  248. procedure printf(Fmtstr : pchar; const Args : array of const);
  249. var
  250. i,j : longint;
  251. argarray : array of longint;
  252. strarray : array of RawByteString;
  253. begin
  254. SetLength(argarray, length(args));
  255. SetLength(strarray, length(args));
  256. j:=0;
  257. for i := low(args) to High(args) do
  258. begin
  259. case args[i].vtype of
  260. vtinteger : argarray[i] := longint(args[i].vinteger);
  261. vtpchar : argarray[i] := longint(args[i].vpchar);
  262. vtchar : argarray[i] := longint(args[i].vchar);
  263. vtpointer : argarray[i] := longint(args[i].vpointer);
  264. vtstring : begin
  265. strarray[j]:=RawByteString(args[i].vstring^);
  266. argarray[i]:=longint(PChar(strarray[j]));
  267. inc(j);
  268. end;
  269. end;
  270. end;
  271. VPrintf(Fmtstr,@argarray[0]);
  272. end;
  273. procedure printf(Fmtstr : string; const Args : array of const);
  274. begin
  275. printf(PChar(RawByteString(Fmtstr)), Args);
  276. end;
  277. end.