I have an issue with function XLib.XSetInputFocus() to set the Focus on an open Window.
It fails often on KDE-Plasma Desktop with GTK2 and with QT5 on Ubuntu 24.04 (and Ubuntu 18.04).
It works on GNOME-Desktop with GTK2 on Ubuntu 24.04.
In all cases with X11 Windows-Manager and Lazarus 3.4.0 (same with Lazarus 2.0.10).
Don't know if a bug is in my code or somewhere else. I search a solution (or workaround), so that it works on KDE-Plasma Desktop with GTK2 on Ubuntu 24.04.
I attached a compilable project, if you want to test on your system. Please report your results, if you test something.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button_read: TButton;
Button_set: TButton;
Button_stop: TButton;
Button_close: TButton;
CheckBox1: TCheckBox;
procedure Button_readClick(Sender: TObject);
procedure Button_setClick(Sender: TObject);
procedure Button_stopClick(Sender: TObject);
procedure Button_closeClick(Sender: TObject);
private
public
end;
var Form1: TForm1;
implementation
{$R *.lfm}
uses math, baseunix, XLib, X;
const FDisplay: PDisplay = nil; {pointer}
function X_openDisplay: boolean;
{opens only once a connection to the X server}
begin
if FDisplay <> nil then exit(true);
FDisplay:=XOpenDisplay(nil);
if FDisplay=nil then writeln('XOpenDisplay() FAILED!');
exit(FDisplay <> nil);
end;
procedure GetFocus(out FPreviousWindow: TWindow; out FPreviousRevert: integer);
{returns the Window-ID and "current focus state" of the window which has the Focus}
begin
FPreviousWindow:=0; FPreviousRevert:=0;
if FDisplay=nil then exit;
XGetInputFocus(FDisplay, @FPreviousWindow, @FPreviousRevert);
write('got: PrevWindow = ', FPreviousWindow, ' PrevRevert = ', FPreviousRevert);
end;
function XErrorHandler(display: PDisplay; event: PXErrorEvent): longint; cdecl;
{Error-Handler for RestoreFocus()}
begin
writeln;
writeln('X-Error: code=', event^.error_code, ', request code=', event^.request_code);
// error code: https://xorg.freedesktop.org/archive/X11R7.7/doc/xproto/x11protocol.html#Encoding::Errors
// 1 - BadRequest
// 2 - BadValue
// 3 - BadWindow
// request code: https://xorg.freedesktop.org/archive/X11R7.7/doc/xproto/x11protocol.html#Encoding::Requests
// 42 - SetInputFocus
Result:=0;
end;
procedure RestoreFocus(FPreviousWindow: TWindow; FPreviousRevert: Integer);
{sets the Focus to the specified Window}
var PrevErrorHandler: TXErrorHandler;
begin
if (FDisplay=nil) or (FPreviousWindow=0) then
begin
writeln('RestoreFocus() FAILED!');
exit;
end;
writeln('SET PrevWindow=', FPreviousWindow, ' PrevRevert=', FPreviousRevert);
PrevErrorHandler:=XSetErrorHandler(@XErrorHandler);
XSetErrorHandler(@XErrorHandler); {activate Error-Handler}
XSetInputFocus(FDisplay, FPreviousWindow, FPreviousRevert, CurrentTime);
XSync(FDisplay, False);
XFlush(FDisplay);
XSetErrorHandler(PrevErrorHandler); {restore Error-Handler}
end;
procedure wait_ms_AppProcMsg(ms: word);
{waits 'ms' milliseconds and calls Application.ProcessMessages repeatedly}
const Step = 125;
var n: word;
begin
while ms > 0 do
begin
n:=Min(Step,ms);
sleep(n);
Application.ProcessMessages;
dec(ms,n);
end;
end;
const stop: boolean = true; {set to 'True' by Buttons 'Stop' and 'Close'}
procedure read_Window_IDs;
{reads and lists repeatedly the Windows-ID, which currently has the Focus}
var PreviousWindow: TWindow; {qword}
PreviousRevert: longint;
begin
writeln('read_Windows_ID-Start');
if not X_openDisplay then exit;
stop:=false;
repeat GetFocus(PreviousWindow,PreviousRevert); writeln;
wait_ms_AppProcMsg(2000); {waits 2000 ms}
until stop; {until 1 of 2 buttons pressed}
writeln('read_Windows_ID-Stop');
end;
function takeParam(p: integer): qword;
{returns ParamStr(p) as 'qword'}
var res: qword;
code: integer;
begin
val(ParamStr(p), res,code);
if code=0 then exit(res);
writeln('Parameter ', p, ' is INVALID!');
exit(0);
end;
procedure set_Window_IDs;
{sets the Focus alternating to 2 Windows, specified by command line parameters}
var prevWin1,prevWin2,prevWinS,prevWinG: TWindow; {qword}
prevRev1,prevRev2,prevRevS,prevRevG, nr: longint;
ok: boolean;
begin
writeln('set_Windows_ID-Start');
if ParamCount < 4 then
begin
writeln('Syntax: <Window-ID1> <focus state1> <Window-ID2> <focus state2>');
exit;
end;
prevWin1:=takeParam(1);
prevRev1:=takeParam(2);
prevWin2:=takeParam(3);
prevRev2:=takeParam(4);
if (prevWin1=0) or (prevWin2=0) then exit;
if not X_openDisplay then exit;
if Form1.CheckBox1.Checked then
begin
writeln('PRESS CTRL-C TO ABORT');
Form1.Hide;
wait_ms_AppProcMsg(1000); {waits 1000 ms}
end;
stop:=false;
nr:=1;
repeat
if odd(nr) then begin prevWinS:=prevWin1; prevRevS:=prevRev1; end
else begin prevWinS:=prevWin2; prevRevS:=prevRev2; end;
wait_ms_AppProcMsg(1500); {waits 1500 ms}
write(nr, ') ');
RestoreFocus(prevWinS,prevRevS); {set Focus}
if stop then BREAK; {if 1 of 2 buttons pressed}
wait_ms_AppProcMsg(500); {waits 500 ms}
GetFocus(prevWinG,prevRevG); {read Focus again}
ok:=(prevWinG=prevWinS) and (prevRevG=prevRevS);
if ok then writeln(' => ok') else writeln(' => FAILED!!!');
if not Form1.Visible and (nr >= 4) then Form1.Visible:=true; {make Form visible again}
inc(nr);
until stop; {until 1 of 2 buttons pressed}
writeln('set_Windows_ID-Stop');
end;
procedure TForm1.Button_readClick(Sender: TObject);
begin
// writeln(GetLCLWidgetTypeName);
if stop then read_Window_IDs else writeln('First stop running job!');
end;
procedure TForm1.Button_setClick(Sender: TObject);
begin
{$IFDEF LCLGTK2} writeln('gtk2'); {$ELSE} writeln('qt5'); {$ENDIF}
if stop then set_Window_IDs else writeln('First stop running job!');
end;
procedure TForm1.Button_stopClick(Sender: TObject);
begin
stop:=true;
end;
procedure TForm1.Button_closeClick(Sender: TObject);
begin
stop:=true;
writeln;
Close;
end;
end.
Above functions to read and set the Focus are deviated from the code from paweld in reply #13 from
https://forum.lazarus.freepascal.org/index.php/topic,75042.0.htmlHow to test on your system: - start at least 2 programs, so we have open windows to set the Focus there. I tested e.g. with Kate (Texteditor), Synaptic, GParted, Firefox and Lazarus.
- compile and run my attached demo in a
Console - press button "read Window-IDs". Klick in the 2 windows of above 2 programs. You'll see their Window-IDs and "current focus state" in the Console.
- press button "Close"
- run my demo again, but now with 4 command line parameters with the privious showed values: <Window-ID1> <focus state1> <Window-ID2> <focus state2>.
Example: project1 92277613 2 92277618 2. You may add a comment (only for yourself) after these 4 values.
- press button "set Window-IDs"
If it works correctly (in my case on GNOME): - the Focus should switch every 2 seconds between the 2 Windows, whose parameters you provided
- while my demo runs, you may click in these 2 windows and all other open windows, it should not disturb and the Focus continues to switch between the 2 Windows
- press buttons "Stop" to stop or "Close" to abort.
Issues which I have on KDE-Plasma Desktop:a) with Lazarus:Lazarus behaves different (better) than all other programs which I tested. What I get:
- start my demo and query the Window-IDs of 2 open Lazarus-Editors (from the same IDE). I used Lazarus 3.4.0 (1.8.4 also worked).
- start my demo again with those 4 values as command line parameters
- press button "set Window-IDs". It does *not* work, the Focus is *not* set, the Console shows always "FAILED"
- klick in one of these 2 Lazarus-Editors or *any other* open Lazarus-Window (e.g. Messages, Main-Dialog, Find-Dialog). Now it works!
- klick into any other open Window (not from Lazarus). Now it stops to work and always fails.
- if you click again into one of the Lazarus-Windows, it works again, and so on.
Alternatively you can do this with Lazarus:
- if "set Window-IDs" is running, press button "Stop"
- press button "set Window-IDs" and *immediately* (in max. 1500 ms) klick in an arbitrary open Lazarus-Window. Now it works!
- if you klick into any other open Window (not from Lazarus), it fails again.
Alternatively you can do this with Lazarus:
- if "set Window-IDs" is running, press button "Stop"
- activate CheckBox "Hide this Form before set Window-IDs"
- klick in an arbitrary open Lazarus-Window to set the Focus there (if the Focus would now be in another Window, it would *not* work).
- press button "set Window-IDs". Now it works (if the Focus was before in Lazarus).
Without this CheckBox active this Focus trick does *not* work.
b) with other programs:I found *no* other program, where my demo worked on KDE-Plasma (e.g. not with Kate (Texteditor), Synaptic, GParted, Firefox).
Does anybody see a bug in my code?
Has anybody an idea for a workaround for KDE-Plasma Desktop with GTK2 on Ubuntu 24.04?
Thanks in advance.