-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
123 lines (89 loc) · 3.21 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// main.c
// studyngc
//
// Created by Wesley Jesus on 07/09/24.
//
#include <gtk/gtk.h>
#include "task_widget.c"
//#include "memory_widget.c"
long get_memory_usage_kb() {
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
return usage.ru_maxrss; // Retorna o uso de memória máxima em KB
}
// Função de callback para atualizar o GtkLabel com o uso de memória
static gboolean update_memory_usage(GtkLabel *label) {
long memory_usage_kb = get_memory_usage_kb();
printf("Uso de memória: %ld KB\n", memory_usage_kb);
double memory_usage_mb = memory_usage_kb / 1024.0;
printf("Uso de memória: %.2f MB\n", memory_usage_mb);
// Obtém o uso de memória
long memory_usage = get_memory_usage_kb();
double memory_in_mb = memory_usage / 1024.0;
// Converte o uso de memória para string
char memory_text[64];
snprintf(memory_text, sizeof(memory_text), "Uso de memória: %.0f MB", memory_in_mb);
// Atualiza o texto do GtkLabel
gtk_label_set_text(label, memory_text);
// Retorna TRUE para continuar chamando periodicamente
return TRUE;
}
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
int
create_button(GtkWidget *box)
{
GtkWidget *button;
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
gtk_box_append(GTK_BOX (box), button);
}
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *button;
GtkWidget *label;
label = gtk_label_new("Obtendo uso de memória...");
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
// ####### box init #######
gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_halign (box, GTK_ALIGN_CENTER);
gtk_widget_set_valign (box, GTK_ALIGN_CENTER);
gtk_window_set_child (GTK_WINDOW (window), box);
// ####### box end #######
// Atualiza o label a cada segundo com o uso de memória
g_timeout_add_seconds(1, (GSourceFunc)update_memory_usage, GTK_LABEL(label));
gtk_box_append(GTK_BOX (box), label);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
//g_signal_connect_swapped(button, "clicked", G_CALLBACK (gtk_window_destroy), window);
g_signal_connect_swapped(button, "clicked", G_CALLBACK (create_button), box);
gtk_box_append(GTK_BOX (box), button);
create_task(box);
// exibe a tela
gtk_window_present (GTK_WINDOW (window));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
//link a tela com o gtk_application
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
// livrar os objetos da memoria
g_object_unref (app);
return status;
}