FLTK logo

[master] 07fd262 - Hybrid Wayland/X11 platform: improve control of chosen backend.

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] 07fd262 - Hybrid Wayland/X11 platform: improve control of chosen backend. "ManoloFLTK" Aug 30, 2022  
 
commit 07fd2628febb512192239cc252087deb62da4866
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Tue Aug 30 17:37:55 2022 +0200
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Tue Aug 30 17:37:55 2022 +0200

    Hybrid Wayland/X11 platform: improve control of chosen backend.

 FL/Fl.H                                          |  2 ++
 FL/platform.H                                    |  1 -
 src/Fl.cxx                                       |  4 ++--
 src/drivers/Wayland/Fl_Wayland_Screen_Driver.H   |  3 +--
 src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx | 23 +++++++++++++++++++++--
 src/drivers/Wayland/Fl_Wayland_System_Driver.H   |  1 +
 src/drivers/Wayland/Fl_Wayland_System_Driver.cxx | 20 +++++++++-----------
 src/drivers/Wayland/fl_wayland_platform_init.cxx | 20 +++++++++++++++-----
 8 files changed, 51 insertions(+), 23 deletions(-)

diff --git FL/Fl.H FL/Fl.H
index 4cee6c7..b0f8e60 100644
--- FL/Fl.H
+++ FL/Fl.H
@@ -61,6 +61,8 @@ extern FL_EXPORT const char* fl_local_ctrl;  ///< string pointer used in shortcu
 extern FL_EXPORT const char* fl_local_meta;  ///< string pointer used in shortcuts, you can change it to another language
 extern FL_EXPORT const char* fl_local_shift; ///< string pointer used in shortcuts, you can change it to another language
 
+extern FL_EXPORT void fl_disable_wayland();
+
 /** \defgroup  callback_functions  Callback Function Typedefs
 
   \brief Typedefs defined in <FL/Fl.H> for callback or handler functions passed as function parameters.
diff --git FL/platform.H FL/platform.H
index 81d5a73..834a323 100644
--- FL/platform.H
+++ FL/platform.H
@@ -76,6 +76,5 @@ extern FL_EXPORT void fl_close_display();
 extern FL_EXPORT Window fl_window;
 extern FL_EXPORT int fl_parse_color(const char* p, uchar& r, uchar& g, uchar& b);
 extern FL_EXPORT void fl_open_callback(void (*)(const char *));
-extern FL_EXPORT void fl_disable_wayland();
 
 #endif // !FL_PLATFORM_H
diff --git src/Fl.cxx src/Fl.cxx
index 3ba27a7..b7fe351 100644
--- src/Fl.cxx
+++ src/Fl.cxx
@@ -2016,8 +2016,8 @@ void fl_close_display()
 }
 
 /** Prevent the FLTK library from using its wayland backend.
- Call this early in your main(), before fl_open_display() runs.
- This has no effect on non-Wayland platforms.
+ Call this early in your main(), before fl_open_display() runs, or any window is created, or the screen is accessed.
+ This function has no effect on non-Wayland platforms.
  */
 void fl_disable_wayland()
 {
diff --git src/drivers/Wayland/Fl_Wayland_Screen_Driver.H src/drivers/Wayland/Fl_Wayland_Screen_Driver.H
index 64aae85..d5196b5 100644
--- src/drivers/Wayland/Fl_Wayland_Screen_Driver.H
+++ src/drivers/Wayland/Fl_Wayland_Screen_Driver.H
@@ -66,8 +66,6 @@ public:
   static FL_EXPORT struct wl_display *wl_display;
   // use it to make sure the Wayland leg was selected and fl_open_display() has run
   static struct wl_registry *wl_registry;
-  // true when an app is forbidden to use its Wayland leg
-  static bool wld_disabled;
   static void insertion_point_location(int x, int y, int height);
   static bool insertion_point_location(int *px, int *py, int *pwidth, int *pheight);
   int get_mouse_unscaled(int &xx, int &yy);
@@ -176,6 +174,7 @@ public:
   static compositor_name compositor; // identifies the used Wayland compositor
   void set_spot(int font, int height, int x, int y, int w, int h, Fl_Window *win);
   void reset_spot();
+  static bool undo_wayland_backend_if_needed();
 };
 
 
