Debian Codegraph @

src/window/unix/castlewindow_xf86vmode.inc

raw
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 {$ifdef read_interface_uses}
18 XF86VMode,
19 {$endif}
20
21 { This implements TCastleApplication video resizing with XF86VMode }
22
23 {$ifdef read_application_interface}
24 private
25 VideoModeChanged: boolean;
26 { *ModeInfos are valid only if VideoModeChanged }
27 CurrentModeInfo, OriginalModeInfo: TXF86VidModeModeInfo;
28
29 FUseVirtualXScreen: boolean;
30 public
31 { jezeli UseVirtualXScreen to pod X-ami zmiana rozmiarow Video przez
32 VideoChange nie spowoduje zmiany ScreenWidth/H i bedziemy miec
33 okno np. 800x600 na ekranie 640x480. To jest wlasnie virtual screen
34 X-ow. W sumie moze to byc fajna rzecz dla jakichs programow uzytkowych ktore
35 pozwalaja userowi na operowanie na ekranie jakimis kontrolkami
36 ale nie dla typowych pelnoekranowych gier/dem : dla nas zmiana
37 rozmiaru ekranu ma na celu zmniejszenie/zwiekszenie dokladnosci
38 renderowanego obrazu. Wiec domyslnie UseVirtualXScreen = false. }
39 property UseVirtualXScreen: boolean
40 read FUseVirtualXScreen write FUseVirtualXScreen default false;
41 {$endif read_application_interface}
42
43 {$ifdef read_implementation}
44
45 function XF86VidModeModeLineToModeInfo(const ModeLine: TXF86VidModeModeLine;
46 DotClock: Longint): TXF86VidModeModeInfo;
47 begin
48 result.dotclock := DotClock;
49 result.hdisplay := ModeLine.hdisplay;
50 result.hsyncstart := ModeLine.hsyncstart;
51 result.hsyncend := ModeLine.hsyncend;
52 result.htotal := ModeLine.htotal;
53 result.hskew := ModeLine.hskew;
54 result.vdisplay := ModeLine.vdisplay;
55 result.vsyncstart := ModeLine.vsyncstart;
56 result.vsyncend := ModeLine.vsyncend;
57 result.vtotal := ModeLine.vtotal;
58 result.flags := ModeLine.flags;
59 result.privsize := 0;
60 { TODO: should I try to copy privsize here somehow ?
61 TODO: should we in TryVideoChange and VideoReset and here do something
62 with c_private fields ? Manual is unclear to me... }
63 end;
64
65 function TCastleApplication.TryVideoChange: boolean;
66 var
67 {vidModeMajorVersion, vidModeMinorVersion: integer;}
68 i, bestmode: integer;
69
70 modesCount: integer;
71 modes : PPXF86VidModeModeInfo;
72
73 OriginalMode: TXF86VidModeModeLine;
74 OriginalModeDotClock: Longint;
75
76 { we don't need this information, but XF86VidModeQueryExtension on my Debian
77 segfaults when passed nil as 2nd or 3rd param. So I pass it some valid
78 pointers... }
79 DummyEventBase: LongInt;
80 DummyErrorBase: LongInt;
81 begin
82 { we don't change BitsPerPixel now, so we do VideoChange only if VideoResize. }
83 if not VideoResize then exit(true);
84
85 InitializeXDisplay;
86
87 {TESTOWE:}
88 { XF86VidModeQueryVersion(XDisplay, @vidModeMajorVersion,
89 @vidModeMinorVersion);
90 Writeln(Format('XF86VidModeExtension-Version %d.%d', [vidModeMajorVersion,
91 vidModeMinorVersion]));}
92
93 if not XF86VidModeQueryExtension(XDisplay, @DummyEventBase,
94 @DummyErrorBase) then
95 { X server doesn't implement this extension (at least on this display) }
96 Exit(false);
97
98 { if this is first VideoChange then save desktop resolution before
99 switching modes }
100 if not VideoModeChanged then
101 begin
102 XF86VidModeGetModeLine(XDisplay, XScreen, @OriginalModeDotClock,
103 @OriginalMode);
104 OriginalModeInfo := XF86VidModeModeLineToModeInfo(OriginalMode, OriginalModeDotClock);
105 end;
106
107 XF86VidModeGetAllModeLines(XDisplay, XScreen, @modesCount, @modes);
108 try
109 {
110 for i := 0 to modesCount-1 do
111 WritelnLog('X11 Screen Sizes', 'Mode %d: %d x %d',
112 [I, Modes[i]^.hdisplay, Modes[i]^.vdisplay]); }
113
114 { look for mode with requested resolution }
115 bestmode := -1;
116 for i := 0 to modesCount-1 do
117 if (not VideoResize) or
118 ((modes[i]^.hdisplay = VideoResizeWidth) and
119 (modes[i]^.vdisplay = VideoResizeHeight)) then
120 { TODO: jak tu dopasowac BitsPerPixel }
121 bestMode := i;
122
123 if bestmode < 0 then exit(false);
124
125 CurrentModeInfo := modes[bestMode]^;
126 XF86VidModeSwitchToMode(XDisplay, XScreen, @CurrentModeInfo);
127
128 {SetViewport jest przydatne gdy zmienimy sie z duzego ekranu na mniejszy i
129 nie chcemy uzywac VirtualXScreen. Wtedy czasem nasz maly ekranik jest
130 nieco z boku i user musi nakierowac myszka aby byl on w srodku.
131 Ponizsze SetViewport zrobi to za niego. }
132 XF86VidModeSetViewPort(XDisplay, XScreen, 0, 0);
133
134 VideoModeChanged := true;
135 result := true;
136 finally XFree(modes) end;
137 end;
138
139 procedure TCastleApplication.VideoReset;
140 begin
141 if VideoModeChanged then
142 begin
143 { switch back to original desktop resolution }
144 XF86VidModeSwitchToMode(XDisplay, XScreen, @OriginalModeInfo);
145 VideoModeChanged := false;
146 end;
147 end;
148
149 function TCastleApplication.ScreenWidth: integer;
150 begin
151 InitializeXDisplay;
152
153 { jezeli VideoModeChanged to nie chcemy uzywac rozmiarow screenu (bo wtedy
154 po utworzeniu okienka fullscreen mielibysmy okno o wymiarach np. 800x600
155 na ekranie 640x480. Po tym oknie musielibysmy sie przewijac myszka.
156 Zdecydowanie, to nie jest nasz cel : zazwyczaj my zmieniajac rozdzielczosc
157 chcemy miec po prostu wieksza/mniejsza dokladnosc obrazu zmieniajac ilosc pixeli
158 na ktorych ten obraz ma sie miescic. }
159 if VideoModeChanged and (not UseVirtualXScreen) then
160 result := CurrentModeInfo.hdisplay
161 else
162 result := XDisplayWidth(XDisplay, XScreen);
163 end;
164
165 function TCastleApplication.ScreenHeight: integer;
166 begin
167 InitializeXDisplay;
168
169 if VideoModeChanged and (not UseVirtualXScreen) then
170 result := CurrentModeInfo.vdisplay
171 else
172 result := XDisplayHeight(XDisplay, XScreen);
173 end;
174
175 {$endif read_implementation}