[SOLVED] Taking a screenshot on Linux fails w. Wayland windowmanager (X11 works) lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2025-06-27 12:40 I was in doubt if subforum "Programming / Operating Systems / Linux" might be better - I hope I picked the best one. Otherwise please move. Thanks. I use 2 different procedures to take a screenshot of a complete monitor. Both work perfectly on Linux Ubuntu 24.04 with Gnome Desktop and X11 window manager. But if I switch to Wayland window manager (on the same OS and the same computer), both procedures fail. This happens with Lazarus 2.0.10 / FPC 3.2.0 and with Lazarus 3.4.0 / FPC 3.2.2 (both with GTK2). I want to improve my 2 procedures so that they work with Wayland too. What must I do? Switching to BGRABitmap is not an option in this particular project. Here are my 2 procedures (where I added some debug outputs): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function getScreenDC: HDC;   {helper: returns the Handle for the Device Context (DC) of the Desktop-Window}   var h: HDC; {= THandle}   begin   h:=GetDC(0); {returns the handle of a Device Context (DC) for the client area of a window}   if h <> 0 then exit(h); {if OK}    writeln('Getting Handle for the DC of the Desktop-Window failed!');   halt; {this never occured}   end; {getScreenDC} procedure saveScreenshotA(filespec: string);   {stores a screenshot of all monitors into a PNG-file}   const DEB: bool = True; {show Debug- Infos?}   var PNG: TPortableNetworkGraphic;       ScreenDC: HDC; {= THandle}   begin   ScreenDC:=getScreenDC; {get Handle for the Device Context of the Desktop-Window}if DEB then writeln('Handle=', ScreenDC);   PNG:=TPortableNetworkGraphic.Create;   tryif DEB then writeln('AAA');      PNG.LoadFromDevice(ScreenDC); {takes the screenshot}if DEB then writeln('BBB');      PNG.SaveToFile(filespec);if DEB then writeln('CCC');   finally      PNG.Free;      ReleaseDC(0,ScreenDC);if DEB then writeln('DDD');   end; {try}   end; {saveScreenshotA} procedure saveScreenShotB(filespec: string);   {stores a screenshot of the primary monitor into a PNG-file. Notes for below code:    (1) fixes a bug in Lazarus 2.0.10 + 2.2.4, that command 'RD:=PNG.Canvas.ClipRect'        returns 2x 2000 because of a not initialized Handle of the Canvas.    (2) Linux only: without this fix the resulting PNG-file would be completely black}    procedure show_TRect(R: TRect; msg: string = '');      {displays the content of a 'TRect'}      begin    if msg <> '' then write(msg);      writeln('X1=', R.Left, ' Y1=', R.Top, ' X2=', R.Right, ' Y2=', R.Bottom,              ' b=', R.Width, ' h=', R.Height);      end;    const DEB: bool = True; {show Debug- Infos?}   var PNG: TPortableNetworkGraphic;       M: TMonitor;       CVS: TCanvas; {canvas of the screen}       RS,RD: TRect;       w,h: integer;   begin   M:=Screen.PrimaryMonitor; {monitor to use}   CVS:=TCanvas.Create;      {canvas of the screen}    try      CVS.Handle:=getScreenDC; {get Handle for the Device Context of the Desktop-Window}if DEB then writeln('Handle=', CVS.Handle);      PNG:=TPortableNetworkGraphic.Create;      tryif DEB then writeln('PixelFormat=', PNG.PixelFormat);         PNG.PixelFormat:=pf24bit;         w:=M.Width;                    {get size of wanted monitor: }         h:=M.Height;if DEB then writeln('w=', w, ' h=', h); {=> w=1280 h=1024}if DEB then writeln('PixelsPerInch=', M.PixelsPerInch); {=> 96}         RS:=M.BoundsRect;              {RS=Source-Rect}if DEB then show_TRect(RS, 'RS: ');     {=> X1=0 Y1=0 X2=1280 Y2=1024} if DEB then writeln('AAA');         PNG.SetSize(w,h);              {set size of PNG}if DEB then writeln('BBB');          PNG.Canvas.CopyMode:=cmSrcCopy;         PNG.Canvas.Pixels[0,0]:=0;   {fixes a bug, see above in (1)}if DEB then writeln('CCC');         RD:=PNG.Canvas.ClipRect;       {RD=Destination-Rect}if DEB then show_TRect(RD, 'RD: '); {$IFDEF LINUX}         inc(RD.Bottom); {fixes a bug only on Linux, see above in (2)}if DEB then show_TRect(RD, 'RD: ');{$ENDIF}             {copy a rectangular from 'RS' to 'RD': }if DEB then writeln('DDD');         PNG.Canvas.CopyRect(RD,CVS,RS); {Destination,Source-Canvas,Source}if DEB then writeln('EEE');         PNG.SaveToFile(filespec);if DEB then writeln('FFF');       finally       PNG.Free;         ReleaseDC(0,CVS.Handle);      end; {try2}   finally      CVS.Free;   end; {try1}   end; {saveScreenShotB} As said, with X11 both procedures work perfectly. But with Wayland window manager I face this problems: Procedure saveScreenshotA() throws a window with this error message (see screenshot1):    Failed to get raw image from device.    Press OK to ignore and risk data corruption.    Press Abort to kill the program. and breaks without storing a file. The debug output shows: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Handle=123608961461312AAAWARNING: TGtk2WidgetSet.RawImage_FromDrawable: gdk_image_get failedDDDwhich means, that procedure PNG.LoadFromDevice(ScreenDC) throws this error. Procedure saveScreenShotB() generates no Error, but the stored file contains garbage (see screenshot2). Instead of a screenshot, it contains 12 times the program's icon and the rest is completely black. The debug output shows: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Handle=126870986107456PixelFormat=pfDevicew=1280 h=1024PixelsPerInch=96RS: X1=0 Y1=0 X2=1280 Y2=1024 b=1280 h=1024AAABBBCCCRD: X1=0 Y1=0 X2=1280 Y2=1024 b=1280 h=1024RD: X1=0 Y1=0 X2=1280 Y2=1025 b=1280 h=1025DDDEEEFFF Please how can I improve my 2 procedures so that they work with Wayland too? Thanks in advance. ---------------------------------------------------------------------- rdxdt · 2025-06-27 13:56 Wayland screen capture must be done through pipewire due to the security provided by the Wayland protocol so one unauthorized application can't snoop at the screen/ other windows. Applications must explicitly request the user to grant them access to a screen or window. This is done through xdg-desktop-portal. The actual window content is delivered through PipeWire in the form of a video stream. To access it, an application has to connect to this stream and negotiate a format with the stream source. ---------------------------------------------------------------------- Hartmut · 2025-06-27 17:11 Thanks a lot rdxdt for this valuable infos. It's not good news for me (but this is not your fault). I understood, that a program which wants to take a screenshot has to use pipewire to get the screenshot as a stream. I'd never heard about pipewire before but I found it in https://pipewire.org. Seems to be not so easy to use it for this purpose... xdg-desktop-portal I don't know too. I found it in https://wiki.archlinux.org/title/XDG_Desktop_Portal. Seems that it has to be installed for the currently used 'backend' (which seems to be the same as the currently used 'Desktop' like GNOME or KDE-Plasma). Quote from: rdxdt on June 27, 2025, 03:56:52 pmApplications must explicitly request the user to grant them access to a screen or window. I don't understand this sentence:  - which application must request something? The application which wants to take a screenshot or the application which draws something on the screen (which then shall be captured)?  - 'the user' is the currently logged in user?  - and this 'to grant access' is done only once in xdg-desktop-portal and will then work for the future until this access is removed some day? Sorry for my questions, but this all is completely new for me. ---------------------------------------------------------------------- PascalDragon · 2025-06-28 14:25 Quote from: Hartmut on June 27, 2025, 07:11:45 pmQuote from: rdxdt on June 27, 2025, 03:56:52 pmApplications must explicitly request the user to grant them access to a screen or window. I don't understand this sentence:  - which application must request something? The application which wants to take a screenshot or the application which draws something on the screen (which then shall be captured)? The application that want to capture something. The desktop environment will usually pop up some dialog saying that application XYZ wants to capture the desktop and whether this should be allowed. Quote from: Hartmut on June 27, 2025, 07:11:45 pm - 'the user' is the currently logged in user? Correct. Quote from: Hartmut on June 27, 2025, 07:11:45 pm - and this 'to grant access' is done only once in xdg-desktop-portal and will then work for the future until this access is removed some day? It depends on what the user selects. The popup dialog often contains an option to grant this access only once or permanently. ---------------------------------------------------------------------- Fred vS · 2025-06-28 14:40 Quote from: PascalDragon on June 28, 2025, 04:25:33 pmIt depends on what the user selects. The popup dialog often contains an option to grant this access only once or permanently. This is exactly what has been integrated into XLibre with its Xnamespace concept to have the same level of security as Wayland (but ready to use and, of course, with the possibility of accessing it once or permanently). ---------------------------------------------------------------------- Hartmut · 2025-06-28 16:15 Thanks a lot PascalDragon for that explanations. Now I have a rough imagination how pipewire and xdg-desktop-portal have to be used together. Seems to be a lot of work to get this all running. I must move this project into the future because for the next time I have enough other duties. Quote from: Fred vS on June 28, 2025, 04:40:36 pmThis is exactly what has been integrated into XLibre with its Xnamespace concept to have the same level of security as Wayland I read that Xlibre is a fork of the Xorg Xserver. So it seems to be an alternative to Wayland. But in my case I'm looking explicitly for a solution for Wayland. If someone shall implement a solution in FPC for pipewire (and is willing to share this here) I would be very interested. ---------------------------------------------------------------------- Thaddy · 2025-06-28 16:40 Well the answer is already explained and even step-by-step. It will never be as easy as taking a handle and saving the buffer anymore. Even with X-fork.