You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@guacamole.apache.org by "jjheinon (via GitHub)" <gi...@apache.org> on 2026/05/14 11:08:04 UTC

[PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

jjheinon opened a new pull request, #673:
URL: https://github.com/apache/guacamole-server/pull/673

   Fixing remote app render issues with this patch, using per-window guac_display_layer for remote apps. Also introducing a fix for stale desktop background on RDPGFX.
   
   Using per-window guac_display_layer for remote apps and also prevent dirty-region propagation to desktop background. Added support for UpdateSurfaceArea (FreeRDP 2.x) and UpdateWindowFromSurface (FreeRDP 3.x)
   
   When Guacamole's RDP plugin is configured with both RemoteApp and the RDPGFX pipeline enabled, remote application windows fail to render.
   With GFX active, FreeRDP does not update RAIL window contents via BeginPaint/Endpaint, but via RDPGFX surfaces using windowId and callbacks.
   For FreeRDP 3.x: UpdateWindowFromSurface is called per window-mapped surface with its accumulated invalid region.
   For FreeRDP 2.x: UpdateSurfaceArea is called per surface with a rect list.
   
   This patch has a dedicated guac_display_layer for each remote app and derives visibility via showState. Surface content painted with RDPGFX callbacks and scaled to correct size.
   Layer is kept hidden until both window position is known and at least one surface paint request has arrived, avoiding flashes and race conditions.
   
   Tested remote apps with FreeRDP 3.26.0 with the latest staging/1.6.1 branch, also built with 2.11.7.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#issuecomment-4757981733

   @jjheinon I've marked this as a draft for now as it looks like you're still pretty actively working through things and making changes. When you're happy that it's ready for a final review and merge, just hit that Ready for review button.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3426889191


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -808,6 +1019,12 @@ static BOOL guac_rdp_rail_monitored_desktop(rdpContext* context,
     if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ZORDER)
             && monitoredDesktop->windowIds != NULL) {
 
+        for (guac_common_list_element* current = rdp_client->rail_windows->head;
+                current != NULL; current = current->next) {
+            guac_rdp_rail_window* rail_window = current->data;
+            rail_window->in_monitored_desktop_zorder = 0;

Review Comment:
   We got the official z-index for this window from the server, so we don't need to handle it separately anymore.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3254307422


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -594,10 +718,107 @@ static BOOL guac_rdp_rail_window_delete(rdpContext* context,
     guac_common_list_unlock(rdp_client->rail_windows);
 
     return TRUE;
+}
+
+/**
+ * Handler for RAIL monitored desktop orders. These orders include the
+ * server-side z-order of RemoteApp windows as well setting active window.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing desktop order flags.
+ *
+ * @param monitoredDesktop
+ *     A pointer to the monitored desktop state sent by the server.
+ *
+ * @return
+ *     TRUE if the desktop order was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_monitored_desktop(rdpContext* context,

Review Comment:
   The window ordering was broken earlier, now we obey server sent z-order for the windows. We also set active window according to server orders.
   Guacamole side does not do any window manager stuff, we just do what server says.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3524847725


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -228,54 +269,1855 @@ static UINT guac_rdp_rail_handshake_ex(RailClientContext* rail,
     return guac_rdp_rail_complete_handshake(rail);
 }
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+/* 
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The RDP server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The RDPGFX surface ID currently mapped to this window, if known.
+     */
+    UINT16 surface_id;
+    int has_surface_id;
+
+    /**
+     * The RDP server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;

Review Comment:
   This is a bit of a nit, but, in keeping with style used throughout the rest of the code, I think these variables need to be documented individually. I also think there's a technical reason behind it - I believe there are some automated processes that use the documentation to generate things like API documentation (e.g. https://guacamole.apache.org/doc/libguac/), and that documenting multiple variables with a single comment may cause some problems with that.
   
   There are several places throughout the code, here, where this needs to be tweaked.



##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,146 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+/**
+ * Raises and activates the topmost tracked RAIL window containing the given
+ * display coordinates, if any.
+ *
+ * This is used as a local fallback for RemoteApp+GFX window ordering. Windows
+ * may accept a click on a RemoteApp window without sending a monitored desktop
+ * z-order or DESKTOP_ACTIVE_WND update. In that case, FreeRDP receives the
+ * input normally, but Guacamole's separate per-window layers would remain in
+ * the old visual order unless the clicked layer is raised locally. Sending
+ * ClientActivate mirrors native FreeRDP clients and makes sure the RDP server
+ * targets the clicked RemoteApp window before handling the mouse click.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param x
+ *     The X coordinate of the mouse event.
+ *
+ * @param y
+ *     The Y coordinate of the mouse event.
+ */
+void guac_rdp_rail_raise_window_at(struct guac_rdp_client* rdp_client,
+        int x, int y);
+
+/**
+ * Tracks which RAIL window owns the current mouse drag.
+ *
+ * FreeRDP sends RDP mouse input in desktop coordinates even for RemoteApp. This
+ * function records the clicked window so later drag/release events stay associated with it.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param mask
+ *     The current Guacamole mouse button mask.
+ *
+ * @param x
+ *     The X coordinate of the mouse event.
+ *
+ * @param y
+ *     The Y coordinate of the mouse event.
+ */
+void guac_rdp_rail_track_mouse_event(struct guac_rdp_client* rdp_client,
+        int mask, int x, int y);
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Handles notification that the given RDPGFX surface has been mapped to the
+ * given RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface mapping.
+ *
+ * @param surface_id
+ *     The server-side ID of the mapped RDPGFX surface.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to which the surface was mapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK. Surface mapping failures are treated as non-fatal
+ *     tracking failures. Window contents may not render (event gets logged), but the
+ *     RDPGFX channel remains usable.
+ */
+UINT guac_rdp_rail_map_window_for_surface(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT64 window_id);
+
+/**
+ * Handles notification that an RDPGFX surface is no longer mapped to the given
+ * RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface unmapping.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that was unmapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK is always returned. Surface unmapping failures are ignored as the window might not be tracked.

Review Comment:
   If you could wrap this line at 80 columns that'd be nice :-).



##########
src/protocols/rdp/input-queue.c:
##########
@@ -59,6 +60,8 @@ static void guac_rdp_handle_mouse_event(guac_rdp_client* rdp_client,
     guac_user* user = event->user;
     int x = event->details.mouse.x;
     int y = event->details.mouse.y;
+    int rdp_x = x;
+    int rdp_y = y;

Review Comment:
   I'm looking through the code, here, and I'm not seeing a reason why we need to make copies of the `x` and `y` variables in this function? Does one of the other functions where they are passed as an argument through here potentially update them? Or is there some other need to duplicate them?



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -66,20 +79,30 @@ static UINT guac_rdp_rail_complete_handshake(RailClientContext* rail) {
     guac_client* client = (guac_client*) rail->custom;
     guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
 
+    /* Do not advertise TS_RAIL_CLIENTSTATUS_ALLOWLOCALMOVESIZE. Guacamole
+     * does not manage native local windows and does not send ClientWindowMove
+     * when a local move/resize completes. Advertising that capability can
+     * cause the server to enter a move/resize path that Guacamole cannot
+     * finish, leaving subsequent input ignored after dragging a RemoteApp
+     * window. */
     RAIL_CLIENT_STATUS_ORDER client_status = {
-        .flags =
-                TS_RAIL_CLIENTSTATUS_ALLOWLOCALMOVESIZE
-              | TS_RAIL_CLIENTSTATUS_APPBAR_REMOTING_SUPPORTED
+        .flags = TS_RAIL_CLIENTSTATUS_APPBAR_REMOTING_SUPPORTED
     };
 
     /* Send client status */
-    guac_client_log(client, GUAC_LOG_TRACE, "Sending RAIL client status.");
+    guac_client_log(client, GUAC_LOG_DEBUG,

Review Comment:
   Is there a reason why this should be `DEBUG` level and not remain `TRACE`? This seems like pretty low-level log messages, particularly including the actual flags, so I'm thinking `TRACE` is more appropriate?



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -136,11 +159,24 @@ static UINT guac_rdp_rail_complete_handshake(RailClientContext* rail) {
     };
 
     /* Execute desired RemoteApp command */
-    guac_client_log(client, GUAC_LOG_TRACE, "Executing remote application.");
+    guac_client_log(client, GUAC_LOG_TRACE,
+            "Executing remote application: program=\"%s\", args=\"%s\", "
+            "working-dir=\"%s\", flags=0x%04x.",
+            exec.RemoteApplicationProgram != NULL
+                ? exec.RemoteApplicationProgram : "",
+            exec.RemoteApplicationArguments != NULL
+                ? exec.RemoteApplicationArguments : "",
+            exec.RemoteApplicationWorkingDir != NULL
+                ? exec.RemoteApplicationWorkingDir : "",
+            (unsigned int) exec.flags);
     pthread_mutex_lock(&(rdp_client->message_lock));
     status = rail->ClientExecute(rail, &exec);
     pthread_mutex_unlock(&(rdp_client->message_lock));
 
+    guac_client_log(client, GUAC_LOG_DEBUG,

Review Comment:
   Since the log message above for the `Executing remote application...` log is `TRACE`, this should probably also be `TRACE` rather than `DEBUG`.



##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +206,49 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx
+            || !rdp_client->rail_background_painted)
+        current_context = guac_display_layer_open_raw(default_layer);

Review Comment:
   This there some reason why this particular operation - opening the raw display layer - needs to happen prior to the `gdi_resize()` call just below this? It looks like there are two identical `if` statements, here, and I'm just wondering if this `guac_display_layer_open_raw()` call could be moved inside the `if` statement down on line 224, or if it needs to happen prior to the FreeRDP GDI buffer resize?



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -228,54 +269,1855 @@ static UINT guac_rdp_rail_handshake_ex(RailClientContext* rail,
     return guac_rdp_rail_complete_handshake(rail);
 }
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+/* 
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The RDP server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The RDPGFX surface ID currently mapped to this window, if known.
+     */
+    UINT16 surface_id;
+    int has_surface_id;
+
+    /**
+     * The RDP server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether an RDPGFX surface update arrived before the RAIL window order.
+     */
+    int has_deferred_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list. Used to avoid applying the raise to 
+     * windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known. If the window does not yet 
+     * have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The latest dimensions reported by RAIL window orders.
+     */
+    int order_width;
+    int order_height;
+
+    /**
+     * The latest client-area bounds reported by RAIL window orders.
+     */
+    int client_x;
+    int client_y;
+    int client_width;
+    int client_height;
+    int has_client_area;
+
+    /**
+     * The dimensions of the mapped RDPGFX surface represented by the layer.
+     */
+    int surface_width;
+    int surface_height;
+
+    /**
+     * Number of RDPGFX paint operations currently using this window's layer.
+     */
+    int paint_refs;
+
+    /**
+     * Whether this window has been removed from the tracked window list and
+     * should be freed once all outstanding paints finish.
+     */
+    int deleted;
+
+} guac_rdp_rail_window;
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects);
+#endif
+
 /**
- * A callback function that is executed when an update for a RAIL window is
- * received from the RDP server.
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
  *
- * @param context
- *     A pointer to the rdpContext structure used by FreeRDP to handle the
- *     window update.
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
  *
- * @param orderInfo
- *     A pointer to the data structure that contains information about what
- *     window was updated what updates were performed.
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
  *
- * @param windowState
- *     A pointer to the data structure that contains details of the updates
- *     to the window, as indicated by flags in the orderInfo field.
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
  *
  * @return
- *     TRUE if the client-side processing of the updates as successful; otherwise
- *     FALSE. This implementation always returns TRUE.
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
  */
-static BOOL guac_rdp_rail_window_update(rdpContext* context,
-        RAIL_CONST WINDOW_ORDER_INFO* orderInfo,
-        RAIL_CONST WINDOW_STATE_ORDER* windowState) {
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
 
-    guac_client* client = ((rdp_freerdp_context*) context)->client;
     guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
 
-    guac_client_log(client, GUAC_LOG_TRACE, "RAIL window update callback: %d", orderInfo->fieldFlags);
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
 
-    UINT32 fieldFlags = orderInfo->fieldFlags;
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
 
-    /* If the flag for window visibilty is set, check visibility. */
-    if (fieldFlags & WINDOW_ORDER_FIELD_SHOW) {
-        guac_client_log(client, GUAC_LOG_TRACE, "RAIL window visibility change: %d", windowState->showState);
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
 
-        /* State is either hidden or minimized - send restore command. */
-        if (windowState->showState == GUAC_RDP_RAIL_WINDOW_STATE_MINIMIZED) {
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
 
-            guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window minimized, sending restore command.");
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
 
-            RAIL_SYSCOMMAND_ORDER syscommand;
-            syscommand.windowId = orderInfo->windowId;
-            syscommand.command = SC_RESTORE;
-            rdp_client->rail_interface->ClientSystemCommand(rdp_client->rail_interface, &syscommand);
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the tracked RAIL window currently mapped to the given RDPGFX
+ * surface, or NULL if no such mapping is known. The rail_windows list lock
+ * must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param surface_id
+ *     The server-side ID of the RDPGFX surface to look up.
+ *
+ * @return
+ *     The matching tracked RAIL window, or NULL if no such mapping exists.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window_for_surface(
+        guac_rdp_client* rdp_client, UINT16 surface_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_surface_id
+                && rail_window->surface_id == surface_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    rail_window->z_order = z;
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {
+
+    int z = 1;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->z_order >= z)
+            z = rail_window->z_order + 1;
+    }
+
+    return z;
+
+}
+
+/**
+ * Raises all tracked windows owned by the given parent window above that
+ * parent's layer. The rail_windows list lock must be held.
+ * This is needed for showing popup/dialog windows owned by another window,
+ * which might otherwise stay hidden behind their owner or other RemoteApp windows.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param owner_window_id
+ *     The server-side ID of the owner window.
+ *
+ * @param z
+ *     The z-order to assign to the first owned window.
+ *
+ * @return
+ *     The next available z-order after all owned windows have been raised.
+ */
+static int guac_rdp_rail_raise_owned_windows(guac_rdp_client* rdp_client,
+        UINT64 owner_window_id, int z) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_rail_order
+                && rail_window->owner_window_id == owner_window_id
+                && rail_window->window_id != owner_window_id) {
+            guac_rdp_rail_stack_window(rail_window, z++);
+            z = guac_rdp_rail_raise_owned_windows(rdp_client,
+                    rail_window->window_id, z);
         }
+
+        current = current->next;
     }
 
-    return TRUE;
+    return z;
+
+}
+
+/**
+ * Raises the given window above its owner, if that owner is currently being
+ * tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param rail_window
+ *     The window that should be raised above its owner.
+ */
+static void guac_rdp_rail_raise_window_above_owner(
+        guac_rdp_client* rdp_client, guac_rdp_rail_window* rail_window) {
+
+    if (rail_window == NULL || rail_window->owner_window_id == 0)
+        return;
+
+    guac_rdp_rail_window* owner = guac_rdp_rail_get_window(rdp_client,
+            rail_window->owner_window_id);
+
+    if (owner == NULL)
+        return;
+
+    int z = guac_rdp_rail_get_top_z(rdp_client);
+
+    guac_rdp_rail_stack_window(rail_window, z);
+
+}
+
+/**
+ * Sends a RAIL Client Activate order for the given window.
+ *
+ * RAIL servers expect explicit activation notifications when the active
+ * RemoteApp window changes.
+ *
+ * Even if the window_id is 64 bits, MS RAIL protocol activation orders use a 32-bit window ID.
+ * Ignore any invalid window IDs that cannot be represented without truncating to 32-bits.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the window to activate or deactivate.
+ *
+ * @param enabled
+ *     Non-zero to activate the window, zero to deactivate it.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the activation order was sent successfully, or a
+ *     protocol-specific error code otherwise.
+ */
+static UINT guac_rdp_rail_send_activate(guac_rdp_client* rdp_client,
+        UINT64 window_id, BOOL enabled) {
+
+    if (rdp_client->rail_interface == NULL
+            || rdp_client->rail_interface->ClientActivate == NULL)
+        return CHANNEL_RC_OK;
+
+    if (window_id > UINT32_MAX)
+        return CHANNEL_RC_OK;
+
+    RAIL_ACTIVATE_ORDER activate = {
+        .windowId = (UINT32) window_id,
+        .enabled = enabled
+    };
+
+    pthread_mutex_lock(&(rdp_client->message_lock));
+    UINT status = rdp_client->rail_interface->ClientActivate(
+            rdp_client->rail_interface, &activate);
+    pthread_mutex_unlock(&(rdp_client->message_lock));
+
+    return status;
+
+}
+
+/**
+ * Sends the RAIL activation to switch focus to the given window.
+ *
+ * The previously active window is first deactivated, followed by activation
+ * of the requested window. The locally tracked active window ID is updated
+ * only if the activation request succeeds.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that should be activated.
+ */
+static void guac_rdp_rail_activate_window(guac_rdp_client* rdp_client,
+        UINT64 window_id) {
+
+    if (rdp_client->rail_active_window_id == window_id)
+        return;
+
+    if (guac_rdp_rail_send_activate(rdp_client, window_id, TRUE)
+            == CHANNEL_RC_OK)
+        rdp_client->rail_active_window_id = window_id;
+
+}
+
+void guac_rdp_rail_raise_window_at(guac_rdp_client* rdp_client,
+        int x, int y) {
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return;
+
+    if (rdp_client->rail_windows == NULL)
+        return;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* clicked_window = NULL;
+    UINT64 activate_window_id = 0;
+    int clicked_z = 0;
+    int has_no_surface_window = 0;
+    int highest_no_surface_z = 0;
+    int suppress_activation = 0;
+    int has_adjacent_nosurface_window = 0;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+
+        guac_rdp_rail_window* rail_window = current->data;
+        if (rail_window == NULL)
+            continue;
+
+        if (rail_window->visible && rail_window->has_rail_order
+                && !rail_window->has_surface) {
+
+            if (!has_no_surface_window
+                    || rail_window->z_order > highest_no_surface_z) {
+                highest_no_surface_z = rail_window->z_order;
+                has_no_surface_window = 1;
+            }
+
+            continue;
+        }
+
+        if (!rail_window->visible || !rail_window->has_surface
+                || !rail_window->has_rail_order)
+            continue;
+
+        if (x < rail_window->x || y < rail_window->y
+                || x >= rail_window->x + rail_window->layer_width
+                || y >= rail_window->y + rail_window->layer_height)
+            continue;
+
+        if (clicked_window == NULL || rail_window->z_order > clicked_z) {
+            clicked_window = rail_window;
+            clicked_z = rail_window->z_order;
+        }
+
+    }

Review Comment:
   Might be nice to have a few comments in here to document the code flow in-line.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -228,54 +269,1855 @@ static UINT guac_rdp_rail_handshake_ex(RailClientContext* rail,
     return guac_rdp_rail_complete_handshake(rail);
 }
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+/* 
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The RDP server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The RDPGFX surface ID currently mapped to this window, if known.
+     */
+    UINT16 surface_id;
+    int has_surface_id;
+
+    /**
+     * The RDP server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether an RDPGFX surface update arrived before the RAIL window order.
+     */
+    int has_deferred_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list. Used to avoid applying the raise to 
+     * windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known. If the window does not yet 
+     * have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The latest dimensions reported by RAIL window orders.
+     */
+    int order_width;
+    int order_height;
+
+    /**
+     * The latest client-area bounds reported by RAIL window orders.
+     */
+    int client_x;
+    int client_y;
+    int client_width;
+    int client_height;
+    int has_client_area;
+
+    /**
+     * The dimensions of the mapped RDPGFX surface represented by the layer.
+     */
+    int surface_width;
+    int surface_height;
+
+    /**
+     * Number of RDPGFX paint operations currently using this window's layer.
+     */
+    int paint_refs;
+
+    /**
+     * Whether this window has been removed from the tracked window list and
+     * should be freed once all outstanding paints finish.
+     */
+    int deleted;
+
+} guac_rdp_rail_window;
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects);
+#endif
+
 /**
- * A callback function that is executed when an update for a RAIL window is
- * received from the RDP server.
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
  *
- * @param context
- *     A pointer to the rdpContext structure used by FreeRDP to handle the
- *     window update.
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
  *
- * @param orderInfo
- *     A pointer to the data structure that contains information about what
- *     window was updated what updates were performed.
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
  *
- * @param windowState
- *     A pointer to the data structure that contains details of the updates
- *     to the window, as indicated by flags in the orderInfo field.
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
  *
  * @return
- *     TRUE if the client-side processing of the updates as successful; otherwise
- *     FALSE. This implementation always returns TRUE.
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
  */
