looper/backends/ui/gtk/main.cpp

99 lines
No EOL
3.6 KiB
C++

#include "main.h"
#include "main_window.hpp"
#include <assets/assets.h>
#include <license.hpp>
#include <log.hpp>
using namespace Looper::Log;
std::string GtkBackend::get_id() {
return "gtk";
}
std::string GtkBackend::get_name() {
return "GTK frontend";
}
void g_log_handler(const gchar *domain, GLogLevelFlags flags, const gchar *message, gpointer userdata) {
bool fatal = flags & GLogLevelFlags::G_LOG_FLAG_FATAL;
bool recursion = flags & GLogLevelFlags::G_LOG_FLAG_RECURSION;
GLogLevelFlags level = (GLogLevelFlags)(flags & GLogLevelFlags::G_LOG_LEVEL_MASK);
/*
G_LOG_LEVEL_ERROR = 1 << 2,
G_LOG_LEVEL_CRITICAL = 1 << 3,
G_LOG_LEVEL_WARNING = 1 << 4,
G_LOG_LEVEL_MESSAGE = 1 << 5,
G_LOG_LEVEL_INFO = 1 << 6,
G_LOG_LEVEL_DEBUG = 1 << 7,
*/
LogStream *stream;
switch (level) {
case GLogLevelFlags::G_LOG_LEVEL_ERROR: {
stream = new LogStream({domain}, {&ERROR});
} break;
case GLogLevelFlags::G_LOG_LEVEL_CRITICAL: {
stream = new LogStream({"CRITICAL", domain}, {stderr});
} break;
case GLogLevelFlags::G_LOG_LEVEL_WARNING: {
stream = new LogStream({domain}, {&WARNING});
} break;
case GLogLevelFlags::G_LOG_LEVEL_INFO: {
stream = new LogStream({domain}, {&INFO});
} break;
case GLogLevelFlags::G_LOG_LEVEL_DEBUG: {
stream = new LogStream({domain}, {&DEBUG});
} break;
default: {
stream = new LogStream({domain}, {stderr});
} break;
}
stream->writeln(message);
delete stream;
}
int GtkBackend::run(std::vector<std::string> realArgs, int argc, char **argv) {
int ret = UIBackend::run(realArgs, argc, argv);
if (ret != 0) {
return ret;
}
g_log_set_default_handler(g_log_handler, nullptr);
//g_log_writer
Glib::set_application_name("Looper");
auto app = Gtk::Application::create("com.complecwaft.Looper.GTK", Gio::Application::Flags::NON_UNIQUE);
char *gtk_frontend_css = (char*)malloc(gtk_frontend_css_size + 1);
memcpy(gtk_frontend_css, gtk_frontend_css_data, gtk_frontend_css_size);
gtk_frontend_css[gtk_frontend_css_size] = '\0';
std::string gtk_frontend_css_str = std::string(gtk_frontend_css);
free(gtk_frontend_css);
auto provider = Gtk::CssProvider::create();
provider->load_from_string(gtk_frontend_css_str);
app->signal_startup().connect([=,this]() {
app->add_action("quit", [app]() {
app->quit();
});
Gtk::StyleContext::add_provider_for_display(Gdk::Display::get_default(), provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
});
app->signal_activate().connect([=,this] {
auto *win = new MainWindow(playback, app);
win->present();
});
app_ptr = app.get();
ret = app->run(argc, argv);
app_ptr = nullptr;
return ret;
}
void GtkBackend::QuitHandler() {
if (app_ptr != nullptr) {
((Gtk::Application*)app_ptr)->quit();
}
}
void GtkBackend::add_licenses() {
auto &license_data = get_license_data();
auto gtkmm = LicenseData("GtkMM", "lgpl-2.1");
auto glibmm = LicenseData("GlibMM", "lgpl-2.1");
auto glib = LicenseData("Glib", "lgpl-2.1-or-later");
auto gtk = LicenseData("GTK", "lgpl-2.0");
LOAD_LICENSE(gtkmm, lgpl_2_1);
LOAD_LICENSE(glibmm, lgpl_2_1);
LOAD_LICENSE(glib, lgpl_2_1);
LOAD_LICENSE(gtk, lgpl_2_0);
license_data.insert(gtkmm);
license_data.insert(glibmm);
license_data.insert(glib);
license_data.insert(gtk);
}