Skip to content

Commit

Permalink
fix(resources): compile resources and fix error management
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 28, 2019
1 parent 08787f3 commit 05d87c9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
1 change: 0 additions & 1 deletion include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ struct swappy_state_ui {
};

struct swappy_state {
GResource *resource;
GtkApplication *app;

struct swappy_state_ui *ui;
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ subdir('protocol')

executable(
'swappy',
swappy_resources,
files([
'src/main.c',
'src/notification.c',
Expand Down
8 changes: 2 additions & 6 deletions res/meson.build
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
pkgdatadir = join_paths(get_option('prefix'), get_option('datadir'), meson.project_name())

# Import the gnome module and use a GNOME function to ensure that application
# resources will be compiled.
gnome = import('gnome')

gnome.compile_resources('swappy',
swappy_resources = gnome.compile_resources('swappy',
'swappy.gresource.xml',
gresource_bundle: true,
install: true,
install_dir: pkgdatadir,
cname: 'swappy_resources'
)
8 changes: 4 additions & 4 deletions res/swappy.gresource.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/swappy">
<file>style/swappy.css</file>
<file>swappy.ui</file>
</gresource>
<gresource prefix="/me/jtheoof/swappy">
<file>style/swappy.css</file>
<file>swappy.ui</file>
</gresource>
</gresources>
42 changes: 21 additions & 21 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ void application_finish(struct swappy_state *state) {
g_free(state->storage_path);
g_free(state->geometry_str);
g_free(state->geometry);
g_resources_unregister(state->resource);
g_free(state->ui);
g_object_unref(state->app);
}
Expand Down Expand Up @@ -447,11 +446,13 @@ static void apply_css(GtkWidget *widget, GtkStyleProvider *provider) {
}
}

static void load_css(struct swappy_state *state) {
static bool load_css(struct swappy_state *state) {
GtkCssProvider *provider = gtk_css_provider_new();
gtk_css_provider_load_from_resource(provider, "/swappy/style/swappy.css");
gtk_css_provider_load_from_resource(provider,
"/me/jtheoof/swappy/style/swappy.css");
apply_css(GTK_WIDGET(state->ui->window), GTK_STYLE_PROVIDER(provider));
g_object_unref(provider);
return true;
}

static bool load_layout(struct swappy_state *state) {
Expand All @@ -460,8 +461,8 @@ static bool load_layout(struct swappy_state *state) {

/* Construct a GtkBuilder instance and load our UI description */
GtkBuilder *builder = gtk_builder_new();
if (gtk_builder_add_from_resource(builder, "/swappy/swappy.ui", &error) ==
0) {
if (gtk_builder_add_from_resource(builder, "/me/jtheoof/swappy/swappy.ui",
&error) == 0) {
g_printerr("Error loading file: %s", error->message);
g_clear_error(&error);
return false;
Expand Down Expand Up @@ -521,14 +522,21 @@ static bool load_layout(struct swappy_state *state) {
return true;
}

static void init_gtk_window(struct swappy_state *state) {
static bool init_gtk_window(struct swappy_state *state) {
g_info("activating application ----------");

load_layout(state);
load_css(state);
if (!load_layout(state)) {
return false;
}

if (!load_css(state)) {
return false;
}

update_ui_stroke_size_widget(state);
update_ui_undo_redo(state);

return true;
}

static gboolean is_geometry_valid(struct swappy_state *state) {
Expand All @@ -539,7 +547,7 @@ static gint command_line_handler(GtkApplication *app,
GApplicationCommandLine *cmdline,
struct swappy_state *state) {
if (!is_geometry_valid(state)) {
g_warning("geometry parameter is missing");
g_printerr("geometry parameter is missing\n");
return EXIT_FAILURE;
}

Expand All @@ -550,17 +558,18 @@ static gint command_line_handler(GtkApplication *app,
}

if (!screencopy_init(state)) {
g_warning("unable to initialize zwlr_screencopy_v1");
g_printerr("unable to initialize zwlr_screencopy_v1\n");
return false;
}

init_gtk_window(state);
if (!init_gtk_window(state)) {
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

bool application_init(struct swappy_state *state) {
GError *error = NULL;
const GOptionEntry cli_options[] = {
{
.long_name = "geometry",
Expand All @@ -582,17 +591,8 @@ bool application_init(struct swappy_state *state) {

g_application_add_main_option_entries(G_APPLICATION(state->app), cli_options);

state->resource = g_resource_load("build/meson-out/swappy.gresource", &error);

if (error != NULL) {
g_critical("unable to load swappy resource file: %s", error->message);
g_error_free(error);
}

state->ui = g_new(struct swappy_state_ui, 1);

g_resources_register(state->resource);

g_signal_connect(state->app, "command-line", G_CALLBACK(command_line_handler),
state);

Expand Down
9 changes: 7 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

int main(int argc, char *argv[]) {
struct swappy_state state = {0};
int status;

state.argc = argc;
state.argv = argv;
Expand All @@ -36,10 +37,14 @@ int main(int argc, char *argv[]) {
exit(1);
}

application_run(&state);
status = application_run(&state);

gtk_main();
if (status == 0) {
gtk_main();
}

application_finish(&state);
wayland_finish(&state);

return status;
}

0 comments on commit 05d87c9

Please sign in to comment.