-static BOOL guac_rdp_rail_window_update(rdpContext* context,
-        RAIL_CONST WINDOW_ORDER_INFO* orderInfo,
-        RAIL_CONST WINDOW_STATE_ORDER* windowState) {
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
 
-    guac_client* client = ((rdp_freerdp_context*) context)->client;
     guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
 
-    guac_client_log(client, GUAC_LOG_TRACE, "RAIL window update callback: %d", orderInfo->fieldFlags);
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
 
-    UINT32 fieldFlags = orderInfo->fieldFlags;
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
 
-    /* If the flag for window visibilty is set, check visibility. */
-    if (fieldFlags & WINDOW_ORDER_FIELD_SHOW) {
-        guac_client_log(client, GUAC_LOG_TRACE, "RAIL window visibility change: %d", windowState->showState);
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
 
-        /* State is either hidden or minimized - send restore command. */
-        if (windowState->showState == GUAC_RDP_RAIL_WINDOW_STATE_MINIMIZED) {
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
 
-            guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window minimized, sending restore command.");
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
 
-            RAIL_SYSCOMMAND_ORDER syscommand;
-            syscommand.windowId = orderInfo->windowId;
-            syscommand.command = SC_RESTORE;
-            rdp_client->rail_interface->ClientSystemCommand(rdp_client->rail_interface, &syscommand);
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the tracked RAIL window currently mapped to the given RDPGFX
+ * surface, or NULL if no such mapping is known. The rail_windows list lock
+ * must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param surface_id
+ *     The server-side ID of the RDPGFX surface to look up.
+ *
+ * @return
+ *     The matching tracked RAIL window, or NULL if no such mapping exists.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window_for_surface(
+        guac_rdp_client* rdp_client, UINT16 surface_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_surface_id
+                && rail_window->surface_id == surface_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    rail_window->z_order = z;
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {
+
+    int z = 1;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->z_order >= z)
+            z = rail_window->z_order + 1;
+    }
+
+    return z;
+
+}
+
+/**
+ * Raises all tracked windows owned by the given parent window above that
+ * parent's layer. The rail_windows list lock must be held.
+ * This is needed for showing popup/dialog windows owned by another window,
+ * which might otherwise stay hidden behind their owner or other RemoteApp windows.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param owner_window_id
+ *     The server-side ID of the owner window.
+ *
+ * @param z
+ *     The z-order to assign to the first owned window.
+ *
+ * @return
+ *     The next available z-order after all owned windows have been raised.
+ */
+static int guac_rdp_rail_raise_owned_windows(guac_rdp_client* rdp_client,
+        UINT64 owner_window_id, int z) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_rail_order
+                && rail_window->owner_window_id == owner_window_id
+                && rail_window->window_id != owner_window_id) {
+            guac_rdp_rail_stack_window(rail_window, z++);
+            z = guac_rdp_rail_raise_owned_windows(rdp_client,
+                    rail_window->window_id, z);
         }
+
+        current = current->next;
     }
 
