#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <gtk/gtk.h>
GtkWidget* window;
GtkWidget* label;
static char strbuf[255];
char* appname;
static gboolean show_time(gpointer user_data) {
time_t rawtime;
struct tm* time_info;
time(&rawtime);
time_info = localtime(&rawtime);
sprintf(strbuf, "%s", asctime(time_info));
gtk_label_set_text(GTK_LABEL(label), strbuf);
}
static gboolean show_ten(gpointer user_data) {
static int count = 0;
sprintf(strbuf, "count: %d", count);
count = (count + 1) % 10;
gtk_label_set_text(GTK_LABEL(label), strbuf);
}
static gboolean show_sum(gpointer user_data) {
static int sum = 0;
static int counter = 0;
if (counter < 1000) {
++counter;
sum += counter;
sprintf(strbuf, "sum: %d", sum);
gtk_label_set_text(GTK_LABEL(label), strbuf);
}
}
static void activate (GtkApplication *app, gpointer user_data) {
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), appname);
gtk_window_set_default_size (GTK_WINDOW (window), 150, 150);
label = gtk_label_new("");
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show_all (window);
}
int run_app(char* _appname, GSourceFunc function, int argc, char** argv) {
appname = _appname;
GtkApplication *app;
int status;
app = gtk_application_new (appname, G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
g_timeout_add(1000, function, NULL);
status = g_application_run(G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
int
gcc `pkg-config --cflags gtk+-3.0` -o main
if (fork() == 0) {
return run_app("org.weathfold.Count", show_ten, argc, argv);
}
if (fork() == 0) {
return run_app("org.weathfold.Sum", show_sum, argc, argv);
}
if (fork() == 0) {
return run_app("org.weathfold.Time", show_time, argc, argv);
}
}