ultimatepp/uppsrc/CtrlCore/GtkApp.cpp
Zbigniew Rębacz 511ff1e991
Initial version of GTK Wayland backend (#205)
* Obtaining Gtk backend and runtime and displaying it in About box.

* .fixes

* .csometics

* Initial iteration for GTK on Wayland. Works suprisingly stable.

* Disable X11Utils when Wayland backend detected.

* Cosmetics

* .native

* First iteration of CSD.

* .working

* .working

* Calculating additional window spaced used by CSD.

* Fix max window size problem.

* Fix issue with rendering.

* .refactoring

* Fix splash screen when SSD is enable.

* Fix issue with mouse scrolling when CSD is enable.

* Not ideal fix for no keyboard input in parent window.

* Fix problem with XDisplay compilation and change name of GdkBackend to GtkBackend.

* Introduce new WAYLAND flag.

* Ctrl::GetWndScreenRect() fix for X11.

* Fix to compile on mac

---------

Co-authored-by: Zbigniew Rębacz <zbigniew.rebacz@hotmail.com>
2025-02-02 11:47:32 +01:00

123 lines
2.7 KiB
C++

#include <CtrlCore/CtrlCore.h>
#ifdef GUI_GTK
#include <X11/Xlib.h>
#define CATCH_ERRORS 1
namespace Upp {
#define LLOG(x) // DLOG(x)
#if CATCH_ERRORS
void CatchError(const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
RLOG((const char *)message);
// __BREAK__;
}
#endif
void Ctrl::PanicMsgBox(const char *title, const char *text)
{
LLOG("PanicMsgBox " << title << ": " << text);
UngrabMouse();
char m[2000];
*m = 0;
if(system("which gxmessage") == 0)
strcpy(m, "gxmessage -center \"");
else
if(system("which kdialog") == 0)
strcpy(m, "kdialog --error \"");
else
if(system("which xmessage") == 0)
strcpy(m, "xmessage -center \"");
if(*m) {
strcat(m, title);
strcat(m, "\n");
strcat(m, text);
strcat(m, "\"");
IGNORE_RESULT(system(m));
}
else {
GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE, "%s: %s", title, text);
gtk_dialog_run(GTK_DIALOG (dialog));
gtk_widget_destroy(dialog);
}
__BREAK__;
}
int Ctrl::scale;
bool running_on_wayland;
bool RunningOnWayland()
{
return running_on_wayland;
}
void Ctrl::ThemeChanged(void *)
{
PostReSkin();
}
bool InitGtkApp(int argc, char **argv, const char **envptr)
{
LLOG(rmsecs() << " InitGtkApp");
#if GTK_CHECK_VERSION(3, 10, 0)
String backends = "x11,wayland";
#ifdef GUI_GTK_WAYLAND
backends = "wayland,x11";
#endif
gdk_set_allowed_backends(backends);
#endif
if (!gtk_init_check(&argc, &argv)) {
Cerr() << t_("Failed to initialized GTK app!") << "\n";
return false;
}
if (GdkBackend::IsX11()) {
XInitThreads(); // otherwise there are errors despide GuiLock
}
EnterGuiMutex();
Ctrl::SetUHDEnabled(true);
Ctrl::scale = 1;
#if GTK_CHECK_VERSION(3, 10, 0)
Ctrl::scale = gdk_window_get_scale_factor(gdk_screen_get_root_window(gdk_screen_get_default()));
#endif
Ctrl::GlobalBackBuffer();
Ctrl::ReSkin();
g_timeout_add(20, (GSourceFunc) Ctrl::TimeHandler, NULL);
InstallPanicMessageBox(Ctrl::PanicMsgBox);
if (GdkBackend::IsX11())
gdk_window_add_filter(NULL, Ctrl::RootKeyFilter, NULL);
#if CATCH_ERRORS
g_log_set_default_handler (CatchError, 0);
#endif
GtkSettings *settings = gtk_settings_get_default();
if(settings) {
g_signal_connect_swapped(settings, "notify::gtk-theme-name", G_CALLBACK(Ctrl::ThemeChanged), NULL);
g_signal_connect_swapped(settings, "notify::gtk-application-prefer-dark-theme", G_CALLBACK(Ctrl::ThemeChanged), NULL);
}
return true;
}
void ExitGtkApp()
{
LeaveGuiMutex();
}
}
#endif