summaryrefslogtreecommitdiff
path: root/tools/perf/ui/gtk/util.c
blob: 5823ff0d7c2a712ea1571fe3f0f6102885d7686b (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
#include "../util.h"
#include "gtk.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/compiler.h>
#include <linux/zalloc.h>

struct perf_gtk_context *pgctx;

struct perf_gtk_context *perf_gtk__activate_context(GtkWidget *window)
{
	struct perf_gtk_context *ctx;

	ctx = malloc(sizeof(*pgctx));
	if (ctx)
		ctx->main_window = window;

	return ctx;
}

int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
{
	if (!perf_gtk__is_active_context(*ctx))
		return -1;

	zfree(ctx);
	return 0;
}

/*
 * perf_gtk__error() can be called re-entrantly, since the dialog isn't
 * modal and its nested loop still pumps events for the main window.
 * Track every currently running loop instead of a single pointer, so a
 * nested call can't clobber an outer call's loop and leak it.
 */
static GSList *perf_gtk__error_loops;

static void perf_gtk__quit_loop(gpointer data, gpointer user_data __maybe_unused)
{
	g_main_loop_quit(data);
}

void perf_gtk__quit_error_dialog(void)
{
	g_slist_foreach(perf_gtk__error_loops, perf_gtk__quit_loop, NULL);
}

static void perf_gtk__dialog_response(GtkDialog *dialog,
				      gint response_id __maybe_unused,
				      gpointer data __maybe_unused)
{
	gtk_window_destroy(GTK_WINDOW(dialog));
}

static int perf_gtk__error(const char *format, va_list args)
{
	char *msg;
	GtkWidget *dialog;
	GMainLoop *loop;
	va_list args_copy;

	va_copy(args_copy, args);
	if (!perf_gtk__is_active_context(pgctx) ||
	    vasprintf(&msg, format, args_copy) < 0) {
		va_end(args_copy);
		fprintf(stderr, "Error:\n");
		vfprintf(stderr, format, args);
		fprintf(stderr, "\n");
		return -1;
	}
	va_end(args_copy);

	dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(pgctx->main_window),
					GTK_DIALOG_DESTROY_WITH_PARENT,
					GTK_MESSAGE_ERROR,
					GTK_BUTTONS_CLOSE,
					"<b>Error</b>\n\n%s", msg);

	/*
	 * "response" only fires when a button is clicked; DESTROY_WITH_PARENT
	 * destroys the dialog directly without it. Quit from "destroy"
	 * instead, which fires either way, so the nested loop below can't
	 * outlive the dialog and hang.
	 */
	loop = g_main_loop_new(NULL, FALSE);
	perf_gtk__error_loops = g_slist_prepend(perf_gtk__error_loops, loop);
	g_signal_connect(dialog, "response",
			 G_CALLBACK(perf_gtk__dialog_response), NULL);
	g_signal_connect_swapped(dialog, "destroy",
				 G_CALLBACK(g_main_loop_quit), loop);

	gtk_widget_set_visible(dialog, TRUE);
	g_main_loop_run(loop);
	perf_gtk__error_loops = g_slist_remove(perf_gtk__error_loops, loop);
	g_main_loop_unref(loop);

	free(msg);
	return 0;
}

static int perf_gtk__warning_info_bar(const char *format, va_list args)
{
	char *msg;
	va_list args_copy;

	va_copy(args_copy, args);
	if (!perf_gtk__is_active_context(pgctx) ||
	    vasprintf(&msg, format, args_copy) < 0) {
		va_end(args_copy);
		fprintf(stderr, "Warning:\n");
		vfprintf(stderr, format, args);
		fprintf(stderr, "\n");
		return -1;
	}
	va_end(args_copy);

	gtk_label_set_text(GTK_LABEL(pgctx->message_label), msg);
	gtk_info_bar_set_message_type(GTK_INFO_BAR(pgctx->info_bar),
				      GTK_MESSAGE_WARNING);
	gtk_widget_set_visible(pgctx->info_bar, TRUE);

	free(msg);
	return 0;
}

struct perf_error_ops perf_gtk_eops = {
	.error		= perf_gtk__error,
	.warning	= perf_gtk__warning_info_bar,
};