xref: /openbmc/linux/tools/perf/ui/gtk/progress.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 
4 #include "gtk.h"
5 #include "../progress.h"
6 #include "util.h"
7 
8 static GtkWidget *dialog;
9 static GtkWidget *progress;
10 
11 static void gtk_ui_progress__update(struct ui_progress *p)
12 {
13 	double fraction = p->total ? 1.0 * p->curr / p->total : 0.0;
14 	char buf[1024];
15 
16 	if (dialog == NULL) {
17 		GtkWidget *vbox = gtk_vbox_new(TRUE, 5);
18 		GtkWidget *label = gtk_label_new(p->title);
19 
20 		dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
21 		progress = gtk_progress_bar_new();
22 
23 		gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 3);
24 		gtk_box_pack_start(GTK_BOX(vbox), progress, TRUE, TRUE, 3);
25 
26 		gtk_container_add(GTK_CONTAINER(dialog), vbox);
27 
28 		gtk_window_set_title(GTK_WINDOW(dialog), "perf");
29 		gtk_window_resize(GTK_WINDOW(dialog), 300, 80);
30 		gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
31 
32 		gtk_widget_show_all(dialog);
33 	}
34 
35 	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), fraction);
36 	snprintf(buf, sizeof(buf), "%"PRIu64" / %"PRIu64, p->curr, p->total);
37 	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), buf);
38 
39 	/* we didn't call gtk_main yet, so do it manually */
40 	while (gtk_events_pending())
41 		gtk_main_iteration();
42 }
43 
44 static void gtk_ui_progress__finish(void)
45 {
46 	/* this will also destroy all of its children */
47 	gtk_widget_destroy(dialog);
48 
49 	dialog = NULL;
50 }
51 
52 static struct ui_progress_ops gtk_ui_progress__ops = {
53 	.update		= gtk_ui_progress__update,
54 	.finish		= gtk_ui_progress__finish,
55 };
56 
57 void gtk_ui_progress__init(void)
58 {
59 	ui_progress__ops = &gtk_ui_progress__ops;
60 }
61