| 1 |
{%MainUnit ../castlewindow.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ CastleWindow backend using Xlib |
| 18 |
and glX (for desktop OpenGL) or EGL (for OpenGL ES). } |
| 19 |
|
| 20 |
{$I castlewindow_dialogs_by_messages.inc} |
| 21 |
|
| 22 |
{$ifdef USE_EGL} |
| 23 |
{$I castlewindow_egl.inc} |
| 24 |
{$else} |
| 25 |
{$I castlewindow_glx.inc} |
| 26 |
{$endif} |
| 27 |
|
| 28 |
{$ifdef read_interface_uses} |
| 29 |
Xlib, CastleInternalXlib, XUtil, X, KeySym, CursorFont, |
| 30 |
{$endif} |
| 31 |
|
| 32 |
{$ifdef read_window_interface} |
| 33 |
private |
| 34 |
windowXID: TWindow; |
| 35 |
parentwinXID: TWindow; { refreshed on ReparentNotify, needed for X_Configureevent } |
| 36 |
{$ifndef USE_EGL} |
| 37 |
Colormap: TColormap; |
| 38 |
{$endif} |
| 39 |
|
| 40 |
function HandleXEvent(const Event: TXEvent): boolean; |
| 41 |
procedure UpdateCursor; |
| 42 |
{$endif read_window_interface} |
| 43 |
|
| 44 |
{$ifdef read_application_interface} |
| 45 |
private |
| 46 |
FXDisplayName: String; |
| 47 |
FXScreen: Integer; |
| 48 |
InitializeXDisplayDone: boolean; |
| 49 |
XCursors: array [TMouseCursor] of TCursor; |
| 50 |
FXDisplay: PDisplay; |
| 51 |
|
| 52 |
{ Used to capture "quit window" message. Initialized in InitializeXDisplay. } |
| 53 |
WMDeleteWindow: TAtom; |
| 54 |
{ Arrays for fast conversion between X keysyms (cut off to least significant |
| 55 |
byte) to our TKey values. The most significant byte of X keysym determines |
| 56 |
which array to use. } |
| 57 |
KeySym0ToKey, KeySymFFToKey: TKeysBytes; |
| 58 |
{ KeySymToKey; zamienia ksym na TKey, zwraca keyNone jezeli |
| 59 |
nie dalo sie skonwertowac (bo moze ten keysym nie ma odpowiednika |
| 60 |
w naszym malym TKey ? a moze ksym byl = NoSymbol ?) } |
| 61 |
function KeySymToKey(ksym: TKeySym): TKey; |
| 62 |
{ Seek on OpenWindows window with WindowXID = seekWindowXID. |
| 63 |
Returns nil if not found. } |
| 64 |
function FindWindowXID(seekWindowXID: TWindow): TCastleWindow; |
| 65 |
|
| 66 |
{ Initialize and check X display. |
| 67 |
Call this once you're sure that XDisplayName is set to |
| 68 |
a value desired by user (so, after processing --display command-line |
| 69 |
option), and only when you're sure you need to connect to the X server |
| 70 |
(to open a window, or query Application.ScreenWidth / ScreenHeight). |
| 71 |
|
| 72 |
This initializes and checks some things determined by the X display: |
| 73 |
|
| 74 |
@unorderedList( |
| 75 |
@item initializes XScreen, |
| 76 |
@item checks for glX extension, |
| 77 |
@item gets WM_DELETE_WINDOW. |
| 78 |
) } |
| 79 |
procedure InitializeXDisplay; |
| 80 |
|
| 81 |
public |
| 82 |
function HandleXEvent(Event: TXEvent): boolean; |
| 83 |
|
| 84 |
{ X display name used to communicate with X server (to open windows, |
| 85 |
query screen size and so on). } |
| 86 |
property XDisplayName: String read FXDisplayName write FXDisplayName; |
| 87 |
property XDisplay: PDisplay read FXDisplay; |
| 88 |
property XScreen: Integer read FXScreen; |
| 89 |
{$endif read_application_interface} |
| 90 |
|
| 91 |
{$ifdef read_implementation} |
| 92 |
|
| 93 |
const |
| 94 |
AcceptedEventsMask = ExposureMask or StructureNotifyMask or |
| 95 |
KeyPressMask or KeyReleaseMask {key events} or |
| 96 |
ButtonPressMask or ButtonReleaseMask or PointerMotionMask {mouse events} or |
| 97 |
FocusChangeMask; |
| 98 |
|
| 99 |
{ TCastleWindow -------------------------------------------------------- } |
| 100 |
|
| 101 |
procedure TCastleWindow.OpenBackend; |
| 102 |
|
| 103 |
procedure InitializeDpi; |
| 104 |
var |
| 105 |
DW, DWMM: Integer; |
| 106 |
NewDpi: Single; |
| 107 |
begin |
| 108 |
DW := XDisplayWidth(Application.XDisplay, Application.XScreen); |
| 109 |
DWMM := XDisplayWidthMM(Application.XDisplay, Application.XScreen); |
| 110 |
NewDpi := DW / (DWMM / 25.4); |
| 111 |
|
| 112 |
// Too verbose by default, UpdateUIScale will show it if UIScaling is usDpiScale. |
| 113 |
// WritelnLog('X11', 'Dots (pixels) per inch is %f (%f * default %f)', |
| 114 |
// [NewDpi, NewDpi / DefaultDpi, DefaultDpi]); |
| 115 |
Container.Dpi := NewDpi; |
| 116 |
end; |
| 117 |
|
| 118 |
var |
| 119 |
AttrValueMask: Cardinal; |
| 120 |
Attr: TXSetWindowAttributes; |
| 121 |
CorrectLeft, CorrectTop, CorrectWidth, CorrectHeight: Integer; |
| 122 |
sizeHints: TXSizeHints; |
| 123 |
WindTitleProp: TXTextProperty; |
| 124 |
ClassHint: TXClassHint; |
| 125 |
WMHints: TXWMHints; |
| 126 |
PWindTitle: PChar; |
| 127 |
const |
| 128 |
AllPointerEventsMask = ButtonPressMask or ButtonReleaseMask or |
| 129 |
EnterWindowMask or LeaveWindowMask or PointerMotionMask; |
| 130 |
begin |
| 131 |
Application.InitializeXDisplay; |
| 132 |
|
| 133 |
Attr.event_mask := AcceptedEventsMask; |
| 134 |
Attr.border_pixel := 0; |
| 135 |
AttrValueMask := CWBorderPixel or CWEventMask; |
| 136 |
|
| 137 |
parentwinXID := XRootWindow(Application.XDisplay, Application.XScreen); |
| 138 |
|
| 139 |
{$ifndef USE_EGL} |
| 140 |
{ Creates XVisual } |
| 141 |
ContextCreateEarly; |
| 142 |
|
| 143 |
Colormap := XCreateColormap(Application.XDisplay, ParentWinXID, |
| 144 |
XVisual^.Visual, AllocNone); |
| 145 |
Attr.Colormap := Colormap; |
| 146 |
AttrValueMask := AttrValueMask or CWColormap; |
| 147 |
|
| 148 |
ContextCreate; |
| 149 |
{$endif} |
| 150 |
|
| 151 |
if FullScreen then |
| 152 |
begin |
| 153 |
Attr.override_redirect := XBool_true; |
| 154 |
AttrValueMask := AttrValueMask or CWOverrideRedirect; |
| 155 |
CorrectLeft := 0; |
| 156 |
CorrectTop := 0; |
| 157 |
CorrectWidth := Application.ScreenWidth; |
| 158 |
CorrectHeight := Application.ScreenHeight; |
| 159 |
end else |
| 160 |
begin |
| 161 |
CorrectLeft := Left; |
| 162 |
CorrectTop := Top; |
| 163 |
CorrectWidth := Width; |
| 164 |
CorrectHeight := Height; |
| 165 |
end; |
| 166 |
|
| 167 |
windowXID := XCreateWindow(Application.XDisplay, parentwinXID, |
| 168 |
CorrectLeft, CorrectTop, CorrectWidth, CorrectHeight, 0, |
| 169 |
{$ifndef USE_EGL} XVisual^.depth {$else} CopyFromParent {$endif}, |
| 170 |
InputOutput, |
| 171 |
{$ifndef USE_EGL} XVisual^.visual {$else} PVisual(CopyFromParent) {$endif}, |
| 172 |
AttrValueMask, @Attr); |
| 173 |
|
| 174 |
if FullScreen and Visible then |
| 175 |
begin |
| 176 |
{ XMapWindow must be called before doing all the "grab" operations. |
| 177 |
This also means that this whole piece of code cannot be done |
| 178 |
when Visible = @false. } |
| 179 |
XMapWindow(Application.XDisplay, windowXID); |
| 180 |
check( XGrabKeyboard(Application.XDisplay, windowXID, XBool_True, GrabModeAsync, GrabModeAsync, |
| 181 |
CurrentTime) = GrabSuccess, 'cannot grab the keyboard'); |
| 182 |
check( XGrabPointer(Application.XDisplay, windowXID, XBool_True, |
| 183 |
AcceptedEventsMask and AllPointerEventsMask, |
| 184 |
GrabModeAsync, GrabModeAsync, windowXID, None, CurrentTime) = GrabSuccess, |
| 185 |
'cannot grab the pointer'); |
| 186 |
|
| 187 |
{ In FullScreen we don't use SizeHints. |
| 188 |
We don't need them anyway, as in fullscreen the window can never by resized. |
| 189 |
|
| 190 |
Also, in FullScreen we do not use WMDeleteWindow that allows to implement |
| 191 |
OnCloseQuery. But in FullScreen (with override_redirect) user cannot |
| 192 |
tell the window manager "I want to close this window", |
| 193 |
there are no window decorations and all keys (including Alt+F4) are grabbed. |
| 194 |
So we don't need WMDeleteWindow anyway. |
| 195 |
|
| 196 |
TODO: |
| 197 |
- Implement FullScreen without grabbing the keys and override_redirect. |
| 198 |
- When this is done, WMDeleteWindow should be used with fullscreen too. } |
| 199 |
end else |
| 200 |
begin |
| 201 |
{ only set window title and handle wm_delete_events if in windowed mode } |
| 202 |
XSetWMProtocols(Application.XDisplay, windowXID, @Application.WMDeleteWindow, 1); |
| 203 |
|
| 204 |
{ ready sizeHints structure } |
| 205 |
sizeHints.x := CorrectLeft; |
| 206 |
sizeHints.y := CorrectTop; |
| 207 |
sizeHints.width := CorrectWidth; |
| 208 |
sizeHints.height := CorrectHeight; |
| 209 |
sizeHints.flags := USSize or USPosition; |
| 210 |
if ResizeAllowed = raAllowed then |
| 211 |
begin |
| 212 |
sizeHints.min_width := minWidth; |
| 213 |
sizeHints.min_height := minHeight; |
| 214 |
sizeHints.max_width := maxWidth; |
| 215 |
sizeHints.max_height := maxHeight; |
| 216 |
end else |
| 217 |
begin |
| 218 |
sizeHints.min_width := CorrectWidth; |
| 219 |
sizeHints.min_height := CorrectHeight; |
| 220 |
sizeHints.max_width := CorrectWidth; |
| 221 |
sizeHints.max_height := CorrectHeight; |
| 222 |
end; |
| 223 |
sizeHints.flags := sizeHints.flags or PMinSize or PMaxSize; |
| 224 |
|
| 225 |
{ ready WMHints, ClassHints } |
| 226 |
WMHints.flags := InputHint; |
| 227 |
WMHints.input := XBool_True; |
| 228 |
ClassHint.res_name := StrNew(PChar(ApplicationName)); |
| 229 |
ClassHint.res_class := StrNew(PChar(ApplicationName)); |
| 230 |
|
| 231 |
{ ready WindTitle } |
| 232 |
PWindTitle := PChar(GetWholeCaption); |
| 233 |
Check( XStringListToTextProperty(@PWindTitle, 1, @WindTitleProp) <> 0, 'not enough memory for XStringListToTextProperty'); |
| 234 |
|
| 235 |
{ use prepared WindTitleProp, Size/WM/ClassHints to give a lot of |
| 236 |
hints to WindowManager (that will hopefully honour them...) } |
| 237 |
XSetWMProperties_Pascal(Application.XDisplay, windowXID, @WindTitleProp, @WindTitleProp, |
| 238 |
@sizeHints, @WMHints, @ClassHint); |
| 239 |
|
| 240 |
{ realease allocated memory for structs for XSetWMProperties } |
| 241 |
XFree(WindTitleProp.value); |
| 242 |
StrDispose(ClassHint.res_name); |
| 243 |
StrDispose(ClassHint.res_class); |
| 244 |
|
| 245 |
if Visible then |
| 246 |
XMapWindow(Application.XDisplay, windowXID); |
| 247 |
end; |
| 248 |
|
| 249 |
{$ifdef USE_EGL} |
| 250 |
{ In case of EGL we need WindowXID. } |
| 251 |
ContextCreate(windowXID); |
| 252 |
{$endif} |
| 253 |
|
| 254 |
{ applying FullScreen changes to the Left / Top / Width / Height } |
| 255 |
if FullScreen then |
| 256 |
begin |
| 257 |
FLeft := 0; |
| 258 |
FTop := 0; |
| 259 |
DoResize(Application.ScreenWidth, Application.ScreenHeight, false); |
| 260 |
end; |
| 261 |
|
| 262 |
UpdateCursor; |
| 263 |
|
| 264 |
Application.OpenWindowsAdd(Self); |
| 265 |
|
| 266 |
MakeCurrent; |
| 267 |
InitializeDpi; |
| 268 |
end; |
| 269 |
|
| 270 |
procedure TCastleWindow.CloseBackend; |
| 271 |
var |
| 272 |
dummy_event: TXEvent; |
| 273 |
begin |
| 274 |
ContextDestroy; |
| 275 |
|
| 276 |
if FFullScreenBackend and Visible then |
| 277 |
begin |
| 278 |
XUnGrabPointer(Application.XDisplay, CurrentTime); |
| 279 |
XUnGrabKeyboard(Application.XDisplay, CurrentTime); |
| 280 |
end; |
| 281 |
|
| 282 |
{ czyscimy kolejke komunikatow dla tego okienka. |
| 283 |
To nie daje nam absolutnej gwarancji ze do naszej kolejki nie trafia |
| 284 |
nigdy komunikaty juz zamknietych okienek bo przeciez messsage queue |
| 285 |
jest wypelniana w innym procesie wiec my mozemy wyczyscic a tu |
| 286 |
zaraz potem cos nam wpadnie. Wiec i tak w Application.ProcessMessage |
| 287 |
musimy sprawdzic czy dostalismy message dla istniejacego okienka. |
| 288 |
ALE ponizsze moze nam zaoszczedzic sporo czasu w ProcessMessage |
| 289 |
bo mimo wszystko zapewne zazwyczaj oczywsci kolejke z wiekszosci |
| 290 |
smieci dla tego okienka. } |
| 291 |
while XBool(XCheckWindowEvent( |
| 292 |
Application.XDisplay, windowXID, Longint($FFFFFFFF), @dummy_event)) <> XBool_False do ; |
| 293 |
|
| 294 |
if windowXID <> None then |
| 295 |
begin |
| 296 |
XDestroyWindow(Application.XDisplay, windowXID); |
| 297 |
windowXID := None |
| 298 |
end; |
| 299 |
|
| 300 |
{$ifndef USE_EGL} |
| 301 |
if Colormap <> None then |
| 302 |
begin |
| 303 |
XFreeColormap(Application.XDisplay, Colormap); |
| 304 |
Colormap := None; |
| 305 |
end; |
| 306 |
|
| 307 |
ContextDestroyLate; |
| 308 |
{$endif} |
| 309 |
end; |
| 310 |
|
| 311 |
procedure TCastleWindow.SetCursor(const Value: TMouseCursor); |
| 312 |
begin |
| 313 |
if FCursor <> Value then |
| 314 |
begin |
| 315 |
FCursor := Value; |
| 316 |
if not Closed then |
| 317 |
UpdateCursor; |
| 318 |
end; |
| 319 |
end; |
| 320 |
|
| 321 |
procedure TCastleWindow.UpdateCursor; |
| 322 |
|
| 323 |
function CreateNoneCursor: TCursor; |
| 324 |
var |
| 325 |
bm_no: TPixmap; |
| 326 |
cmap: TColormap; |
| 327 |
black, dummy: TXColor; |
| 328 |
const |
| 329 |
bm_no_data: array[0..7] of Byte = (0, 0, 0, 0, 0, 0, 0, 0); |
| 330 |
begin |
| 331 |
{ Based on [http://www.linuxforums.org/forum/linux-programming-scripting/59012-xlib-hide-mouse-pointer.html] } |
| 332 |
cmap := DefaultColormap(Application.XDisplay, DefaultScreen(Application.XDisplay)); |
| 333 |
XAllocNamedColor(Application.XDisplay, cmap, 'black', @black, @dummy); |
| 334 |
try |
| 335 |
bm_no := XCreateBitmapFromData(Application.XDisplay, WindowXID, @bm_no_data, 8, 8); |
| 336 |
try |
| 337 |
Result := XCreatePixmapCursor(Application.XDisplay, bm_no, bm_no, @black, @black, 0, 0); |
| 338 |
finally |
| 339 |
if bm_no <> None then |
| 340 |
XFreePixmap(Application.XDisplay, bm_no); |
| 341 |
end; |
| 342 |
finally |
| 343 |
XFreeColors(Application.XDisplay, cmap, @black.pixel, 1, 0); |
| 344 |
end; |
| 345 |
end; |
| 346 |
|
| 347 |
const |
| 348 |
XCursorShapeFromCastle: array [mcStandard .. High(TMouseCursor)] of UInt32 = |
| 349 |
( XC_left_ptr, // mcStandard |
| 350 |
XC_watch, // mcWait |
| 351 |
XC_xterm, // mcText |
| 352 |
XC_hand2, // mcHand |
| 353 |
XC_sb_v_double_arrow, // mcResizeVertical |
| 354 |
XC_sb_h_double_arrow, // mcResizeHorizontal |
| 355 |
XC_top_left_corner, // mcResizeTopLeft |
| 356 |
XC_top_side, // mcResizeTop |
| 357 |
XC_top_right_corner, // mcResizeTopRight |
| 358 |
XC_left_side, // mcResizeLeft |
| 359 |
XC_right_side, // mcResizeRight |
| 360 |
XC_bottom_left_corner, // mcResizeBottomLeft |
| 361 |
XC_bottom_side, // mcResizeBottom |
| 362 |
XC_bottom_right_corner // mcResizeBottomRight |
| 363 |
); |
| 364 |
begin |
| 365 |
if InternalCursor = mcDefault then |
| 366 |
begin |
| 367 |
XUndefineCursor(Application.XDisplay, WindowXID); |
| 368 |
end else |
| 369 |
begin |
| 370 |
if Application.XCursors[InternalCursor] = X.None then |
| 371 |
begin |
| 372 |
{ initialize XCursors[InternalCursor] } |
| 373 |
if InternalCursor in [mcNone, mcForceNone] then |
| 374 |
Application.XCursors[InternalCursor] := CreateNoneCursor |
| 375 |
else |
| 376 |
Application.XCursors[InternalCursor] := XCreateFontCursor( |
| 377 |
Application.XDisplay, XCursorShapeFromCastle[InternalCursor]); |
| 378 |
end; |
| 379 |
|
| 380 |
XDefineCursor(Application.XDisplay, WindowXID, Application.XCursors[InternalCursor]); |
| 381 |
end; |
| 382 |
end; |
| 383 |
|
| 384 |
procedure TCastleWindow.SetMousePosition(const Value: TVector2); |
| 385 |
var |
| 386 |
P: TVector2Integer; |
| 387 |
begin |
| 388 |
if not Closed then |
| 389 |
begin |
| 390 |
|
| 391 |
{ Do not set Mouse.CursorPos to the same value, to make sure we don't cause |
| 392 |
unnecessary OnMotion on some systems while actual MousePosition didn't change. } |
| 393 |
if TVector2.PerfectlyEquals(Value, FMousePosition) then Exit; |
| 394 |
|
| 395 |
P := CastleToLeftTopInt(Value); |
| 396 |
XWarpPointer(Application.XDisplay, X.None, WindowXID, 0, 0, 0, 0, P.X, P.Y); |
| 397 |
end; |
| 398 |
end; |
| 399 |
|
| 400 |
procedure TCastleWindow.CreateBackend; |
| 401 |
begin |
| 402 |
windowXID := None; |
| 403 |
|
| 404 |
{ We don't do here anything depending on X server, |
| 405 |
and thus this doesn't require calling Application.InitializeXDisplay. |
| 406 |
This way, merely calling TCastleWindow.Create |
| 407 |
doesn't require X server runnning. } |
| 408 |
end; |
| 409 |
|
| 410 |
procedure TCastleWindow.SetCaption(const Part: TCaptionPart; const Value: String); |
| 411 |
var |
| 412 |
PValue: PChar; |
| 413 |
WindTitleProp: TXTextProperty; |
| 414 |
begin |
| 415 |
FCaption[Part] := Value; |
| 416 |
|
| 417 |
if windowXID <> None then |
| 418 |
begin |
| 419 |
{ create, use, free WindTitleProp } |
| 420 |
PValue := PChar(GetWholeCaption); |
| 421 |
Check( XStringListToTextProperty(@PValue, 1, @WindTitleProp) <> 0, |
| 422 |
'not enough memory for XStringListToTextProperty'); |
| 423 |
XSetTextProperty(Application.XDisplay, windowXID, @WindTitleProp, XInternAtom(Application.XDisplay, 'WM_NAME', XBool_True) ); |
| 424 |
XFree(WindTitleProp.value); |
| 425 |
end; |
| 426 |
end; |
| 427 |
|
| 428 |
function TCastleWindow.RedirectKeyDownToMenuClick: boolean; |
| 429 |
begin |
| 430 |
Result := true; |
| 431 |
end; |
| 432 |
|
| 433 |
procedure TCastleWindow.BackendMenuInitialize; |
| 434 |
begin |
| 435 |
{ does nothing } |
| 436 |
end; |
| 437 |
|
| 438 |
procedure TCastleWindow.BackendMenuFinalize; |
| 439 |
begin |
| 440 |
{ does nothing } |
| 441 |
end; |
| 442 |
|
| 443 |
procedure TCastleWindow.MenuUpdateCaption(Entry: TMenuEntryWithCaption); |
| 444 |
begin |
| 445 |
{ does nothing } |
| 446 |
end; |
| 447 |
|
| 448 |
procedure TCastleWindow.MenuUpdateEnabled(Entry: TMenuEntryWithCaption); |
| 449 |
begin |
| 450 |
{ does nothing } |
| 451 |
end; |
| 452 |
|
| 453 |
procedure TCastleWindow.MenuUpdateChecked(Entry: TMenuItemChecked); |
| 454 |
begin |
| 455 |
{ does nothing } |
| 456 |
end; |
| 457 |
|
| 458 |
function TCastleWindow.MenuUpdateCheckedFast: boolean; |
| 459 |
begin |
| 460 |
Result := true; // MenuUpdateChecked does nothing, so yes, it's fast :) |
| 461 |
end; |
| 462 |
|
| 463 |
procedure TCastleWindow.MenuInsert(const Parent: TMenu; |
| 464 |
const ParentPosition: Integer; const Entry: TMenuEntry); |
| 465 |
begin |
| 466 |
{ does nothing } |
| 467 |
end; |
| 468 |
|
| 469 |
procedure TCastleWindow.MenuDelete(const Parent: TMenu; |
| 470 |
const ParentPosition: Integer; const Entry: TMenuEntry); |
| 471 |
begin |
| 472 |
{ does nothing } |
| 473 |
end; |
| 474 |
|
| 475 |
function TCastleWindow.HandleXEvent(const Event: TXEvent): boolean; |
| 476 |
|
| 477 |
procedure X_KeyPress(const xEvent: TXKeyEvent); |
| 478 |
var |
| 479 |
k: TKeySym; |
| 480 |
CharKey: char; |
| 481 |
EventString: String; |
| 482 |
Key: TKey; |
| 483 |
begin |
| 484 |
{ calculate k, CharKey } |
| 485 |
if XLookupString(@xEvent, @CharKey, 1, @k, nil) = 0 then |
| 486 |
EventString := '' |
| 487 |
else |
| 488 |
EventString := CharKey; |
| 489 |
|
| 490 |
{ maybe do DoKeyDown } |
| 491 |
case k of |
| 492 |
XK_Shift_L: SetPrivateModifiersDown(mkShift, false, true); |
| 493 |
XK_Shift_R: SetPrivateModifiersDown(mkShift, true, true); |
| 494 |
XK_Control_L: SetPrivateModifiersDown(mkCtrl, false, true); |
| 495 |
XK_Control_R: SetPrivateModifiersDown(mkCtrl, true, true); |
| 496 |
XK_Alt_L: SetPrivateModifiersDown(mkAlt, false, true); |
| 497 |
XK_Alt_R: SetPrivateModifiersDown(mkAlt, true, true); |
| 498 |
else begin |
| 499 |
Key := Application.KeySymToKey(k); |
| 500 |
if (Key <> keyNone) or (EventString <> '') then DoKeyDown(Key, EventString); |
| 501 |
end; |
| 502 |
end; |
| 503 |
end; |
| 504 |
|
| 505 |
procedure X_KeyRelease(const xEvent: TXKeyEvent); |
| 506 |
var |
| 507 |
k: TKeySym; |
| 508 |
CharKey: char; //we don't use it further anywhere |
| 509 |
Key: TKey; |
| 510 |
begin |
| 511 |
{ Note: "XLib Progr Manual" warns that KeyRelease is not guaranteed on |
| 512 |
"some very old systems". I didn't ever see such system... |
| 513 |
Anyway, in such case we can't make DoKeyUp, OnKeyUp, all Keys will remain |
| 514 |
pressed forever until window unfocused... } |
| 515 |
|
| 516 |
{ calculate k, CharKey } |
| 517 |
XLookupString(@xEvent, @CharKey, 1, @k, nil); |
| 518 |
|
| 519 |
{ maybe do DoKeyUp } |
| 520 |
case k of |
| 521 |
XK_Shift_L: SetPrivateModifiersDown(mkShift, false, false); |
| 522 |
XK_Shift_R: SetPrivateModifiersDown(mkShift, true, false); |
| 523 |
XK_Control_L: SetPrivateModifiersDown(mkCtrl, false, false); |
| 524 |
XK_Control_R: SetPrivateModifiersDown(mkCtrl, true, false); |
| 525 |
XK_Alt_L: SetPrivateModifiersDown(mkAlt, false, false); |
| 526 |
XK_Alt_R: SetPrivateModifiersDown(mkAlt, true, false); |
| 527 |
else begin |
| 528 |
Key := Application.KeySymToKey(k); |
| 529 |
if Key <> keyNone then DoKeyUp(Key); |
| 530 |
end; |
| 531 |
end; |
| 532 |
end; |
| 533 |
|
| 534 |
procedure X_ConfigureNotify(const xEvent: TXConfigureEvent); |
| 535 |
var |
| 536 |
dummy_win: TWindow; |
| 537 |
begin |
| 538 |
{ event.x , event.y are relative to our parent. |
| 539 |
We always create our window giving XRootWindow(Application.XDisplay, Application.XScreen) |
| 540 |
as parent and we want our fLeft, fTop to be relitive to that |
| 541 |
window. BUT window managers may redirect our window and put |
| 542 |
it in some thick border, for example. Than this border is our parent |
| 543 |
and our position is given relative to that border. But we want it |
| 544 |
to be relative to XRootWindow(...) to be useful for future window |
| 545 |
creation !! So we translate coords from our parentwinXID to |
| 546 |
XRootWindow(...) } |
| 547 |
XTranslateCoordinates(Application.XDisplay, |
| 548 |
parentwinXID, XRootWindow(Application.XDisplay, Application.XScreen), |
| 549 |
{event.x, event.y,}0, 0, @fLeft, @fTop, @dummy_win); |
| 550 |
|
| 551 |
{ TODO: for now, Left and Top indicate corner of the window *with frame* |
| 552 |
(while they should indicate corner of GL area only, according to interface). } |
| 553 |
DoResize(xEvent.width, xEvent.height, false); |
| 554 |
end; |
| 555 |
|
| 556 |
function xbtnToMouseButton(button: Cardinal; var mbtn: TCastleMouseButton): boolean; |
| 557 |
begin |
| 558 |
result := true; |
| 559 |
case button of |
| 560 |
Button1 : mbtn := buttonLeft; |
| 561 |
Button2 : mbtn := buttonMiddle; |
| 562 |
Button3 : mbtn := buttonRight; |
| 563 |
else Result := false; |
| 564 |
end; |
| 565 |
end; |
| 566 |
|
| 567 |
procedure X_ButtonPress(const xEvent: TXButtonPressedEvent); |
| 568 |
var |
| 569 |
btn: TCastleMouseButton; |
| 570 |
begin |
| 571 |
if xbtnToMouseButton(xEvent.button, btn) then |
| 572 |
DoMouseDown(LeftTopToCastle(xEvent.x, xEvent.y), btn, 0) else |
| 573 |
case xEvent.Button of |
| 574 |
Button4: DoMouseWheel( 1, true); |
| 575 |
Button5: DoMouseWheel(-1, true); |
| 576 |
6: DoMouseWheel( 1, false); { not sure if this is possible } |
| 577 |
7: DoMouseWheel(-1, false); { not sure if this is possible } |
| 578 |
end; |
| 579 |
end; |
| 580 |
|
| 581 |
procedure X_ButtonRelease(const xEvent: TXButtonReleasedEvent); |
| 582 |
var |
| 583 |
btn: TCastleMouseButton; |
| 584 |
begin |
| 585 |
if xbtnToMouseButton(xEvent.button, btn) then |
| 586 |
DoMouseUp(LeftTopToCastle(xEvent.x, xEvent.y), btn, 0) |
| 587 |
end; |
| 588 |
|
| 589 |
procedure X_MotionNotify(const xEvent: TXMotionEvent); |
| 590 |
begin |
| 591 |
{ We could refresh MousePressed now. But no need for now? |
| 592 |
mousePressed:=[]; |
| 593 |
if (Button1Mask and event.state) <> 0 then Include(mousePressed, buttonLeft); |
| 594 |
if (Button2Mask and event.state) <> 0 then Include(mousePressed, buttonMiddle); |
| 595 |
if (Button3Mask and event.state) <> 0 then Include(mousePressed, buttonRight); |
| 596 |
} |
| 597 |
DoMotion(InputMotion(MousePosition, |
| 598 |
LeftTopToCastle(xEvent.x, xEvent.y), |
| 599 |
MousePressed, 0)); |
| 600 |
end; |
| 601 |
|
| 602 |
procedure X_FocusOut(const xEvent: TXFocusOutEvent); |
| 603 |
begin |
| 604 |
{ Clear KeysDown and MousePressed information. |
| 605 |
Otherwise, we could miss the keyup or mouseup event, if user pressed |
| 606 |
the key/mouse with our window in focus, then (holding the key/mouse) |
| 607 |
switched to other window. } |
| 608 |
ReleaseAllKeysAndMouse; |
| 609 |
end; |
| 610 |
|
| 611 |
begin |
| 612 |
Result := true; |
| 613 |
case Event._type of |
| 614 |
Expose: { XExposeEvent } |
| 615 |
if Event.xexpose.count = 0 then Invalidate; |
| 616 |
(*MapNotify: needRedisplay := true; { XMapEvent } <- unused *) |
| 617 |
KeyPress: X_KeyPress(Event.xkey); { XKeyEvent } |
| 618 |
KeyRelease: X_KeyRelease(Event.xkey); |
| 619 |
ButtonPress: X_ButtonPress(Event.xbutton); { XButtonPressEvent } |
| 620 |
ButtonRelease: X_ButtonRelease(Event.xbutton); { XButtonReleaseEvent } |
| 621 |
MotionNotify: X_MotionNotify(Event.xmotion); { XPointerMovedEvent } |
| 622 |
ConfigureNotify: X_ConfigureNotify(Event.xconfigure); { XConfigureEvent } |
| 623 |
ClientMessage : { XClientMessageEvent - no corresponding XXxxEvent mask } |
| 624 |
if (event.xclient.data.l[0] = Integer(Application.WMDeleteWindow)) then |
| 625 |
DoCloseQuery; |
| 626 |
ReparentNotify : parentwinXID := Event.xreparent.parent; |
| 627 |
FocusOut: X_FocusOut(Event.xfocus); |
| 628 |
else Result := false; |
| 629 |
end; |
| 630 |
end; |
| 631 |
|
| 632 |
procedure TCastleWindow.UpdateFullScreenBackend; |
| 633 |
begin |
| 634 |
SimpleUpdateFullScreenBackend; |
| 635 |
end; |
| 636 |
|
| 637 |
procedure TCastleWindow.BackendInsideUpdate; |
| 638 |
begin |
| 639 |
end; |
| 640 |
|
| 641 |
{ TCastleApplication ------------------------------------------------------- } |
| 642 |
|
| 643 |
function TCastleApplication.FindWindowXID(seekWindowXID: TWindow): TCastleWindow; |
| 644 |
var |
| 645 |
i: integer; |
| 646 |
begin |
| 647 |
for i := 0 to OpenWindowsCount-1 do |
| 648 |
if OpenWindows[i].WindowXID = seekWindowXID then |
| 649 |
Exit(OpenWindows[i]); |
| 650 |
Result := nil; |
| 651 |
end; |
| 652 |
|
| 653 |
function TCastleApplication.KeySymToKey(KSym: TKeySym): TKey; |
| 654 |
begin |
| 655 |
Result := keyNone; |
| 656 |
if ksym <> NoSymbol then |
| 657 |
case ksym and $FF00 of |
| 658 |
{ Note that KeySym*ToKey values may be keyNone, and so we may return keyNone. } |
| 659 |
$0000: Exit(KeySym0ToKey [ksym and $FF]); |
| 660 |
$FF00: Exit(KeySymFFToKey[ksym and $FF]); |
| 661 |
end; |
| 662 |
end; |
| 663 |
|
| 664 |
procedure TCastleApplication.InitializeXDisplay; |
| 665 |
{$ifdef DARWIN} |
| 666 |
const |
| 667 |
AltXDisplayName = ':0'; |
| 668 |
{$endif} |
| 669 |
begin |
| 670 |
if not InitializeXDisplayDone then |
| 671 |
begin |
| 672 |
{ process FXDisplayName by Xlib.XDisplayName, to make it look nicer } |
| 673 |
FXDisplayName := Xlib.XDisplayName(PCharOrNil(FXDisplayName)); |
| 674 |
FXDisplay := XOpenDisplay(PCharOrNil(FXDisplayName)); |
| 675 |
|
| 676 |
if XDisplay = nil then |
| 677 |
begin |
| 678 |
{$ifdef DARWIN} |
| 679 |
Writeln(ErrOutput, Format('Opening the default X display "%s" failed, retrying with "%s" to try to attach to running X server on macOS.', |
| 680 |
[XDisplayName, AltXDisplayName])); |
| 681 |
|
| 682 |
FXDisplayName := Xlib.XDisplayName(PCharOrNil(AltXDisplayName)); |
| 683 |
FXDisplay := XOpenDisplay(PCharOrNil(FXDisplayName)); |
| 684 |
|
| 685 |
if XDisplay = nil then |
| 686 |
{$endif} |
| 687 |
raise Exception.CreateFmt('XOpenDisplay could not open display "%s"', [XDisplayName]); |
| 688 |
end; |
| 689 |
|
| 690 |
// TODO: Is it OK for a plugin to just get XDefaultScreen below? |
| 691 |
FXScreen := XDefaultScreen(XDisplay); |
| 692 |
WMDeleteWindow := XInternAtom(XDisplay, 'WM_DELETE_WINDOW', XBool_True); |
| 693 |
InitializeXDisplayDone := true; |
| 694 |
end; |
| 695 |
end; |
| 696 |
|
| 697 |
procedure TCastleApplication.CreateBackend; |
| 698 |
const |
| 699 |
{ Under Kylix, capital letters are XK_? and lower are XK_l?. |
| 700 |
Under FPC, ... are XKc_? ... are XK_?. } |
| 701 |
XK_a_lower = {$ifdef DCC} XK_la {$endif} {$ifdef FPC} XK_a {$endif}; |
| 702 |
XK_z_lower = {$ifdef DCC} XK_lz {$endif} {$ifdef FPC} XK_z {$endif}; |
| 703 |
XK_a_upper = {$ifdef DCC} XK_a {$endif} {$ifdef FPC} XKc_a {$endif}; |
| 704 |
XK_z_upper = {$ifdef DCC} XK_z {$endif} {$ifdef FPC} XKc_z {$endif}; |
| 705 |
var |
| 706 |
b: byte; |
| 707 |
begin |
| 708 |
{ See ~/sources/fpc/trunk/packages/x11/src/keysym.pp } |
| 709 |
|
| 710 |
{ initialize KeySym*ToKey } |
| 711 |
for b := Low(b) to High(b) do KeySym0ToKey[b] := keyNone; |
| 712 |
for b := Low(b) to High(b) do KeySymFFToKey[b] := keyNone; |
| 713 |
|
| 714 |
KeySymFFToKey[XK_Page_Up and $FF] := keyPageUp; |
| 715 |
KeySymFFToKey[XK_Page_Down and $FF] := keyPageDown; |
| 716 |
KeySymFFToKey[XK_Home and $FF] := keyHome; |
| 717 |
KeySymFFToKey[XK_End and $FF] := keyEnd; |
| 718 |
KeySymFFToKey[XK_Left and $FF] := keyArrowLeft; |
| 719 |
KeySymFFToKey[XK_Right and $FF] := keyArrowRight; |
| 720 |
KeySymFFToKey[XK_Up and $FF] := keyArrowUp; |
| 721 |
KeySymFFToKey[XK_Down and $FF] := keyArrowDown; |
| 722 |
KeySymFFToKey[XK_Delete and $FF] := keyDelete; |
| 723 |
KeySymFFToKey[XK_Insert and $FF] := keyInsert; |
| 724 |
KeySymFFToKey[XK_BackSpace and $FF] := keyBackSpace; |
| 725 |
KeySymFFToKey[XK_Tab and $FF] := keyTab; |
| 726 |
KeySymFFToKey[XK_KP_Add and $FF] := keyNumpadPlus; |
| 727 |
KeySymFFToKey[XK_KP_Subtract and $FF] := keyNumpadMinus; |
| 728 |
KeySymFFToKey[XK_Escape and $FF] := keyEscape; |
| 729 |
KeySymFFToKey[XK_Return and $FF] := keyEnter; |
| 730 |
KeySymFFToKey[XK_Print and $FF] := keyPrintScreen; |
| 731 |
KeySymFFToKey[XK_Caps_Lock and $FF] := keyCapsLock; |
| 732 |
KeySymFFToKey[XK_Scroll_Lock and $FF] := keyScrollLock; |
| 733 |
KeySymFFToKey[XK_Num_Lock and $FF] := keyNumLock; |
| 734 |
KeySymFFToKey[XK_Pause and $FF] := keyPause; |
| 735 |
|
| 736 |
KeySymFFToKey[XK_KP_0 and $FF] := keyNumpad0; |
| 737 |
KeySymFFToKey[XK_KP_1 and $FF] := keyNumpad1; |
| 738 |
KeySymFFToKey[XK_KP_2 and $FF] := keyNumpad2; |
| 739 |
KeySymFFToKey[XK_KP_3 and $FF] := keyNumpad3; |
| 740 |
KeySymFFToKey[XK_KP_4 and $FF] := keyNumpad4; |
| 741 |
KeySymFFToKey[XK_KP_5 and $FF] := keyNumpad5; |
| 742 |
KeySymFFToKey[XK_KP_6 and $FF] := keyNumpad6; |
| 743 |
KeySymFFToKey[XK_KP_7 and $FF] := keyNumpad7; |
| 744 |
KeySymFFToKey[XK_KP_8 and $FF] := keyNumpad8; |
| 745 |
KeySymFFToKey[XK_KP_9 and $FF] := keyNumpad9; |
| 746 |
KeySymFFToKey[XK_KP_End and $FF] := keyNumpadEnd; |
| 747 |
KeySymFFToKey[XK_KP_Down and $FF] := keyNumpadDown; |
| 748 |
KeySymFFToKey[XK_KP_Next and $FF] := keyNumpadPageDown; |
| 749 |
KeySymFFToKey[XK_KP_Left and $FF] := keyNumpadLeft; |
| 750 |
KeySymFFToKey[XK_KP_Begin and $FF] := keyNumpadBegin; |
| 751 |
KeySymFFToKey[XK_KP_Right and $FF] := keyNumpadRight; |
| 752 |
KeySymFFToKey[XK_KP_Home and $FF] := keyNumpadHome; |
| 753 |
KeySymFFToKey[XK_KP_Up and $FF] := keyNumpadUp; |
| 754 |
KeySymFFToKey[XK_KP_Prior and $FF] := keyNumpadPageUp; |
| 755 |
KeySymFFToKey[XK_KP_Insert and $FF] := keyNumpadInsert; |
| 756 |
KeySymFFToKey[XK_KP_Delete and $FF] := keyNumpadDelete; |
| 757 |
KeySymFFToKey[XK_KP_Enter and $FF] := keyNumpadEnter; |
| 758 |
KeySymFFToKey[XK_KP_Multiply and $FF] := keyNumpadMultiply; |
| 759 |
KeySymFFToKey[XK_KP_Divide and $FF] := keyNumpadDivide; |
| 760 |
|
| 761 |
for b := 0 to 11 do KeySymFFToKey[(XK_F1+b) and $FF] := TKey(Ord(keyF1) + b); |
| 762 |
|
| 763 |
for b := XK_0 to XK_9 do KeySym0ToKey[b] := TKey(b); |
| 764 |
for b := XK_a_lower to XK_z_lower do KeySym0ToKey[b] := TKey(Ord(UpCase(Chr(b)))); |
| 765 |
for b := XK_a_upper to XK_z_upper do KeySym0ToKey[b] := TKey(b); |
| 766 |
KeySym0ToKey[XK_Space] := keySpace; |
| 767 |
KeySym0ToKey[XK_comma] := keyComma; |
| 768 |
KeySym0ToKey[XK_period] := keyPeriod; |
| 769 |
KeySym0ToKey[XK_bracketleft] := keyLeftBracket; |
| 770 |
KeySym0ToKey[XK_bracketright] := keyRightBracket; |
| 771 |
KeySym0ToKey[XK_apostrophe] := keyApostrophe; |
| 772 |
KeySym0ToKey[XK_semicolon] := keySemicolon; |
| 773 |
KeySym0ToKey[XK_slash] := keySlash; |
| 774 |
KeySym0ToKey[XK_grave] := keyBackQuote; |
| 775 |
KeySym0ToKey[XK_minus] := keyMinus; |
| 776 |
KeySym0ToKey[XK_plus] := keyPlus; |
| 777 |
KeySym0ToKey[XK_equal] := keyEqual; |
| 778 |
KeySym0ToKey[XK_backslash] := keyBackSlash; |
| 779 |
end; |
| 780 |
|
| 781 |
procedure TCastleApplication.DestroyBackend; |
| 782 |
var |
| 783 |
C: TMouseCursor; |
| 784 |
begin |
| 785 |
if XDisplay <> nil then |
| 786 |
begin |
| 787 |
for C := Low(C) to High(C) do |
| 788 |
if XCursors[C] <> X.None then |
| 789 |
begin |
| 790 |
XFreeCursor(XDisplay, XCursors[C]); |
| 791 |
XCursors[C] := X.None; |
| 792 |
end; |
| 793 |
|
| 794 |
XCloseDisplay(XDisplay); |
| 795 |
end; |
| 796 |
end; |
| 797 |
|
| 798 |
function TCastleApplication.BackendName: String; |
| 799 |
begin |
| 800 |
Result := 'Xlib+' + {$ifndef USE_EGL} 'glX' {$else} 'EGL' {$endif}; |
| 801 |
end; |
| 802 |
|
| 803 |
function TCastleApplication.HandleXEvent(Event: TXEvent): boolean; |
| 804 |
var |
| 805 |
Window: TCastleWindow; |
| 806 |
begin |
| 807 |
Result := false; |
| 808 |
if event._type = MappingNotify then |
| 809 |
begin |
| 810 |
XRefreshKeyboardMapping(@event); |
| 811 |
Result := true; |
| 812 |
end else |
| 813 |
begin |
| 814 |
{ Events for a particular window. |
| 815 |
We check is the window present on Application.OpenWindows, |
| 816 |
finding window instance based on windowXID } |
| 817 |
Window := Application.FindWindowXID(event.xany.window); |
| 818 |
if Window <> nil then |
| 819 |
Result := Window.HandleXEvent(event); |
| 820 |
end; |
| 821 |
end; |
| 822 |
|
| 823 |
{$ifndef CASTLE_WINDOW_USE_XF86VMODE} |
| 824 |
function TCastleApplication.ScreenWidth: integer; |
| 825 |
begin |
| 826 |
InitializeXDisplay; |
| 827 |
Result := XDisplayWidth(XDisplay, XScreen); |
| 828 |
end; |
| 829 |
|
| 830 |
function TCastleApplication.ScreenHeight: integer; |
| 831 |
begin |
| 832 |
InitializeXDisplay; |
| 833 |
Result := XDisplayHeight(XDisplay, XScreen); |
| 834 |
end; |
| 835 |
|
| 836 |
{$endif} |
| 837 |
|
| 838 |
{ TWindowContainer ----------------------------------------------------------- } |
| 839 |
|
| 840 |
function TWindowContainer.SettingMousePositionCausesMotion: Boolean; |
| 841 |
begin |
| 842 |
Result := true; |
| 843 |
end; |
| 844 |
|
| 845 |
{$endif read_implementation} |