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 10 struct perf_gtk_context *pgctx; 11 12 struct perf_gtk_context *perf_gtk__activate_context(GtkWidget *window) 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 23 int perf_gtk__deactivate_context(struct perf_gtk_context **ctx) 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 107 struct perf_error_ops perf_gtk_eops = { 108 .error = perf_gtk__error, 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 }; 115