test-gspawn.c

Kentaro HAYASHI, 04/07/2014 12:29 AM

Download (2.25 KB)

 
1
#include <glib.h>
2
#include <glib/gi18n.h>
3
#include <gtk/gtk.h>
4

    
5
#include <winsock2.h>
6

    
7
static gint child_stdout;
8

    
9
static void update_check_cb(GPid pid, gint status, gpointer data)
10
{
11
        g_print("update_check_cb\n");
12

    
13
        if (!child_stdout) {
14
                g_spawn_close_pid(pid);
15
                return;
16
        }
17
        gchar buf[1024];
18
        gint size = read(child_stdout, buf, sizeof(buf) - 1);
19
        if (size < 0) {
20
                closesocket(child_stdout);
21
                child_stdout = 0;
22
                g_spawn_close_pid(pid);
23
                return;
24
        }
25
        buf[size] = '\0';
26

    
27
        closesocket(child_stdout);
28
        child_stdout = 0;
29
        g_spawn_close_pid(pid);
30

    
31
        g_print("%s\n", buf);
32
}
33

    
34
void cb_spawn(GtkWidget *widget, gpointer data)
35
{
36

    
37
        gchar *cmdline[8] = {"curl", "--silent", "--max-time", "10"};
38
        gint argc = 4;
39
        GPid pid;
40
        GError *error = NULL;
41
        gchar *url = "http://sylpheed.sraoss.jp/version.txt?ver=3.4.1&os=win";
42
        g_print("spawn_curl: getting from %s\n", url);
43

    
44
        if (child_stdout > 0) {
45
                g_print("update check is in progress\n");
46
                return;
47
        }
48

    
49
        child_stdout = 0;
50

    
51
        
52
        cmdline[argc++] = url;
53
        cmdline[argc++] = NULL;
54

    
55
        if (g_spawn_async_with_pipes
56
            (NULL, cmdline, NULL,
57
                 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
58
                 NULL, NULL, &pid,
59
             NULL, &child_stdout, NULL, &error) == FALSE) {
60
                g_warning("Couldn't execute curl");
61
                if (error) {
62
                        g_warning("g_spawn_async_with_pipes: %s",
63
                                  error->message);
64
                        g_error_free(error);
65
                }
66
                return;
67
        }
68
        if (pid == 0) {
69
                g_warning("Couldn't get PID of child process");
70
                if (child_stdout) {
71
                        closesocket(child_stdout);
72
                        child_stdout = 0;
73
                }
74
                return;
75
        }
76

    
77
        g_child_watch_add(pid, update_check_cb, GINT_TO_POINTER(FALSE));
78
        g_print("watch pid done: %d\n", pid);
79

    
80
}
81

    
82
int main(int argc, char *argv[])
83
{
84
        GtkWidget *window, *button;
85
        
86
        gtk_init(&argc, &argv);
87

    
88
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
89
        gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
90
        g_signal_connect(G_OBJECT(window), "destroy",
91
                         G_CALLBACK(gtk_main_quit), NULL);
92

    
93
        button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
94
        g_signal_connect(G_OBJECT(button), "clicked",
95
                          G_CALLBACK(cb_spawn), NULL);
96
        gtk_container_add(GTK_CONTAINER(window), button);
97

    
98
        gtk_widget_show_all(window);
99

    
100
        gtk_main();
101
        return 0;
102
}
103