-    return TRUE;
+    return z;
+
+}
+
+/**
+ * Raises the given window above its owner, if that owner is currently being
+ * tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param rail_window
+ *     The window that should be raised above its owner.
+ */
+static void guac_rdp_rail_raise_window_above_owner(
+        guac_rdp_client* rdp_client, guac_rdp_rail_window* rail_window) {
+
+    if (rail_window == NULL || rail_window->owner_window_id == 0)
+        return;
+
+    guac_rdp_rail_window* owner = guac_rdp_rail_get_window(rdp_client,
+            rail_window->owner_window_id);
+
+    if (owner == NULL)
+        return;
+
+    int z = guac_rdp_rail_get_top_z(rdp_client);
+
+    guac_rdp_rail_stack_window(rail_window, z);
+
+}
+
+/**
+ * Sends a RAIL Client Activate order for the given window.
+ *
+ * RAIL servers expect explicit activation notifications when the active
+ * RemoteApp window changes.
+ *
+ * Even if the window_id is 64 bits, MS RAIL protocol activation orders use a 32-bit window ID.
+ * Ignore any invalid window IDs that cannot be represented without truncating to 32-bits.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the window to activate or deactivate.
+ *
+ * @param enabled
+ *     Non-zero to activate the window, zero to deactivate it.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the activation order was sent successfully, or a
+ *     protocol-specific error code otherwise.
+ */
+static UINT guac_rdp_rail_send_activate(guac_rdp_client* rdp_client,
+        UINT64 window_id, BOOL enabled) {
+
+    if (rdp_client->rail_interface == NULL
+            || rdp_client->rail_interface->ClientActivate == NULL)
+        return CHANNEL_RC_OK;
+
+    if (window_id > UINT32_MAX)
+        return CHANNEL_RC_OK;
+
+    RAIL_ACTIVATE_ORDER activate = {
+        .windowId = (UINT32) window_id,
+        .enabled = enabled
+    };
+
+    pthread_mutex_lock(&(rdp_client->message_lock));
+    UINT status = rdp_client->rail_interface->ClientActivate(
+            rdp_client->rail_interface, &activate);
+    pthread_mutex_unlock(&(rdp_client->message_lock));
+
+    return status;
+
+}
+
+/**
+ * Sends the RAIL activation to switch focus to the given window.
+ *
+ * The previously active window is first deactivated, followed by activation
+ * of the requested window. The locally tracked active window ID is updated
+ * only if the activation request succeeds.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that should be activated.
+ */
+static void guac_rdp_rail_activate_window(guac_rdp_client* rdp_client,
+        UINT64 window_id) {
+
+    if (rdp_client->rail_active_window_id == window_id)
+        return;
+
+    if (guac_rdp_rail_send_activate(rdp_client, window_id, TRUE)
+            == CHANNEL_RC_OK)
+        rdp_client->rail_active_window_id = window_id;
+
+}
+
+void guac_rdp_rail_raise_window_at(guac_rdp_client* rdp_client,
+        int x, int y) {
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return;
+
+    if (rdp_client->rail_windows == NULL)
+        return;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* clicked_window = NULL;
+    UINT64 activate_window_id = 0;
+    int clicked_z = 0;
+    int has_no_surface_window = 0;
+    int highest_no_surface_z = 0;
+    int suppress_activation = 0;
+    int has_adjacent_nosurface_window = 0;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+
+        guac_rdp_rail_window* rail_window = current->data;
+        if (rail_window == NULL)
+            continue;
+
+        if (rail_window->visible && rail_window->has_rail_order
+                && !rail_window->has_surface) {
+
+            if (!has_no_surface_window
+                    || rail_window->z_order > highest_no_surface_z) {
+                highest_no_surface_z = rail_window->z_order;
+                has_no_surface_window = 1;
+            }
+
+            continue;
+        }
+
+        if (!rail_window->visible || !rail_window->has_surface
+                || !rail_window->has_rail_order)
+            continue;
+
+        if (x < rail_window->x || y < rail_window->y
+                || x >= rail_window->x + rail_window->layer_width
+                || y >= rail_window->y + rail_window->layer_height)
+            continue;
+
+        if (clicked_window == NULL || rail_window->z_order > clicked_z) {
+            clicked_window = rail_window;
+            clicked_z = rail_window->z_order;
+        }
+
+    }
+
+    if (clicked_window != NULL) {
+        int z = guac_rdp_rail_get_top_z(rdp_client);
+
+        /* A higher no-surface RAIL window can represent a menu before its
+         * drawable popup surface arrives. Avoid reactivating the window below
+         * it, or the menu may close before the click is delivered. */
+        if (has_no_surface_window
+                && highest_no_surface_z > clicked_window->z_order) {
+            suppress_activation = 1;
+        }
+
+        /* Right-click context menus may not report an owner window, but in
+         * that case they're stacked immediately above a visible no-surface
+         * RAIL window (surface and window are separate). Treat that pairing
+         * as a menu popup and skip pre-click activation so menu-item clicks
+         * are not dismissed. */
+        if (has_no_surface_window
+                && highest_no_surface_z == clicked_window->z_order - 1) {
+            suppress_activation = 1;
+            has_adjacent_nosurface_window = 1;
+        }
+
+        /* Regular menu popups are owned windows. Activating them before the
+         * click can dismiss or disturb the menu selection. */
+        if (clicked_window->owner_window_id != 0) {
+            suppress_activation = 1;
+        }
+
+        if (!suppress_activation)
+            activate_window_id = clicked_window->window_id;
+
+        if (!has_adjacent_nosurface_window
+                && !clicked_window->in_monitored_desktop_zorder
+                && clicked_window->z_order < z - 1) {
+            guac_rdp_rail_stack_window(clicked_window, z);
+            guac_rdp_rail_raise_owned_windows(rdp_client,
+                    clicked_window->window_id, z + 1);
+            rdp_client->gdi_modified = 1;
+        }
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    if (activate_window_id != 0)
+        guac_rdp_rail_activate_window(rdp_client, activate_window_id);
+
+}
+
+void guac_rdp_rail_track_mouse_event(guac_rdp_client* rdp_client,
+        int mask, int x, int y) {
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return;
+
+    if (rdp_client->rail_windows == NULL)
+        return;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* target_window = NULL;
+
+    if (rdp_client->mouse_button_mask != 0
+            && rdp_client->rail_mouse_window_id != 0)
+        target_window = guac_rdp_rail_get_window(rdp_client,
+                rdp_client->rail_mouse_window_id);
+
+    if (target_window == NULL) {
+
+        int target_z = 0;
+
+        for (guac_common_list_element* current = rdp_client->rail_windows->head;
+                current != NULL; current = current->next) {
+
+            guac_rdp_rail_window* rail_window = current->data;
+            if (rail_window == NULL)
+                continue;
+
+            if (!rail_window->visible || !rail_window->has_surface
+                    || !rail_window->has_rail_order)
+                continue;
+
+            if (x < rail_window->x || y < rail_window->y
+                    || x >= rail_window->x + rail_window->layer_width
+                    || y >= rail_window->y + rail_window->layer_height)
+                continue;
+
+            if (target_window == NULL || rail_window->z_order > target_z) {
+                target_window = rail_window;
+                target_z = rail_window->z_order;
+            }
+
+        }
+
+    }
+
+    if (target_window != NULL
+            && rdp_client->mouse_button_mask == 0 && mask != 0)
+        rdp_client->rail_mouse_window_id = target_window->window_id;
+
+    if (mask == 0)
+        rdp_client->rail_mouse_window_id = 0;
+
+    guac_common_list_unlock(rdp_client->rail_windows);

Review Comment:
   Similar to above, a handful of comments in here would be helpful.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3254304706


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -306,10 +403,14 @@ static UINT guac_rdp_rail_complete_handshake(RailClientContext* rail) {
     guac_client* client = (guac_client*) rail->custom;
     guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
 
+    /* Do not advertise TS_RAIL_CLIENTSTATUS_ALLOWLOCALMOVESIZE. Guacamole
+     * does not manage native local windows and does not send ClientWindowMove
+     * when a local move/resize completes. Advertising that capability can
+     * cause the server to enter a move/resize path that Guacamole cannot
+     * finish, leaving subsequent input ignored after dragging a RemoteApp
+     * window. */
     RAIL_CLIENT_STATUS_ORDER client_status = {
-        .flags =

Review Comment:
   Cannot use TS_RAIL_CLIENTSTATUS_ALLOWLOCALMOVESIZE, or we might lose mouse input. We obey positioning info from server instead, which fixes the issue.
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420615458


##########
src/protocols/rdp/gdi.c:
##########
@@ -83,10 +102,16 @@ BOOL guac_rdp_gdi_begin_paint(rdpContext* context) {
     guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
     rdp_client->current_context = current_context;
 
-    /* Resynchronize default layer buffer details with FreeRDP's GDI */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    /*
+     * For full desktop sessions, the default layer shares FreeRDP's primary
+     * GDI buffer. For RemoteApp sessions, keep the default layer on its own
+     * black buffer and allow FreeRDP to paint the hidden server desktop only
+     * into its own primary buffer.
+     */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx

Review Comment:
   Use primary buffer only if RAIL is disabled, GFX is disabled or both are enabled, but we're not yet received first paint event. This fixes the issue where invalid credentials ends up to Windows login screen on non-NLA mode, but login screen is not visible. Earlier we rendered black screen in that case.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531043459


##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +206,49 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx
+            || !rdp_client->rail_background_painted)
+        current_context = guac_display_layer_open_raw(default_layer);

Review Comment:
   guac_display_layer_open_raw() needs to happen before gdi_resize() to prevent rendering thread accessing the buffer without write lock. guac_display_layer_open_raw() acquires the lock before we do any resizing for the buffer. Without lock, we may get artifacts or worse.
   It's mentioned in the comment, changed the wording to be more precise.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub) via dev" <de...@guacamole.apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r4598026753


##########
src/protocols/rdp/channels/rdpgfx.c:
##########
@@ -58,16 +61,72 @@ static void guac_rdp_rdpgfx_channel_connected(rdpContext* context,
     if (strcmp(args->name, RDPGFX_DVC_CHANNEL_NAME) != 0)
         return;
 
-    /* Init GDI-backed support for the Graphics Pipeline */

Review Comment:
   It's now updated below
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3253257467


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,235 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows list lock must be held.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve or create.
+ *
+ * @return
+ *     The matching or newly-created RAIL window, or NULL if the window could not be created.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate window state.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 0);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate display layer.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);
+
+    guac_common_list_add(rdp_client->rail_windows, rail_window);
+
+    return rail_window;
+}
+
+/**
+ * Updates the visibility of the Guacamole layer backing the given RAIL window.
+ * The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should have its opacity updated.
+ */
+static void guac_rdp_rail_update_window_opacity(
+        guac_rdp_rail_window* rail_window) {
+
+    int opacity = (rail_window->visible
+            && rail_window->has_surface
+            && rail_window->has_rail_order) ? 0xFF : 0;
+    guac_display_layer_set_opacity(rail_window->layer, opacity);
+}
+
+/**
+ * Frees the resources associated with the given RAIL window.
+ *
+ * @param data
+ *     The RAIL window to free.
+ */
+static void guac_rdp_rail_free_window_data(void* data) {
+
+    guac_rdp_rail_window* rail_window = data;

Review Comment:
   This may work as-is, but shouldn't this be explicitly type-cast?
   ```
   guac_rdp_rail_window* rail_window = (guac_rdp_rail_window*) data;



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420571242


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,376 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @return
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
+ */
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
+
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
+
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
+
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
+
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows list lock must be held.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve or create.
+ *
+ * @return
+ *     The matching or newly-created RAIL window, or NULL if the window could not be created.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    guac_display_layer* parent_layer =
+        guac_rdp_rail_get_window_layer(client);
+
+    if (parent_layer == NULL)
+        return NULL;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate window state.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    /* Allocate a child layer so window z-order stays within the RAIL group. */
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 1);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate display layer.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(rail_window->layer, parent_layer);
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);
+
+    guac_common_list_add(rdp_client->rail_windows, rail_window);
+
+    return rail_window;
+}
+
+/**
+ * Updates the visibility of the Guacamole layer backing the given RAIL window.
+ * The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should have its opacity updated.
+ */
+static void guac_rdp_rail_update_window_opacity(
+        guac_rdp_rail_window* rail_window) {
+
+    int opacity = (rail_window->visible
+            && rail_window->has_surface
+            && rail_window->has_rail_order) ? 0xFF : 0;
+    guac_display_layer_set_opacity(rail_window->layer, opacity);
+}
+
+static void guac_rdp_rail_paint_background(guac_rdp_client* rdp_client) {

Review Comment:
   Moved the blackground painting here, as earlier we rendered the black background for RAIL apps too soon before the login was done. This caused screen to remain black if login failed.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420615458


##########
src/protocols/rdp/gdi.c:
##########
@@ -83,10 +102,16 @@ BOOL guac_rdp_gdi_begin_paint(rdpContext* context) {
     guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
     rdp_client->current_context = current_context;
 
-    /* Resynchronize default layer buffer details with FreeRDP's GDI */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    /*
+     * For full desktop sessions, the default layer shares FreeRDP's primary
+     * GDI buffer. For RemoteApp sessions, keep the default layer on its own
+     * black buffer and allow FreeRDP to paint the hidden server desktop only
+     * into its own primary buffer.
+     */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx

Review Comment:
   Use primary buffer only if RAIL is disabled, GFX is disabled or both are enabled, but we're not yet received first paint event. This fixes the issue where invalid login ends up to Windows login screen on non-NLA mode. Earlier we rendered black screen in that case, now we start rendering black background only after the first RAIL windows arrive.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3254307422


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -594,10 +718,107 @@ static BOOL guac_rdp_rail_window_delete(rdpContext* context,
     guac_common_list_unlock(rdp_client->rail_windows);
 
     return TRUE;
+}
+
+/**
+ * Handler for RAIL monitored desktop orders. These orders include the
+ * server-side z-order of RemoteApp windows as well setting active window.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing desktop order flags.
+ *
+ * @param monitoredDesktop
+ *     A pointer to the monitored desktop state sent by the server.
+ *
+ * @return
+ *     TRUE if the desktop order was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_monitored_desktop(rdpContext* context,

Review Comment:
   The window ordering was broken earlier, now we obey server sent z-order for the windows. We also set active window according to server orders. Server z-ordering is the source of truth (if available).
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420716751


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -276,6 +733,393 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
     }
 
     return TRUE;
+}
+
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing information about the window that was deleted.
+ * 
+ * @return
+ *     TRUE if the window deletion was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_common_list_element* element =
+        guac_rdp_rail_get_window_element(rdp_client, orderInfo->windowId);
+
+    if (element != NULL) {
+        guac_rdp_rail_free_window_data(element->data);
+        guac_common_list_remove(rdp_client->rail_windows, element);
+        rdp_client->gdi_modified = 1;
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    return TRUE;
+}
+
+/**
+ * Handler for RAIL monitored desktop orders. These orders include the
+ * server-side z-order of RemoteApp windows as well setting active window.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing desktop order flags.
+ *
+ * @param monitoredDesktop
+ *     A pointer to the monitored desktop state sent by the server.
+ *
+ * @return
+ *     TRUE if the desktop order was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_monitored_desktop(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo,
+        RAIL_CONST MONITORED_DESKTOP_ORDER* monitoredDesktop) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    if (!(orderInfo->fieldFlags & (WINDOW_ORDER_FIELD_DESKTOP_ZORDER
+                    | WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)))
+        return TRUE;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ZORDER)
+            && monitoredDesktop->windowIds != NULL) {
+
+        /* The server-provided list is authoritative for visual stacking. */
+        for (UINT32 i = 0; i < monitoredDesktop->numWindowIds; i++) {
+
+            guac_rdp_rail_window* rail_window =
+                guac_rdp_rail_get_window(rdp_client,
+                        monitoredDesktop->windowIds[i]);
+
+            int z = (int) (monitoredDesktop->numWindowIds - i);
+
+            if (rail_window != NULL) {
+                guac_rdp_rail_stack_window(rail_window, z);
+                rdp_client->gdi_modified = 1;
+            }
+
+        }
+
+    }
+
+    if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)
+            && monitoredDesktop->activeWindowId != 0) {
+
+        /* Active window indicates focus only; do not use it to raise layers. */
+        guac_rdp_rail_window* active_window =
+            guac_rdp_rail_get_window(rdp_client,
+                    monitoredDesktop->activeWindowId);
+
+        if (active_window != NULL) {
+
+            if (!active_window->has_rail_order) {
+                active_window->has_rail_order = 1;
+                active_window->visible = 1;
+                guac_rdp_rail_update_window_opacity(active_window);
+            }
+
+            rdp_client->gdi_modified = 1;
+        }
+
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    return TRUE;
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+UINT guac_rdp_rail_map_window_for_surface(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT64 window_id) {
+
+    (void) context;
+    (void) surface_id;
+    (void) window_id;
+
+    return CHANNEL_RC_OK;
+}
+
+UINT guac_rdp_rail_unmap_window_for_surface(RdpgfxClientContext* context,
+        UINT64 window_id) {
+
+    (void) context;
+    (void) window_id;
+
+    return CHANNEL_RC_OK;
+}
+
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows list lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param surface
+ *     The RDPGFX surface containing the updated window contents.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface to copy.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+ */
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    if (surface == NULL || surface->windowId == 0
+            || rects == NULL || rect_count == 0)
+        return CHANNEL_RC_OK;
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, surface->windowId);
+
+    if (rail_window == NULL) {
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    if (!rail_window->has_rail_order) {
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    if (!rail_window->visible) {
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    /* Per FreeRDP's gdi/gfx.c and xf_gfx.c, mappedWidth/Height and
+     * outputTargetWidth/Height are initialized to the same value at surface
+     * creation, and only diverge when an explicit MapSurfaceToScaledWindow
+     * order rescales the surface. We honor any divergence but optimize for
+     * the common 1:1 case with a plain copy. */
+    int source_width = surface->mappedWidth;
+    int source_height = surface->mappedHeight;
+    int target_width = surface->outputTargetWidth;
+    int target_height = surface->outputTargetHeight;
+
+    if (target_width <= 0)
+        target_width = source_width;
+    if (target_height <= 0)
+        target_height = source_height;
+
+    if (source_width <= 0 || source_height <= 0
+            || target_width <= 0 || target_height <= 0) {
+        rail_window->has_surface = 0;
+        guac_rdp_rail_update_window_opacity(rail_window);
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    int scaled = (source_width != target_width
+            || source_height != target_height);
+
+    if (rail_window->layer_width != target_width
+            || rail_window->layer_height != target_height) {
+        guac_display_layer_resize(rail_window->layer,
+                target_width, target_height);
+        rail_window->layer_width = target_width;
+        rail_window->layer_height = target_height;
+    }
+
+    guac_display_layer_raw_context* layer_context =
+        guac_display_layer_open_raw(rail_window->layer);
+
+    guac_rect source_bounds;
+    guac_rect_init(&source_bounds, 0, 0, source_width, source_height);
+
+    /* RDPGFX window surfaces are copied as opaque layer content. We don't
+     * want holes on our RemoteApp windows. */
+    UINT32 dst_format = guac_rdp_get_native_pixel_format(FALSE);
+    int copied = 0;
+
+    for (UINT32 i = 0; i < rect_count; i++) {
+
+        const RECTANGLE_16* rect = &rects[i];
+        guac_rect src_rect;
+        guac_rect_init(&src_rect, rect->left, rect->top,
+                rect->right - rect->left, rect->bottom - rect->top);
+
+        guac_rect_constrain(&src_rect, &source_bounds);
+        if (guac_rect_is_empty(&src_rect))
+            continue;
+
+        guac_rect dst_rect;
+        if (scaled) {
+            dst_rect.left =
+                ((UINT64) src_rect.left * target_width) / source_width;
+            dst_rect.top =
+                ((UINT64) src_rect.top * target_height) / source_height;
+            dst_rect.right =
+                (((UINT64) src_rect.right * target_width)
+                    + source_width - 1) / source_width;
+            dst_rect.bottom =
+                (((UINT64) src_rect.bottom * target_height)
+                    + source_height - 1) / source_height;
+        }
+        else {
+            dst_rect = src_rect;
+        }
+
+        guac_rect_constrain(&dst_rect, &layer_context->bounds);
+        if (guac_rect_is_empty(&dst_rect))
+            continue;
+
+        BOOL success;
+        if (!scaled)
+            success = freerdp_image_copy(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    dst_format,
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    NULL, FREERDP_FLIP_NONE);
+        else
+            success = freerdp_image_scale(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    dst_format,
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    guac_rect_width(&src_rect),
+                    guac_rect_height(&src_rect));
+
+        if (!success) {
+            guac_display_layer_close_raw(rail_window->layer, layer_context);
+            guac_common_list_unlock(rdp_client->rail_windows);
+            return ERROR_INTERNAL_ERROR;
+        }
+
+        guac_rect_extend(&layer_context->dirty, &dst_rect);
+        copied = 1;
+    }
+
+    guac_display_layer_close_raw(rail_window->layer, layer_context);
+    if (!copied) {
+        guac_common_list_unlock(rdp_client->rail_windows);

Review Comment:
   Let's not render background before window rendering has started and we have received app data.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420720232


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -276,6 +733,393 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
     }
 
     return TRUE;
+}
+
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing information about the window that was deleted.
+ * 
+ * @return
+ *     TRUE if the window deletion was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_common_list_element* element =
+        guac_rdp_rail_get_window_element(rdp_client, orderInfo->windowId);
+
+    if (element != NULL) {
+        guac_rdp_rail_free_window_data(element->data);
+        guac_common_list_remove(rdp_client->rail_windows, element);
+        rdp_client->gdi_modified = 1;
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    return TRUE;
+}
+
+/**
+ * Handler for RAIL monitored desktop orders. These orders include the
+ * server-side z-order of RemoteApp windows as well setting active window.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing desktop order flags.
+ *
+ * @param monitoredDesktop
+ *     A pointer to the monitored desktop state sent by the server.
+ *
+ * @return
+ *     TRUE if the desktop order was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_monitored_desktop(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo,
+        RAIL_CONST MONITORED_DESKTOP_ORDER* monitoredDesktop) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    if (!(orderInfo->fieldFlags & (WINDOW_ORDER_FIELD_DESKTOP_ZORDER
+                    | WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)))
+        return TRUE;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ZORDER)
+            && monitoredDesktop->windowIds != NULL) {
+
+        /* The server-provided list is authoritative for visual stacking. */
+        for (UINT32 i = 0; i < monitoredDesktop->numWindowIds; i++) {
+
+            guac_rdp_rail_window* rail_window =
+                guac_rdp_rail_get_window(rdp_client,
+                        monitoredDesktop->windowIds[i]);
+
+            int z = (int) (monitoredDesktop->numWindowIds - i);
+
+            if (rail_window != NULL) {
+                guac_rdp_rail_stack_window(rail_window, z);
+                rdp_client->gdi_modified = 1;
+            }
+
+        }
+
+    }
+
+    if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)
+            && monitoredDesktop->activeWindowId != 0) {
+
+        /* Active window indicates focus only; do not use it to raise layers. */
+        guac_rdp_rail_window* active_window =
+            guac_rdp_rail_get_window(rdp_client,
+                    monitoredDesktop->activeWindowId);
+
+        if (active_window != NULL) {
+
+            if (!active_window->has_rail_order) {
+                active_window->has_rail_order = 1;
+                active_window->visible = 1;
+                guac_rdp_rail_update_window_opacity(active_window);
+            }
+
+            rdp_client->gdi_modified = 1;
+        }
+
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    return TRUE;
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+UINT guac_rdp_rail_map_window_for_surface(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT64 window_id) {
+
+    (void) context;
+    (void) surface_id;
+    (void) window_id;
+
+    return CHANNEL_RC_OK;
+}
+
+UINT guac_rdp_rail_unmap_window_for_surface(RdpgfxClientContext* context,
+        UINT64 window_id) {
+
+    (void) context;
+    (void) window_id;
+
+    return CHANNEL_RC_OK;
+}
+
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows list lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param surface
+ *     The RDPGFX surface containing the updated window contents.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface to copy.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+ */
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    if (surface == NULL || surface->windowId == 0
+            || rects == NULL || rect_count == 0)
+        return CHANNEL_RC_OK;
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, surface->windowId);
+
+    if (rail_window == NULL) {
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    if (!rail_window->has_rail_order) {
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    if (!rail_window->visible) {
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    /* Per FreeRDP's gdi/gfx.c and xf_gfx.c, mappedWidth/Height and
+     * outputTargetWidth/Height are initialized to the same value at surface
+     * creation, and only diverge when an explicit MapSurfaceToScaledWindow
+     * order rescales the surface. We honor any divergence but optimize for
+     * the common 1:1 case with a plain copy. */
+    int source_width = surface->mappedWidth;
+    int source_height = surface->mappedHeight;
+    int target_width = surface->outputTargetWidth;
+    int target_height = surface->outputTargetHeight;
+
+    if (target_width <= 0)
+        target_width = source_width;
+    if (target_height <= 0)
+        target_height = source_height;
+
+    if (source_width <= 0 || source_height <= 0
+            || target_width <= 0 || target_height <= 0) {
+        rail_window->has_surface = 0;
+        guac_rdp_rail_update_window_opacity(rail_window);
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+
+    int scaled = (source_width != target_width
+            || source_height != target_height);
+
+    if (rail_window->layer_width != target_width
+            || rail_window->layer_height != target_height) {
+        guac_display_layer_resize(rail_window->layer,
+                target_width, target_height);
+        rail_window->layer_width = target_width;
+        rail_window->layer_height = target_height;
+    }
+
+    guac_display_layer_raw_context* layer_context =
+        guac_display_layer_open_raw(rail_window->layer);
+
+    guac_rect source_bounds;
+    guac_rect_init(&source_bounds, 0, 0, source_width, source_height);
+
+    /* RDPGFX window surfaces are copied as opaque layer content. We don't
+     * want holes on our RemoteApp windows. */
+    UINT32 dst_format = guac_rdp_get_native_pixel_format(FALSE);
+    int copied = 0;
+
+    for (UINT32 i = 0; i < rect_count; i++) {
+
+        const RECTANGLE_16* rect = &rects[i];
+        guac_rect src_rect;
+        guac_rect_init(&src_rect, rect->left, rect->top,
+                rect->right - rect->left, rect->bottom - rect->top);
+
+        guac_rect_constrain(&src_rect, &source_bounds);
+        if (guac_rect_is_empty(&src_rect))
+            continue;
+
+        guac_rect dst_rect;
+        if (scaled) {
+            dst_rect.left =
+                ((UINT64) src_rect.left * target_width) / source_width;
+            dst_rect.top =
+                ((UINT64) src_rect.top * target_height) / source_height;
+            dst_rect.right =
+                (((UINT64) src_rect.right * target_width)
+                    + source_width - 1) / source_width;
+            dst_rect.bottom =
+                (((UINT64) src_rect.bottom * target_height)
+                    + source_height - 1) / source_height;
+        }
+        else {
+            dst_rect = src_rect;
+        }
+
+        guac_rect_constrain(&dst_rect, &layer_context->bounds);
+        if (guac_rect_is_empty(&dst_rect))
+            continue;
+
+        BOOL success;
+        if (!scaled)
+            success = freerdp_image_copy(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    dst_format,
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    NULL, FREERDP_FLIP_NONE);
+        else
+            success = freerdp_image_scale(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    dst_format,
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    guac_rect_width(&src_rect),
+                    guac_rect_height(&src_rect));
+
+        if (!success) {
+            guac_display_layer_close_raw(rail_window->layer, layer_context);
+            guac_common_list_unlock(rdp_client->rail_windows);
+            return ERROR_INTERNAL_ERROR;
+        }
+
+        guac_rect_extend(&layer_context->dirty, &dst_rect);
+        copied = 1;
+    }
+
+    guac_display_layer_close_raw(rail_window->layer, layer_context);
+    if (!copied) {
+        guac_common_list_unlock(rdp_client->rail_windows);
+        return CHANNEL_RC_OK;
+    }
+    guac_rdp_rail_paint_background(rdp_client);

Review Comment:
   Render only after data has been received. This prevents us blocking Windows login dialog on non-NLA logins, if credentials are incorrect.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531066449


##########
src/protocols/rdp/input-queue.c:
##########
@@ -59,6 +60,8 @@ static void guac_rdp_handle_mouse_event(guac_rdp_client* rdp_client,
     guac_user* user = event->user;
     int x = event->details.mouse.x;
     int y = event->details.mouse.y;
+    int rdp_x = x;
+    int rdp_y = y;

Review Comment:
   This was a leftover from when I was debugging scaling issues and had to preserve the x and y coordinates. It's no longer necessary, removed it.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3252813385


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -259,6 +482,45 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
     UINT32 fieldFlags = orderInfo->fieldFlags;
 
+    /* Track window location and visibility for RDPGFX RemoteApp rendering. */
+    if (guac_rdp_rail_uses_gfx(rdp_client)) {

Review Comment:
   Okay, that's fair - definitely not worth holding up getting this part of it merged so that this fix can go out with 1.6.1. Maybe I'll circle back to that another time...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3253226714


##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +202,46 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL)
+        current_context = guac_display_layer_open_raw(default_layer);
 
     /* Resize FreeRDP's GDI buffer */
     BOOL retval = gdi_resize(context->gdi, width, height);
     GUAC_ASSERT(gdi->primary_buffer != NULL);
 
-    /* Update our reference to the GDI buffer, as well as any structural
-     * details, which may now all be different */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    if (rdp_client->settings->remote_app == NULL) {
+        /* Full desktop render path: 
+         * Update our reference to the GDI buffer, as well as any structural
+         * details, which may now all be different */
+        guac_rdp_gdi_use_primary_buffer(current_context, gdi);
+
+        /* Resize layer to match new display dimensions and underlying buffer */
+        guac_display_layer_resize(default_layer, gdi->width, gdi->height);
+        guac_display_layer_close_raw(default_layer, current_context);
+    }
+    else {

Review Comment:
   Part of the background color bugfix. Remote app rendering path doesn't call guac_rdp_gdi_use_primary_buffer



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub) via dev" <de...@guacamole.apache.org>.
jjheinon commented on PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#issuecomment-6919524020

   - Rebased to apache:patch
   - Fixed the RAIL handshake changes for FreeRDP 3.27+. Compiled with FreeRDP 3.32.2 and 2.11.5.
   - Disabled H.264/AVC444 for RemoteApp to avoid ordering issues.
   - Force full repaint after resize.
   - Logged clearer error message when NLA is required by server but disabled.
   Still need to test it a bit.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3253226714


##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +202,46 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL)
+        current_context = guac_display_layer_open_raw(default_layer);
 
     /* Resize FreeRDP's GDI buffer */
     BOOL retval = gdi_resize(context->gdi, width, height);
     GUAC_ASSERT(gdi->primary_buffer != NULL);
 
-    /* Update our reference to the GDI buffer, as well as any structural
-     * details, which may now all be different */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    if (rdp_client->settings->remote_app == NULL) {
+        /* Full desktop render path: 
+         * Update our reference to the GDI buffer, as well as any structural
+         * details, which may now all be different */
+        guac_rdp_gdi_use_primary_buffer(current_context, gdi);
+
+        /* Resize layer to match new display dimensions and underlying buffer */
+        guac_display_layer_resize(default_layer, gdi->width, gdi->height);
+        guac_display_layer_close_raw(default_layer, current_context);
+    }
+    else {

Review Comment:
   Part of the background color bugfix for resize.
   Remote app rendering path keeps layers detached and doesn't call guac_rdp_gdi_use_primary_buffer



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3253227788


##########
src/protocols/rdp/gdi.c:
##########
@@ -35,6 +35,25 @@
 
 #include <stddef.h>
 
+/**
+ * Resynchronize default layer buffer details with FreeRDP's GDI.
+ * In RemoteApp sessions, this mapping would expose FreeRDP's hidden desktop
+ * buffer behind application windows, so this should only be used in full desktop mode.
+ *
+ * @param current_context
+ *     The raw context that should reference the primary GDI buffer.
+ *
+ * @param gdi
+ *     The FreeRDP GDI state whose primary buffer should be referenced.
+ */
+static void guac_rdp_gdi_use_primary_buffer(

Review Comment:
   Made this a function, as the code is called from multiple places.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3253227788


##########
src/protocols/rdp/gdi.c:
##########
@@ -35,6 +35,25 @@
 
 #include <stddef.h>
 
+/**
+ * Resynchronize default layer buffer details with FreeRDP's GDI.
+ * In RemoteApp sessions, this mapping would expose FreeRDP's hidden desktop
+ * buffer behind application windows, so this should only be used in full desktop mode.
+ *
+ * @param current_context
+ *     The raw context that should reference the primary GDI buffer.
+ *
+ * @param gdi
+ *     The FreeRDP GDI state whose primary buffer should be referenced.
+ */
+static void guac_rdp_gdi_use_primary_buffer(

Review Comment:
   Made this a function, as it's now called in two places.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3252851722


##########
src/protocols/rdp/rdp.c:
##########
@@ -536,6 +536,54 @@ static int guac_rdp_handle_connection(guac_client* client) {
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
     guac_display_layer_resize(default_layer, rdp_client->settings->width, rdp_client->settings->height);
 
+    /*
+     * For RemoteApp connections, the default layer
+     * represents the (invisible) desktop behind the application windows.
+     * Fill it once with opaque black; guac_rdp_gdi_end_paint refuses to
+     * mark it dirty again for the rest of the session, so FreeRDP's
+     * ongoing painting of the server desktop into the shared GDI primary
+     * buffer never reaches the client.
+     *
+     * Without this, GFX-enabled RemoteApp sessions show the server's
+     * desktop behind the application
+     * windows because gdi->primary_buffer shares memory with the default
+     * layer and FreeRDP paints all windowId==0 surfaces into it.
+     */
+    if (rdp_client->settings->remote_app != NULL) {
+
+        guac_display_layer_raw_context* bg_ctx =

Review Comment:
   Looks like this is not quite working, we set the background black too early. Desktop background (or in some cases, welcome screen) is still visible. Fixing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3426874994


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -687,8 +867,15 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
         if (rail_window != NULL) {
 
-            int needs_opacity_update = !rail_window->has_rail_order;
+            int had_rail_order = rail_window->has_rail_order;
+            int needs_opacity_update = !had_rail_order;
             rail_window->has_rail_order = 1;
+            int needs_raise = 0;
+
+            if (fieldFlags & WINDOW_ORDER_FIELD_OWNER) {

Review Comment:
   A window order includes the owner field. Make sure it's shown above the owner window. We don't call guac_rdp_rail_raise_window_above_owner immediately here, but afterwards when lock has been released.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3426874994


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -687,8 +867,15 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
         if (rail_window != NULL) {
 
-            int needs_opacity_update = !rail_window->has_rail_order;
+            int had_rail_order = rail_window->has_rail_order;
+            int needs_opacity_update = !had_rail_order;
             rail_window->has_rail_order = 1;
+            int needs_raise = 0;
+
+            if (fieldFlags & WINDOW_ORDER_FIELD_OWNER) {

Review Comment:
   A window order includes the owner field. Make sure it's shown above the owner window.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3426859770


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -276,10 +312,154 @@ static guac_common_list_element* guac_rdp_rail_get_window_element(
 static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
         int z) {
 
+    rail_window->z_order = z;
+
     guac_display_layer_stack(rail_window->layer, z);
 
 }
 
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {

Review Comment:
   When launching multiple remote apps, the current window layering code couldn't handle popup/child windows properly. Now we handle that by tracking the window ownership and making sure the active window popups are visible.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3426859770


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -276,10 +312,154 @@ static guac_common_list_element* guac_rdp_rail_get_window_element(
 static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
         int z) {
 
+    rail_window->z_order = z;
+
     guac_display_layer_stack(rail_window->layer, z);
 
 }
 
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {

Review Comment:
   The current window layering code couldn't handle popup/child windows properly. Now we handle that by tracking the window ownership and making sure the active window popups are visible.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3445812406


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -167,6 +996,11 @@ static UINT guac_rdp_rail_execute_result(RailClientContext* context,
 
     guac_client* client = (guac_client*) context->custom;
 
+    if (execResult->execResult == RAIL_EXEC_E_NOT_IN_ALLOWLIST) {

Review Comment:
   Apparently it is possible to get RAIL_EXEC_E_NOT_IN_ALLOWLIST error even when RemoteApp is launched properly, if the app in question launches other processes that are not in the AllowList.
   In this case you will get logged in and the app flashes on the screen but then you get kicked out.
   Not sure what the optimal behavior would be in this case, but kept the old functionality and just added a warning, so the Windows admin can go and modify their remote app configuration.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "stephzero1 (via GitHub)" <gi...@apache.org>.
stephzero1 commented on PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#issuecomment-4450629777

   great work


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3252800814


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,219 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == window_id)
+            return current;
+        current = current->next;
+    }
+
+    return NULL;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows_lock must be held.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve or create.
+ *
+ * @return
+ *     The matching or newly-created RAIL window, or NULL if the window could not be created.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 0);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);

Review Comment:
   visible -property is a server side flag, telling the window should be visible.
   We just hide it on the client side by setting layer opacity to 0, until both window coordinates and first surface data have both arrived.
   If we set visibility to 0, the window never appears.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3253227007


##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +202,46 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL)
+        current_context = guac_display_layer_open_raw(default_layer);
 
     /* Resize FreeRDP's GDI buffer */
     BOOL retval = gdi_resize(context->gdi, width, height);
     GUAC_ASSERT(gdi->primary_buffer != NULL);
 
-    /* Update our reference to the GDI buffer, as well as any structural
-     * details, which may now all be different */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    if (rdp_client->settings->remote_app == NULL) {
+        /* Full desktop render path: 
+         * Update our reference to the GDI buffer, as well as any structural
+         * details, which may now all be different */
+        guac_rdp_gdi_use_primary_buffer(current_context, gdi);

Review Comment:
   Moved the code here to the function guac_rdp_gdi_use_primary_buffer



##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +202,46 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL)
+        current_context = guac_display_layer_open_raw(default_layer);
 
     /* Resize FreeRDP's GDI buffer */
     BOOL retval = gdi_resize(context->gdi, width, height);
     GUAC_ASSERT(gdi->primary_buffer != NULL);
 
-    /* Update our reference to the GDI buffer, as well as any structural
-     * details, which may now all be different */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    if (rdp_client->settings->remote_app == NULL) {
+        /* Full desktop render path: 
+         * Update our reference to the GDI buffer, as well as any structural
+         * details, which may now all be different */
+        guac_rdp_gdi_use_primary_buffer(current_context, gdi);

Review Comment:
   Moved the code to the function guac_rdp_gdi_use_primary_buffer



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3254305464


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -416,6 +517,30 @@ static UINT guac_rdp_rail_execute_result(RailClientContext* context,
 
 }
 
+/**
+ * Callback invoked if the RDP server requests client-side local movement or
+ * resizing of a RemoteApp window.
+ *
+ * @param rail
+ *     The RailClientContext structure used by FreeRDP to handle the RAIL
+ *     channel for the current RDP session.
+ *
+ * @param localMoveSize
+ *     The local move/size request sent by the server.
+ *
+ * @return
+ *     CHANNEL_RC_OK (zero) as the order has been ignored successfully.
+ */
+static UINT guac_rdp_rail_local_move_size(RailClientContext* rail,

Review Comment:
   Just in case server sends the request, we answer with CHANNEL_RC_OK.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420571242


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,376 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @return
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
+ */
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
+
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
+
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
+
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
+
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows list lock must be held.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve or create.
+ *
+ * @return
+ *     The matching or newly-created RAIL window, or NULL if the window could not be created.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    guac_display_layer* parent_layer =
+        guac_rdp_rail_get_window_layer(client);
+
+    if (parent_layer == NULL)
+        return NULL;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate window state.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    /* Allocate a child layer so window z-order stays within the RAIL group. */
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 1);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate display layer.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(rail_window->layer, parent_layer);
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);
+
+    guac_common_list_add(rdp_client->rail_windows, rail_window);
+
+    return rail_window;
+}
+
+/**
+ * Updates the visibility of the Guacamole layer backing the given RAIL window.
+ * The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should have its opacity updated.
+ */
+static void guac_rdp_rail_update_window_opacity(
+        guac_rdp_rail_window* rail_window) {
+
+    int opacity = (rail_window->visible
+            && rail_window->has_surface
+            && rail_window->has_rail_order) ? 0xFF : 0;
+    guac_display_layer_set_opacity(rail_window->layer, opacity);
+}
+
+static void guac_rdp_rail_paint_background(guac_rdp_client* rdp_client) {

Review Comment:
   Moved the rendering here, as earlier we rendered the black background for RAIL apps too soon before the login was done. This caused screen to remain black if login failed.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531076814


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -66,20 +79,30 @@ static UINT guac_rdp_rail_complete_handshake(RailClientContext* rail) {
     guac_client* client = (guac_client*) rail->custom;
     guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
 
+    /* Do not advertise TS_RAIL_CLIENTSTATUS_ALLOWLOCALMOVESIZE. Guacamole
+     * does not manage native local windows and does not send ClientWindowMove
+     * when a local move/resize completes. Advertising that capability can
+     * cause the server to enter a move/resize path that Guacamole cannot
+     * finish, leaving subsequent input ignored after dragging a RemoteApp
+     * window. */
     RAIL_CLIENT_STATUS_ORDER client_status = {
-        .flags =
-                TS_RAIL_CLIENTSTATUS_ALLOWLOCALMOVESIZE
-              | TS_RAIL_CLIENTSTATUS_APPBAR_REMOTING_SUPPORTED
+        .flags = TS_RAIL_CLIENTSTATUS_APPBAR_REMOTING_SUPPORTED
     };
 
     /* Send client status */
-    guac_client_log(client, GUAC_LOG_TRACE, "Sending RAIL client status.");
+    guac_client_log(client, GUAC_LOG_DEBUG,

Review Comment:
   No reason, that was temporary, changed it back now.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531043459


##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +206,49 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx
+            || !rdp_client->rail_background_painted)
+        current_context = guac_display_layer_open_raw(default_layer);

Review Comment:
   guac_display_layer_open_raw() needs to happen before gdi_resize() to prevent rendering thread accessing the buffer without write lock. guac_display_layer_open_raw() acquires the lock before we do any resizing for the buffer.
   It's mentioned in the comment, changed the wording to be more precise.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531043459


##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +206,49 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx
+            || !rdp_client->rail_background_painted)
+        current_context = guac_display_layer_open_raw(default_layer);

Review Comment:
   guac_display_layer_open_raw() needs to happen before gdi_resize() to prevent rendering thread accessing the buffer while it is being modified without write lock. guac_display_layer_open_raw() acquires the lock before we do any resizing for the buffer. Without lock, we may get artifacts or worse.
   It's mentioned in the comment, changed the wording to be more precise.
   Maybe thr code should be changed to have two separate if branches instead of this.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3249493347


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,219 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;

Review Comment:
   It looks like this and several of the functions you implement are implementing a linked list. There's already a generic linked-list implementation provided within the guacd code:
   
   https://github.com/apache/guacamole-server/blob/main/src/common/common/list.h
   
   It might be worth at least considering re-using that rather than re-implementing all the linked list functions separately, here.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,219 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == window_id)
+            return current;
+        current = current->next;
+    }
+
+    return NULL;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows_lock must be held.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve or create.
+ *
+ * @return
+ *     The matching or newly-created RAIL window, or NULL if the window could not be created.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 0);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);

Review Comment:
   Why is the `visible` property being set to `1` if the layer is being hidden? Shouldn't these two items roughly match each other?



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,219 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ */

Review Comment:
   This comment block still needs a `@return` block.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -259,6 +482,45 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
     UINT32 fieldFlags = orderInfo->fieldFlags;
 
+    /* Track window location and visibility for RDPGFX RemoteApp rendering. */
+    if (guac_rdp_rail_uses_gfx(rdp_client)) {

Review Comment:
   Is GFX rendering really required in order to track RAIL windows and use separate layers for updates? It seems like this is something we could implement across the board for RAIL windows, using GFX when it is supported, but falling back to non-GFX methods?



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,219 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == window_id)
+            return current;
+        current = current->next;
+    }
+
+    return NULL;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows_lock must be held.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve or create.
+ *
+ * @return
+ *     The matching or newly-created RAIL window, or NULL if the window could not be created.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 0);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }

Review Comment:
   It may be worth adding a `DEBUG`-level log in here to indicate more precisely what the failure is, as these existing `WARNING` messages are identical and won't help an admin track down the actual reason for the failure (memory allocation vs. display layer allocation).



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -279,6 +541,322 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
 }
 
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing information about the window that was deleted.
+ * 
+ * @return
+ *     TRUE if the window deletion was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* previous = NULL;
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == orderInfo->windowId) {
+            guac_rdp_rail_free_window(rdp_client, previous, current);
+            rdp_client->gdi_modified = 1;
+            break;
+        }
+        previous = current;
+        current = current->next;
+    }
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return TRUE;
+
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows_lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param surface
+ *     The RDPGFX surface containing the updated window contents.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface to copy.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+ */
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    if (surface == NULL || surface->windowId == 0
+            || rects == NULL || rect_count == 0)
+        return CHANNEL_RC_OK;
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_or_create_window(client, surface->windowId);
+
+    if (rail_window == NULL) {
+        pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+        return CHANNEL_RC_OK;
+    }
+
+    /* Per FreeRDP's gdi/gfx.c and xf_gfx.c, mappedWidth/Height and
+     * outputTargetWidth/Height are initialized to the same value at surface
+     * creation, and only diverge when an explicit MapSurfaceToScaledWindow
+     * order rescales the surface. We honor any divergence but optimize for
+     * the common 1:1 case with a plain copy. */
+    int source_width = surface->mappedWidth;
+    int source_height = surface->mappedHeight;
+    int target_width = surface->outputTargetWidth;
+    int target_height = surface->outputTargetHeight;
+
+    if (target_width <= 0) 
+        target_width = source_width;
+    if (target_height <= 0) 
+        target_height = source_height;
+
+    if (source_width <= 0 || source_height <= 0
+            || target_width <= 0 || target_height <= 0) {
+        rail_window->has_surface = 0;
+        guac_rdp_rail_update_window_opacity(rail_window);
+        pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+        return CHANNEL_RC_OK;
+    }
+
+    int scaled = (source_width != target_width
+            || source_height != target_height);
+
+    if (rail_window->layer_width != target_width
+            || rail_window->layer_height != target_height) {
+        guac_display_layer_resize(rail_window->layer,
+                target_width, target_height);
+        rail_window->layer_width = target_width;
+        rail_window->layer_height = target_height;
+    }
+
+    guac_display_layer_raw_context* layer_context =
+        guac_display_layer_open_raw(rail_window->layer);
+
+    guac_rect source_bounds;
+    guac_rect_init(&source_bounds, 0, 0, source_width, source_height);
+
+    for (UINT32 i = 0; i < rect_count; i++) {
+
+        const RECTANGLE_16* rect = &rects[i];
+        guac_rect src_rect;
+        guac_rect_init(&src_rect, rect->left, rect->top,
+                rect->right - rect->left, rect->bottom - rect->top);
+
+        guac_rect_constrain(&src_rect, &source_bounds);
+        if (guac_rect_is_empty(&src_rect))
+            continue;
+
+        guac_rect dst_rect;
+        if (scaled) {
+            dst_rect.left =
+                ((UINT64) src_rect.left * target_width) / source_width;
+            dst_rect.top =
+                ((UINT64) src_rect.top * target_height) / source_height;
+            dst_rect.right =
+                (((UINT64) src_rect.right * target_width)
+                    + source_width - 1) / source_width;
+            dst_rect.bottom =
+                (((UINT64) src_rect.bottom * target_height)
+                    + source_height - 1) / source_height;
+        }
+        else {
+            dst_rect = src_rect;
+        }
+
+        guac_rect_constrain(&dst_rect, &layer_context->bounds);
+        if (guac_rect_is_empty(&dst_rect))
+            continue;
+
+        BOOL success;
+        if (!scaled)
+            success = freerdp_image_copy(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    guac_rdp_get_native_pixel_format(TRUE),
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    NULL, FREERDP_FLIP_NONE);
+        else
+            success = freerdp_image_scale(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    guac_rdp_get_native_pixel_format(TRUE),
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    guac_rect_width(&src_rect),
+                    guac_rect_height(&src_rect));
+
+        if (!success) {
+            guac_display_layer_close_raw(rail_window->layer, layer_context);
+            pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+            return ERROR_INTERNAL_ERROR;
+        }
+
+        guac_rect_extend(&layer_context->dirty, &dst_rect);
+
+    }
+
+    guac_display_layer_close_raw(rail_window->layer, layer_context);
+
+    rail_window->has_surface = 1;
+    guac_rdp_rail_update_window_opacity(rail_window);
+    rdp_client->gdi_modified = 1;
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return CHANNEL_RC_OK;
+
+}
+
+/**
+* Callback which is invoked when one or more areas of an RDPGFX surface have
+* been updated.
+*
+* @param context
+*     The RDPGFX client context associated with the surface update.
+*
+* @param surface_id
+*     The server-side ID of the updated RDPGFX surface.
+*
+* @param rect_count
+*     The number of rectangles within the rects array.
+*
+* @param rects
+*     The updated rectangular regions of the surface.
+*
+* @return
+*     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+*/
+UINT guac_rdp_rail_update_surface_area(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    rdpGdi* gdi = (rdpGdi*) context->custom;
+    if (gdi == NULL || gdi->context == NULL || gdi->suppressOutput)
+        return CHANNEL_RC_OK;
+
+    if (context->GetSurfaceData == NULL)
+        return CHANNEL_RC_OK;
+
+    gdiGfxSurface* surface =
+        (gdiGfxSurface*) context->GetSurfaceData(context, surface_id);
+
+    /* Only window-mapped surfaces are routed through us; non-RAIL surfaces
+     * are handled by FreeRDP's default GDI pipeline. We return early without
+     * touching invalidRegion so the default path can consume it normally on
+     * the next UpdateSurfaces tick. */
+    if (surface == NULL || surface->windowId == 0)
+        return CHANNEL_RC_OK;
+
+    guac_client* client = ((rdp_freerdp_context*) gdi->context)->client;
+    return guac_rdp_rail_paint_surface(client, surface, rect_count, rects);
+
+}
+
+#ifdef HAVE_RDPGFX_WINDOW_SURFACE_UPDATE
+/**
+* Callback which is invoked when an RDPGFX surface associated with a RAIL
+* window has been updated.
+*
+* @param context
+*     The RDPGFX client context associated with the surface update.
+*
+* @param surface
+*     The RDPGFX surface containing the updated window contents.
+*
+* @return
+*     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+*/
+UINT guac_rdp_rail_update_window_from_surface(RdpgfxClientContext* context,
+        gdiGfxSurface* surface) {
+
+    rdpGdi* gdi = (rdpGdi*) context->custom;
+    if (gdi == NULL || gdi->context == NULL || gdi->suppressOutput)
+        return CHANNEL_RC_OK;
+
+    if (surface == NULL || surface->windowId == 0)
+        return CHANNEL_RC_OK;
+
+    guac_client* client = ((rdp_freerdp_context*) gdi->context)->client;
+
+    /* Snapshot the rect list before painting. region16_rects() returns a
+     * pointer into the region's internal storage, which is only valid until
+     * the region is next modified. FreeRDP's GDI pipeline clears
+     * invalidRegion after this callback returns. */
+    UINT32 rect_count = 0;
+    const RECTANGLE_16* rects =
+        region16_rects(&surface->invalidRegion, &rect_count);
+
+    return guac_rdp_rail_paint_surface(client, surface, rect_count, rects);
+
+}
+#endif
+#endif
+
+/**
+* Frees all tracked RAIL windows associated with the given RDP client.
+*
+* @param rdp_client
+*     The RDP client whose tracked RAIL windows should be freed.
+*/

Review Comment:
   This function also can be documented in `rail.h` and not, here.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -279,6 +541,322 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
 }
 
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing information about the window that was deleted.
+ * 
+ * @return
+ *     TRUE if the window deletion was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* previous = NULL;
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == orderInfo->windowId) {
+            guac_rdp_rail_free_window(rdp_client, previous, current);
+            rdp_client->gdi_modified = 1;
+            break;
+        }
+        previous = current;
+        current = current->next;
+    }
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return TRUE;
+
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows_lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param surface
+ *     The RDPGFX surface containing the updated window contents.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface to copy.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+ */
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    if (surface == NULL || surface->windowId == 0
+            || rects == NULL || rect_count == 0)
+        return CHANNEL_RC_OK;
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_or_create_window(client, surface->windowId);
+
+    if (rail_window == NULL) {
+        pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+        return CHANNEL_RC_OK;
+    }
+
+    /* Per FreeRDP's gdi/gfx.c and xf_gfx.c, mappedWidth/Height and
+     * outputTargetWidth/Height are initialized to the same value at surface
+     * creation, and only diverge when an explicit MapSurfaceToScaledWindow
+     * order rescales the surface. We honor any divergence but optimize for
+     * the common 1:1 case with a plain copy. */
+    int source_width = surface->mappedWidth;
+    int source_height = surface->mappedHeight;
+    int target_width = surface->outputTargetWidth;
+    int target_height = surface->outputTargetHeight;
+
+    if (target_width <= 0) 
+        target_width = source_width;
+    if (target_height <= 0) 
+        target_height = source_height;
+
+    if (source_width <= 0 || source_height <= 0
+            || target_width <= 0 || target_height <= 0) {
+        rail_window->has_surface = 0;
+        guac_rdp_rail_update_window_opacity(rail_window);
+        pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+        return CHANNEL_RC_OK;
+    }
+
+    int scaled = (source_width != target_width
+            || source_height != target_height);
+
+    if (rail_window->layer_width != target_width
+            || rail_window->layer_height != target_height) {
+        guac_display_layer_resize(rail_window->layer,
+                target_width, target_height);
+        rail_window->layer_width = target_width;
+        rail_window->layer_height = target_height;
+    }
+
+    guac_display_layer_raw_context* layer_context =
+        guac_display_layer_open_raw(rail_window->layer);
+
+    guac_rect source_bounds;
+    guac_rect_init(&source_bounds, 0, 0, source_width, source_height);
+
+    for (UINT32 i = 0; i < rect_count; i++) {
+
+        const RECTANGLE_16* rect = &rects[i];
+        guac_rect src_rect;
+        guac_rect_init(&src_rect, rect->left, rect->top,
+                rect->right - rect->left, rect->bottom - rect->top);
+
+        guac_rect_constrain(&src_rect, &source_bounds);
+        if (guac_rect_is_empty(&src_rect))
+            continue;
+
+        guac_rect dst_rect;
+        if (scaled) {
+            dst_rect.left =
+                ((UINT64) src_rect.left * target_width) / source_width;
+            dst_rect.top =
+                ((UINT64) src_rect.top * target_height) / source_height;
+            dst_rect.right =
+                (((UINT64) src_rect.right * target_width)
+                    + source_width - 1) / source_width;
+            dst_rect.bottom =
+                (((UINT64) src_rect.bottom * target_height)
+                    + source_height - 1) / source_height;
+        }
+        else {
+            dst_rect = src_rect;
+        }
+
+        guac_rect_constrain(&dst_rect, &layer_context->bounds);
+        if (guac_rect_is_empty(&dst_rect))
+            continue;
+
+        BOOL success;
+        if (!scaled)
+            success = freerdp_image_copy(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    guac_rdp_get_native_pixel_format(TRUE),
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    NULL, FREERDP_FLIP_NONE);
+        else
+            success = freerdp_image_scale(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    guac_rdp_get_native_pixel_format(TRUE),
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    guac_rect_width(&src_rect),
+                    guac_rect_height(&src_rect));
+
+        if (!success) {
+            guac_display_layer_close_raw(rail_window->layer, layer_context);
+            pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+            return ERROR_INTERNAL_ERROR;
+        }
+
+        guac_rect_extend(&layer_context->dirty, &dst_rect);
+
+    }
+
+    guac_display_layer_close_raw(rail_window->layer, layer_context);
+
+    rail_window->has_surface = 1;
+    guac_rdp_rail_update_window_opacity(rail_window);
+    rdp_client->gdi_modified = 1;
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return CHANNEL_RC_OK;
+
+}
+
+/**
+* Callback which is invoked when one or more areas of an RDPGFX surface have
+* been updated.
+*
+* @param context
+*     The RDPGFX client context associated with the surface update.
+*
+* @param surface_id
+*     The server-side ID of the updated RDPGFX surface.
+*
+* @param rect_count
+*     The number of rectangles within the rects array.
+*
+* @param rects
+*     The updated rectangular regions of the surface.
+*
+* @return
+*     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+*/

Review Comment:
   Since this is defined in `rail.h`, there's no need to document it, here.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -279,6 +541,322 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
 }
 
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing information about the window that was deleted.
+ * 
+ * @return
+ *     TRUE if the window deletion was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* previous = NULL;
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == orderInfo->windowId) {
+            guac_rdp_rail_free_window(rdp_client, previous, current);
+            rdp_client->gdi_modified = 1;
+            break;
+        }
+        previous = current;
+        current = current->next;
+    }
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return TRUE;
+
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows_lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param surface
+ *     The RDPGFX surface containing the updated window contents.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface to copy.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+ */
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    if (surface == NULL || surface->windowId == 0
+            || rects == NULL || rect_count == 0)
+        return CHANNEL_RC_OK;
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_or_create_window(client, surface->windowId);
+
+    if (rail_window == NULL) {
+        pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+        return CHANNEL_RC_OK;
+    }
+
+    /* Per FreeRDP's gdi/gfx.c and xf_gfx.c, mappedWidth/Height and
+     * outputTargetWidth/Height are initialized to the same value at surface
+     * creation, and only diverge when an explicit MapSurfaceToScaledWindow
+     * order rescales the surface. We honor any divergence but optimize for
+     * the common 1:1 case with a plain copy. */
+    int source_width = surface->mappedWidth;
+    int source_height = surface->mappedHeight;
+    int target_width = surface->outputTargetWidth;
+    int target_height = surface->outputTargetHeight;
+
+    if (target_width <= 0) 
+        target_width = source_width;
+    if (target_height <= 0) 
+        target_height = source_height;
+
+    if (source_width <= 0 || source_height <= 0
+            || target_width <= 0 || target_height <= 0) {
+        rail_window->has_surface = 0;
+        guac_rdp_rail_update_window_opacity(rail_window);
+        pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+        return CHANNEL_RC_OK;
+    }
+
+    int scaled = (source_width != target_width
+            || source_height != target_height);
+
+    if (rail_window->layer_width != target_width
+            || rail_window->layer_height != target_height) {
+        guac_display_layer_resize(rail_window->layer,
+                target_width, target_height);
+        rail_window->layer_width = target_width;
+        rail_window->layer_height = target_height;
+    }
+
+    guac_display_layer_raw_context* layer_context =
+        guac_display_layer_open_raw(rail_window->layer);
+
+    guac_rect source_bounds;
+    guac_rect_init(&source_bounds, 0, 0, source_width, source_height);
+
+    for (UINT32 i = 0; i < rect_count; i++) {
+
+        const RECTANGLE_16* rect = &rects[i];
+        guac_rect src_rect;
+        guac_rect_init(&src_rect, rect->left, rect->top,
+                rect->right - rect->left, rect->bottom - rect->top);
+
+        guac_rect_constrain(&src_rect, &source_bounds);
+        if (guac_rect_is_empty(&src_rect))
+            continue;
+
+        guac_rect dst_rect;
+        if (scaled) {
+            dst_rect.left =
+                ((UINT64) src_rect.left * target_width) / source_width;
+            dst_rect.top =
+                ((UINT64) src_rect.top * target_height) / source_height;
+            dst_rect.right =
+                (((UINT64) src_rect.right * target_width)
+                    + source_width - 1) / source_width;
+            dst_rect.bottom =
+                (((UINT64) src_rect.bottom * target_height)
+                    + source_height - 1) / source_height;
+        }
+        else {
+            dst_rect = src_rect;
+        }
+
+        guac_rect_constrain(&dst_rect, &layer_context->bounds);
+        if (guac_rect_is_empty(&dst_rect))
+            continue;
+
+        BOOL success;
+        if (!scaled)
+            success = freerdp_image_copy(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    guac_rdp_get_native_pixel_format(TRUE),
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    NULL, FREERDP_FLIP_NONE);
+        else
+            success = freerdp_image_scale(
+                    GUAC_DISPLAY_LAYER_RAW_BUFFER(layer_context, dst_rect),
+                    guac_rdp_get_native_pixel_format(TRUE),
+                    layer_context->stride,
+                    0, 0,
+                    guac_rect_width(&dst_rect),
+                    guac_rect_height(&dst_rect),
+                    surface->data, surface->format, surface->scanline,
+                    src_rect.left, src_rect.top,
+                    guac_rect_width(&src_rect),
+                    guac_rect_height(&src_rect));
+
+        if (!success) {
+            guac_display_layer_close_raw(rail_window->layer, layer_context);
+            pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+            return ERROR_INTERNAL_ERROR;
+        }
+
+        guac_rect_extend(&layer_context->dirty, &dst_rect);
+
+    }
+
+    guac_display_layer_close_raw(rail_window->layer, layer_context);
+
+    rail_window->has_surface = 1;
+    guac_rdp_rail_update_window_opacity(rail_window);
+    rdp_client->gdi_modified = 1;
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return CHANNEL_RC_OK;
+
+}
+
+/**
+* Callback which is invoked when one or more areas of an RDPGFX surface have
+* been updated.
+*
+* @param context
+*     The RDPGFX client context associated with the surface update.
+*
+* @param surface_id
+*     The server-side ID of the updated RDPGFX surface.
+*
+* @param rect_count
+*     The number of rectangles within the rects array.
+*
+* @param rects
+*     The updated rectangular regions of the surface.
+*
+* @return
+*     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+*/
+UINT guac_rdp_rail_update_surface_area(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    rdpGdi* gdi = (rdpGdi*) context->custom;
+    if (gdi == NULL || gdi->context == NULL || gdi->suppressOutput)
+        return CHANNEL_RC_OK;
+
+    if (context->GetSurfaceData == NULL)
+        return CHANNEL_RC_OK;
+
+    gdiGfxSurface* surface =
+        (gdiGfxSurface*) context->GetSurfaceData(context, surface_id);
+
+    /* Only window-mapped surfaces are routed through us; non-RAIL surfaces
+     * are handled by FreeRDP's default GDI pipeline. We return early without
+     * touching invalidRegion so the default path can consume it normally on
+     * the next UpdateSurfaces tick. */
+    if (surface == NULL || surface->windowId == 0)
+        return CHANNEL_RC_OK;
+
+    guac_client* client = ((rdp_freerdp_context*) gdi->context)->client;
+    return guac_rdp_rail_paint_surface(client, surface, rect_count, rects);
+
+}
+
+#ifdef HAVE_RDPGFX_WINDOW_SURFACE_UPDATE
+/**
+* Callback which is invoked when an RDPGFX surface associated with a RAIL
+* window has been updated.
+*
+* @param context
+*     The RDPGFX client context associated with the surface update.
+*
+* @param surface
+*     The RDPGFX surface containing the updated window contents.
+*
+* @return
+*     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+*/

Review Comment:
   As this function is defined in `rail.h`, the documentation can be solely there and need not be duplicated, here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3254330655


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,235 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows list lock must be held.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve or create.
+ *
+ * @return
+ *     The matching or newly-created RAIL window, or NULL if the window could not be created.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate window state.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 0);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window 0x%08x tracking failed: "
+                "could not allocate display layer.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);
+
+    guac_common_list_add(rdp_client->rail_windows, rail_window);
+
+    return rail_window;
+}
+
+/**
+ * Updates the visibility of the Guacamole layer backing the given RAIL window.
+ * The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should have its opacity updated.
+ */
+static void guac_rdp_rail_update_window_opacity(
+        guac_rdp_rail_window* rail_window) {
+
+    int opacity = (rail_window->visible
+            && rail_window->has_surface
+            && rail_window->has_rail_order) ? 0xFF : 0;
+    guac_display_layer_set_opacity(rail_window->layer, opacity);
+}
+
+/**
+ * Frees the resources associated with the given RAIL window.
+ *
+ * @param data
+ *     The RAIL window to free.
+ */
+static void guac_rdp_rail_free_window_data(void* data) {
+
+    guac_rdp_rail_window* rail_window = data;

Review Comment:
   No, void* implicitly converts to any pointer type. That would be required in C++ though, but not in C.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub) via dev" <de...@guacamole.apache.org>.
necouchman commented on PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#issuecomment-6878324556

   @jjheinon Any chance you can fix up the conflicts, here, and we can get this into the `patch` branch?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420615458


##########
src/protocols/rdp/gdi.c:
##########
@@ -83,10 +102,16 @@ BOOL guac_rdp_gdi_begin_paint(rdpContext* context) {
     guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
     rdp_client->current_context = current_context;
 
-    /* Resynchronize default layer buffer details with FreeRDP's GDI */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    /*
+     * For full desktop sessions, the default layer shares FreeRDP's primary
+     * GDI buffer. For RemoteApp sessions, keep the default layer on its own
+     * black buffer and allow FreeRDP to paint the hidden server desktop only
+     * into its own primary buffer.
+     */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx

Review Comment:
   Use primary buffer only if RAIL is disabled, GFX is disabled or both are enabled, but we're not yet received first paint event. This fixes the issue where invalid login ends up to Windows login screen on non-NLA mode. Earlier we rendered black screen in that case.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3426859770


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -276,10 +312,154 @@ static guac_common_list_element* guac_rdp_rail_get_window_element(
 static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
         int z) {
 
+    rail_window->z_order = z;
+
     guac_display_layer_stack(rail_window->layer, z);
 
 }
 
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {

Review Comment:
   When launching multiple remote apps, the current window layering code couldn't handle popup/child windows properly. Now we handle that by tracking the window ownership and making sure the active window popups are visible and do not get hidden under other app windows.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3441946101


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -166,6 +551,15 @@ static UINT guac_rdp_rail_execute_result(RailClientContext* context,
         const RAIL_EXEC_RESULT_ORDER* execResult) {
 
     guac_client* client = (guac_client*) context->custom;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (execResult->execResult == RAIL_EXEC_E_NOT_IN_ALLOWLIST

Review Comment:
   This was an interesting edge case. Apparently it's possible for Windows RemoteApp to launch another process, which does not belong to allowed app listing for the RemoteApp session.
   In this case, the requested RAIL app launches properly (and renders the app window), but server sends RAIL_EXEC_E_NOT_IN_ALLOWLIST when it prevents launching the other app process.
   
   Not sure what should be the optimal behavior in this case, but removed the modification and reverted back to the original behavior (connection fails), but added log message about it so the Windows admin can make the requested changes on their environment.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#issuecomment-4970311887

   @jjheinon Thanks - there are a few conflicts that need to be resolved, now. I also see @mike-jumper moved this from staging/1.6.1 to patch branch.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#issuecomment-4896600895

   > Note that the build currently fails when using FreeRDP on Rocky9 (2.11.7-7.el9_8.3):
   > 
   > ```
   > channels/rail.c: In function ‘guac_rdp_rail_repaint_surface’:
   > channels/rail.c:1089:47: error: ‘guac_rdp_client’ has no member named ‘rdpgfx_interface’; did you mean ‘rail_interface’?
   >  1089 |     RdpgfxClientContext* rdpgfx = rdp_client->rdpgfx_interface;
   >       |                                               ^~~~~~~~~~~~~~~~
   >       |                                               rail_interface
   > channels/rail.c: In function ‘guac_rdp_rail_window_update’:
   > channels/rail.c:1353:31: error: implicit declaration of function ‘ConvertWCharNToUtf8Alloc’ [-Werror=implicit-function-declaration]
   >  1353 |                 char* title = ConvertWCharNToUtf8Alloc(title_string,
   >       |                               ^~~~~~~~~~~~~~~~~~~~~~~~
   > channels/rail.c:1353:31: error: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   > channels/rail.c: In function ‘guac_rdp_rail_paint_surface’:
   > channels/rail.c:1921:23: error: implicit declaration of function ‘freerdp_image_copy_no_overlap’ [-Werror=implicit-function-declaration]
   >  1921 |             success = freerdp_image_copy_no_overlap(
   >       |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   > cc1: all warnings being treated as errors
   > make[4]: *** [Makefile:1146: channels/libguac_client_rdp_la-rail.lo] Error 1
   > make[4]: Leaving directory '/devpool/guacamole/guacamole-server/src/protocols/rdp'
   > make[3]: *** [Makefile:1473: all-recursive] Error 1
   > make[3]: Leaving directory '/devpool/guacamole/guacamole-server/src/protocols/rdp'
   > make[2]: *** [Makefile:763: all] Error 2
   > make[2]: Leaving directory '/devpool/guacamole/guacamole-server/src/protocols/rdp'
   > make[1]: *** [Makefile:544: all-recursive] Error 1
   > make[1]: Leaving directory '/devpool/guacamole/guacamole-server'
   > make: *** [Makefile:464: all] Error 2
   > ```
   
   
   Fixed. Tested it now compiles and works on FreeRDP 2.11.7 and FreeRDP 3.26.0. RDPGFX is automatically disabled, if using FreeRDP 2. RDPGFX can be enabled or disabled with FreeRDP 3.
   Tested on Ubuntu 24.04 (ARM64).
    


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531043459


##########
src/protocols/rdp/gdi.c:
##########
@@ -169,27 +206,49 @@ BOOL guac_rdp_gdi_desktop_resize(rdpContext* context) {
     GUAC_ASSERT(rdp_client->current_context == NULL);
 #endif
 
-    /* All potential drawing operations must occur while holding an open context */
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
-    guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
+    guac_display_layer_raw_context* current_context = NULL;
+
+    /* Full desktop sessions share the resized GDI buffer with the default
+     * layer, and must hold an open raw context while the external buffer
+     * details change. */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx
+            || !rdp_client->rail_background_painted)
+        current_context = guac_display_layer_open_raw(default_layer);

Review Comment:
   guac_display_layer_open_raw() needs to happen before gdi_resize() to prevent rendering thread accessing the buffer while it is being modified without write lock. guac_display_layer_open_raw() acquires the lock before we do any resizing for the buffer. Without lock, we may get artifacts or worse.
   It's mentioned in the comment, changed the wording to be more precise.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub) via dev" <de...@guacamole.apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r4592180492


##########
src/protocols/rdp/channels/rdpgfx.c:
##########
@@ -17,7 +17,10 @@
  * under the License.
  */
 
+#include "config.h"

Review Comment:
   `config.h` is automatically included by the `Makefile` and need not be manually included.



##########
src/libguac/tests/display/resize.c:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "display-plan.h"
+#include "display-priv.h"
+#include "guacamole/mem.h"
+
+#include <CUnit/CUnit.h>
+#include <stdint.h>
+
+/* Identical pixels must be resent when resizing clears the client layer. */
+static void check_resize(int old_size, int new_size, int report_dirty) {
+
+    uint32_t pixels[128 * 128] = { 0 };
+    guac_display_layer_cell cells[4] = { 0 };
+    guac_display_layer layer = {
+        .last_frame = {
+            .width = old_size,
+            .height = old_size,
+            .buffer = (unsigned char*) pixels,
+            .buffer_stride = 128 * sizeof(uint32_t)
+        },
+        .pending_frame = {
+            .width = new_size,
+            .height = new_size,
+            .buffer = (unsigned char*) pixels,
+            .buffer_stride = 128 * sizeof(uint32_t)
+        },
+        .pending_frame_cells = cells,
+        .pending_frame_cells_width = 2,
+        .pending_frame_cells_height = 2
+    };
+    guac_display* display = guac_mem_zalloc(sizeof(guac_display));
+    display->pending_frame.layers = &layer;
+
+    if (report_dirty)
+        guac_rect_init(&layer.pending_frame.dirty, 0, 0, 1, 1);
+
+    guac_display_plan* plan = PFW_LFR_guac_display_plan_create(display);
+    if (old_size == new_size) {
+        CU_ASSERT_PTR_NULL(plan);
+        if (plan != NULL)
+            guac_display_plan_free(plan);
+        guac_mem_free(display);
+        return;
+    }
+
+    CU_ASSERT_PTR_NOT_NULL_FATAL(plan);
+    size_t pixels_sent = 0;
+    for (size_t i = 0; i < plan->length; i++) {
+        guac_rect* rect = &plan->ops[i].dest;
+        CU_ASSERT_EQUAL(plan->ops[i].type, GUAC_DISPLAY_PLAN_OPERATION_IMG);
+        CU_ASSERT(rect->left >= 0 && rect->top >= 0);
+        CU_ASSERT(rect->right <= new_size && rect->bottom <= new_size);
+        pixels_sent += guac_rect_width(rect) * guac_rect_height(rect);
+    }
+
+    CU_ASSERT_EQUAL(pixels_sent, new_size * new_size);
+    guac_display_plan_free(plan);
+    guac_mem_free(display);
+
+}
+
+void test_display__resize_repaints_unchanged_pixels(void) {
+
+    check_resize(64, 128, 0);
+    check_resize(64, 128, 1);
+    check_resize(128, 64, 0);
+    check_resize(128, 64, 1);
+
+}
+
+void test_display__unchanged_frame_needs_no_repaint(void) {

Review Comment:
   Please document this function.



##########
src/libguac/tests/display/resize.c:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "display-plan.h"
+#include "display-priv.h"
+#include "guacamole/mem.h"
+
+#include <CUnit/CUnit.h>
+#include <stdint.h>
+
+/* Identical pixels must be resent when resizing clears the client layer. */
+static void check_resize(int old_size, int new_size, int report_dirty) {

Review Comment:
   Please properly document this function.



##########
src/libguac/tests/display/resize.c:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "display-plan.h"
+#include "display-priv.h"
+#include "guacamole/mem.h"
+
+#include <CUnit/CUnit.h>
+#include <stdint.h>
+
+/* Identical pixels must be resent when resizing clears the client layer. */
+static void check_resize(int old_size, int new_size, int report_dirty) {
+
+    uint32_t pixels[128 * 128] = { 0 };
+    guac_display_layer_cell cells[4] = { 0 };
+    guac_display_layer layer = {
+        .last_frame = {
+            .width = old_size,
+            .height = old_size,
+            .buffer = (unsigned char*) pixels,
+            .buffer_stride = 128 * sizeof(uint32_t)
+        },
+        .pending_frame = {
+            .width = new_size,
+            .height = new_size,
+            .buffer = (unsigned char*) pixels,
+            .buffer_stride = 128 * sizeof(uint32_t)
+        },
+        .pending_frame_cells = cells,
+        .pending_frame_cells_width = 2,
+        .pending_frame_cells_height = 2
+    };
+    guac_display* display = guac_mem_zalloc(sizeof(guac_display));
+    display->pending_frame.layers = &layer;
+
+    if (report_dirty)
+        guac_rect_init(&layer.pending_frame.dirty, 0, 0, 1, 1);
+
+    guac_display_plan* plan = PFW_LFR_guac_display_plan_create(display);
+    if (old_size == new_size) {
+        CU_ASSERT_PTR_NULL(plan);
+        if (plan != NULL)
+            guac_display_plan_free(plan);
+        guac_mem_free(display);
+        return;
+    }
+
+    CU_ASSERT_PTR_NOT_NULL_FATAL(plan);
+    size_t pixels_sent = 0;
+    for (size_t i = 0; i < plan->length; i++) {
+        guac_rect* rect = &plan->ops[i].dest;
+        CU_ASSERT_EQUAL(plan->ops[i].type, GUAC_DISPLAY_PLAN_OPERATION_IMG);
+        CU_ASSERT(rect->left >= 0 && rect->top >= 0);
+        CU_ASSERT(rect->right <= new_size && rect->bottom <= new_size);
+        pixels_sent += guac_rect_width(rect) * guac_rect_height(rect);
+    }
+
+    CU_ASSERT_EQUAL(pixels_sent, new_size * new_size);
+    guac_display_plan_free(plan);
+    guac_mem_free(display);
+
+}
+
+void test_display__resize_repaints_unchanged_pixels(void) {

Review Comment:
   Please document this function.



##########
src/protocols/rdp/gdi.c:
##########
@@ -17,6 +17,8 @@
  * under the License.
  */
 
+#include "config.h"
+

Review Comment:
   This can be removed - included automatically by Makefile.



##########
src/protocols/rdp/channels/rdpgfx.c:
##########
@@ -58,16 +61,72 @@ static void guac_rdp_rdpgfx_channel_connected(rdpContext* context,
     if (strcmp(args->name, RDPGFX_DVC_CHANNEL_NAME) != 0)
         return;
 
-    /* Init GDI-backed support for the Graphics Pipeline */

Review Comment:
   Rather than eliminating this comment, how about just updating it 😉 ?



##########
src/protocols/vnc/vnc.c:
##########


Review Comment:
   What's the rationale for including changes to the VNC client code in a very specific RDP-related RAIL change?



##########
configure.ac:
##########
@@ -865,6 +865,13 @@ then
                        have_freerdp=no])
 fi
 
+# FreeRDP 3.x headers themselves reference deprecated types and functions.
+if test "x${freerdp_version}" = "x(3.x)"
+then
+    RDP_CFLAGS="$RDP_CFLAGS -Wno-deprecated-declarations"
+    CPPFLAGS="$CPPFLAGS -Wno-deprecated-declarations"
+fi
+

Review Comment:
   I'm not a fan of this - I think we should be handling the deprecated declarations by either removing them entirely or using `#if...#else` conditions to deal with it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub) via dev" <de...@guacamole.apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r4598097259


##########
src/protocols/rdp/gdi.c:
##########
@@ -17,6 +17,8 @@
  * under the License.
  */
 
+#include "config.h"
+

Review Comment:
   removed it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3426874994


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -687,8 +867,15 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
         if (rail_window != NULL) {
 
-            int needs_opacity_update = !rail_window->has_rail_order;
+            int had_rail_order = rail_window->has_rail_order;
+            int needs_opacity_update = !had_rail_order;
             rail_window->has_rail_order = 1;
+            int needs_raise = 0;
+
+            if (fieldFlags & WINDOW_ORDER_FIELD_OWNER) {

Review Comment:
   A window order includes the owner field. Make sure it's shown above the owner window.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3438529263


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,332 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+typedef struct guac_rdp_rail_window {

Review Comment:
   Ah, okay - if you could just:
   * Remove the empty line between the comment block and the start of the `typedef`
   * Start the comment with the proper `/**`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420615458


##########
src/protocols/rdp/gdi.c:
##########
@@ -83,10 +102,16 @@ BOOL guac_rdp_gdi_begin_paint(rdpContext* context) {
     guac_display_layer_raw_context* current_context = guac_display_layer_open_raw(default_layer);
     rdp_client->current_context = current_context;
 
-    /* Resynchronize default layer buffer details with FreeRDP's GDI */
-    current_context->buffer = gdi->primary_buffer;
-    current_context->stride = gdi->stride;
-    guac_rect_init(&current_context->bounds, 0, 0, gdi->width, gdi->height);
+    /*
+     * For full desktop sessions, the default layer shares FreeRDP's primary
+     * GDI buffer. For RemoteApp sessions, keep the default layer on its own
+     * black buffer and allow FreeRDP to paint the hidden server desktop only
+     * into its own primary buffer.
+     */
+    if (rdp_client->settings->remote_app == NULL
+            || !rdp_client->settings->enable_gfx

Review Comment:
   Use primary buffer only if RAIL is disabled, GFX is disabled or both are enabled, but we're not yet received first paint event. This fixes the issue where invalid login ends up to Windows login screen on non-NLA mode, but login screen is not visible. Earlier we rendered black screen in that case.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420593104


##########
src/protocols/rdp/channels/rdpgfx.c:
##########
@@ -58,16 +61,48 @@ static void guac_rdp_rdpgfx_channel_connected(rdpContext* context,
     if (strcmp(args->name, RDPGFX_DVC_CHANNEL_NAME) != 0)
         return;
 
-    /* Init GDI-backed support for the Graphics Pipeline */
     RdpgfxClientContext* rdpgfx = (RdpgfxClientContext*) args->pInterface;
     rdpGdi* gdi = context->gdi;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+    guac_rdp_settings* settings = rdp_client->settings;
+    BOOL gfx_initialized;
 
-    if (!gdi_graphics_pipeline_init(gdi, rdpgfx))
+/*
+ * Choose the RAIL+GFX hookup based on FreeRDP capabilities:
+ *
+ *   If UpdateWindowFromSurface is available, use the normal graphics
+ *   pipeline and render RemoteApp windows through that callback.
+ *
+ *   If only UpdateSurfaceArea is available, use the _ex initializer and
+ *   route RemoteApp window updates through our custom surface-area handler.
+ *
+ *   On older FreeRDP versions, fall back to the default pipeline. In that
+ *   case, RemoteApp with GFX will not render correctly and GFX must be
+ *   disabled.
+ */
+     
+#if defined(HAVE_RDPGFX_SURFACE_AREA_UPDATE) \

Review Comment:
   This was changed a bit, we kept on using UpdateSurfaceArea even when newer UpdateWindowFromSurface was available. Now it works better.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3420564247


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,332 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+typedef struct guac_rdp_rail_window {

Review Comment:
   Added comment.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531186301


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -228,54 +269,1855 @@ static UINT guac_rdp_rail_handshake_ex(RailClientContext* rail,
     return guac_rdp_rail_complete_handshake(rail);
 }
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+/* 
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The RDP server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The RDPGFX surface ID currently mapped to this window, if known.
+     */
+    UINT16 surface_id;
+    int has_surface_id;
+
+    /**
+     * The RDP server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether an RDPGFX surface update arrived before the RAIL window order.
+     */
+    int has_deferred_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list. Used to avoid applying the raise to 
+     * windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known. If the window does not yet 
+     * have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The latest dimensions reported by RAIL window orders.
+     */
+    int order_width;
+    int order_height;
+
+    /**
+     * The latest client-area bounds reported by RAIL window orders.
+     */
+    int client_x;
+    int client_y;
+    int client_width;
+    int client_height;
+    int has_client_area;
+
+    /**
+     * The dimensions of the mapped RDPGFX surface represented by the layer.
+     */
+    int surface_width;
+    int surface_height;
+
+    /**
+     * Number of RDPGFX paint operations currently using this window's layer.
+     */
+    int paint_refs;
+
+    /**
+     * Whether this window has been removed from the tracked window list and
+     * should be freed once all outstanding paints finish.
+     */
+    int deleted;
+
+} guac_rdp_rail_window;
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects);
+#endif
+
 /**
- * A callback function that is executed when an update for a RAIL window is
- * received from the RDP server.
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
  *
- * @param context
- *     A pointer to the rdpContext structure used by FreeRDP to handle the
- *     window update.
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
  *
- * @param orderInfo
- *     A pointer to the data structure that contains information about what
- *     window was updated what updates were performed.
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
  *
- * @param windowState
- *     A pointer to the data structure that contains details of the updates
- *     to the window, as indicated by flags in the orderInfo field.
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
  *
  * @return
- *     TRUE if the client-side processing of the updates as successful; otherwise
- *     FALSE. This implementation always returns TRUE.
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
  */
-static BOOL guac_rdp_rail_window_update(rdpContext* context,
-        RAIL_CONST WINDOW_ORDER_INFO* orderInfo,
-        RAIL_CONST WINDOW_STATE_ORDER* windowState) {
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
 
-    guac_client* client = ((rdp_freerdp_context*) context)->client;
     guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
 
-    guac_client_log(client, GUAC_LOG_TRACE, "RAIL window update callback: %d", orderInfo->fieldFlags);
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
 
-    UINT32 fieldFlags = orderInfo->fieldFlags;
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
 
-    /* If the flag for window visibilty is set, check visibility. */
-    if (fieldFlags & WINDOW_ORDER_FIELD_SHOW) {
-        guac_client_log(client, GUAC_LOG_TRACE, "RAIL window visibility change: %d", windowState->showState);
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
 
-        /* State is either hidden or minimized - send restore command. */
-        if (windowState->showState == GUAC_RDP_RAIL_WINDOW_STATE_MINIMIZED) {
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
 
-            guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window minimized, sending restore command.");
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
 
-            RAIL_SYSCOMMAND_ORDER syscommand;
-            syscommand.windowId = orderInfo->windowId;
-            syscommand.command = SC_RESTORE;
-            rdp_client->rail_interface->ClientSystemCommand(rdp_client->rail_interface, &syscommand);
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the tracked RAIL window currently mapped to the given RDPGFX
+ * surface, or NULL if no such mapping is known. The rail_windows list lock
+ * must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param surface_id
+ *     The server-side ID of the RDPGFX surface to look up.
+ *
+ * @return
+ *     The matching tracked RAIL window, or NULL if no such mapping exists.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window_for_surface(
+        guac_rdp_client* rdp_client, UINT16 surface_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_surface_id
+                && rail_window->surface_id == surface_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    rail_window->z_order = z;
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {
+
+    int z = 1;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->z_order >= z)
+            z = rail_window->z_order + 1;
+    }
+
+    return z;
+
+}
+
+/**
+ * Raises all tracked windows owned by the given parent window above that
+ * parent's layer. The rail_windows list lock must be held.
+ * This is needed for showing popup/dialog windows owned by another window,
+ * which might otherwise stay hidden behind their owner or other RemoteApp windows.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param owner_window_id
+ *     The server-side ID of the owner window.
+ *
+ * @param z
+ *     The z-order to assign to the first owned window.
+ *
+ * @return
+ *     The next available z-order after all owned windows have been raised.
+ */
+static int guac_rdp_rail_raise_owned_windows(guac_rdp_client* rdp_client,
+        UINT64 owner_window_id, int z) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_rail_order
+                && rail_window->owner_window_id == owner_window_id
+                && rail_window->window_id != owner_window_id) {
+            guac_rdp_rail_stack_window(rail_window, z++);
+            z = guac_rdp_rail_raise_owned_windows(rdp_client,
+                    rail_window->window_id, z);
         }
+
+        current = current->next;
     }
 
-    return TRUE;
+    return z;
+
+}
+
+/**
+ * Raises the given window above its owner, if that owner is currently being
+ * tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param rail_window
+ *     The window that should be raised above its owner.
+ */
+static void guac_rdp_rail_raise_window_above_owner(
+        guac_rdp_client* rdp_client, guac_rdp_rail_window* rail_window) {
+
+    if (rail_window == NULL || rail_window->owner_window_id == 0)
+        return;
+
+    guac_rdp_rail_window* owner = guac_rdp_rail_get_window(rdp_client,
+            rail_window->owner_window_id);
+
+    if (owner == NULL)
+        return;
+
+    int z = guac_rdp_rail_get_top_z(rdp_client);
+
+    guac_rdp_rail_stack_window(rail_window, z);
+
+}
+
+/**
+ * Sends a RAIL Client Activate order for the given window.
+ *
+ * RAIL servers expect explicit activation notifications when the active
+ * RemoteApp window changes.
+ *
+ * Even if the window_id is 64 bits, MS RAIL protocol activation orders use a 32-bit window ID.
+ * Ignore any invalid window IDs that cannot be represented without truncating to 32-bits.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the window to activate or deactivate.
+ *
+ * @param enabled
+ *     Non-zero to activate the window, zero to deactivate it.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the activation order was sent successfully, or a
+ *     protocol-specific error code otherwise.
+ */
+static UINT guac_rdp_rail_send_activate(guac_rdp_client* rdp_client,
+        UINT64 window_id, BOOL enabled) {
+
+    if (rdp_client->rail_interface == NULL
+            || rdp_client->rail_interface->ClientActivate == NULL)
+        return CHANNEL_RC_OK;
+
+    if (window_id > UINT32_MAX)
+        return CHANNEL_RC_OK;
+
+    RAIL_ACTIVATE_ORDER activate = {
+        .windowId = (UINT32) window_id,
+        .enabled = enabled
+    };
+
+    pthread_mutex_lock(&(rdp_client->message_lock));
+    UINT status = rdp_client->rail_interface->ClientActivate(
+            rdp_client->rail_interface, &activate);
+    pthread_mutex_unlock(&(rdp_client->message_lock));
+
+    return status;
+
+}
+
+/**
+ * Sends the RAIL activation to switch focus to the given window.
+ *
+ * The previously active window is first deactivated, followed by activation
+ * of the requested window. The locally tracked active window ID is updated
+ * only if the activation request succeeds.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that should be activated.
+ */
+static void guac_rdp_rail_activate_window(guac_rdp_client* rdp_client,
+        UINT64 window_id) {
+
+    if (rdp_client->rail_active_window_id == window_id)
+        return;
+
+    if (guac_rdp_rail_send_activate(rdp_client, window_id, TRUE)
+            == CHANNEL_RC_OK)
+        rdp_client->rail_active_window_id = window_id;
+
+}
+
+void guac_rdp_rail_raise_window_at(guac_rdp_client* rdp_client,
+        int x, int y) {
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return;
+
+    if (rdp_client->rail_windows == NULL)
+        return;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* clicked_window = NULL;
+    UINT64 activate_window_id = 0;
+    int clicked_z = 0;
+    int has_no_surface_window = 0;
+    int highest_no_surface_z = 0;
+    int suppress_activation = 0;
+    int has_adjacent_nosurface_window = 0;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+
+        guac_rdp_rail_window* rail_window = current->data;
+        if (rail_window == NULL)
+            continue;
+
+        if (rail_window->visible && rail_window->has_rail_order
+                && !rail_window->has_surface) {
+
+            if (!has_no_surface_window
+                    || rail_window->z_order > highest_no_surface_z) {
+                highest_no_surface_z = rail_window->z_order;
+                has_no_surface_window = 1;
+            }
+
+            continue;
+        }
+
+        if (!rail_window->visible || !rail_window->has_surface
+                || !rail_window->has_rail_order)
+            continue;
+
+        if (x < rail_window->x || y < rail_window->y
+                || x >= rail_window->x + rail_window->layer_width
+                || y >= rail_window->y + rail_window->layer_height)
+            continue;
+
+        if (clicked_window == NULL || rail_window->z_order > clicked_z) {
+            clicked_window = rail_window;
+            clicked_z = rail_window->z_order;
+        }
+
+    }
+
+    if (clicked_window != NULL) {
+        int z = guac_rdp_rail_get_top_z(rdp_client);
+
+        /* A higher no-surface RAIL window can represent a menu before its
+         * drawable popup surface arrives. Avoid reactivating the window below
+         * it, or the menu may close before the click is delivered. */
+        if (has_no_surface_window
+                && highest_no_surface_z > clicked_window->z_order) {
+            suppress_activation = 1;
+        }
+
+        /* Right-click context menus may not report an owner window, but in
+         * that case they're stacked immediately above a visible no-surface
+         * RAIL window (surface and window are separate). Treat that pairing
+         * as a menu popup and skip pre-click activation so menu-item clicks
+         * are not dismissed. */
+        if (has_no_surface_window
+                && highest_no_surface_z == clicked_window->z_order - 1) {
+            suppress_activation = 1;
+            has_adjacent_nosurface_window = 1;
+        }
+
+        /* Regular menu popups are owned windows. Activating them before the
+         * click can dismiss or disturb the menu selection. */
+        if (clicked_window->owner_window_id != 0) {
+            suppress_activation = 1;
+        }
+
+        if (!suppress_activation)
+            activate_window_id = clicked_window->window_id;
+
+        if (!has_adjacent_nosurface_window
+                && !clicked_window->in_monitored_desktop_zorder
+                && clicked_window->z_order < z - 1) {
+            guac_rdp_rail_stack_window(clicked_window, z);
+            guac_rdp_rail_raise_owned_windows(rdp_client,
+                    clicked_window->window_id, z + 1);
+            rdp_client->gdi_modified = 1;
+        }
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    if (activate_window_id != 0)
+        guac_rdp_rail_activate_window(rdp_client, activate_window_id);
+
+}
+
+void guac_rdp_rail_track_mouse_event(guac_rdp_client* rdp_client,
+        int mask, int x, int y) {
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return;
+
+    if (rdp_client->rail_windows == NULL)
+        return;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* target_window = NULL;
+
+    if (rdp_client->mouse_button_mask != 0
+            && rdp_client->rail_mouse_window_id != 0)
+        target_window = guac_rdp_rail_get_window(rdp_client,
+                rdp_client->rail_mouse_window_id);
+
+    if (target_window == NULL) {
+
+        int target_z = 0;
+
+        for (guac_common_list_element* current = rdp_client->rail_windows->head;
+                current != NULL; current = current->next) {
+
+            guac_rdp_rail_window* rail_window = current->data;
+            if (rail_window == NULL)
+                continue;
+
+            if (!rail_window->visible || !rail_window->has_surface
+                    || !rail_window->has_rail_order)
+                continue;
+
+            if (x < rail_window->x || y < rail_window->y
+                    || x >= rail_window->x + rail_window->layer_width
+                    || y >= rail_window->y + rail_window->layer_height)
+                continue;
+
+            if (target_window == NULL || rail_window->z_order > target_z) {
+                target_window = rail_window;
+                target_z = rail_window->z_order;
+            }
+
+        }
+
+    }
+
+    if (target_window != NULL
+            && rdp_client->mouse_button_mask == 0 && mask != 0)
+        rdp_client->rail_mouse_window_id = target_window->window_id;
+
+    if (mask == 0)
+        rdp_client->rail_mouse_window_id = 0;
+
+    guac_common_list_unlock(rdp_client->rail_windows);

Review Comment:
   Added



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531162028


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -228,54 +269,1855 @@ static UINT guac_rdp_rail_handshake_ex(RailClientContext* rail,
     return guac_rdp_rail_complete_handshake(rail);
 }
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+/* 
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The RDP server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The RDPGFX surface ID currently mapped to this window, if known.
+     */
+    UINT16 surface_id;
+    int has_surface_id;
+
+    /**
+     * The RDP server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether an RDPGFX surface update arrived before the RAIL window order.
+     */
+    int has_deferred_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list. Used to avoid applying the raise to 
+     * windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known. If the window does not yet 
+     * have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The latest dimensions reported by RAIL window orders.
+     */
+    int order_width;
+    int order_height;
+
+    /**
+     * The latest client-area bounds reported by RAIL window orders.
+     */
+    int client_x;
+    int client_y;
+    int client_width;
+    int client_height;
+    int has_client_area;
+
+    /**
+     * The dimensions of the mapped RDPGFX surface represented by the layer.
+     */
+    int surface_width;
+    int surface_height;
+
+    /**
+     * Number of RDPGFX paint operations currently using this window's layer.
+     */
+    int paint_refs;
+
+    /**
+     * Whether this window has been removed from the tracked window list and
+     * should be freed once all outstanding paints finish.
+     */
+    int deleted;
+
+} guac_rdp_rail_window;
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects);
+#endif
+
 /**
- * A callback function that is executed when an update for a RAIL window is
- * received from the RDP server.
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
  *
- * @param context
- *     A pointer to the rdpContext structure used by FreeRDP to handle the
- *     window update.
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
  *
- * @param orderInfo
- *     A pointer to the data structure that contains information about what
- *     window was updated what updates were performed.
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
  *
- * @param windowState
- *     A pointer to the data structure that contains details of the updates
- *     to the window, as indicated by flags in the orderInfo field.
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
  *
  * @return
- *     TRUE if the client-side processing of the updates as successful; otherwise
- *     FALSE. This implementation always returns TRUE.
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
  */
-static BOOL guac_rdp_rail_window_update(rdpContext* context,
-        RAIL_CONST WINDOW_ORDER_INFO* orderInfo,
-        RAIL_CONST WINDOW_STATE_ORDER* windowState) {
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
 
-    guac_client* client = ((rdp_freerdp_context*) context)->client;
     guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
 
-    guac_client_log(client, GUAC_LOG_TRACE, "RAIL window update callback: %d", orderInfo->fieldFlags);
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
 
-    UINT32 fieldFlags = orderInfo->fieldFlags;
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
 
-    /* If the flag for window visibilty is set, check visibility. */
-    if (fieldFlags & WINDOW_ORDER_FIELD_SHOW) {
-        guac_client_log(client, GUAC_LOG_TRACE, "RAIL window visibility change: %d", windowState->showState);
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
 
-        /* State is either hidden or minimized - send restore command. */
-        if (windowState->showState == GUAC_RDP_RAIL_WINDOW_STATE_MINIMIZED) {
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
 
-            guac_client_log(client, GUAC_LOG_DEBUG, "RAIL window minimized, sending restore command.");
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
 
-            RAIL_SYSCOMMAND_ORDER syscommand;
-            syscommand.windowId = orderInfo->windowId;
-            syscommand.command = SC_RESTORE;
-            rdp_client->rail_interface->ClientSystemCommand(rdp_client->rail_interface, &syscommand);
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the tracked RAIL window currently mapped to the given RDPGFX
+ * surface, or NULL if no such mapping is known. The rail_windows list lock
+ * must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param surface_id
+ *     The server-side ID of the RDPGFX surface to look up.
+ *
+ * @return
+ *     The matching tracked RAIL window, or NULL if no such mapping exists.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window_for_surface(
+        guac_rdp_client* rdp_client, UINT16 surface_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_surface_id
+                && rail_window->surface_id == surface_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    rail_window->z_order = z;
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {
+
+    int z = 1;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->z_order >= z)
+            z = rail_window->z_order + 1;
+    }
+
+    return z;
+
+}
+
+/**
+ * Raises all tracked windows owned by the given parent window above that
+ * parent's layer. The rail_windows list lock must be held.
+ * This is needed for showing popup/dialog windows owned by another window,
+ * which might otherwise stay hidden behind their owner or other RemoteApp windows.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param owner_window_id
+ *     The server-side ID of the owner window.
+ *
+ * @param z
+ *     The z-order to assign to the first owned window.
+ *
+ * @return
+ *     The next available z-order after all owned windows have been raised.
+ */
+static int guac_rdp_rail_raise_owned_windows(guac_rdp_client* rdp_client,
+        UINT64 owner_window_id, int z) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window != NULL
+                && rail_window->has_rail_order
+                && rail_window->owner_window_id == owner_window_id
+                && rail_window->window_id != owner_window_id) {
+            guac_rdp_rail_stack_window(rail_window, z++);
+            z = guac_rdp_rail_raise_owned_windows(rdp_client,
+                    rail_window->window_id, z);
         }
+
+        current = current->next;
     }
 
-    return TRUE;
+    return z;
+
+}
+
+/**
+ * Raises the given window above its owner, if that owner is currently being
+ * tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param rail_window
+ *     The window that should be raised above its owner.
+ */
+static void guac_rdp_rail_raise_window_above_owner(
+        guac_rdp_client* rdp_client, guac_rdp_rail_window* rail_window) {
+
+    if (rail_window == NULL || rail_window->owner_window_id == 0)
+        return;
+
+    guac_rdp_rail_window* owner = guac_rdp_rail_get_window(rdp_client,
+            rail_window->owner_window_id);
+
+    if (owner == NULL)
+        return;
+
+    int z = guac_rdp_rail_get_top_z(rdp_client);
+
+    guac_rdp_rail_stack_window(rail_window, z);
+
+}
+
+/**
+ * Sends a RAIL Client Activate order for the given window.
+ *
+ * RAIL servers expect explicit activation notifications when the active
+ * RemoteApp window changes.
+ *
+ * Even if the window_id is 64 bits, MS RAIL protocol activation orders use a 32-bit window ID.
+ * Ignore any invalid window IDs that cannot be represented without truncating to 32-bits.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the window to activate or deactivate.
+ *
+ * @param enabled
+ *     Non-zero to activate the window, zero to deactivate it.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the activation order was sent successfully, or a
+ *     protocol-specific error code otherwise.
+ */
+static UINT guac_rdp_rail_send_activate(guac_rdp_client* rdp_client,
+        UINT64 window_id, BOOL enabled) {
+
+    if (rdp_client->rail_interface == NULL
+            || rdp_client->rail_interface->ClientActivate == NULL)
+        return CHANNEL_RC_OK;
+
+    if (window_id > UINT32_MAX)
+        return CHANNEL_RC_OK;
+
+    RAIL_ACTIVATE_ORDER activate = {
+        .windowId = (UINT32) window_id,
+        .enabled = enabled
+    };
+
+    pthread_mutex_lock(&(rdp_client->message_lock));
+    UINT status = rdp_client->rail_interface->ClientActivate(
+            rdp_client->rail_interface, &activate);
+    pthread_mutex_unlock(&(rdp_client->message_lock));
+
+    return status;
+
+}
+
+/**
+ * Sends the RAIL activation to switch focus to the given window.
+ *
+ * The previously active window is first deactivated, followed by activation
+ * of the requested window. The locally tracked active window ID is updated
+ * only if the activation request succeeds.
+ * *
+ * @param rdp_client
+ *     The RDP client whose RAIL channel should be used.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that should be activated.
+ */
+static void guac_rdp_rail_activate_window(guac_rdp_client* rdp_client,
+        UINT64 window_id) {
+
+    if (rdp_client->rail_active_window_id == window_id)
+        return;
+
+    if (guac_rdp_rail_send_activate(rdp_client, window_id, TRUE)
+            == CHANNEL_RC_OK)
+        rdp_client->rail_active_window_id = window_id;
+
+}
+
+void guac_rdp_rail_raise_window_at(guac_rdp_client* rdp_client,
+        int x, int y) {
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return;
+
+    if (rdp_client->rail_windows == NULL)
+        return;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_rdp_rail_window* clicked_window = NULL;
+    UINT64 activate_window_id = 0;
+    int clicked_z = 0;
+    int has_no_surface_window = 0;
+    int highest_no_surface_z = 0;
+    int suppress_activation = 0;
+    int has_adjacent_nosurface_window = 0;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+
+        guac_rdp_rail_window* rail_window = current->data;
+        if (rail_window == NULL)
+            continue;
+
+        if (rail_window->visible && rail_window->has_rail_order
+                && !rail_window->has_surface) {
+
+            if (!has_no_surface_window
+                    || rail_window->z_order > highest_no_surface_z) {
+                highest_no_surface_z = rail_window->z_order;
+                has_no_surface_window = 1;
+            }
+
+            continue;
+        }
+
+        if (!rail_window->visible || !rail_window->has_surface
+                || !rail_window->has_rail_order)
+            continue;
+
+        if (x < rail_window->x || y < rail_window->y
+                || x >= rail_window->x + rail_window->layer_width
+                || y >= rail_window->y + rail_window->layer_height)
+            continue;
+
+        if (clicked_window == NULL || rail_window->z_order > clicked_z) {
+            clicked_window = rail_window;
+            clicked_z = rail_window->z_order;
+        }
+
+    }

Review Comment:
   Added comments to make it easier to follow.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub) via dev" <de...@guacamole.apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r4598079868


##########
src/libguac/tests/display/resize.c:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "display-plan.h"
+#include "display-priv.h"
+#include "guacamole/mem.h"
+
+#include <CUnit/CUnit.h>
+#include <stdint.h>
+
+/* Identical pixels must be resent when resizing clears the client layer. */
+static void check_resize(int old_size, int new_size, int report_dirty) {
+
+    uint32_t pixels[128 * 128] = { 0 };
+    guac_display_layer_cell cells[4] = { 0 };
+    guac_display_layer layer = {
+        .last_frame = {
+            .width = old_size,
+            .height = old_size,
+            .buffer = (unsigned char*) pixels,
+            .buffer_stride = 128 * sizeof(uint32_t)
+        },
+        .pending_frame = {
+            .width = new_size,
+            .height = new_size,
+            .buffer = (unsigned char*) pixels,
+            .buffer_stride = 128 * sizeof(uint32_t)
+        },
+        .pending_frame_cells = cells,
+        .pending_frame_cells_width = 2,
+        .pending_frame_cells_height = 2
+    };
+    guac_display* display = guac_mem_zalloc(sizeof(guac_display));
+    display->pending_frame.layers = &layer;
+
+    if (report_dirty)
+        guac_rect_init(&layer.pending_frame.dirty, 0, 0, 1, 1);
+
+    guac_display_plan* plan = PFW_LFR_guac_display_plan_create(display);
+    if (old_size == new_size) {
+        CU_ASSERT_PTR_NULL(plan);
+        if (plan != NULL)
+            guac_display_plan_free(plan);
+        guac_mem_free(display);
+        return;
+    }
+
+    CU_ASSERT_PTR_NOT_NULL_FATAL(plan);
+    size_t pixels_sent = 0;
+    for (size_t i = 0; i < plan->length; i++) {
+        guac_rect* rect = &plan->ops[i].dest;
+        CU_ASSERT_EQUAL(plan->ops[i].type, GUAC_DISPLAY_PLAN_OPERATION_IMG);
+        CU_ASSERT(rect->left >= 0 && rect->top >= 0);
+        CU_ASSERT(rect->right <= new_size && rect->bottom <= new_size);
+        pixels_sent += guac_rect_width(rect) * guac_rect_height(rect);
+    }
+
+    CU_ASSERT_EQUAL(pixels_sent, new_size * new_size);
+    guac_display_plan_free(plan);
+    guac_mem_free(display);
+
+}
+
+void test_display__resize_repaints_unchanged_pixels(void) {

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub) via dev" <de...@guacamole.apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r4598060936


##########
src/protocols/vnc/vnc.c:
##########


Review Comment:
   It was a separate VNC repaint fix after resizing, part of my codebase. It was not supposed to be part of this feature branch. Removed it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3531110758


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -228,54 +269,1855 @@ static UINT guac_rdp_rail_handshake_ex(RailClientContext* rail,
     return guac_rdp_rail_complete_handshake(rail);
 }
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+/* 
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The RDP server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The RDPGFX surface ID currently mapped to this window, if known.
+     */
+    UINT16 surface_id;
+    int has_surface_id;
+
+    /**
+     * The RDP server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;

Review Comment:
   Fixed this and five other places in rail.c
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#issuecomment-4887047565

   Note that the build currently fails when using FreeRDP on Rocky9 (2.11.7-7.el9_8.3):
   ```
   channels/rail.c: In function ‘guac_rdp_rail_repaint_surface’:
   channels/rail.c:1089:47: error: ‘guac_rdp_client’ has no member named ‘rdpgfx_interface’; did you mean ‘rail_interface’?
    1089 |     RdpgfxClientContext* rdpgfx = rdp_client->rdpgfx_interface;
         |                                               ^~~~~~~~~~~~~~~~
         |                                               rail_interface
   channels/rail.c: In function ‘guac_rdp_rail_window_update’:
   channels/rail.c:1353:31: error: implicit declaration of function ‘ConvertWCharNToUtf8Alloc’ [-Werror=implicit-function-declaration]
    1353 |                 char* title = ConvertWCharNToUtf8Alloc(title_string,
         |                               ^~~~~~~~~~~~~~~~~~~~~~~~
   channels/rail.c:1353:31: error: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   channels/rail.c: In function ‘guac_rdp_rail_paint_surface’:
   channels/rail.c:1921:23: error: implicit declaration of function ‘freerdp_image_copy_no_overlap’ [-Werror=implicit-function-declaration]
    1921 |             success = freerdp_image_copy_no_overlap(
         |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   cc1: all warnings being treated as errors
   make[4]: *** [Makefile:1146: channels/libguac_client_rdp_la-rail.lo] Error 1
   make[4]: Leaving directory '/devpool/guacamole/guacamole-server/src/protocols/rdp'
   make[3]: *** [Makefile:1473: all-recursive] Error 1
   make[3]: Leaving directory '/devpool/guacamole/guacamole-server/src/protocols/rdp'
   make[2]: *** [Makefile:763: all] Error 2
   make[2]: Leaving directory '/devpool/guacamole/guacamole-server/src/protocols/rdp'
   make[1]: *** [Makefile:544: all-recursive] Error 1
   make[1]: Leaving directory '/devpool/guacamole/guacamole-server'
   make: *** [Makefile:464: all] Error 2
   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3243930644


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,185 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == window_id)
+            return current;
+        current = current->next;
+    }
+
+    return NULL;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows_lock must be held.
+ */

Review Comment:
   Please complete the function documentation (see above).



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,185 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == window_id)
+            return current;
+        current = current->next;
+    }
+
+    return NULL;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows_lock must be held.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 0);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);
+
+    rail_window->next = rdp_client->rail_windows;
+    rdp_client->rail_windows = rail_window;
+
+    return rail_window;
+
+}
+
+/**
+ * Updates the visibility of the Guacamole layer backing the given RAIL window.
+ * The rail_windows_lock must be held.
+ */

