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