]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/status.c
Merge with trunk
[sojka/lightdm.git] / tests / src / status.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <glib.h>
5 #include <gio/gio.h>
6 #include <gio/gunixsocketaddress.h>
7 #include <unistd.h>
8
9 #include "status.h"
10
11 static GSocket *status_socket = NULL;
12 static StatusRequestFunc request_func = NULL;
13
14 static gboolean
15 status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
16 {
17     int length;
18     gchar buffer[1024];
19     ssize_t n_read;
20     GError *error = NULL;  
21
22     n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
23     if (n_read > 0)
24         n_read = g_socket_receive (socket, buffer, length, NULL, &error);
25     if (n_read == 0)
26     {
27         if (request_func)
28             request_func (NULL);
29         return FALSE;
30     }
31     if (error)
32         g_warning ("Error reading from socket: %s", error->message);
33     g_clear_error (&error);
34
35     if (n_read > 0 && request_func)
36     {
37         buffer[n_read] = '\0';
38         request_func (buffer);
39     }
40
41     return TRUE;
42 }
43
44 void
45 status_connect (StatusRequestFunc request_cb)
46 {
47     gchar *path;
48     GSocketAddress *address;
49     gboolean result;
50     GSource *source;
51     GError *error = NULL;
52
53     request_func = request_cb;
54
55     status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
56     if (error)
57         g_printerr ("Unable to open socket for status: %s\n", error->message);
58     g_clear_error (&error);
59     if (!status_socket)
60         return;
61
62     path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".s", NULL);
63     address = g_unix_socket_address_new (path);
64     result = g_socket_connect (status_socket, address, NULL, &error);
65     g_object_unref (address);
66     if (error)
67         g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message);
68     g_clear_error (&error);
69     g_free (path);
70     if (!result)
71         return;
72
73     source = g_socket_create_source (status_socket, G_IO_IN, NULL);
74     g_source_set_callback (source, (GSourceFunc) status_request_cb, NULL, NULL);
75     g_source_attach (source, NULL);   
76 }
77
78 void
79 status_notify (const gchar *format, ...)
80 {
81     gchar status[1024];
82     va_list ap;
83
84     va_start (ap, format);
85     vsnprintf (status, 1024, format, ap);
86     va_end (ap);
87
88     if (status_socket)
89     {
90         GError *error = NULL;
91         int length;
92
93         length = strlen (status);
94         g_socket_send (status_socket, (gchar *) &length, sizeof (length), NULL, &error);
95         g_socket_send (status_socket, status, strlen (status), NULL, &error);
96         if (error)
97             g_printerr ("Failed to write to status socket: %s\n", error->message);
98         g_clear_error (&error);
99     }
100     else
101         g_printerr ("%s\n", status);
102 }