Review Comment:
   Please complete the function documentation (see above).



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -279,6 +507,258 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
 }
 
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* previous = NULL;
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == orderInfo->windowId) {
+            guac_rdp_rail_free_window(rdp_client, previous, current);
+            rdp_client->gdi_modified = 1;
+            break;
+        }
+        previous = current;
+        current = current->next;
+    }
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return TRUE;
+
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows_lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ */
+static UINT guac_rdp_rail_paint_surface(guac_client* client,
+        gdiGfxSurface* surface, UINT32 rect_count, const RECTANGLE_16* rects) {
+
+    if (surface == NULL || surface->windowId == 0
+            || rects == NULL || rect_count == 0)
+        return CHANNEL_RC_OK;
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_or_create_window(client, surface->windowId);
+
+    if (rail_window == NULL) {
+        pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+        return CHANNEL_RC_OK;
+    }
+
+    /* Per FreeRDP's gdi/gfx.c and xf_gfx.c, mappedWidth/Height and
+     * outputTargetWidth/Height are initialized to the same value at surface
+     * creation, and only diverge when an explicit MapSurfaceToScaledWindow
+     * order rescales the surface. We honor any divergence but optimize for
+     * the common 1:1 case with a plain copy. */
+    int source_width = surface->mappedWidth;
+    int source_height = surface->mappedHeight;
+    int target_width = surface->outputTargetWidth;
+    int target_height = surface->outputTargetHeight;
+
+    if (target_width <= 0) target_width = source_width;
+    if (target_height <= 0) target_height = source_height;

Review Comment:
   Please avoid these single-line `if` statements, in keeping with the established style in the code.



##########
src/protocols/rdp/gdi.c:
##########
@@ -128,14 +128,24 @@ BOOL guac_rdp_gdi_end_paint(rdpContext* context) {
      * checking and bailing out here if an external bug breaks that. */
     GUAC_ASSERT(w <= INT_MAX && h <= INT_MAX);
 
-    /* Mark modified region as dirty, but only within the bounds of the
-     * rendering surface */
-    guac_rect dst_rect;
-    guac_rect_init(&dst_rect, x, y, w, h);
-    guac_rect_constrain(&dst_rect, &current_context->bounds);
-    guac_rect_extend(&current_context->dirty, &dst_rect);
-
-    rdp_client->gdi_modified = 1;
+    /*
+     *For RemoteApp connections the default layer is held
+     * permanently black (filled once in guac_rdp_handle_connection).
+     * Suppress dirty-region propagation so FreeRDP's ongoing painting of
+     * the server desktop into gdi->primary_buffer is never sent to the client.
+     */

Review Comment:
   The comment, here, talks about what happens when RemoteApp is enabled, but it's above a section of code that deals with situations where RemoteApp is *not* in use. The language of this comment should be clarified such that it indicates what the code it is documenting _is_ actually doing.



##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,35 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Updates the Guacamole display layer for a RAIL window from modified areas of
+ * the given RDPGFX surface. Non-RAIL (windowId == 0) surfaces are ignored and
+ * left for FreeRDP's default GDI pipeline.
+ *
+ * Used only as a fallback when UpdateWindowFromSurface is unavailable in the
+ * installed FreeRDP. The invalidRegion of the surface is NOT cleared.
+ */
+UINT guac_rdp_rail_update_surface_area(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT32 rect_count, const RECTANGLE_16* rects);
+
+#ifdef HAVE_RDPGFX_WINDOW_SURFACE_UPDATE
+/**
+ * Updates the Guacamole display layer for a RAIL window from the given
+ * window-mapped RDPGFX surface. FreeRDP's GDI pipeline calls this once per
+ * surface per frame, with the surface's accumulated invalidRegion, and
+ * subsequently clears that region itself.
+ */
+UINT guac_rdp_rail_update_window_from_surface(RdpgfxClientContext* context,
+        gdiGfxSurface* surface);
+#endif
+#endif
+
+/**
+ * Frees all RAIL window state tracked for RemoteApp rendering. Safe to call
+ * even when no RAIL windows are tracked.
+ */

Review Comment:
   Please document properly.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,185 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ */

Review Comment:
   Please complete the function documentation - it should have `@param` tags for all parameters and a `@return` tag for the return value.



##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,35 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Updates the Guacamole display layer for a RAIL window from modified areas of
+ * the given RDPGFX surface. Non-RAIL (windowId == 0) surfaces are ignored and
+ * left for FreeRDP's default GDI pipeline.
+ *
+ * Used only as a fallback when UpdateWindowFromSurface is unavailable in the
+ * installed FreeRDP. The invalidRegion of the surface is NOT cleared.
+ */
+UINT guac_rdp_rail_update_surface_area(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT32 rect_count, const RECTANGLE_16* rects);
+
+#ifdef HAVE_RDPGFX_WINDOW_SURFACE_UPDATE
+/**
+ * Updates the Guacamole display layer for a RAIL window from the given
+ * window-mapped RDPGFX surface. FreeRDP's GDI pipeline calls this once per
+ * surface per frame, with the surface's accumulated invalidRegion, and
+ * subsequently clears that region itself.
+ */

Review Comment:
   Please document properly.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,185 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == window_id)
+            return current;
+        current = current->next;
+    }
+
+    return NULL;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, creating it if necessary.
+ * The rail_windows_lock must be held.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_or_create_window(
+        guac_client* client, UINT64 window_id) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    guac_rdp_rail_window* rail_window =
+        guac_rdp_rail_get_window(rdp_client, window_id);
+
+    if (rail_window != NULL)
+        return rail_window;
+
+    rail_window = guac_mem_zalloc(sizeof(guac_rdp_rail_window));
+    if (rail_window == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        return NULL;
+    }
+
+    rail_window->layer = guac_display_alloc_layer(rdp_client->display, 0);
+    if (rail_window->layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window 0x%08x could "
+                "not be tracked. Window contents may not render.",
+                (unsigned int) window_id);
+        guac_mem_free(rail_window);
+        return NULL;
+    }
+
+    rail_window->window_id = window_id;
+    rail_window->visible = 1;
+
+    /* Keep the layer hidden until we have both a RAIL order (so we know where
+     * to put it) and at least one surface paint (so we have something to
+     * show). */
+    guac_display_layer_set_opacity(rail_window->layer, 0);
+
+    rail_window->next = rdp_client->rail_windows;
+    rdp_client->rail_windows = rail_window;
+
+    return rail_window;
+
+}
+
+/**
+ * Updates the visibility of the Guacamole layer backing the given RAIL window.
+ * The rail_windows_lock must be held.
+ */
+static void guac_rdp_rail_update_window_opacity(
+        guac_rdp_rail_window* rail_window) {
+
+    int opacity = (rail_window->visible
+            && rail_window->has_surface
+            && rail_window->has_rail_order) ? 0xFF : 0;
+    guac_display_layer_set_opacity(rail_window->layer, opacity);
+
+}
+
+/**
+ * Removes the given RAIL window from the list and frees its resources. The
+ * rail_windows_lock must be held.
+ */

