util.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <string.h>
  7. struct perf_gtk_context *pgctx;
  8. struct perf_gtk_context *perf_gtk__activate_context(GtkWidget *window)
  9. {
  10. struct perf_gtk_context *ctx;
  11. ctx = malloc(sizeof(*pgctx));
  12. if (ctx)
  13. ctx->main_window = window;
  14. return ctx;
  15. }
  16. int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
  17. {
  18. if (!perf_gtk__is_active_context(*ctx))
  19. return -1;
  20. zfree(ctx);
  21. return 0;
  22. }
  23. static int perf_gtk__error(const char *format, va_list args)
  24. {
  25. char *msg;
  26. GtkWidget *dialog;
  27. if (!perf_gtk__is_active_context(pgctx) ||
  28. vasprintf(&msg, format, args) < 0) {
  29. fprintf(stderr, "Error:\n");
  30. vfprintf(stderr, format, args);
  31. fprintf(stderr, "\n");
  32. return -1;
  33. }
  34. dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(pgctx->main_window),
  35. GTK_DIALOG_DESTROY_WITH_PARENT,
  36. GTK_MESSAGE_ERROR,
  37. GTK_BUTTONS_CLOSE,
  38. "<b>Error</b>\n\n%s", msg);
  39. gtk_dialog_run(GTK_DIALOG(dialog));
  40. gtk_widget_destroy(dialog);
  41. free(msg);
  42. return 0;
  43. }
  44. #ifdef HAVE_GTK_INFO_BAR_SUPPORT
  45. static int perf_gtk__warning_info_bar(const char *format, va_list args)
  46. {
  47. char *msg;
  48. if (!perf_gtk__is_active_context(pgctx) ||
  49. vasprintf(&msg, format, args) < 0) {
  50. fprintf(stderr, "Warning:\n");
  51. vfprintf(stderr, format, args);
  52. fprintf(stderr, "\n");
  53. return -1;
  54. }
  55. gtk_label_set_text(GTK_LABEL(pgctx->message_label), msg);
  56. gtk_info_bar_set_message_type(GTK_INFO_BAR(pgctx->info_bar),
  57. GTK_MESSAGE_WARNING);
  58. gtk_widget_show(pgctx->info_bar);
  59. free(msg);
  60. return 0;
  61. }
  62. #else
  63. static int perf_gtk__warning_statusbar(const char *format, va_list args)
  64. {
  65. char *msg, *p;
  66. if (!perf_gtk__is_active_context(pgctx) ||
  67. vasprintf(&msg, format, args) < 0) {
  68. fprintf(stderr, "Warning:\n");
  69. vfprintf(stderr, format, args);
  70. fprintf(stderr, "\n");
  71. return -1;
  72. }
  73. gtk_statusbar_pop(GTK_STATUSBAR(pgctx->statbar),
  74. pgctx->statbar_ctx_id);
  75. /* Only first line can be displayed */
  76. p = strchr(msg, '\n');
  77. if (p)
  78. *p = '\0';
  79. gtk_statusbar_push(GTK_STATUSBAR(pgctx->statbar),
  80. pgctx->statbar_ctx_id, msg);
  81. free(msg);
  82. return 0;
  83. }
  84. #endif
  85. struct perf_error_ops perf_gtk_eops = {
  86. .error = perf_gtk__error,
  87. #ifdef HAVE_GTK_INFO_BAR_SUPPORT
  88. .warning = perf_gtk__warning_info_bar,
  89. #else
  90. .warning = perf_gtk__warning_statusbar,
  91. #endif
  92. };