sysandroid.inc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2015 by Yury Sidorov,
  4. member of the Free Pascal development team.
  5. Android-specific part of the System unit.
  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. procedure atexit(p: pointer); cdecl; external;
  13. var
  14. _SaveStdOut: THandle;
  15. _SaveStdErr: THandle;
  16. procedure SysAndroidLibExit; cdecl;
  17. var
  18. ioclosed: boolean;
  19. begin
  20. // Check if stdio is closed now
  21. ioclosed:=do_syscall(syscall_nr_fcntl, TSysParam(1), 1 {F_GETFD}) = -1;
  22. // If stdio is closed, restore stdout and stderr
  23. if ioclosed then
  24. begin
  25. FpDup2(_SaveStdOut, 1);
  26. FpDup2(_SaveStdErr, 2);
  27. end;
  28. // Close saved handles
  29. FpClose(_SaveStdOut);
  30. FpClose(_SaveStdErr);
  31. // Finalize the library
  32. lib_exit;
  33. // Close stdout and stderr if stdio has been closed
  34. if ioclosed then
  35. begin
  36. FpClose(1);
  37. FpClose(2);
  38. end;
  39. end;
  40. procedure SysInitAndroidLib; [public, alias:'FPC_LIB_INIT_ANDROID'];
  41. begin
  42. { Starting from Android 4.4 stdio handles are closed by libc prior to calling
  43. finalization routines of shared libraries. This causes a error while trying to
  44. writeln during library finalization and finally a crash because the error can
  45. not be printer too.
  46. It is needed to save stdout and stderr handles by duplicating them and restore
  47. them before library finalization.
  48. }
  49. _SaveStdOut:=FpDup(1);
  50. _SaveStdErr:=FpDup(2);
  51. // Register the finalization routine
  52. atexit(@SysAndroidLibExit);
  53. end;
  54. function __system_property_get(name:Pchar; value:Pchar):longint;cdecl;external 'c' name '__system_property_get';
  55. function GetSystemProperty(Name: PAnsiChar): shortstring;
  56. begin
  57. SetLength(Result, __system_property_get(Name, @Result[1]));
  58. end;
  59. var
  60. _ApiLevel: shortint = -1;
  61. function SystemApiLevel: shortint;
  62. var
  63. s: string;
  64. c: integer;
  65. begin
  66. if _ApiLevel < 0 then
  67. begin
  68. s:=GetSystemProperty('ro.build.version.sdk');
  69. Val(s, _ApiLevel, c);
  70. if c <> 0 then
  71. _ApiLevel:=0;
  72. end;
  73. Result:=_ApiLevel;
  74. end;