Review Comment:
   Please complete the function documentation (see above).



##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,35 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Updates the Guacamole display layer for a RAIL window from modified areas of
+ * the given RDPGFX surface. Non-RAIL (windowId == 0) surfaces are ignored and
+ * left for FreeRDP's default GDI pipeline.
+ *
+ * Used only as a fallback when UpdateWindowFromSurface is unavailable in the
+ * installed FreeRDP. The invalidRegion of the surface is NOT cleared.
+ */

Review Comment:
   As in the `rail.c` code, functions here need to be fully-documented, including `@param` tags for each parameter and `@return` tags documenting the return value.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -279,6 +507,258 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
 }
 
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    pthread_mutex_lock(&(rdp_client->rail_windows_lock));
+
+    guac_rdp_rail_window* previous = NULL;
+    guac_rdp_rail_window* current = rdp_client->rail_windows;
+
+    while (current != NULL) {
+        if (current->window_id == orderInfo->windowId) {
+            guac_rdp_rail_free_window(rdp_client, previous, current);
+            rdp_client->gdi_modified = 1;
+            break;
+        }
+        previous = current;
+        current = current->next;
+    }
+
+    pthread_mutex_unlock(&(rdp_client->rail_windows_lock));
+
+    return TRUE;
+
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows_lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ */

