| 1 |
{ |
| 2 |
Copyright 2002-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ @abstract(Various helper things for Xlib.) |
| 17 |
|
| 18 |
In initialization of this unit |
| 19 |
we register our own Xlib ErrorHandler that doesn't halt the program |
| 20 |
in case of error. It raises @link(EXlibError) with appropriate Message. |
| 21 |
This allows to use ObjectPascal exceptions to handle Xlib errors, |
| 22 |
so we can gracefully finalize our program, or (in special |
| 23 |
cases) catch the exception etc. Default Xlib ErrorHandler |
| 24 |
was just printing error message and unconditionally stopping program, |
| 25 |
so it wasn't very nice. |
| 26 |
|
| 27 |
In finalization of this unit we set back previous error handler. |
| 28 |
} |
| 29 |
|
| 30 |
unit CastleInternalXlib; |
| 31 |
|
| 32 |
{$I castleconf.inc} |
| 33 |
|
| 34 |
interface |
| 35 |
|
| 36 |
uses Xlib, X, XUtil, SysUtils, CastleUtils; |
| 37 |
|
| 38 |
const |
| 39 |
{ } |
| 40 |
XlibDLL = 'libX11.so'; |
| 41 |
XmuDLL = 'libXmu.so'; |
| 42 |
|
| 43 |
type |
| 44 |
XBool = Xlib.TBool; |
| 45 |
|
| 46 |
TXStandardColormap_Array = array [0..High(Word)] of TXStandardColormap; |
| 47 |
PXStandardColormap_Array = ^TXStandardColormap_Array; |
| 48 |
|
| 49 |
const |
| 50 |
XBool_true = 1; |
| 51 |
XBool_false = 0; |
| 52 |
|
| 53 |
type |
| 54 |
EXlibError = class(Exception); |
| 55 |
|
| 56 |
{ ---------------------------------------------------------------------------- } |
| 57 |
{ @section(xutil.h (XLibDLL)) } |
| 58 |
|
| 59 |
{ } |
| 60 |
function XSetStandardProperties(dpy: PDisplay; win: TWindow; window_name: pchar; |
| 61 |
icon_name: pchar; icon_pixmap: TPixmap; argv: PPChar; argc: Integer; |
| 62 |
hints: PXSizeHints): integer; cdecl; external XLibDLL; |
| 63 |
function XSetStandardProperties_Pascal(dpy: PDisplay; win: TWindow; |
| 64 |
window_name: pchar; icon_name: pchar; icon_pixmap: TPixmap; hints: PXSizeHints) |
| 65 |
:integer; |
| 66 |
|
| 67 |
procedure XSetWMProperties_Pascal(Display: PDisplay; W: TWindow; |
| 68 |
WindowName: PXTextProperty; IconName: PXTextProperty; |
| 69 |
NormalHints: PXSizeHints; WMHints: PXWMHints; ClassHints: PXClassHint); |
| 70 |
|
| 71 |
{ ---------------------------------------------------------------------------- } |
| 72 |
{ @section(xlib.h (XLibDLL)) } |
| 73 |
|
| 74 |
{ } |
| 75 |
function XParseGeometry(parsestring: PChar; x_return, y_return: PInteger; |
| 76 |
width_return, height_return: PUInt32): integer; cdecl; external XlibDLL; |
| 77 |
|
| 78 |
{ ---------------------------------------------------------------------------- } |
| 79 |
{ @section(Xmu/StdCmap.h (XmuDLL)) } |
| 80 |
|
| 81 |
{ } |
| 82 |
function XmuLookupStandardColormap(dpy: PDisplay; screen: integer; |
| 83 |
AVisualid: TVisualID; depth: UInt32; AProperty: TAtom; replace, retain: XBool) |
| 84 |
:TStatus; cdecl; external XmuDLL; |
| 85 |
|
| 86 |
implementation |
| 87 |
|
| 88 |
type |
| 89 |
TPCharArray = packed array[0..High(Word)]of PChar; |
| 90 |
PPCharArray=^TPCharArray; |
| 91 |
|
| 92 |
procedure CreateArgCV(out argc_ret: Longint; out argv_ret: PPChar); |
| 93 |
{ Na podstawie ParamCount i ParamStr konstruujemy argc i argv |
| 94 |
aby udawaly parametry main () z ANSI C. |
| 95 |
Zawsze zwalniaj potem z pamieci argv przez DestroyArgV. } |
| 96 |
var i: Integer; |
| 97 |
argv: PPCharArray absolute argv_ret; |
| 98 |
begin |
| 99 |
argc_ret := ParamCount+1; |
| 100 |
GetMem(Pointer(argv),(argc_ret+1)*SizeOf(PChar)); |
| 101 |
for i := 0 to ParamCount do |
| 102 |
argv^[i] := StrNew(PChar(ParamStr(i))); { KOPIUJEMY ParamStr do nowego PChara- tak najbezpieczniej } |
| 103 |
argv^[argc_ret] := nil; { ostatni element tablicy argv[] powinien byc ustawiony na nil } |
| 104 |
end; |
| 105 |
|
| 106 |
procedure DestroyArgV(var argv_ret: PPChar); |
| 107 |
var i: integer; |
| 108 |
argv: PPCharArray absolute argv_ret; |
| 109 |
begin |
| 110 |
for i := 0 to ParamCount do StrDispose(argv^[i]); |
| 111 |
FreeMemNiling(Pointer(argv)); |
| 112 |
end; |
| 113 |
|
| 114 |
function XSetStandardProperties_Pascal(dpy: PDisplay; win: TWindow; |
| 115 |
window_name: pchar; icon_name: pchar;icon_pixmap: TPixmap; |
| 116 |
hints: PXSizeHints): integer; |
| 117 |
{ simplified version of XSetStandardProperties } |
| 118 |
var argc: Longint; |
| 119 |
argv: PPChar; |
| 120 |
begin |
| 121 |
CreateArgCV(argc, argv); |
| 122 |
result := XSetStandardProperties(dpy, win, window_name, icon_name, |
| 123 |
icon_pixmap, argv, argc, hints); |
| 124 |
DestroyArgV(argv); |
| 125 |
end; |
| 126 |
|
| 127 |
procedure XSetWMProperties_Pascal(Display: PDisplay; W: TWindow; |
| 128 |
WindowName: PXTextProperty; |
| 129 |
IconName: PXTextProperty; NormalHints: PXSizeHints; WMHints: |
| 130 |
PXWMHints; ClassHints: PXClassHint); |
| 131 |
var argc: Longint; |
| 132 |
argv: PPChar; |
| 133 |
begin |
| 134 |
CreateArgCV(argc, argv); |
| 135 |
XSetWMProperties(Display, W, WindowName, IconName, argv, argc, NormalHints, WMHints, ClassHints); |
| 136 |
DestroyArgV(argv); |
| 137 |
end; |
| 138 |
|
| 139 |
{ some mine things ------------------------------------------------------------ } |
| 140 |
|
| 141 |
function XlibErrorHandler_RaiseEXlibError(display: PDisplay; error: PXErrorEvent) |
| 142 |
:integer; cdecl; |
| 143 |
{ rzuca wyjatek EXlibError z odpowiednio skonstruowanym Text'em. |
| 144 |
Ta funkcja jest dobra aby ja zarejestrowac przez XSetErrorHandler. } |
| 145 |
var error_name_buf, major_request_name_buf :array[0..1023]of char; |
| 146 |
s: string; |
| 147 |
begin |
| 148 |
XGetErrorText(display, error^.error_code, @error_name_buf, SizeOf(error_name_buf)); |
| 149 |
XGetErrorDatabaseText(display, 'XRequest', PChar(IntToStr(error^.request_code)), |
| 150 |
'(not found in X database)', |
| 151 |
@major_request_name_buf, SizeOf(major_request_name_buf)); |
| 152 |
|
| 153 |
s := Format('Xlib error ''%s'' (%d) at request ''%s'' (%d)', |
| 154 |
[PChar(@error_name_buf), error^.error_code, |
| 155 |
PChar(@major_request_name_buf), error^.request_code]); |
| 156 |
|
| 157 |
if error^.minor_code <> 0 then |
| 158 |
begin |
| 159 |
|
| 160 |
{ TODO: jak zrobic ponizsze ? Skad wziac ExtensionName ?? Wiem ze ono jest |
| 161 |
zakodowane w request_code. } |
| 162 |
{ XGetErrorDatabaseText(display, 'XRequest', |
| 163 |
PChar(ExtensionName +'.' +IntToStr(error.minor_code)), |
| 164 |
'(not found in X database)', |
| 165 |
@ext_request_name_buf, SizeOf(ext_request_name_buf)); |
| 166 |
s += Format(' (extension request ''%s'' (%d)', |
| 167 |
[PChar(@ext_request_name_buf), error.minor_code]); |
| 168 |
} |
| 169 |
s += Format(' (extension request (%d)', [error^.minor_code]); |
| 170 |
end; |
| 171 |
|
| 172 |
raise EXlibError.Create(s); |
| 173 |
|
| 174 |
result := 0; { this instruction is unreachable, it is only to avoid compiler warnings } |
| 175 |
end; |
| 176 |
|
| 177 |
var oldXLibErrorHandler: TXErrorHandler; |
| 178 |
initialization |
| 179 |
oldXLibErrorHandler := XSetErrorHandler(@XlibErrorHandler_RaiseEXlibError); |
| 180 |
finalization |
| 181 |
XSetErrorHandler(oldXLibErrorHandler); |
| 182 |
end. |