[Bf-blender-cvs] [ff8c0f062a6] master: GHOST/Wayland: code comments, minor improvements

Campbell Barton noreply at git.blender.org
Fri Oct 21 02:09:21 CEST 2022


Commit: ff8c0f062a6ac521b13ebbda3103cda4f9a4dc46
Author: Campbell Barton
Date:   Fri Oct 21 10:00:09 2022 +1100
Branches: master
https://developer.blender.org/rBff8c0f062a6ac521b13ebbda3103cda4f9a4dc46

GHOST/Wayland: code comments, minor improvements

- Clarify how data_offer is used for both the clipboard & drag-and-drop.
- Acquire the clipboards mutex lock before freeing.
- Log a warning when creating a pipe fails.
- Add doxy-sections.

===================================================================

M	intern/ghost/intern/GHOST_SystemWayland.cpp

===================================================================

diff --git a/intern/ghost/intern/GHOST_SystemWayland.cpp b/intern/ghost/intern/GHOST_SystemWayland.cpp
index fb69973eac6..68ad028631c 100644
--- a/intern/ghost/intern/GHOST_SystemWayland.cpp
+++ b/intern/ghost/intern/GHOST_SystemWayland.cpp
@@ -234,7 +234,7 @@ static const GWL_ModifierInfo g_modifier_info_table[MOD_INDEX_NUM] = {
 /** \} */
 
 /* -------------------------------------------------------------------- */
-/** \name Private Types & Defines
+/** \name Internal #GWL_SimpleBuffer Type
  * \{ */
 
 struct GWL_SimpleBuffer {
@@ -250,7 +250,9 @@ static void gwl_simple_buffer_free_data(GWL_SimpleBuffer *buffer)
   buffer->data_size = 0;
 }
 
-static void gwl_simple_buffer_set(GWL_SimpleBuffer *buffer, const char *data, size_t data_size)
+static void gwl_simple_buffer_set_and_take_ownership(GWL_SimpleBuffer *buffer,
+                                                     const char *data,
+                                                     size_t data_size)
 {
   free(const_cast<char *>(buffer->data));
   buffer->data = data;
@@ -274,6 +276,12 @@ static char *gwl_simple_buffer_as_string(const GWL_SimpleBuffer *buffer)
   return buffer_str;
 }
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Internal #GWL_Cursor Type
+ * \{ */
+
 /**
  * From XKB internals, use for converting a scan-code from WAYLAND to a #xkb_keycode_t.
  * Ideally this wouldn't need a local define.
@@ -289,16 +297,26 @@ struct GWL_Cursor {
    * the hardware cursor is used.
    */
   bool is_hardware = true;
+  /** When true, a custom image is used to display the cursor (stored in `wl_image`). */
   bool is_custom = false;
   struct wl_surface *wl_surface = nullptr;
   struct wl_buffer *wl_buffer = nullptr;
   struct wl_cursor_image wl_image = {0};
   struct wl_cursor_theme *wl_theme = nullptr;
   void *custom_data = nullptr;
+  /** The size of `custom_data` in bytes. */
   size_t custom_data_size = 0;
-  int size = 0;
+  /**
+   * The name of the theme (loaded by DBUS, depends on #WITH_GHOST_WAYLAND_DBUS).
+   * When disabled, leave as an empty string and the default theme will be used.
+   */
   std::string theme_name;
-
+  /**
+   * The size of the cursor (when looking up a cursor theme).
+   * This must be scaled by the maximum output scale when passing to wl_cursor_theme_load.
+   * See #update_cursor_scale.
+   * */
+  int theme_size = 0;
   int custom_scale = 1;
 };
 
@@ -309,6 +327,7 @@ struct GWL_Cursor {
  */
 struct GWL_TabletTool {
   struct GWL_Seat *seat = nullptr;
+  /** Tablets have a separate cursor to the 'pointer', this surface is used for cursor drawing. */
   struct wl_surface *wl_surface_cursor = nullptr;
   /** Used to delay clearing tablet focused wl_surface until the frame is handled. */
   bool proximity = false;
@@ -316,23 +335,51 @@ struct GWL_TabletTool {
   GHOST_TabletData data = GHOST_TABLET_DATA_NONE;
 };
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Internal #GWL_DataOffer Type
+ * \{ */
+
+/**
+ * Data storage used for clipboard paste & drag-and-drop.
+ */
 struct GWL_DataOffer {
-  std::unordered_set<std::string> types;
-  uint32_t source_actions = 0;
-  uint32_t dnd_action = 0;
   struct wl_data_offer *id = nullptr;
+  std::unordered_set<std::string> types;
   std::atomic<bool> in_use = false;
+
   struct {
+    /**
+     * Bit-mask with available drop options.
+     * #WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, #WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE.. etc.
+     * The application that initializes the drag may set these depending on modifiers held
+     * \note when dragging begins. Currently ghost doesn't make use of these.
+     */
+    enum wl_data_device_manager_dnd_action source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
+    enum wl_data_device_manager_dnd_action action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
     /** Compatible with #GWL_Seat.xy coordinates. */
     wl_fixed_t xy[2] = {0, 0};
   } dnd;
 };
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Internal #GWL_DataSource Type
+ * \{ */
+
 struct GWL_DataSource {
-  struct wl_data_source *wl_data_source = nullptr;
+  struct wl_data_source *wl_source = nullptr;
   GWL_SimpleBuffer buffer_out;
 };
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Internal #GWL_Seat Type (#wl_seat wrapper & associated types)
+ * \{ */
+
 /**
  * Data used to implement client-side key-repeat.
  *
@@ -605,6 +652,12 @@ struct GWL_Seat {
   uint32_t data_source_serial = 0;
 };
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Internal #GWL_Display Type (#wl_display & #wl_compositor wrapper)
+ * \{ */
+
 struct GWL_Display {
   GHOST_SystemWayland *system = nullptr;
 
@@ -633,8 +686,6 @@ struct GWL_Display {
   GWL_SimpleBuffer clipboard_primary;
 };
 
-#undef LOG
-
 /** \} */
 
 /* -------------------------------------------------------------------- */
@@ -710,8 +761,8 @@ static void display_destroy(GWL_Display *display)
       std::lock_guard lock{seat->data_source_mutex};
       if (seat->data_source) {
         gwl_simple_buffer_free_data(&seat->data_source->buffer_out);
-        if (seat->data_source->wl_data_source) {
-          wl_data_source_destroy(seat->data_source->wl_data_source);
+        if (seat->data_source->wl_source) {
+          wl_data_source_destroy(seat->data_source->wl_source);
         }
         delete seat->data_source;
       }
@@ -833,8 +884,11 @@ static void display_destroy(GWL_Display *display)
     wl_display_disconnect(display->wl_display);
   }
 
-  gwl_simple_buffer_free_data(&display->clipboard);
-  gwl_simple_buffer_free_data(&display->clipboard_primary);
+  {
+    std::lock_guard lock{system_clipboard_mutex};
+    gwl_simple_buffer_free_data(&display->clipboard);
+    gwl_simple_buffer_free_data(&display->clipboard_primary);
+  }
 
   delete display;
 }
@@ -1399,16 +1453,17 @@ static const char *read_file_as_buffer(const int fd, size_t *r_len)
   return buf;
 }
 
-static const char *read_pipe(GWL_DataOffer *data_offer,
-                             const std::string mime_receive,
-                             std::mutex *mutex,
-                             size_t *r_len)
+static const char *read_buffer_from_data_offer(GWL_DataOffer *data_offer,
+                                               const char *mime_receive,
+                                               std::mutex *mutex,
+                                               size_t *r_len)
 {
   int pipefd[2];
   if (UNLIKELY(pipe(pipefd) != 0)) {
-    return {};
+    CLOG_WARN(LOG, "error creating pipe: %s", std::strerror(errno));
+    return nullptr;
   }
-  wl_data_offer_receive(data_offer->id, mime_receive.c_str(), pipefd[1]);
+  wl_data_offer_receive(data_offer->id, mime_receive, pipefd[1]);
   close(pipefd[1]);
 
   data_offer->in_use.store(false);
@@ -1423,16 +1478,18 @@ static const char *read_pipe(GWL_DataOffer *data_offer,
   return buf;
 }
 
-static const char *read_pipe_primary(GWL_PrimarySelection_DataOffer *data_offer,
-                                     const std::string mime_receive,
-                                     std::mutex *mutex,
-                                     size_t *r_len)
+static const char *read_buffer_from_primary_selection_offer(
+    GWL_PrimarySelection_DataOffer *data_offer,
+    const char *mime_receive,
+    std::mutex *mutex,
+    size_t *r_len)
 {
   int pipefd[2];
   if (UNLIKELY(pipe(pipefd) != 0)) {
-    return {};
+    CLOG_WARN(LOG, "error creating pipe: %s", std::strerror(errno));
+    return nullptr;
   }
-  zwp_primary_selection_offer_v1_receive(data_offer->id, mime_receive.c_str(), pipefd[1]);
+  zwp_primary_selection_offer_v1_receive(data_offer->id, mime_receive, pipefd[1]);
   close(pipefd[1]);
 
   data_offer->in_use.store(false);
@@ -1481,8 +1538,8 @@ static void data_source_handle_cancelled(void *data, struct wl_data_source *wl_d
   CLOG_INFO(LOG, 2, "cancelled");
   GWL_Seat *seat = static_cast<GWL_Seat *>(data);
   GWL_DataSource *data_source = seat->data_source;
-  if (seat->data_source->wl_data_source == wl_data_source) {
-    data_source->wl_data_source = nullptr;
+  if (seat->data_source->wl_source == wl_data_source) {
+    data_source->wl_source = nullptr;
   }
 
   wl_data_source_destroy(wl_data_source);
@@ -1553,7 +1610,8 @@ static void data_offer_handle_offer(void *data,
                                     const char *mime_type)
 {
   CLOG_INFO(LOG, 2, "offer (mime_type=%s)", mime_type);
-  static_cast<GWL_DataOffer *>(data)->types.insert(mime_type);
+  GWL_DataOffer *data_offer = static_cast<GWL_DataOffer *>(data);
+  data_offer->types.insert(mime_type);
 }
 
 static void data_offer_handle_source_actions(void *data,
@@ -1561,7 +1619,8 @@ static void data_offer_handle_source_actions(void *data,
                                              const uint32_t source_actions)
 {
   CLOG_INFO(LOG, 2, "source_actions (%u)", source_actions);
-  static_cast<GWL_DataOffer *>(data)->source_actions = source_actions;
+  GWL_DataOffer *data_offer = static_cast<GWL_DataOffer *>(data);
+  data_offer->dnd.source_actions = (enum wl_data_device_manager_dnd_action)source_actions;
 }
 
 static void data_offer_handle_action(void *data,
@@ -1569,7 +1628,8 @@ static void data_offer_handle_action(void *data,
                                      const uint32_t dnd_action)
 {
   CLOG_INFO(LOG, 2, "actions (%u)", dnd_action);
-  static_cast<GWL_DataOffer *>(data)->dnd_action = dnd_action;
+  GWL_DataOffer *data_offer = static_cast<GWL_DataOffer *>(data);
+  data_offer->dnd.action = (enum wl_data_device_manager_dnd_action)dnd_action;
 }
 
 static const struct wl_data_offer_listener data_offer_listener = {
@@ -1693,7 +1753,8 @@ static void data_device_handle_drop(void *data, struct wl_data_device * /*wl_dat
     const wl_fixed_t xy[2] = {UNPACK2(data_offer->dnd.xy)};
 
     size_t data_buf_len = 0;
-    const char *data_buf = read_pipe(data_offer, mime_receive, nullptr, &data_buf_len);
+    const char *data_buf = read_buffer_from_data_offer(
+        data_offer, mime_receive.c_str(), nullptr, &d

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list