summaryrefslogtreecommitdiff
path: root/gtk/channel-inputs.c
blob: 38cd26f4eb119cd2c0981210525d91caaf39559e (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
#include "spice-client.h"
#include "spice-channel-priv.h"

static void send_motion(SpiceChannel *channel)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);
    SpiceMsgcMouseMotion motion;
    spice_msg_out *msg;

    if (!c->inputs.dx && !c->inputs.dy)
        return;

    fprintf(stderr, "%s\n", __FUNCTION__);
    motion.buttons_state = c->inputs.bs;
    motion.dx            = c->inputs.dx;
    motion.dy            = c->inputs.dy;
    msg = spice_msg_out_new(channel, SPICE_MSGC_INPUTS_MOUSE_MOTION);
    c->marshallers->msgc_inputs_mouse_motion(msg->marshaller, &motion);
    spice_msg_out_send(msg);
    spice_msg_out_put(msg);

    c->inputs.motion_count++;
    c->inputs.dx = 0;
    c->inputs.dy = 0;
}

static void send_position(SpiceChannel *channel)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);
    SpiceMsgcMousePosition position;
    spice_msg_out *msg;

    if (c->inputs.dpy == -1)
        return;

    fprintf(stderr, "%s: +%d+%d\n", __FUNCTION__, c->inputs.x, c->inputs.y);
    position.buttons_state = c->inputs.bs;
    position.x             = c->inputs.x;
    position.y             = c->inputs.y;
    position.display_id    = c->inputs.dpy;
    msg = spice_msg_out_new(channel, SPICE_MSGC_INPUTS_MOUSE_POSITION);
    c->marshallers->msgc_inputs_mouse_position(msg->marshaller, &position);
    spice_msg_out_send(msg);
    spice_msg_out_put(msg);

    c->inputs.motion_count++;
    c->inputs.dpy = -1;
}

static void inputs_handle_init(SpiceChannel *channel, spice_msg_in *in)
{
}

static void inputs_handle_modifiers(SpiceChannel *channel, spice_msg_in *in)
{
    fprintf(stderr, "%s: TODO\n", __FUNCTION__);
}

static void inputs_handle_ack(SpiceChannel *channel, spice_msg_in *in)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);
    c->inputs.motion_count -= SPICE_INPUT_MOTION_ACK_BUNCH;
    send_motion(channel);
    send_position(channel);
}

static spice_msg_handler inputs_handlers[] = {
    [ SPICE_MSG_SET_ACK ]                  = base_handle_set_ack,
    [ SPICE_MSG_PING ]                     = base_handle_ping,
    [ SPICE_MSG_NOTIFY ]                   = base_handle_notify,

    [ SPICE_MSG_INPUTS_INIT ]              = inputs_handle_init,
    [ SPICE_MSG_INPUTS_KEY_MODIFIERS ]     = inputs_handle_modifiers,
    [ SPICE_MSG_INPUTS_MOUSE_MOTION_ACK ]  = inputs_handle_ack,
};

spice_channel_info inputs_channel_info = {
    .type      = SPICE_CHANNEL_INPUTS,
    .name      = "inputs",
    .handlers  = inputs_handlers,
    .nhandlers = SPICE_N_ELEMENTS(inputs_handlers),
};

void spice_inputs_motion(SpiceChannel *channel, gint dx, gint dy, gint button_state)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);

    c->inputs.bs  = button_state;
    c->inputs.dx += dx;
    c->inputs.dy += dy;

    if (c->inputs.motion_count < SPICE_INPUT_MOTION_ACK_BUNCH * 2) {
        send_motion(channel);
    }
}

void spice_inputs_position(SpiceChannel *channel, gint x, gint y,
                           gint display, gint button_state)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);

    c->inputs.bs  = button_state;
    c->inputs.x   = x;
    c->inputs.y   = y;
    c->inputs.dpy = display;

    if (c->inputs.motion_count < SPICE_INPUT_MOTION_ACK_BUNCH * 2) {
        send_position(channel);
    }
}

void spice_inputs_button_press(SpiceChannel *channel, gint button, gint button_state)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);
    SpiceMsgcMousePress press = {
        .button = button,
        .buttons_state = button_state,
    };
    spice_msg_out *msg;

    c->inputs.bs  = button_state;

    send_motion(channel);
    send_position(channel);

    msg = spice_msg_out_new(channel, SPICE_MSGC_INPUTS_MOUSE_PRESS);
    c->marshallers->msgc_inputs_mouse_press(msg->marshaller, &press);
    spice_msg_out_send(msg);
    spice_msg_out_put(msg);
}

void spice_inputs_button_release(SpiceChannel *channel, gint button, gint button_state)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);
    SpiceMsgcMouseRelease release = {
        .button = button,
        .buttons_state = button_state,
    };
    spice_msg_out *msg;

    c->inputs.bs  = button_state;

    send_motion(channel);
    send_position(channel);

    msg = spice_msg_out_new(channel, SPICE_MSGC_INPUTS_MOUSE_RELEASE);
    c->marshallers->msgc_inputs_mouse_release(msg->marshaller, &release);
    spice_msg_out_send(msg);
    spice_msg_out_put(msg);
}