summaryrefslogtreecommitdiffstats
path: root/src/spice-grabsequence.c
blob: 50e774d8d73f64b13865b189429faa51a538caa4 (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
168
169
170
171
/*
 * GTK VNC Widget
 *
 * Copyright (C) 2010 Daniel P. Berrange <dan@berrange.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.0 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "config.h"

#include <string.h>
#include <gdk/gdk.h>

#include "spice-grabsequence.h"
#include "spice-grabsequence-priv.h"

GType spice_grab_sequence_get_type(void)
{
	static GType grab_sequence_type = 0;
	static volatile gsize grab_sequence_type_volatile;

	if (g_once_init_enter(&grab_sequence_type_volatile)) {
		grab_sequence_type = g_boxed_type_register_static
			("SpiceGrabSequence",
			 (GBoxedCopyFunc)spice_grab_sequence_copy,
			 (GBoxedFreeFunc)spice_grab_sequence_free);
		g_once_init_leave(&grab_sequence_type_volatile,
				  grab_sequence_type);
	}

	return grab_sequence_type;
}


/**
 * spice_grab_sequence_new:
 * @nkeysyms: length of @keysyms
 * @keysyms: (array length=nkeysyms): the keysym values
 *
 * Creates a new grab sequence from a list of keysym values
 *
 * Returns: (transfer full): a new grab sequence object
 */
SpiceGrabSequence *spice_grab_sequence_new(guint nkeysyms, guint *keysyms)
{
	SpiceGrabSequence *sequence;

	sequence = g_new0(SpiceGrabSequence, 1);
	sequence->nkeysyms = nkeysyms;
	sequence->keysyms = g_new0(guint, nkeysyms);
	memcpy(sequence->keysyms, keysyms, sizeof(guint)*nkeysyms);

	return sequence;
}


/**
 * spice_grab_sequence_new_from_string:
 * @str: a string of '+' separated key names (ex: "Control_L+Alt_L")
 *
 * Creates a new #SpiceGrabSequence from the string representation.
 *
 * Returns: a new #SpiceGrabSequence.
 **/
SpiceGrabSequence *spice_grab_sequence_new_from_string(const gchar *str)
{
	gchar **keysymstr;
	int i;
	SpiceGrabSequence *sequence;

	sequence = g_new0(SpiceGrabSequence, 1);

	keysymstr = g_strsplit(str, "+", 5);

	sequence->nkeysyms = 0;
	while (keysymstr[sequence->nkeysyms])
		sequence->nkeysyms++;

	sequence->keysyms = g_new0(guint, sequence->nkeysyms);
	for (i = 0 ; i < sequence->nkeysyms ; i++) {
		sequence->keysyms[i] =
			(guint)gdk_keyval_from_name(keysymstr[i]);
                if (sequence->keysyms[i] == 0) {
                        g_critical("Invalid key: %s", keysymstr[i]);
                }
        }
	g_strfreev(keysymstr);

	return sequence;

}


/**
 * spice_grab_sequence_copy:
 * @sequence: sequence to copy
 *
 * Creates a copy of the @sequence.
 *
 * Returns: (transfer full): a copy of @sequence
 **/
SpiceGrabSequence *spice_grab_sequence_copy(SpiceGrabSequence *srcSequence)
{
	SpiceGrabSequence *sequence;

	sequence = g_new0(SpiceGrabSequence, 1);
	sequence->nkeysyms = srcSequence->nkeysyms;
	sequence->keysyms = g_new0(guint, srcSequence->nkeysyms);
	memcpy(sequence->keysyms, srcSequence->keysyms,
	       sizeof(guint) * sequence->nkeysyms);

	return sequence;
}


/**
 * spice_grab_sequence_free:
 * @sequence: a #SpiceGrabSequence
 *
 * Free @sequence.
 **/
void spice_grab_sequence_free(SpiceGrabSequence *sequence)
{
	g_free(sequence->keysyms);
	g_free(sequence);
}


/**
 * spice_grab_sequence_as_string:
 * @sequence: a #SpiceGrabSequence
 *
 * Creates a string representing the @sequence.
 *
 * Returns: (transfer full): a newly allocated string representing the key sequence
 **/
gchar *spice_grab_sequence_as_string(SpiceGrabSequence *sequence)
{
	GString *str = g_string_new("");
	int i;

	for (i = 0 ; i < sequence->nkeysyms ; i++) {
		if (i > 0)
			g_string_append_c(str, '+');
		g_string_append(str, gdk_keyval_name(sequence->keysyms[i]));
	}

	return g_string_free(str, FALSE);

}


/*
 * Local variables:
 *  c-indent-level: 8
 *  c-basic-offset: 8
 *  tab-width: 8
 * End:
 */