Linux Perf
util.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0
2 #include "../util.h"
3 #include "../../util/util.h"
4 #include "../../util/debug.h"
5 #include "gtk.h"
6 
7 #include <string.h>
8 
9 
11 
13 {
14  struct perf_gtk_context *ctx;
15 
16  ctx = malloc(sizeof(*pgctx));
17  if (ctx)
18  ctx->main_window = window;
19 
20  return ctx;
21 }
22 
24 {
25  if (!perf_gtk__is_active_context(*ctx))
26  return -1;
27 
28  zfree(ctx);
29  return 0;
30 }
31 
32 static int perf_gtk__error(const char *format, va_list args)
33 {
34  char *msg;
35  GtkWidget *dialog;
36 
37  if (!perf_gtk__is_active_context(pgctx) ||
38  vasprintf(&msg, format, args) < 0) {
39  fprintf(stderr, "Error:\n");
40  vfprintf(stderr, format, args);
41  fprintf(stderr, "\n");
42  return -1;
43  }
44 
45  dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(pgctx->main_window),
46  GTK_DIALOG_DESTROY_WITH_PARENT,
47  GTK_MESSAGE_ERROR,
48  GTK_BUTTONS_CLOSE,
49  "<b>Error</b>\n\n%s", msg);
50  gtk_dialog_run(GTK_DIALOG(dialog));
51 
52  gtk_widget_destroy(dialog);
53  free(msg);
54  return 0;
55 }
56 
57 #ifdef HAVE_GTK_INFO_BAR_SUPPORT
58 static int perf_gtk__warning_info_bar(const char *format, va_list args)
59 {
60  char *msg;
61 
62  if (!perf_gtk__is_active_context(pgctx) ||
63  vasprintf(&msg, format, args) < 0) {
64  fprintf(stderr, "Warning:\n");
65  vfprintf(stderr, format, args);
66  fprintf(stderr, "\n");
67  return -1;
68  }
69 
70  gtk_label_set_text(GTK_LABEL(pgctx->message_label), msg);
71  gtk_info_bar_set_message_type(GTK_INFO_BAR(pgctx->info_bar),
72  GTK_MESSAGE_WARNING);
73  gtk_widget_show(pgctx->info_bar);
74 
75  free(msg);
76  return 0;
77 }
78 #else
79 static int perf_gtk__warning_statusbar(const char *format, va_list args)
80 {
81  char *msg, *p;
82 
83  if (!perf_gtk__is_active_context(pgctx) ||
84  vasprintf(&msg, format, args) < 0) {
85  fprintf(stderr, "Warning:\n");
86  vfprintf(stderr, format, args);
87  fprintf(stderr, "\n");
88  return -1;
89  }
90 
91  gtk_statusbar_pop(GTK_STATUSBAR(pgctx->statbar),
92  pgctx->statbar_ctx_id);
93 
94  /* Only first line can be displayed */
95  p = strchr(msg, '\n');
96  if (p)
97  *p = '\0';
98 
99  gtk_statusbar_push(GTK_STATUSBAR(pgctx->statbar),
100  pgctx->statbar_ctx_id, msg);
101 
102  free(msg);
103  return 0;
104 }
105 #endif
106 
109 #ifdef HAVE_GTK_INFO_BAR_SUPPORT
110  .warning = perf_gtk__warning_info_bar,
111 #else
112  .warning = perf_gtk__warning_statusbar,
113 #endif
114 };
GtkWidget * main_window
Definition: gtk.h:13
void * malloc(YYSIZE_T)
struct perf_gtk_context * pgctx
Definition: util.c:10
GtkWidget * statbar
Definition: gtk.h:20
struct perf_gtk_context * perf_gtk__activate_context(GtkWidget *window)
Definition: util.c:12
static int perf_gtk__warning_statusbar(const char *format, va_list args)
Definition: util.c:79
#define zfree(ptr)
Definition: util.h:25
guint statbar_ctx_id
Definition: gtk.h:21
int(* error)(const char *format, va_list args)
Definition: util.h:15
struct perf_error_ops perf_gtk_eops
Definition: util.c:107
void free(void *)
static bool perf_gtk__is_active_context(struct perf_gtk_context *ctx)
Definition: gtk.h:29
static int perf_gtk__error(const char *format, va_list args)
Definition: util.c:32
static GtkWidget * dialog
Definition: progress.c:8
int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
Definition: util.c:23