diff --git src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx
index cdfc88e..cc6d1b4 100644
--- src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx
+++ src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx
@@ -230,8 +230,6 @@ struct wl_display *Fl_Wayland_Screen_Driver::wl_display = NULL;
 struct wl_registry *Fl_Wayland_Screen_Driver::wl_registry = NULL;
 
 
-bool Fl_Wayland_Screen_Driver::wld_disabled = false;
-
 Fl_Window *Fl_Wayland_Screen_Driver::surface_to_window(struct wl_surface *surface) {
   if (surface) {
     Fl_X *xp = Fl_X::first;
@@ -1089,12 +1087,31 @@ Fl_Wayland_Screen_Driver::Fl_Wayland_Screen_Driver() : Fl_Screen_Driver() {
   reset_cursor();
 }
 
+
+bool Fl_Wayland_Screen_Driver::undo_wayland_backend_if_needed() {
+  const char *backend = getenv("FLTK_BACKEND");
+  if (wl_display && backend && strcmp(backend, "x11") == 0) {
+    wl_display_disconnect(wl_display);
+    wl_display = NULL;
+    delete Fl_Screen_Driver::system_driver;
+    Fl_Screen_Driver::system_driver = NULL;
+    return true;
+  }
+  return false;
+}
+
+
 void Fl_Wayland_Screen_Driver::open_display_platform() {
   static bool beenHereDoneThat = false;
   if (beenHereDoneThat)
     return;
 
   beenHereDoneThat = true;
+  if (undo_wayland_backend_if_needed()) {
+    Fl::screen_driver()->open_display();
+    return;
+  }
+  
   if (!wl_display) {
     wl_display = wl_display_connect(NULL);
     if (!wl_display) {
@@ -1116,6 +1133,8 @@ void Fl_Wayland_Screen_Driver::open_display_platform() {
   }*/
   Fl::add_fd(wl_display_get_fd(wl_display), FL_READ, (Fl_FD_Handler)fd_callback, wl_display);
   fl_create_print_window();
+  Fl_Wayland_System_Driver::too_late_to_disable = true;
+puts("Using Wayland backend");
 }
 
 void Fl_Wayland_Screen_Driver::close_display() {
diff --git src/drivers/Wayland/Fl_Wayland_System_Driver.H src/drivers/Wayland/Fl_Wayland_System_Driver.H
index 75043cd..d363aa1 100644
--- src/drivers/Wayland/Fl_Wayland_System_Driver.H
+++ src/drivers/Wayland/Fl_Wayland_System_Driver.H
@@ -27,6 +27,7 @@ public:
   int get_key(int k);
   virtual void *control_maximize_button(void *data);
   virtual void disable_wayland();
+  static bool too_late_to_disable;
 };
 
 #endif /* FL_WAYLAND_SYSTEM_DRIVER_H */
diff --git src/drivers/Wayland/Fl_Wayland_System_Driver.cxx src/drivers/Wayland/Fl_Wayland_System_Driver.cxx
index 7c3cf9a..158c561 100644
--- src/drivers/Wayland/Fl_Wayland_System_Driver.cxx
+++ src/drivers/Wayland/Fl_Wayland_System_Driver.cxx
@@ -21,6 +21,10 @@
 #include "Fl_Wayland_Screen_Driver.H"
 #include <FL/platform.H>
 #include "../../../libdecor/src/libdecor.h"
+#include <stdlib.h>
+
+
+bool Fl_Wayland_System_Driver::too_late_to_disable = false;
 
 
 int Fl_Wayland_System_Driver::event_key(int k) {
@@ -91,18 +95,12 @@ void *Fl_Wayland_System_Driver::control_maximize_button(void *data) {
 
 
 void Fl_Wayland_System_Driver::disable_wayland() {
-  if (fl_wl_display()) {
+  if (too_late_to_disable) {
     fprintf(stderr, "Error: fl_disable_wayland() cannot be called "
-            "after the Wayland display was opened\n");
+            "after the Wayland display was opened\n"
+            "or a Wayland window was created or the Wayland screen was accessed\n");
     exit(1);
   }
-
-  if (Fl_Wayland_Screen_Driver::wl_display) {
-    wl_display_disconnect(Fl_Wayland_Screen_Driver::wl_display);
-    Fl_Wayland_Screen_Driver::wl_display = NULL;
-    delete Fl_Screen_Driver::system_driver;
-    Fl_Screen_Driver::system_driver = NULL;
-  }
-  Fl_Wayland_Screen_Driver::wld_disabled = true;
-  Fl::system_driver();
+  setenv("FLTK_BACKEND", "x11", 1);
+  Fl_Wayland_Screen_Driver::undo_wayland_backend_if_needed();
 }
diff --git src/drivers/Wayland/fl_wayland_platform_init.cxx src/drivers/Wayland/fl_wayland_platform_init.cxx
index ffdc13a..c153668 100644
--- src/drivers/Wayland/fl_wayland_platform_init.cxx
+++ src/drivers/Wayland/fl_wayland_platform_init.cxx
@@ -41,8 +41,7 @@ Fl_System_Driver *Fl_System_Driver::newSystemDriver() {
   // fprintf(stderr, "FLTK_BACKEND='%s' XDG_RUNTIME_DIR='%s'\n",
   //         backend ? backend : "", xdgrt ? xdgrt : "");
 
-  if (Fl_Wayland_Screen_Driver::wld_disabled ||
-      (backend && strcmp(backend, "x11") == 0)) {
+  if (backend && strcmp(backend, "x11") == 0) {
     return new Fl_X11_System_Driver();
   }
 
@@ -100,12 +99,11 @@ FL_EXPORT Fl_Fontdesc *fl_fonts = built_in_table;
 
 
 Fl_Graphics_Driver *Fl_Graphics_Driver::newMainGraphicsDriver() {
+  Fl_Wayland_Screen_Driver::undo_wayland_backend_if_needed();
   if (Fl_Wayland_Screen_Driver::wl_display) {
     fl_graphics_driver = new Fl_Wayland_Graphics_Driver();
-puts("using Fl_Wayland_Graphics_Driver");
   } else {
     fl_graphics_driver = new Fl_Display_Cairo_Graphics_Driver();
-puts("using Fl_Display_Cairo_Graphics_Driver");
   }
   return fl_graphics_driver;
 }
@@ -118,7 +116,12 @@ Fl_Copy_Surface_Driver *Fl_Copy_Surface_Driver::newCopySurfaceDriver(int w, int
 
 
 Fl_Screen_Driver *Fl_Screen_Driver::newScreenDriver() {
-  if (Fl_Wayland_Screen_Driver::wl_display) return new Fl_Wayland_Screen_Driver();
+  if (!Fl_Screen_Driver::system_driver) Fl::system_driver();
+  Fl_Wayland_Screen_Driver::undo_wayland_backend_if_needed();
+  if (Fl_Wayland_Screen_Driver::wl_display) {
+    Fl_Wayland_System_Driver::too_late_to_disable = true;
+    return new Fl_Wayland_Screen_Driver();
+  }
 
   Fl_X11_Screen_Driver *d = new Fl_X11_Screen_Driver();
   for (int i = 0;  i < MAX_SCREENS; i++) d->screens[i].scale = 1;
@@ -129,6 +132,13 @@ Fl_Screen_Driver *Fl_Screen_Driver::newScreenDriver() {
 
 Fl_Window_Driver *Fl_Window_Driver::newWindowDriver(Fl_Window *w)
 {
+  if (!Fl_Screen_Driver::system_driver) Fl::system_driver();
+  static bool been_here = false;
+  if (!been_here) {
+    been_here = true;
+    Fl_Wayland_System_Driver::too_late_to_disable = true;
+    Fl_Wayland_Screen_Driver::undo_wayland_backend_if_needed();
+  }
   if (Fl_Wayland_Screen_Driver::wl_display) return new Fl_Wayland_Window_Driver(w);
   return new Fl_X11_Window_Driver(w);
 }
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.