Review Comment:
   Please complete function documentation (see above).



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +47,185 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateWindowFromSurface (preferred; called once per surface per frame
+ *     with the surface's accumulated invalid region), or
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ * We never install both at once on the same surface: when the window callback
+ * is available, UpdateSurfaceArea is either not installed at all or no-ops on
+ * window-mapped surfaces.
+ */
+
+struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+    /**
+     * The next RAIL window in the list.
+     */
+    guac_rdp_rail_window* next;
+
+};
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows_lock must be held.
+ */

Review Comment:
   As above, please complete the function documentation.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -279,6 +507,258 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
 }
 
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ */

Review Comment:
   Please complete function documentation (see above).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3413614850


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,332 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ */
+
+typedef struct guac_rdp_rail_window {

Review Comment:
   This `guac_rdp_rail_window` struct needs a comment describing it, it's purpose, etc.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3438597655


##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,118 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+/**
+ * Raises the topmost tracked RAIL window containing the given display
+ * coordinates, if any.
+ *
+ * This is used as a local fallback for RemoteApp+GFX window ordering. Windows
+ * may accept a click on a RemoteApp window without sending a monitored desktop
+ * z-order or DESKTOP_ACTIVE_WND update. In that case, FreeRDP receives the
+ * input normally, but Guacamole's separate per-window layers would remain in
+ * the old visual order unless the clicked layer is raised locally.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param x
+ *     The X coordinate of the mouse event.
+ *
+ * @param y
+ *     The Y coordinate of the mouse event.
+ */
+void guac_rdp_rail_raise_window_at(struct guac_rdp_client* rdp_client,
+        int x, int y);
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Handles notification that the given RDPGFX surface has been mapped to the
+ * given RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface mapping.
+ *
+ * @param surface_id
+ *     The server-side ID of the mapped RDPGFX surface.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to which the surface was mapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface mapping was handled successfully.
+ */
+UINT guac_rdp_rail_map_window_for_surface(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT64 window_id);
+
+/**
+ * Handles notification that an RDPGFX surface is no longer mapped to the given
+ * RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface unmapping.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that was unmapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface unmapping was handled successfully.
+ */
+UINT guac_rdp_rail_unmap_window_for_surface(RdpgfxClientContext* context,
+        UINT64 window_id);
+
+/**
+ * Updates the Guacamole display layer for a RAIL window from modified areas of
+ * the given RDPGFX surface. Non-RAIL (windowId == 0) surfaces are ignored and
+ * left for FreeRDP's default GDI pipeline.
+ *
+ * Used only as a fallback when UpdateWindowFromSurface is unavailable in the
+ * installed FreeRDP. The invalidRegion of the surface is NOT cleared.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface update.
+ *
+ * @param surface_id
+ *     The server-side ID of the updated RDPGFX surface.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.

Review Comment:
   What type of error code? FreeRDP? Guacamole? Generic negative/positive integer?



##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,118 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+/**
+ * Raises the topmost tracked RAIL window containing the given display
+ * coordinates, if any.
+ *
+ * This is used as a local fallback for RemoteApp+GFX window ordering. Windows
+ * may accept a click on a RemoteApp window without sending a monitored desktop
+ * z-order or DESKTOP_ACTIVE_WND update. In that case, FreeRDP receives the
+ * input normally, but Guacamole's separate per-window layers would remain in
+ * the old visual order unless the clicked layer is raised locally.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param x
+ *     The X coordinate of the mouse event.
+ *
+ * @param y
+ *     The Y coordinate of the mouse event.
+ */
+void guac_rdp_rail_raise_window_at(struct guac_rdp_client* rdp_client,
+        int x, int y);
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Handles notification that the given RDPGFX surface has been mapped to the
+ * given RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface mapping.
+ *
+ * @param surface_id
+ *     The server-side ID of the mapped RDPGFX surface.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to which the surface was mapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface mapping was handled successfully.

Review Comment:
   ...or what, if the mapping was not handled correctly?



##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,118 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+/**
+ * Raises the topmost tracked RAIL window containing the given display
+ * coordinates, if any.
+ *
+ * This is used as a local fallback for RemoteApp+GFX window ordering. Windows
+ * may accept a click on a RemoteApp window without sending a monitored desktop
+ * z-order or DESKTOP_ACTIVE_WND update. In that case, FreeRDP receives the
+ * input normally, but Guacamole's separate per-window layers would remain in
+ * the old visual order unless the clicked layer is raised locally.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param x
+ *     The X coordinate of the mouse event.
+ *
+ * @param y
+ *     The Y coordinate of the mouse event.
+ */
+void guac_rdp_rail_raise_window_at(struct guac_rdp_client* rdp_client,
+        int x, int y);
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Handles notification that the given RDPGFX surface has been mapped to the
+ * given RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface mapping.
+ *
+ * @param surface_id
+ *     The server-side ID of the mapped RDPGFX surface.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to which the surface was mapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface mapping was handled successfully.
+ */
+UINT guac_rdp_rail_map_window_for_surface(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT64 window_id);
+
+/**
+ * Handles notification that an RDPGFX surface is no longer mapped to the given
+ * RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface unmapping.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that was unmapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface unmapping was handled successfully.

Review Comment:
   ...or what, if the unmapping was not handled successfully?



##########
src/protocols/rdp/channels/rail.h:
##########
@@ -60,5 +67,118 @@
  */
 void guac_rdp_rail_load_plugin(rdpContext* context);
 
+/**
+ * Raises the topmost tracked RAIL window containing the given display
+ * coordinates, if any.
+ *
+ * This is used as a local fallback for RemoteApp+GFX window ordering. Windows
+ * may accept a click on a RemoteApp window without sending a monitored desktop
+ * z-order or DESKTOP_ACTIVE_WND update. In that case, FreeRDP receives the
+ * input normally, but Guacamole's separate per-window layers would remain in
+ * the old visual order unless the clicked layer is raised locally.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked RAIL windows should be searched.
+ *
+ * @param x
+ *     The X coordinate of the mouse event.
+ *
+ * @param y
+ *     The Y coordinate of the mouse event.
+ */
+void guac_rdp_rail_raise_window_at(struct guac_rdp_client* rdp_client,
+        int x, int y);
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+/**
+ * Handles notification that the given RDPGFX surface has been mapped to the
+ * given RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface mapping.
+ *
+ * @param surface_id
+ *     The server-side ID of the mapped RDPGFX surface.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to which the surface was mapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface mapping was handled successfully.
+ */
+UINT guac_rdp_rail_map_window_for_surface(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT64 window_id);
+
+/**
+ * Handles notification that an RDPGFX surface is no longer mapped to the given
+ * RAIL window.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface unmapping.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window that was unmapped.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface unmapping was handled successfully.
+ */
+UINT guac_rdp_rail_unmap_window_for_surface(RdpgfxClientContext* context,
+        UINT64 window_id);
+
+/**
+ * Updates the Guacamole display layer for a RAIL window from modified areas of
+ * the given RDPGFX surface. Non-RAIL (windowId == 0) surfaces are ignored and
+ * left for FreeRDP's default GDI pipeline.
+ *
+ * Used only as a fallback when UpdateWindowFromSurface is unavailable in the
+ * installed FreeRDP. The invalidRegion of the surface is NOT cleared.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface update.
+ *
+ * @param surface_id
+ *     The server-side ID of the updated RDPGFX surface.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.
+ */
+UINT guac_rdp_rail_update_surface_area(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT32 rect_count, const RECTANGLE_16* rects);
+
+#ifdef HAVE_RDPGFX_WINDOW_SURFACE_UPDATE
+/**
+ * Updates the Guacamole display layer for a RAIL window from the given
+ * window-mapped RDPGFX surface. FreeRDP's GDI pipeline calls this once per
+ * surface per frame, with the surface's accumulated invalidRegion, and
+ * subsequently clears that region itself.
+ *
+ * @param context
+ *     The RDPGFX client context associated with the surface update.
+ *
+ * @param surface
+ *     The RDPGFX surface containing the updated window contents.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.

Review Comment:
   What type of error code?



##########
src/protocols/rdp/channels/rdpgfx.c:
##########
@@ -119,4 +160,3 @@ void guac_rdp_rdpgfx_load_plugin(rdpContext* context) {
     guac_freerdp_dynamic_channel_collection_add(context->settings, "rdpgfx", NULL);
 
 }
-

Review Comment:
   I believe we determined recently that the empty line at the end of the file is actually required on some platforms.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,556 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list.
+     * Used to avoid applying the raise to windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */

Review Comment:
   #nitpick, but I think the `Used to avoid...` portion can be moved to the line above and everything re-spaced to make a nicer comment block.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,556 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list.
+     * Used to avoid applying the raise to windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known.
+     * If the window does not yet have authoritative z-order, we apply this fallback raise only once.

Review Comment:
   Similar to just above - the `If the window...` portion can move to the line above.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,556 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list.
+     * Used to avoid applying the raise to windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known.
+     * If the window does not yet have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @return
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
+ */
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
+
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
+
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
+
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
+
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    rail_window->z_order = z;
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {
+
+    int z = 1;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->z_order >= z)

Review Comment:
   And, here...check to make sure `rail_window` is not null.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -279,6 +945,486 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
 }
 
