1 | /* GDK - The GIMP Drawing Kit |
2 | * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org> |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "config.h" |
19 | |
20 | #include <math.h> |
21 | |
22 | #include "gdkdevicetoolprivate.h" |
23 | #include "gdkenumtypes.h" |
24 | #include "gdkintl.h" |
25 | |
26 | /** |
27 | * GdkDeviceTool: |
28 | * |
29 | * A physical tool associated to a `GdkDevice`. |
30 | */ |
31 | |
32 | G_DEFINE_TYPE (GdkDeviceTool, gdk_device_tool, G_TYPE_OBJECT) |
33 | |
34 | enum { |
35 | TOOL_PROP_0, |
36 | TOOL_PROP_SERIAL, |
37 | TOOL_PROP_TOOL_TYPE, |
38 | TOOL_PROP_AXES, |
39 | TOOL_PROP_HARDWARE_ID, |
40 | N_TOOL_PROPS |
41 | }; |
42 | |
43 | GParamSpec *tool_props[N_TOOL_PROPS] = { 0 }; |
44 | |
45 | static void |
46 | gdk_device_tool_set_property (GObject *object, |
47 | guint prop_id, |
48 | const GValue *value, |
49 | GParamSpec *pspec) |
50 | { |
51 | GdkDeviceTool *tool = GDK_DEVICE_TOOL (object); |
52 | |
53 | switch (prop_id) |
54 | { |
55 | case TOOL_PROP_SERIAL: |
56 | tool->serial = g_value_get_uint64 (value); |
57 | break; |
58 | case TOOL_PROP_TOOL_TYPE: |
59 | tool->type = g_value_get_enum (value); |
60 | break; |
61 | case TOOL_PROP_AXES: |
62 | tool->tool_axes = g_value_get_flags (value); |
63 | break; |
64 | case TOOL_PROP_HARDWARE_ID: |
65 | tool->hw_id = g_value_get_uint64 (value); |
66 | break; |
67 | default: |
68 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
69 | break; |
70 | } |
71 | } |
72 | |
73 | static void |
74 | gdk_device_tool_get_property (GObject *object, |
75 | guint prop_id, |
76 | GValue *value, |
77 | GParamSpec *pspec) |
78 | { |
79 | GdkDeviceTool *tool = GDK_DEVICE_TOOL (object); |
80 | |
81 | switch (prop_id) |
82 | { |
83 | case TOOL_PROP_SERIAL: |
84 | g_value_set_uint64 (value, v_uint64: tool->serial); |
85 | break; |
86 | case TOOL_PROP_TOOL_TYPE: |
87 | g_value_set_enum (value, v_enum: tool->type); |
88 | break; |
89 | case TOOL_PROP_AXES: |
90 | g_value_set_flags (value, v_flags: tool->tool_axes); |
91 | break; |
92 | case TOOL_PROP_HARDWARE_ID: |
93 | g_value_set_uint64 (value, v_uint64: tool->hw_id); |
94 | break; |
95 | default: |
96 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
97 | break; |
98 | } |
99 | } |
100 | |
101 | static void |
102 | gdk_device_tool_class_init (GdkDeviceToolClass *klass) |
103 | { |
104 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
105 | |
106 | object_class->set_property = gdk_device_tool_set_property; |
107 | object_class->get_property = gdk_device_tool_get_property; |
108 | |
109 | /** |
110 | * GdkDeviceTool:serial: (attributes org.gtk.Property.get=gdk_device_tool_get_serial) |
111 | * |
112 | * The serial number of the tool. |
113 | */ |
114 | tool_props[TOOL_PROP_SERIAL] = g_param_spec_uint64 (name: "serial" , |
115 | nick: "Serial" , |
116 | blurb: "Serial number" , |
117 | minimum: 0, G_MAXUINT64, default_value: 0, |
118 | flags: G_PARAM_READWRITE | |
119 | G_PARAM_CONSTRUCT_ONLY | |
120 | G_PARAM_STATIC_STRINGS); |
121 | |
122 | /** |
123 | * GdkDeviceTool:tool-type: (attributes org.gtk.Property.get=gdk_device_tool_get_tool_type) |
124 | * |
125 | * The type of the tool. |
126 | */ |
127 | tool_props[TOOL_PROP_TOOL_TYPE] = g_param_spec_enum (name: "tool-type" , |
128 | nick: "Tool type" , |
129 | blurb: "Tool type" , |
130 | enum_type: GDK_TYPE_DEVICE_TOOL_TYPE, |
131 | default_value: GDK_DEVICE_TOOL_TYPE_UNKNOWN, |
132 | flags: G_PARAM_READWRITE | |
133 | G_PARAM_CONSTRUCT_ONLY | |
134 | G_PARAM_STATIC_STRINGS); |
135 | |
136 | /** |
137 | * GdkDeviceTool:axes: (attributes org.gtk.Property.get=gdk_device_tool_get_axes) |
138 | * |
139 | * The axes of the tool. |
140 | */ |
141 | tool_props[TOOL_PROP_AXES] = g_param_spec_flags (name: "axes" , |
142 | nick: "Axes" , |
143 | blurb: "Tool axes" , |
144 | flags_type: GDK_TYPE_AXIS_FLAGS, default_value: 0, |
145 | flags: G_PARAM_READWRITE | |
146 | G_PARAM_CONSTRUCT_ONLY); |
147 | |
148 | /** |
149 | * GdkDeviceTool:hardware-id: (attributes org.gtk.Property.get=gdk_device_tool_get_hardware_id) |
150 | * |
151 | * The hardware ID of the tool. |
152 | */ |
153 | tool_props[TOOL_PROP_HARDWARE_ID] = g_param_spec_uint64 (name: "hardware-id" , |
154 | nick: "Hardware ID" , |
155 | blurb: "Hardware ID" , |
156 | minimum: 0, G_MAXUINT64, default_value: 0, |
157 | flags: G_PARAM_READWRITE | |
158 | G_PARAM_CONSTRUCT_ONLY | |
159 | G_PARAM_STATIC_STRINGS); |
160 | |
161 | g_object_class_install_properties (oclass: object_class, n_pspecs: N_TOOL_PROPS, pspecs: tool_props); |
162 | } |
163 | |
164 | static void |
165 | gdk_device_tool_init (GdkDeviceTool *tool) |
166 | { |
167 | } |
168 | |
169 | GdkDeviceTool * |
170 | gdk_device_tool_new (guint64 serial, |
171 | guint64 hw_id, |
172 | GdkDeviceToolType type, |
173 | GdkAxisFlags tool_axes) |
174 | { |
175 | return g_object_new (GDK_TYPE_DEVICE_TOOL, |
176 | first_property_name: "serial" , serial, |
177 | "hardware-id" , hw_id, |
178 | "tool-type" , type, |
179 | "axes" , tool_axes, |
180 | NULL); |
181 | } |
182 | |
183 | /** |
184 | * gdk_device_tool_get_serial: (attributes org.gtk.Method.get_property=serial) |
185 | * @tool: a `GdkDeviceTool` |
186 | * |
187 | * Gets the serial number of this tool. |
188 | * |
189 | * This value can be used to identify a physical tool |
190 | * (eg. a tablet pen) across program executions. |
191 | * |
192 | * Returns: The serial ID for this tool |
193 | */ |
194 | guint64 |
195 | gdk_device_tool_get_serial (GdkDeviceTool *tool) |
196 | { |
197 | g_return_val_if_fail (tool != NULL, 0); |
198 | |
199 | return tool->serial; |
200 | } |
201 | |
202 | /** |
203 | * gdk_device_tool_get_hardware_id: (attributes org.gtk.Method.get_property=hardware-id) |
204 | * @tool: a `GdkDeviceTool` |
205 | * |
206 | * Gets the hardware ID of this tool, or 0 if it's not known. |
207 | * |
208 | * When non-zero, the identificator is unique for the given tool model, |
209 | * meaning that two identical tools will share the same @hardware_id, |
210 | * but will have different serial numbers (see |
211 | * [method@Gdk.DeviceTool.get_serial]). |
212 | * |
213 | * This is a more concrete (and device specific) method to identify |
214 | * a `GdkDeviceTool` than [method@Gdk.DeviceTool.get_tool_type], |
215 | * as a tablet may support multiple devices with the same |
216 | * `GdkDeviceToolType`, but different hardware identificators. |
217 | * |
218 | * Returns: The hardware identificator of this tool. |
219 | */ |
220 | guint64 |
221 | gdk_device_tool_get_hardware_id (GdkDeviceTool *tool) |
222 | { |
223 | g_return_val_if_fail (tool != NULL, 0); |
224 | |
225 | return tool->hw_id; |
226 | } |
227 | |
228 | /** |
229 | * gdk_device_tool_get_tool_type: (attributes org.gtk.Method.get_property=tool-type) |
230 | * @tool: a `GdkDeviceTool` |
231 | * |
232 | * Gets the `GdkDeviceToolType` of the tool. |
233 | * |
234 | * Returns: The physical type for this tool. This can be used to |
235 | * figure out what sort of pen is being used, such as an airbrush |
236 | * or a pencil. |
237 | */ |
238 | GdkDeviceToolType |
239 | gdk_device_tool_get_tool_type (GdkDeviceTool *tool) |
240 | { |
241 | g_return_val_if_fail (tool != NULL, GDK_DEVICE_TOOL_TYPE_UNKNOWN); |
242 | |
243 | return tool->type; |
244 | } |
245 | |
246 | /** |
247 | * gdk_device_tool_get_axes: (attributes org.gtk.Method.get_property=axes) |
248 | * @tool: a `GdkDeviceTool` |
249 | * |
250 | * Gets the axes of the tool. |
251 | * |
252 | * Returns: the axes of @tool |
253 | */ |
254 | GdkAxisFlags |
255 | gdk_device_tool_get_axes (GdkDeviceTool *tool) |
256 | { |
257 | g_return_val_if_fail (tool != NULL, 0); |
258 | |
259 | return tool->tool_axes; |
260 | } |
261 | |