summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/ptc/src/x11/x11modesi.inc
blob: 128c1dac9550a22bafe503f9345dee518ff1d244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
{
    This file is part of the PTCPas framebuffer library
    Copyright (C) 2001-2012 Nikolay Nikolov (nickysn@users.sourceforge.net)
    Original C++ version by Christian Nentwich (c.nentwich@cs.ucl.ac.uk)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version
    with the following modification:

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent modules,and
    to copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the terms
    and conditions of the license of that module. An independent module is a
    module which is not derived from or based on this library. If you modify
    this library, you may extend this exception to your version of the library,
    but you are not obligated to do so. If you do not wish to do so, delete this
    exception statement from your version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
}

constructor TX11Modes.Create(ADisplay: PDisplay; AScreen: cint);
begin
  FDisplay := ADisplay;
  FScreen := AScreen;
end;

constructor TX11ModesNoModeSwitching.Create(ADisplay: PDisplay; AScreen: cint);
begin
  inherited;

  FWidth := DisplayWidth(FDisplay, FScreen);
  FHeight := DisplayHeight(FDisplay, FScreen);
end;

procedure TX11ModesNoModeSwitching.GetModes(var AModes: TPTCModeList; ACurrentDesktopFormat: IPTCFormat);
begin
  SetLength(AModes, 1);
  AModes[0] := TPTCMode.Create(FWidth,
                               FHeight,
                               ACurrentDesktopFormat);
end;

procedure TX11ModesNoModeSwitching.SetBestMode(AWidth, AHeight: Integer);
begin
  FWidth := DisplayWidth(FDisplay, FScreen);
  FHeight := DisplayHeight(FDisplay, FScreen);
end;

procedure TX11ModesNoModeSwitching.RestorePreviousMode;
begin
  { do nothing }
end;

function TX11ModesNoModeSwitching.GetWidth: Integer;
begin
  Result := FWidth;
end;

function TX11ModesNoModeSwitching.GetHeight: Integer;
begin
  Result := FHeight;
end;

{$IFDEF ENABLE_X11_EXTENSION_XRANDR}
constructor TX11ModesXrandr.Create(ADisplay: PDisplay; AScreen: cint);
var
  dummy1, dummy2: cint;
  Major, Minor: cint;
begin
  inherited;

  FInMode := False;

  if not XRRQueryExtension(FDisplay, @dummy1, @dummy2) then
    raise TPTCError.Create('Xrandr extension not available');

  XRRQueryVersion(FDisplay, @Major, @Minor); // todo: check
  LOG('Xrandr version: ' + IntToStr(Major) + '.' + IntToStr(Minor));

  FRoot := RootWindow(FDisplay, FScreen);

  { is this necessary? }
  CreateScreenConfig;
  DestroyScreenConfig;
end;

destructor TX11ModesXrandr.Destroy;
begin
  RestorePreviousMode;

  inherited;
end;

procedure TX11ModesXrandr.CreateScreenConfig;
begin
  DestroyScreenConfig;

  FXRRConfig := XRRGetScreenInfo(FDisplay, FRoot);
  if FXRRConfig = nil then
    raise TPTCError.Create('XRRGetScreenInfo failed');
end;

procedure TX11ModesXrandr.DestroyScreenConfig;
var
  tmp: PXRRScreenConfiguration;
begin
  if FXRRConfig <> nil then
  begin
    tmp := FXRRConfig;
    FXRRConfig := nil;
    XRRFreeScreenConfigInfo(tmp);
  end;
end;

procedure TX11ModesXrandr.GetModes(var AModes: TPTCModeList; ACurrentDesktopFormat: IPTCFormat);
var
  ScreenSizes: PXRRScreenSize;
  ScreenSizesNum: cint;
  I: Integer;
begin
  ScreenSizes := XRRConfigSizes(FXRRConfig, @ScreenSizesNum);

  SetLength(AModes, ScreenSizesNum);
  for I := 0 to ScreenSizesNum - 1 do
    AModes[I] := TPTCMode.Create(ScreenSizes[I].width,
                                 ScreenSizes[I].height,
                                 ACurrentDesktopFormat);