+/**
+ * Handler for the RAIL WindowDelete order. Removes any tracked RAIL window
+ * with the given ID.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing information about the window that was deleted.
+ * 
+ * @return
+ *     TRUE if the window deletion was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_window_delete(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    guac_common_list_element* element =
+        guac_rdp_rail_get_window_element(rdp_client, orderInfo->windowId);
+
+    if (element != NULL) {
+        guac_rdp_rail_free_window_data(element->data);
+        guac_common_list_remove(rdp_client->rail_windows, element);
+        rdp_client->gdi_modified = 1;
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    return TRUE;
+
+}
+
+/**
+ * Handler for RAIL monitored desktop orders. These orders include the
+ * server-side z-order of RemoteApp windows as well setting active window.
+ *
+ * @param context
+ *     A pointer to the rdpContext struct associated with the current RDP connection.
+ *
+ * @param orderInfo
+ *     A pointer to the struct containing desktop order flags.
+ *
+ * @param monitoredDesktop
+ *     A pointer to the monitored desktop state sent by the server.
+ *
+ * @return
+ *     TRUE if the desktop order was handled successfully, otherwise FALSE.
+ */
+static BOOL guac_rdp_rail_monitored_desktop(rdpContext* context,
+        RAIL_CONST WINDOW_ORDER_INFO* orderInfo,
+        RAIL_CONST MONITORED_DESKTOP_ORDER* monitoredDesktop) {
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (!guac_rdp_rail_uses_gfx(rdp_client))
+        return TRUE;
+
+    if (!(orderInfo->fieldFlags & (WINDOW_ORDER_FIELD_DESKTOP_ZORDER
+                    | WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)))
+        return TRUE;
+
+    guac_common_list_lock(rdp_client->rail_windows);
+
+    if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ZORDER)
+            && monitoredDesktop->windowIds != NULL) {
+
+        for (guac_common_list_element* current = rdp_client->rail_windows->head;
+                current != NULL; current = current->next) {
+            guac_rdp_rail_window* rail_window = current->data;
+            rail_window->in_monitored_desktop_zorder = 0;
+        }
+
+        /* The server-provided list is authoritative for visual stacking. */
+        for (UINT32 i = 0; i < monitoredDesktop->numWindowIds; i++) {
+
+            guac_rdp_rail_window* rail_window =
+                guac_rdp_rail_get_window(rdp_client,
+                        monitoredDesktop->windowIds[i]);
+
+            int z = (int) (monitoredDesktop->numWindowIds - i);
+
+            if (rail_window != NULL) {
+                rail_window->in_monitored_desktop_zorder = 1;
+                guac_rdp_rail_stack_window(rail_window, z);
+                guac_rdp_rail_raise_owned_windows(rdp_client,
+                        rail_window->window_id, z + 1);
+                rdp_client->gdi_modified = 1;
+            }
+
+        }
+
+    }
+
+    if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)
+            && monitoredDesktop->activeWindowId != 0) {
+
+        /* Treat the server's active-window update as the authoritative focus
+         * change and raise that window above the current RAIL stack. Create
+         * tracking if needed, as some apps can trigger DESKTOP_ACTIVE_WND
+         * before the matching RAIL window update arrives. */
+        guac_rdp_rail_window* active_window =
+            guac_rdp_rail_get_or_create_window(client,
+                    monitoredDesktop->activeWindowId);
+
+        if (active_window != NULL) {
+
+            if (!active_window->has_rail_order) {
+                active_window->has_rail_order = 1;
+                active_window->visible = 1;
+                guac_rdp_rail_update_window_opacity(active_window);
+            }
+
+            if (active_window->has_surface && active_window->visible)
+                guac_rdp_rail_paint_background(rdp_client);
+
+            int z = guac_rdp_rail_get_top_z(rdp_client);
+            guac_rdp_rail_stack_window(active_window, z);
+            guac_rdp_rail_raise_owned_windows(rdp_client,
+                    active_window->window_id, z + 1);
+            rdp_client->gdi_modified = 1;
+        }
+
+    }
+
+    guac_common_list_unlock(rdp_client->rail_windows);
+
+    return TRUE;
+
+}
+
+#ifdef HAVE_RDPGFX_SURFACE_AREA_UPDATE
+UINT guac_rdp_rail_map_window_for_surface(RdpgfxClientContext* context,
+        UINT16 surface_id, UINT64 window_id) {
+
+    (void) context;
+    (void) surface_id;
+    (void) window_id;
+
+    return CHANNEL_RC_OK;
+
+}
+
+UINT guac_rdp_rail_unmap_window_for_surface(RdpgfxClientContext* context,
+        UINT64 window_id) {
+
+    (void) context;
+    (void) window_id;
+
+    return CHANNEL_RC_OK;
+
+}
+
+/**
+ * Copies updated regions from the given RDPGFX surface to the Guacamole layer
+ * associated with that surface's RAIL window. The rail_windows list lock must NOT
+ * be held on entry; this function takes it internally.
+ *
+ * The invalidRegion of the surface is left untouched. FreeRDP's GDI pipeline
+ * owns that region and may consume or clear it independently; touching it
+ * from these callbacks led to misplaced updates in early versions of this
+ * code (menus appearing in the wrong location, etc.) because regions were
+ * being double-consumed.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @param surface
+ *     The RDPGFX surface containing the updated window contents.
+ *
+ * @param rect_count
+ *     The number of rectangles within the rects array.
+ *
+ * @param rects
+ *     The updated rectangular regions of the surface to copy.
+ *
+ * @return
+ *     CHANNEL_RC_OK if the surface was painted successfully, or an error code.

Review Comment:
   What kind of error code?



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,556 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;

Review Comment:
   On these two comments it might be good to clarify that "server-side" means RDP server-side (versus Guacamole server/guacd), just to avoid confusion.



##########
src/protocols/rdp/channels/rdpgfx.c:
##########
@@ -58,16 +61,54 @@ static void guac_rdp_rdpgfx_channel_connected(rdpContext* context,
     if (strcmp(args->name, RDPGFX_DVC_CHANNEL_NAME) != 0)
         return;
 
-    /* Init GDI-backed support for the Graphics Pipeline */
     RdpgfxClientContext* rdpgfx = (RdpgfxClientContext*) args->pInterface;
     rdpGdi* gdi = context->gdi;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+    guac_rdp_settings* settings = rdp_client->settings;
