summaryrefslogtreecommitdiffstats
path: root/bus/server.c
blob: 7245bd3351c059a7d37a51dc9f63838e5f9f2bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* vim:set et sts=4: */
/* bus - The Input Bus
 * Copyright (C) 2008-2009 Huang Peng <shawn.p.huang@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>

#include "server.h"
#include "connection.h"
#include "dbusimpl.h"
#include "ibusimpl.h"

/* functions prototype */
static void      bus_server_class_init  (BusServerClass     *klass);
static void      bus_server_init        (BusServer          *server);
static void      bus_server_destroy     (BusServer          *server);
static void      bus_server_new_connection
                                        (BusServer          *server,
                                         BusConnection      *connection);

static IBusObjectClass  *parent_class = NULL;

GType
bus_server_get_type (void)
{
    static GType type = 0;

    static const GTypeInfo type_info = {
        sizeof (BusServerClass),
        (GBaseInitFunc)     NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc)    bus_server_class_init,
        NULL,               /* class finalize */
        NULL,               /* class data */
        sizeof (BusServer),
        0,
        (GInstanceInitFunc) bus_server_init,
    };

    if (type == 0) {
        type = g_type_register_static (IBUS_TYPE_SERVER,
                    "BusServer",
                    &type_info,
                    (GTypeFlags)0);
    }

    return type;
}

BusServer *
bus_server_get_default (void)
{
    static BusServer *server = NULL;

    if (server == NULL) {
        server = (BusServer *) g_object_new (BUS_TYPE_SERVER,
                                             "connection-type", BUS_TYPE_CONNECTION,
                                             NULL);
        bus_dbus_impl_get_default ();
        bus_ibus_impl_get_default ();
    }
    return server;
}

gboolean
bus_server_listen (BusServer *server)
{
    g_assert (BUS_IS_SERVER (server));

    // const gchar *address = "unix:abstract=/tmp/ibus-c"
    const gchar *address;
    gchar *path;
    gboolean retval;

    path = g_strdup_printf("/tmp/ibus-%s", ibus_get_user_name ());
    mkdir (path, 0775);
    address = ibus_get_address ();

    retval = ibus_server_listen (IBUS_SERVER (server), address);

    if (!retval) {
        g_printerr ("Can not listen on %s! Please try remove directory %s and run again.", address, path);
        exit (-1);
    }

    g_free(path);
    return retval;
}

void
bus_server_run (BusServer *server)
{
    g_assert (BUS_IS_SERVER (server));

    g_main_loop_run (server->loop);
}

void
bus_server_quit (BusServer *server)
{
    g_assert (BUS_IS_SERVER (server));

    g_main_loop_quit (server->loop);
}

static void
bus_server_class_init (BusServerClass *klass)
{
    IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (klass);

    parent_class = (IBusObjectClass *) g_type_class_peek_parent (klass);

    ibus_object_class->destroy = (IBusObjectDestroyFunc) bus_server_destroy;

    IBUS_SERVER_CLASS (klass)->new_connection = (IBusNewConnectionFunc) bus_server_new_connection;
}

static void
bus_server_init (BusServer *server)
{
    server->loop = g_main_loop_new (NULL, FALSE);
    server->dbus = bus_dbus_impl_get_default ();
    server->ibus = bus_ibus_impl_get_default ();
}

static void
bus_server_new_connection (BusServer     *server,
                           BusConnection *connection)
{
    g_assert (BUS_IS_SERVER (server));
    bus_dbus_impl_new_connection (server->dbus, connection);
}

static void
bus_server_destroy (BusServer *server)
{
    g_assert (BUS_IS_SERVER (server));

    ibus_object_destroy ((IBusObject *) server->dbus);
    g_object_unref (server->dbus);
    ibus_object_destroy ((IBusObject *) server->ibus);
    g_object_unref (server->ibus);

    while (g_main_loop_is_running (server->loop)) {
        g_main_loop_quit (server->loop);
    }
    g_main_loop_unref (server->loop);

    IBUS_OBJECT_CLASS (parent_class)->destroy (IBUS_OBJECT (server));
}