~ci-train-bot/unity-system-compositor/unity-system-compositor-ubuntu-zesty-2649

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
/*
 * Copyright © 2015 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
 */

#include "unity_input_service.h"
#include "input_configuration.h"
#include "dbus_message_handle.h"
#include "dbus_event_loop.h"
#include "scoped_dbus_error.h"

#include "unity_input_service_introspection.h" // autogenerated

namespace
{

char const* const dbus_input_interface = "com.canonical.Unity.Input";
char const* const dbus_input_path = "/com/canonical/Unity/Input";
char const* const dbus_input_service_name = "com.canonical.Unity.Input";

}

usc::UnityInputService::UnityInputService(std::shared_ptr<usc::DBusEventLoop> const& loop,
                                          std::string const& address,
                                          std::shared_ptr<usc::InputConfiguration> const& input_config)
    : loop{loop}, connection{std::make_shared<DBusConnectionHandle>(address.c_str())}, input_config{input_config}
{
    loop->add_connection(connection);
    connection->request_name(dbus_input_service_name);
    connection->add_filter(handle_dbus_message_thunk, this);
}

::DBusHandlerResult usc::UnityInputService::handle_dbus_message_thunk(
    ::DBusConnection* connection, DBusMessage* message, void* user_data)
{
    auto const dbus_input_service = static_cast<usc::UnityInputService*>(user_data);
    return dbus_input_service->handle_dbus_message(connection, message, user_data);
}

void usc::UnityInputService::handle_message(DBusMessage* message, void (usc::InputConfiguration::* method)(bool))
{
    ScopedDBusError args_error;
    dbus_bool_t flag{true};
    dbus_message_get_args(
        message, &args_error, DBUS_TYPE_BOOLEAN, &flag, DBUS_TYPE_INVALID);

    if (!args_error)
    {
        (input_config.get()->*method)(flag);

        DBusMessageHandle reply{dbus_message_new_method_return(message)};
        dbus_connection_send(*connection, reply, nullptr);
    }
}

void usc::UnityInputService::handle_message(DBusMessage* message, void (usc::InputConfiguration::* method)(double))
{
    ScopedDBusError args_error;
    double value{true};
    dbus_message_get_args(
        message, &args_error, DBUS_TYPE_DOUBLE, &value, DBUS_TYPE_INVALID);

    if (!args_error)
    {
        (input_config.get()->*method)(value);

        DBusMessageHandle reply{dbus_message_new_method_return(message)};
        dbus_connection_send(*connection, reply, nullptr);
    }
}

void usc::UnityInputService::handle_message(DBusMessage* message, void (usc::InputConfiguration::* method)(int32_t))
{
    ScopedDBusError args_error;
    int32_t value{true};
    dbus_message_get_args(
        message, &args_error, DBUS_TYPE_INT32, &value, DBUS_TYPE_INVALID);

    if (!args_error)
    {
        (input_config.get()->*method)(value);

        DBusMessageHandle reply{dbus_message_new_method_return(message)};
        dbus_connection_send(*connection, reply, nullptr);
    }
}

DBusHandlerResult usc::UnityInputService::handle_dbus_message(
    ::DBusConnection* connection, DBusMessage* message, void* user_data)
{
    ScopedDBusError args_error;

    if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect"))
    {
        DBusMessageHandle reply{
            dbus_message_new_method_return(message),
            DBUS_TYPE_STRING, &unity_input_service_introspection,
            DBUS_TYPE_INVALID};

        dbus_connection_send(connection, reply, nullptr);
    }
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setMousePrimaryButton"))
        handle_message(message, &InputConfiguration::set_mouse_primary_button);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setMouseCursorSpeed"))
        handle_message(message, &InputConfiguration::set_mouse_cursor_speed);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setMouseScrollSpeed"))
        handle_message(message, &InputConfiguration::set_mouse_scroll_speed);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setTouchpadPrimaryButton"))
        handle_message(message, &InputConfiguration::set_touchpad_primary_button);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setTouchpadCursorSpeed"))
        handle_message(message, &InputConfiguration::set_touchpad_cursor_speed);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setTouchpadScrollSpeed"))
        handle_message(message, &InputConfiguration::set_touchpad_scroll_speed);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setTouchpadDisableWhileTyping"))
        handle_message(message, &InputConfiguration::set_disable_touchpad_while_typing);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setTouchpadTapToClick"))
        handle_message(message, &InputConfiguration::set_tap_to_click);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setTouchpadTwoFingerScroll"))
        handle_message(message, &InputConfiguration::set_two_finger_scroll);
    else if (dbus_message_is_method_call(message, dbus_input_interface, "setTouchpadDisableWithMouse"))
        handle_message(message, &InputConfiguration::set_disable_touchpad_with_mouse);
    else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
    {
         DBusMessageHandle reply{
             dbus_message_new_error(message, DBUS_ERROR_FAILED, "Not supported")};

        dbus_connection_send(connection, reply, nullptr);
    }

    if (args_error)
    {
         DBusMessageHandle reply{
             dbus_message_new_error(message, DBUS_ERROR_FAILED, "Invalid arguments")};

        dbus_connection_send(connection, reply, nullptr);
    }

    return DBUS_HANDLER_RESULT_HANDLED;
}