xref: /linux/tools/perf/ui/gtk/util.c (revision 25aee3debe0464f6c680173041fa3de30ec9ff54)
1 #include "../util.h"
2 #include "../../util/debug.h"
3 #include "gtk.h"
4 
5 #include <string.h>
6 
7 
8 struct perf_gtk_context *pgctx;
9 
10 struct perf_gtk_context *perf_gtk__activate_context(GtkWidget *window)
11 {
12 	struct perf_gtk_context *ctx;
13 
14 	ctx = malloc(sizeof(*pgctx));
15 	if (ctx)
16 		ctx->main_window = window;
17 
18 	return ctx;
19 }
20 
21 int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
22 {
23 	if (!perf_gtk__is_active_context(*ctx))
24 		return -1;
25 
26 	free(*ctx);
27 	*ctx = NULL;
28 	return 0;
29 }
30 
31 static int perf_gtk__error(const char *format, va_list args)
32 {
33 	char *msg;
34 	GtkWidget *dialog;
35 
36 	if (!perf_gtk__is_active_context(pgctx) ||
37 	    vasprintf(&msg, format, args) < 0) {
38 		fprintf(stderr, "Error:\n");
39 		vfprintf(stderr, format, args);
40 		fprintf(stderr, "\n");
41 		return -1;
42 	}
43 
44 	dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(pgctx->main_window),
45 					GTK_DIALOG_DESTROY_WITH_PARENT,
46 					GTK_MESSAGE_ERROR,
47 					GTK_BUTTONS_CLOSE,
48 					"<b>Error</b>\n\n%s", msg);
49 	gtk_dialog_run(GTK_DIALOG(dialog));
50 
51 	gtk_widget_destroy(dialog);
52 	free(msg);
53 	return 0;
54 }
55 
56 #ifdef HAVE_GTK_INFO_BAR
57 static int perf_gtk__warning_info_bar(const char *format, va_list args)
58 {
59 	char *msg;
60 
61 	if (!perf_gtk__is_active_context(pgctx) ||
62 	    vasprintf(&msg, format, args) < 0) {
63 		fprintf(stderr, "Warning:\n");
64 		vfprintf(stderr, format, args);
65 		fprintf(stderr, "\n");
66 		return -1;
67 	}
68 
69 	gtk_label_set_text(GTK_LABEL(pgctx->message_label), msg);
70 	gtk_info_bar_set_message_type(GTK_INFO_BAR(pgctx->info_bar),
71 				      GTK_MESSAGE_WARNING);
72 	gtk_widget_show(pgctx->info_bar);
73 
74 	free(msg);
75 	return 0;
76 }
77 #else
78 static int perf_gtk__warning_statusbar(const char *format, va_list args)
79 {
80 	char *msg, *p;
81 
82 	if (!perf_gtk__is_active_context(pgctx) ||
83 	    vasprintf(&msg, format, args) < 0) {
84 		fprintf(stderr, "Warning:\n");
85 		vfprintf(stderr, format, args);
86 		fprintf(stderr, "\n");
87 		return -1;
88 	}
89 
90 	gtk_statusbar_pop(GTK_STATUSBAR(pgctx->statbar),
91 			  pgctx->statbar_ctx_id);
92 
93 	/* Only first line can be displayed */
94 	p = strchr(msg, '\n');
95 	if (p)
96 		*p = '\0';
97 
98 	gtk_statusbar_push(GTK_STATUSBAR(pgctx->statbar),
99 			   pgctx->statbar_ctx_id, msg);
100 
101 	free(msg);
102 	return 0;
103 }
104 #endif
105 
106 struct perf_error_ops perf_gtk_eops = {
107 	.error		= perf_gtk__error,
108 #ifdef HAVE_GTK_INFO_BAR
109 	.warning	= perf_gtk__warning_info_bar,
110 #else
111 	.warning	= perf_gtk__warning_statusbar,
112 #endif
113 };
114 
115 /*
116  * FIXME: Functions below should be implemented properly.
117  *        For now, just add stubs for NO_NEWT=1 build.
118  */
119 #ifdef NO_NEWT_SUPPORT
120 int ui_helpline__show_help(const char *format __used, va_list ap __used)
121 {
122 	return 0;
123 }
124 
125 void ui_progress__update(u64 curr __used, u64 total __used,
126 			 const char *title __used)
127 {
128 }
129 #endif
130