1 // SPDX-License-Identifier: GPL-2.0 2 #include "../util.h" 3 #include "gtk.h" 4 5 #include <stdlib.h> 6 #include <string.h> 7 #include <linux/zalloc.h> 8 9 struct perf_gtk_context *pgctx; 10 11 struct perf_gtk_context *perf_gtk__activate_context(GtkWidget *window) 12 { 13 struct perf_gtk_context *ctx; 14 15 ctx = malloc(sizeof(*pgctx)); 16 if (ctx) 17 ctx->main_window = window; 18 19 return ctx; 20 } 21 22 int perf_gtk__deactivate_context(struct perf_gtk_context **ctx) 23 { 24 if (!perf_gtk__is_active_context(*ctx)) 25 return -1; 26 27 zfree(ctx); 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_SUPPORT 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_SUPPORT 109 .warning = perf_gtk__warning_info_bar, 110 #else 111 .warning = perf_gtk__warning_statusbar, 112 #endif 113 }; 114