GtkMultiProcess

From WeAthFolD, 5 Years ago, written in C, viewed 377 times.
URL https://code.nat.moe/view/bd484964 Embed
Download Paste or View Raw
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5.  
  6. #include <gtk/gtk.h>
  7.  
  8. GtkWidget* window;
  9. GtkWidget* label;
  10. static char strbuf[255];
  11. char* appname;
  12.  
  13. static gboolean show_time(gpointer user_data) {
  14.     time_t rawtime;
  15.     struct tm* time_info;
  16.  
  17.     time(&rawtime);
  18.     time_info = localtime(&rawtime);
  19.    
  20.     sprintf(strbuf, "%s", asctime(time_info));
  21.     gtk_label_set_text(GTK_LABEL(label), strbuf);
  22. }
  23.  
  24. static gboolean show_ten(gpointer user_data) {
  25.     static int count = 0;
  26.  
  27.     sprintf(strbuf, "count: %d", count);
  28.  
  29.     count = (count + 1) % 10;
  30.    
  31.     gtk_label_set_text(GTK_LABEL(label), strbuf);
  32. }
  33.  
  34. static gboolean show_sum(gpointer user_data) {
  35.     static int sum = 0;
  36.     static int counter = 0;
  37.  
  38.     if (counter < 1000) {
  39.         ++counter;
  40.        
  41.         sum += counter;
  42.  
  43.         sprintf(strbuf, "sum: %d", sum);
  44.         gtk_label_set_text(GTK_LABEL(label), strbuf);
  45.     }
  46. }
  47.  
  48. static void activate (GtkApplication *app, gpointer        user_data) {
  49.   window = gtk_application_window_new (app);
  50.   gtk_window_set_title (GTK_WINDOW (window), appname);
  51.   gtk_window_set_default_size (GTK_WINDOW (window), 150, 150);
  52.  
  53.   label = gtk_label_new("");
  54.   gtk_container_add(GTK_CONTAINER(window), label);
  55.  
  56.   gtk_widget_show_all (window);
  57. }
  58.  
  59. int run_app(char* _appname, GSourceFunc function, int argc, char** argv) {
  60.     appname = _appname;
  61.  
  62.     GtkApplication *app;
  63.     int status;
  64.  
  65.     app = gtk_application_new (appname, G_APPLICATION_FLAGS_NONE);
  66.     g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
  67.     g_timeout_add(1000, function, NULL);
  68.     status = g_application_run(G_APPLICATION (app), argc, argv);
  69.     g_object_unref (app);
  70.  
  71.     return status;
  72. }
  73.  
  74. int main (int argc, char **argv) {
  75.     if (fork() == 0) {
  76.         return run_app("org.weathfold.Count", show_ten, argc, argv);
  77.     }
  78.  
  79.     if (fork() == 0) {
  80.         return run_app("org.weathfold.Sum", show_sum, argc, argv);
  81.     }
  82.  
  83.     if (fork() == 0) {
  84.         return run_app("org.weathfold.Time", show_time, argc, argv);
  85.     }
  86. }
  87.  

Replies to GtkMultiProcess rss

Title Name Language When
Re: GtkMultiProcess WeAthFolD c 5 Years ago.

Reply to "GtkMultiProcess"

Here you can reply to the paste above

captcha