end;

class function TX11ModesXrandr.FindBestMode(AScreenSizes: PXRRScreenSize;
                                            AScreenSizesNum: Integer;
                                            ARequestedWidth,
                                            ARequestedHeight: Integer): Integer;
var
  I: Integer;
  min_diff: Integer;
  d_x, d_y: Integer;
  found_mode: Integer;
begin
  { Find exact match }
  for I := 0 to AScreenSizesNum - 1 do
    if (AScreenSizes[I].width = ARequestedWidth) and
       (AScreenSizes[I].height = ARequestedHeight) then
      exit(I);

  { try to find a mode that matches the width first }
  for I := 0 to AScreenSizesNum - 1 do
    if (AScreenSizes[I].width = ARequestedWidth) and
       (AScreenSizes[I].height >= ARequestedHeight) then
      exit(I);

  { Next try to match the height }
  for I := 0 to AScreenSizesNum - 1 do
    if (AScreenSizes[I].width >= ARequestedWidth) and
       (AScreenSizes[I].height = ARequestedHeight) then
      exit(I);

  { Finally, find the mode that is bigger than the requested one and makes }
  { the least difference }
  found_mode := -1;
  min_diff := High(Integer);
  for I := 0 to AScreenSizesNum - 1 do
    if (AScreenSizes[I].width >= ARequestedWidth) and
       (AScreenSizes[I].height >= ARequestedHeight) then
    begin
      d_x := Sqr(AScreenSizes[I].width - ARequestedWidth);
      d_y := Sqr(AScreenSizes[I].height - ARequestedHeight);
      if (d_x + d_y) < min_diff then
      begin
        min_diff := d_x + d_y;
        found_mode := I;
      end;
    end;

  if found_mode <> -1 then
    exit(found_mode);

  raise TPTCError.Create('Cannot find matching video mode');
end;

procedure TX11ModesXrandr.SetBestMode(AWidth, AHeight: Integer);
var
  ScreenSizes: PXRRScreenSize;
  ScreenSizesNum: cint;
  BestMode: Integer;
  CurrentRotation: TRotation;
begin
  CreateScreenConfig;
  try
    if not FInMode then
      SaveCurrentMode;

    ScreenSizes := XRRConfigSizes(FXRRConfig, @ScreenSizesNum);

    BestMode := FindBestMode(ScreenSizes, ScreenSizesNum, AWidth, AHeight);
    FWidth := ScreenSizes[BestMode].width;
    FHeight := ScreenSizes[BestMode].height;

    XRRConfigCurrentConfiguration(FXRRConfig, @CurrentRotation);

    X11CheckSuccess(XRRSetScreenConfig(FDisplay, FXRRConfig, FRoot, BestMode,
                                       CurrentRotation, CurrentTime),
                    'XRRSetScreenConfig failed');

    FInMode := True;
  finally
    DestroyScreenConfig;
  end;
end;

procedure TX11ModesXrandr.SaveCurrentMode;
var
  SizeIndex: TSizeID;
  ScreenSizes: PXRRScreenSize;
  ScreenSizesNum: cint;
begin
  LOG('xrandr: saving previous mode');

  SizeIndex := XRRConfigCurrentConfiguration(FXRRConfig, @FSavedMode.Rotation);
  FSavedMode.Rate := XRRConfigCurrentRate(FXRRConfig);

  ScreenSizes := XRRConfigSizes(FXRRConfig, @ScreenSizesNum);
  if (SizeIndex < 0) or (SizeIndex >= ScreenSizesNum) then
    raise TPTCError.Create('XRRConfigCurrentConfiguration returned a size index outside the range of screen sizes');
  FSavedMode.ScreenSize := ScreenSizes[SizeIndex];
end;

procedure TX11ModesXrandr.RestorePreviousMode;
var
  I: Integer;
  SizeIndex: cint;
  ScreenSizes: PXRRScreenSize;
  ScreenSizesNum: cint;
