~alan-griffiths/mir/verify-smoke-test-runs-ok

« back to all changes in this revision

Viewing changes to src/utils/in.cpp

  • Committer: Daniel van Vugt
  • Date: 2016-03-14 10:13:49 UTC
  • mfrom: (3385 development-branch)
  • mto: This revision was merged to the branch mainline in revision 3398.
  • Revision ID: daniel.van.vugt@canonical.com-20160314101349-olm4fbs915d60nfo
Merge latest trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Author: Andreas Pokorny <andreas.pokorny@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir_toolkit/mir_client_library.h"
 
20
#include "mir_toolkit/mir_input_device.h"
 
21
 
 
22
#include <signal.h>
 
23
 
 
24
#include <iostream>
 
25
#include <future>
 
26
#include <algorithm>
 
27
 
 
28
std::promise<bool> stop;
 
29
void shutdown(int)
 
30
{
 
31
    stop.set_value(true);
 
32
}
 
33
 
 
34
auto query_input_config(MirConnection* con)
 
35
{
 
36
    return std::unique_ptr<MirInputConfig, void (*)(MirInputConfig const*)>(mir_connection_create_input_config(con),
 
37
                                                                            &mir_input_config_destroy);
 
38
}
 
39
 
 
40
std::string capability_to_string(MirInputDeviceCapabilities caps)
 
41
{
 
42
    std::string cap_string;
 
43
    if (caps & mir_input_device_capability_touchpad) cap_string += " touchpad";
 
44
    else if (caps & mir_input_device_capability_pointer) cap_string += " pointer";
 
45
    if (caps & mir_input_device_capability_alpha_numeric) cap_string += " full keyboard";
 
46
    else if (caps & mir_input_device_capability_keyboard) cap_string += " keys";
 
47
    if (caps & mir_input_device_capability_touchscreen) cap_string += " touchscreen";
 
48
    if (caps & mir_input_device_capability_switch) cap_string += " switch";
 
49
    if (caps & mir_input_device_capability_gamepad) cap_string += " gamepad";
 
50
    if (caps & mir_input_device_capability_joystick) cap_string += " joystick";
 
51
    return cap_string;
 
52
}
 
53
 
 
54
std::ostream& operator<<(std::ostream& out, MirInputDevice const& dev)
 
55
{
 
56
    return out << mir_input_device_get_id(&dev)
 
57
            << ':' << mir_input_device_get_name(&dev)
 
58
            << ':' << mir_input_device_get_unique_id(&dev)
 
59
            << ' ' << capability_to_string(mir_input_device_get_capabilities(&dev));
 
60
}
 
61
 
 
62
std::ostream& operator<<(std::ostream& out, MirInputConfig const& config)
 
63
{
 
64
    out << "Attached input devices:" << std::endl;
 
65
    for (size_t i = 0; i != mir_input_config_device_count(&config); ++i)
 
66
        out << *mir_input_config_get_device(&config, i) << std::endl;
 
67
 
 
68
    return out << std::endl;
 
69
}
 
70
 
 
71
void on_config_change(MirConnection* connection, void*)
 
72
{
 
73
    auto config = query_input_config(connection);
 
74
    std::cout << *config;
 
75
}
 
76
 
 
77
int main(int argc, char const* argv[])
 
78
{
 
79
    std::future<bool> wait_for_stop = stop.get_future();
 
80
    signal(SIGINT, shutdown);
 
81
    signal(SIGTERM, shutdown);
 
82
    signal(SIGHUP, shutdown);
 
83
    char const* server = nullptr;
 
84
 
 
85
    for (int a = 1; a < argc; ++a)
 
86
    {
 
87
        const char *arg = argv[a];
 
88
        if (arg[0] == '-')
 
89
        {
 
90
            if (arg[1] == '-' && arg[2] == '\0')
 
91
                break;
 
92
            std::cout << "Usage: " << argv[0] << " [<socket-file>] [--]\n"
 
93
                "Options:\n"
 
94
                "    --  Ignore the rest of the command line."
 
95
                << std::endl;
 
96
            return 0;
 
97
        }
 
98
        else
 
99
        {
 
100
            server = arg;
 
101
        }
 
102
    }
 
103
 
 
104
    MirConnection *conn = mir_connect_sync(server, argv[0]);
 
105
    if (!mir_connection_is_valid(conn))
 
106
    {
 
107
        std::cerr << "Could not connect to display server: " << mir_connection_get_error_message(conn) << std::endl;
 
108
        return 1;
 
109
    }
 
110
 
 
111
    on_config_change(conn, nullptr);
 
112
    mir_connection_set_input_config_change_callback(conn, on_config_change, nullptr);
 
113
    wait_for_stop.get();
 
114
    return 0;
 
115
}