+    BOOL gfx_initialized;
 
-    if (!gdi_graphics_pipeline_init(gdi, rdpgfx))
+/*
+ * Choose the RAIL+GFX hookup based on FreeRDP capabilities:
+ *
+ *   If UpdateWindowFromSurface is available, use the normal graphics
+ *   pipeline and render RemoteApp windows through that callback.
+ *
+ *   If only UpdateSurfaceArea is available, use the _ex initializer and
+ *   route RemoteApp window updates through our custom surface-area handler.
+ *
+ *   On older FreeRDP versions, fall back to the default pipeline. In that
+ *   case, RemoteApp with GFX will not render correctly and GFX must be
+ *   disabled.
+ */
+#if defined(HAVE_RDPGFX_WINDOW_SURFACE_UPDATE)
+    gfx_initialized = (settings->remote_app != NULL)
+        ? gdi_graphics_pipeline_init_ex(gdi, rdpgfx,
+                guac_rdp_rail_map_window_for_surface,
+                guac_rdp_rail_unmap_window_for_surface, NULL)
+        : gdi_graphics_pipeline_init(gdi, rdpgfx);
+#elif defined(HAVE_RDPGFX_SURFACE_AREA_UPDATE)
+    gfx_initialized = (settings->remote_app != NULL)
+        ? gdi_graphics_pipeline_init_ex(gdi, rdpgfx,
+                guac_rdp_rail_map_window_for_surface,
+                guac_rdp_rail_unmap_window_for_surface,
+                guac_rdp_rail_update_surface_area)
+        : gdi_graphics_pipeline_init(gdi, rdpgfx);
+#else
+    gfx_initialized = gdi_graphics_pipeline_init(gdi, rdpgfx);
+#endif
+
+    if (!gfx_initialized) {
         guac_client_log(client, GUAC_LOG_WARNING, "Rendering backend for RDPGFX "
                 "channel could not be loaded. Graphics may not render at all!");
-    else
+    } else {

Review Comment:
   Please don't cuddle braces.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -166,6 +551,15 @@ static UINT guac_rdp_rail_execute_result(RailClientContext* context,
         const RAIL_EXEC_RESULT_ORDER* execResult) {
 
     guac_client* client = (guac_client*) context->custom;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (execResult->execResult == RAIL_EXEC_E_NOT_IN_ALLOWLIST

Review Comment:
   Why would we just ignore it? If the application is not in the allow list, the server shouldn't actually provide the application, and we shouldn't get anything, correct?



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,556 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list.
+     * Used to avoid applying the raise to windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known.
+     * If the window does not yet have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @return
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
+ */
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
+
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
+
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
+
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
+
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)

Review Comment:
   Similarly, here, might be worth checking to make sure `rail_window` is not null before trying to grab the member.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,556 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list.
+     * Used to avoid applying the raise to windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known.
+     * If the window does not yet have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @return
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
+ */
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
+
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
+
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
+
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
+
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)

Review Comment:
   It might be worth adding a check, here for `rail_window != null`, just to protect against a null pointer de-reference.



##########
src/protocols/rdp/channels/rail.c:
##########
@@ -37,6 +48,556 @@
 #include <stddef.h>
 #include <string.h>
 
+/*
+ * RemoteApp (RAIL) with the RDPGFX pipeline.
+ *
+ * When both RAIL and GFX are active, the RDP server delivers per-window
+ * graphical updates as RDPGFX surfaces tagged with a non-zero windowId
+ * (gdiGfxSurface->windowId). The default GDI graphics pipeline only knows how
+ * to paint into a single primary desktop buffer, so without intervention these
+ * window-mapped surfaces are silently dropped on the floor.
+ *
+ * This module tracks each RAIL window with its own guac_display_layer,
+ * positioned by the RAIL window-order stream and painted from the corresponding
+ * RDPGFX surface via either:
+ *
+ *   - UpdateSurfaceArea (fallback for older FreeRDP without the window
+ *     callback; called with rect lists for any surface update).
+ *
+ *   - UpdateWindowFromSurface, when available.
+ *
+ * Each guac_rdp_rail_window stores the per-window state needed to combine
+ * RAIL window ordering with RDPGFX surface updates and render the result as a
+ * Guacamole layer.
+ */
+
+typedef struct guac_rdp_rail_window {
+
+    /**
+     * The server-side ID of the RAIL window.
+     */
+    UINT64 window_id;
+
+    /**
+     * The server-side ID of the owning RAIL window, if any.
+     */
+    UINT64 owner_window_id;
+
+    /**
+     * The Guacamole layer that renders the contents of this window.
+     */
+    guac_display_layer* layer;
+
+    /**
+     * The position of this window within the remote desktop.
+     */
+    int x;
+    int y;
+
+    /**
+     * Whether this window should be visible (per RAIL show state).
+     */
+    int visible;
+
+    /**
+     * Whether this window has received at least one graphical update from an
+     * RDPGFX surface.
+     */
+    int has_surface;
+
+    /**
+     * Whether a RAIL WindowCreate/Update order has been observed for this
+     * window. Layers created speculatively from a surface update (before any
+     * RAIL order arrives) are held off-screen until this is set.
+     */
+    int has_rail_order;
+
+    /**
+     * Whether this window was included in the most recent server-provided
+     * monitored desktop z-order list.
+     * Used to avoid applying the raise to windows that already have authoritative z-order.
+     * We obey the server provided z-order, whenever it is available.
+     */
+    int in_monitored_desktop_zorder;
+
+    /**
+     * Whether this window has already been raised due to receiving its first
+     * visible paint after its RAIL order was known.
+     * If the window does not yet have authoritative z-order, we apply this fallback raise only once.
+     */
+    int painted_raise_done;
+
+    /**
+    * The last z-order assigned to the Guacamole layer for this window.
+    *
+    * Guacamole layer stacking is managed locally, separately from the server's
+    * RAIL window IDs. Tracking the assigned z-order lets us find the current
+    * topmost layer and raise another window above it without relying on list
+    * order or window IDs.
+    */
+    int z_order;
+
+    /**
+     * The current dimensions of the Guacamole layer.
+     */
+    int layer_width;
+    int layer_height;
+
+} guac_rdp_rail_window;
+
+/**
+ * Returns whether RAIL window surfaces should be rendered through Guacamole
+ * layers for the given RDP client.
+ *
+ * RemoteApp windows are rendered as separate Guacamole layers only when both
+ * RemoteApp and RDPGFX are enabled. If GFX is disabled, rendering falls back to
+ * the normal GDI/primary-buffer path and the RAIL layer tracking code should
+ * stay inactive.
+ *
+ * @param rdp_client
+ *     The RDP client whose settings should be checked.
+ *
+ * @return
+ *     Non-zero if RAIL window surfaces should be rendered through Guacamole layers.
+ */
+static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
+    return rdp_client->settings != NULL
+        && rdp_client->settings->remote_app != NULL
+        && rdp_client->settings->enable_gfx;
+}
+
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @return
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
+ */
+static guac_display_layer* guac_rdp_rail_get_window_layer(
+        guac_client* client) {
+
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
+    if (rdp_client->rail_window_layer != NULL) {
+        guac_rdp_rail_update_window_layer_bounds(rdp_client);
+        return rdp_client->rail_window_layer;
+    }
+
+    guac_display_layer* layer =
+        guac_display_alloc_layer(rdp_client->display, 0);
+
+    if (layer == NULL) {
+        guac_client_log(client, GUAC_LOG_WARNING, "RAIL window container "
+                "could not be allocated. Window contents may not render.");
+        return NULL;
+    }
+
+    guac_display_layer_set_parent(layer,
+            guac_display_default_layer(rdp_client->display));
+    guac_display_layer_move(layer, 0, 0);
+    guac_display_layer_stack(layer, 0);
+
+    rdp_client->rail_window_layer = layer;
+    guac_rdp_rail_update_window_layer_bounds(rdp_client);
+
+    return layer;
+
+}
+
+/**
+ * Returns the RAIL window having the given ID, or NULL if no such window is
+ * being tracked. The rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The matching RAIL window, or NULL if no such window is being tracked.
+ */
+static guac_rdp_rail_window* guac_rdp_rail_get_window(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return rail_window;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Returns the list element containing the RAIL window having the given ID, or
+ * NULL if no such window is being tracked. The rail_windows list lock must be
+ * held.
+ *
+ * @param rdp_client
+ *     The RDP client instance whose tracked RAIL windows should be searched.
+ *
+ * @param window_id
+ *     The server-side ID of the RAIL window to retrieve.
+ *
+ * @return
+ *     The list element containing the matching RAIL window, or NULL if no
+ *     such window is being tracked.
+ */
+static guac_common_list_element* guac_rdp_rail_get_window_element(
+        guac_rdp_client* rdp_client, UINT64 window_id) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->window_id == window_id)
+            return current;
+
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * Updates the stacking order of the Guacamole layer backing the given RAIL
+ * window. The rail_windows list lock must be held.
+ *
+ * @param rail_window
+ *     The RAIL window whose backing Guacamole layer should be stacked.
+ *
+ * @param z
+ *     The new stacking order.
+ */
+static void guac_rdp_rail_stack_window(guac_rdp_rail_window* rail_window,
+        int z) {
+
+    rail_window->z_order = z;
+
+    guac_display_layer_stack(rail_window->layer, z);
+
+}
+
+/**
+ * Returns a z-order value above all currently tracked RAIL windows. The
+ * rail_windows list lock must be held.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be counted.
+ *
+ * @return
+ *     A z-order value above all currently tracked RAIL windows.
+ */
+static int guac_rdp_rail_get_top_z(guac_rdp_client* rdp_client) {
+
+    int z = 1;
+
+    for (guac_common_list_element* current = rdp_client->rail_windows->head;
+            current != NULL; current = current->next) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->z_order >= z)
+            z = rail_window->z_order + 1;
+    }
+
+    return z;
+
+}
+
+/**
+ * Raises all tracked windows owned by the given parent window above that
+ * parent's layer. The rail_windows list lock must be held.
+ * This is needed for showing popup/dialog windows owned by another window,
+ * which might otherwise stay hidden behind their owner or other RemoteApp windows.
+ *
+ * @param rdp_client
+ *     The RDP client whose tracked windows should be searched.
+ *
+ * @param owner_window_id
+ *     The server-side ID of the owner window.
+ *
+ * @param z
+ *     The z-order to assign to the first owned window.
+ *
+ * @return
+ *     The next available z-order after all owned windows have been raised.
+ */
+static int guac_rdp_rail_raise_owned_windows(guac_rdp_client* rdp_client,
+        UINT64 owner_window_id, int z) {
+
+    guac_common_list_element* current = rdp_client->rail_windows->head;
+
+    while (current != NULL) {
+        guac_rdp_rail_window* rail_window = current->data;
+
+        if (rail_window->owner_window_id == owner_window_id) {

Review Comment:
   And, here...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3252811007


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -259,6 +482,45 @@ static BOOL guac_rdp_rail_window_update(rdpContext* context,
 
     UINT32 fieldFlags = orderInfo->fieldFlags;
 
+    /* Track window location and visibility for RDPGFX RemoteApp rendering. */
+    if (guac_rdp_rail_uses_gfx(rdp_client)) {

Review Comment:
   Don't know if that's feasible without major changes, as GFX rendering tracks window ownership and gives us the surface data later via windowId, but non-GFX rendering renders directly to primary desktop buffer, without ownership information which app owns the pixels. It renders only rectangles without available metadata. Figuring out to which layer those pixels should go would most likely be problematic.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3253223256


##########
src/protocols/rdp/rdp.c:
##########
@@ -536,6 +536,54 @@ static int guac_rdp_handle_connection(guac_client* client) {
     guac_display_layer* default_layer = guac_display_default_layer(rdp_client->display);
     guac_display_layer_resize(default_layer, rdp_client->settings->width, rdp_client->settings->height);
 
+    /*
+     * For RemoteApp connections, the default layer
+     * represents the (invisible) desktop behind the application windows.
+     * Fill it once with opaque black; guac_rdp_gdi_end_paint refuses to
+     * mark it dirty again for the rest of the session, so FreeRDP's
+     * ongoing painting of the server desktop into the shared GDI primary
+     * buffer never reaches the client.
+     *
+     * Without this, GFX-enabled RemoteApp sessions show the server's
+     * desktop behind the application
+     * windows because gdi->primary_buffer shares memory with the default
+     * layer and FreeRDP paints all windowId==0 surfaces into it.
+     */
+    if (rdp_client->settings->remote_app != NULL) {
+
+        guac_display_layer_raw_context* bg_ctx =

Review Comment:
   This code was correct, the problem was elsewhere.
   Basically, FreeRDPs GDI buffer points to Guacamole's default layer and kept updating it, so pixels leaked to RAIL app background.
   Remote app rendering path now separates default layer from gdi->primary_buffer, so the background remains black.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3254302139


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -126,6 +126,77 @@ static int guac_rdp_rail_uses_gfx(guac_rdp_client* rdp_client) {
         && rdp_client->settings->enable_gfx;
 }
 
+/**
+ * Updates the RemoteApp window container layer to cover the default layer.
+ *
+ * @param rdp_client
+ *     The RDP client whose RemoteApp window container should be resized.
+ */
+static void guac_rdp_rail_update_window_layer_bounds(
+        guac_rdp_client* rdp_client) {
+
+    if (rdp_client->rail_window_layer == NULL)
+        return;
+
+    guac_display_layer* default_layer =
+        guac_display_default_layer(rdp_client->display);
+
+    guac_rect bounds;
+    guac_display_layer_get_bounds(default_layer, &bounds);
+
+    int width = guac_rect_width(&bounds);
+    int height = guac_rect_height(&bounds);
+
+    if (width > 0 && height > 0)
+        guac_display_layer_resize(rdp_client->rail_window_layer,
+                width, height);
+
+}
+
+/**
+ * Returns the layer used as the common parent for all RemoteApp window layers,
+ * creating it if necessary. This additional layer keeps RemoteApp window
+ * z-order local to the window group, preventing those windows from stacking
+ * above Guacamole's cursor layer.
+ *
+ * @param client
+ *     The Guacamole client associated with the RDP session.
+ *
+ * @return
+ *     The RemoteApp window container layer, or NULL if it could not be
+ *     allocated.
+ */
+static guac_display_layer* guac_rdp_rail_get_window_layer(

Review Comment:
   Remote App window ordering was broken and fixing that causes mouse layer to be rendered under the app windows. Attempt to fix that with negative zIndexes was not allowed/wrong approach.
   So instead, we render all rail windows in the same container (in correct order), that stays under the cursor layer.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub)" <gi...@apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r3254308680


##########
src/protocols/rdp/channels/rail.c:
##########
@@ -683,6 +914,10 @@ static UINT guac_rdp_rail_paint_surface(guac_client* client,
     guac_rect source_bounds;
     guac_rect_init(&source_bounds, 0, 0, source_width, source_height);
 
+    /* RDPGFX window surfaces are copied as opaque layer content. We don't
+     * want holes on our RemoteApp windows. */
+    UINT32 dst_format = guac_rdp_get_native_pixel_format(FALSE);

Review Comment:
   GFX windows might have alpha channel, we don't want that when copying the layers to Guacamole side. (managed to get holes in paint app, where there should have been black instead). This fixes the issue.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub) via dev" <de...@guacamole.apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r4598047052


##########
configure.ac:
##########
@@ -865,6 +865,13 @@ then
                        have_freerdp=no])
 fi
 
+# FreeRDP 3.x headers themselves reference deprecated types and functions.
+if test "x${freerdp_version}" = "x(3.x)"
+then
+    RDP_CFLAGS="$RDP_CFLAGS -Wno-deprecated-declarations"
+    CPPFLAGS="$CPPFLAGS -Wno-deprecated-declarations"
+fi
+

Review Comment:
   Not fan of this either, but FreeRDP 3.32.2 fails to compile if not disabling deprecation warnings (as Guacamole sets -Werror)
   
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GUACAMOLE-2123: Fix remote app render issues. Use per-window guac_dis… [guacamole-server]

Posted by "jjheinon (via GitHub) via dev" <de...@guacamole.apache.org>.
jjheinon commented on code in PR #673:
URL: https://github.com/apache/guacamole-server/pull/673#discussion_r4598076509


##########
src/protocols/rdp/channels/rdpgfx.c:
##########
@@ -17,7 +17,10 @@
  * under the License.
  */
 
+#include "config.h"

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org