begin
  if not FInMode then
    exit;

  LOG('xrandr: restoring previous mode');

  CreateScreenConfig;
  try
    SizeIndex := -1;
    ScreenSizes := XRRConfigSizes(FXRRConfig, @ScreenSizesNum);
    for I := 0 to ScreenSizesNum - 1 do
      if (ScreenSizes[I].width   = FSavedMode.ScreenSize.width  ) and
         (ScreenSizes[I].height  = FSavedMode.ScreenSize.height ) and
         (ScreenSizes[I].mwidth  = FSavedMode.ScreenSize.mwidth ) and
         (ScreenSizes[I].mheight = FSavedMode.ScreenSize.mheight) then
      begin
        SizeIndex := I;
        Break;
      end;
    if SizeIndex = -1 then
      for I := 0 to ScreenSizesNum - 1 do
        if (ScreenSizes[I].width  = FSavedMode.ScreenSize.width ) and
           (ScreenSizes[I].height = FSavedMode.ScreenSize.height) then
        begin
          SizeIndex := I;
          Break;
        end;
    if SizeIndex = -1 then
      raise TPTCError.Create('xrandr: saved mode size not found in size list');

    X11CheckSuccess(XRRSetScreenConfigAndRate(FDisplay,
                                              FXRRConfig,
                                              FRoot,
                                              SizeIndex,
                                              FSavedMode.Rotation,
                                              FSavedMode.Rate,
                                              CurrentTime),
                    'XRRSetScreenConfigAndRate failed');

    FInMode := False;
  finally
    DestroyScreenConfig;
  end;
end;

function TX11ModesXrandr.GetWidth: Integer;
begin
  Result := FWidth;
end;

function TX11ModesXrandr.GetHeight: Integer;
begin
  Result := FHeight;
end;
{$ENDIF ENABLE_X11_EXTENSION_XRANDR}

{$IFDEF ENABLE_X11_EXTENSION_XF86VIDMODE}
constructor TX11ModesXF86VidMode.Create(ADisplay: PDisplay; AScreen: Integer);
var
  dummy1, dummy2: cint;
begin
  inherited;

  FSavedMode := nil;
  FSavedDotClock := 0;
  FModeList := nil;
  FModeListCount := 0;

  if not XF86VidModeQueryExtension(FDisplay, @dummy1, @dummy2) then
    raise TPTCError.Create('VidMode extension not available');
end;

destructor TX11ModesXF86VidMode.Destroy;
begin
  if FSavedMode <> nil then
  begin
    RestorePreviousMode;
    if FSavedMode^.privsize <> 0 then
      XFree(FSavedMode^.c_private);
    Dispose(FSavedMode);
  end;

  if FModeList <> nil then
    XFree(FModeList);

  inherited Destroy;
end;

{todo: move the saving of the previous mode to a separate function...}
procedure TX11ModesXF86VidMode.RetrieveModeList;
begin
  { if we have been called before, do nothing }
  if FModeList <> nil then
    exit;

  { Save previous mode }
  New(FSavedMode);
  FillChar(FSavedMode^, SizeOf(FSavedMode^), 0);
  XF86VidModeGetModeLine(FDisplay, FScreen, @FSavedDotClock, FSavedMode);

  { Get all available video modes }
  XF86VidModeGetAllModeLines(FDisplay, FScreen, @FModeListCount, @FModeList);
end;

procedure TX11ModesXF86VidMode.GetModes(var AModes: TPTCModeList; ACurrentDesktopFormat: IPTCFormat);
var
  I: Integer;
begin
  RetrieveModeList;

  SetLength(AModes, FModeListCount);
  for I := 0 to FModeListCount - 1 do
    AModes[I] := TPTCMode.Create(FModeList[I]^.hdisplay, FModeList[I]^.vdisplay, ACurrentDesktopFormat);
end;

function TX11ModesXF86VidMode.FindNumberOfBestMode(AWidth, AHeight: Integer): Integer;
var
  min_diff: Integer;
  d_x, d_y: Integer;
  found_mode: Integer;
  I: Integer;
