summaryrefslogtreecommitdiff
path: root/src/drivers/Wayland/fl_wayland_platform_init.cxx
blob: 4c4477740fc9f14eb2060aae84744f4cd91d5241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Wayland-specific code to initialize wayland support.
//
// Copyright 2022-2023 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

#include <FL/fl_config.h>
#include "Fl_Wayland_Copy_Surface_Driver.H"
#include "Fl_Wayland_Graphics_Driver.H"
#include "Fl_Wayland_Screen_Driver.H"
#include "../Unix/Fl_Unix_System_Driver.H"
#include "Fl_Wayland_Window_Driver.H"
#include "Fl_Wayland_Image_Surface_Driver.H"
#include "../Base/Fl_Base_Pen_Events.H"
#ifdef FLTK_USE_X11
#  include "../Xlib/Fl_Xlib_Copy_Surface_Driver.H"
#  include "../Cairo/Fl_X11_Cairo_Graphics_Driver.H"
#  include "../X11/Fl_X11_Screen_Driver.H"
#  include "../X11/Fl_X11_Window_Driver.H"
#  include "../Xlib/Fl_Xlib_Image_Surface_Driver.H"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>


#ifdef FLTK_USE_X11

static bool attempt_wayland() {
  if (Fl_Wayland_Screen_Driver::wl_display) return true;
  static bool first = true;
  static bool disable_wl = false;
  if (first) { // get the value if it exists and cache it
    void *sym = Fl_Posix_System_Driver::dlopen_or_dlsym(NULL, "fl_disable_wayland");
    if (sym) {
      disable_wl = *(bool *)sym;
      // printf("fl_disable_wayland = %s\n", disable_wl ? "true" : "false");
    }
    first = false;
  }
  if (disable_wl)
    return false;
  const char *backend = ::getenv("FLTK_BACKEND");
  // fprintf(stderr, "FLTK_BACKEND='%s'\n", backend ? backend : "");
  if (backend && strcmp(backend, "x11") == 0) {
    return false;
  }

  if (backend && strcmp(backend, "wayland") == 0) {
    Fl_Wayland_Screen_Driver::wl_display = wl_display_connect(NULL);
    if (!Fl_Wayland_Screen_Driver::wl_display) {
      fprintf(stderr, "Error: no Wayland connection available, FLTK_BACKEND='wayland'\n");
      exit(1);
    }
    return true;
  }

  if (!backend) {
    // env var XDG_RUNTIME_DIR is required for Wayland
    const char *xdgrt = ::getenv("XDG_RUNTIME_DIR");
    if (xdgrt) {
      // is a Wayland connection available ?
      Fl_Wayland_Screen_Driver::wl_display = wl_display_connect(NULL);
      if (Fl_Wayland_Screen_Driver::wl_display) { // Yes, use Wayland drivers
        // puts("using wayland");
        return true;
      }
    }
    // no Wayland connection or environment variable XDG_RUNTIME_DIR not set,
    // falling back to X11
    return false;
  }

  fprintf(stderr, "Error: unexpected value of FLTK_BACKEND: '%s'\n", backend);
  exit(1);
  return false;
}

#endif // FLTK_USE_X11


Fl_System_Driver *Fl_System_Driver::newSystemDriver() {
  return new Fl_Unix_System_Driver();
}


Fl_Graphics_Driver *Fl_Graphics_Driver::newMainGraphicsDriver() {
#ifdef FLTK_USE_X11
  if (!attempt_wayland()) return new Fl_X11_Cairo_Graphics_Driver();
#endif
  return new Fl_Wayland_Graphics_Driver();
}


Fl_Copy_Surface_Driver *Fl_Copy_Surface_Driver::newCopySurfaceDriver(int w, int h) {
#ifdef FLTK_USE_X11
  if (!Fl_Wayland_Screen_Driver::wl_display) return new Fl_Xlib_Copy_Surface_Driver(w, h);
#endif
  return new Fl_Wayland_Copy_Surface_Driver(w, h);
}


Fl_Screen_Driver *Fl_Screen_Driver::newScreenDriver() {
  if (!Fl_Screen_Driver::system_driver) Fl::system_driver();
#ifdef FLTK_USE_X11
  if (attempt_wayland()) {
    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;
  d->current_xft_dpi = 0.; // means the value of the Xft.dpi resource is still unknown
  return d;
#else
  return new Fl_Wayland_Screen_Driver();
#endif
}


Fl_Window_Driver *Fl_Window_Driver::newWindowDriver(Fl_Window *w)
{
#ifdef FLTK_USE_X11
  if (!attempt_wayland()) return new Fl_X11_Window_Driver(w);
#endif
  return new Fl_Wayland_Window_Driver(w);
}


Fl_Image_Surface_Driver *Fl_Image_Surface_Driver::newImageSurfaceDriver(int w, int h, int high_res, Fl_Offscreen off)
{
#ifdef FLTK_USE_X11
  if (!attempt_wayland())
    return new Fl_Xlib_Image_Surface_Driver(w, h, high_res, off);
#endif
  return new Fl_Wayland_Image_Surface_Driver(w, h, high_res, off);
}

#if defined(FLTK_HAVE_PEN_SUPPORT)

namespace Fl {
namespace Pen {
Driver default_driver;
Driver& driver = default_driver;
} // namespace Pen
} // namespace Fl

#endif // FLTK_HAVE_PEN_SUPPORT