begin
  { try an exact match }
  for I := 0 to FModeListCount - 1 do
    if (FModeList[I]^.hdisplay = AWidth) and (FModeList[I]^.vdisplay = AHeight) then
      exit(I);

  { try to find a mode that matches the width first }
  for I := 0 to FModeListCount - 1 do
    if (FModeList[I]^.hdisplay = AWidth) and (FModeList[I]^.vdisplay >= AHeight) then
      exit(I);

  { Next try to match the height }
  for I := 0 to FModeListCount - 1 do
    if (FModeList[I]^.hdisplay >= AWidth) and (FModeList[I]^.vdisplay = AHeight) then
      exit(I);

  { Finally, find the mode that is bigger than the requested one and makes }
  { the least difference }
  found_mode := -1;
  min_diff := High(Integer);
  for I := 0 to FModeListCount - 1 do
    if (FModeList[I]^.hdisplay >= AWidth) and (FModeList[I]^.vdisplay >= AHeight) then
    begin
      d_x := Sqr(FModeList[I]^.hdisplay - AWidth);
      d_y := Sqr(FModeList[I]^.vdisplay - AHeight);
      if (d_x + d_y) < min_diff then
      begin
        min_diff := d_x + d_y;
        found_mode := I;
      end;
    end;

  if found_mode <> -1 then
    Result := found_mode
  else
    raise TPTCError.Create('Cannot find matching video mode');
end;

procedure TX11ModesXF86VidMode.SetBestMode(AWidth, AHeight: Integer);
var
  BestMode: Integer;
begin
  RetrieveModeList;

  BestMode := FindNumberOfBestMode(AWidth, AHeight);
  if not XF86VidModeSwitchToMode(FDisplay, FScreen, FModeList[BestMode]) then
    raise TPTCError.Create('Error switching to the requested video mode');

  FWidth := FModeList[BestMode]^.hdisplay;
  FHeight := FModeList[BestMode]^.vdisplay;

  XWarpPointer(FDisplay, None, RootWindow(FDisplay, FScreen), 0, 0, 0, 0,
               FWidth div 2,
               FHeight div 2);

  if not XF86VidModeSetViewPort(FDisplay, FScreen, 0, 0) then
    raise TPTCError.Create('Error moving the viewport to the upper-left corner');
end;

procedure TX11ModesXF86VidMode.RestorePreviousMode;
var
  ModeInfo: TXF86VidModeModeInfo;
begin
  if FSavedMode <> nil then
  begin
    {FSavedMode is a TXF86VidModeModeLine, but XF86VidModeSwitchToMode wants a
                     TXF86VidModeModeInfo :}
    FillChar(ModeInfo, SizeOf(ModeInfo), 0);
    ModeInfo.dotclock := FSavedDotClock;
    ModeInfo.hdisplay := FSavedMode^.hdisplay;
    ModeInfo.hsyncstart := FSavedMode^.hsyncstart;
    ModeInfo.hsyncend := FSavedMode^.hsyncend;
    ModeInfo.htotal := FSavedMode^.htotal;
    ModeInfo.vdisplay := FSavedMode^.vdisplay;
    ModeInfo.vsyncstart := FSavedMode^.vsyncstart;
    ModeInfo.vsyncend := FSavedMode^.vsyncend;
    ModeInfo.vtotal := FSavedMode^.vtotal;
    ModeInfo.flags := FSavedMode^.flags;
    ModeInfo.privsize := FSavedMode^.privsize;
    ModeInfo.c_private := FSavedMode^.c_private;

    XF86VidModeSwitchToMode(FDisplay, FScreen, @ModeInfo);
  end;
end;

function TX11ModesXF86VidMode.GetWidth: Integer;
begin
  Result := FWidth;
end;

function TX11ModesXF86VidMode.GetHeight: Integer;
begin
  Result := FHeight;
end;
{$ENDIF ENABLE_X11_EXTENSION_XF86VIDMODE}