summaryrefslogtreecommitdiffstats
path: root/app/widgets/gimpdeviceinfo-coords.c
blob: 024cd5b1b4d3d350f2742af4337804b89ad13d4d (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "widgets-types.h"

#include "gimpdeviceinfo.h"
#include "gimpdeviceinfo-coords.h"


static const GimpCoords default_coords = GIMP_COORDS_DEFAULT_VALUES;


/*  public functions  */

gboolean
gimp_device_info_get_event_coords (GimpDeviceInfo *info,
                                   GdkWindow      *window,
                                   const GdkEvent *event,
                                   GimpCoords     *coords)
{
  gdouble x;

  if (event && gdk_event_get_axis (event, GDK_AXIS_X, &x))
    {
      *coords = default_coords;

      coords->x = x;
      gdk_event_get_axis (event, GDK_AXIS_Y, &coords->y);

      /* translate event coordinates to window coordinates, only
       * happens if we drag a guide from a ruler
       */
      if (event->any.window &&
          event->any.window != window)
        {
          GtkWidget *src_widget;
          GtkWidget *dest_widget;

          src_widget = gtk_get_event_widget ((GdkEvent *) event);
          gdk_window_get_user_data (window, (gpointer) &dest_widget);

          if (src_widget && dest_widget)
            {
              gint offset_x;
              gint offset_y;

              if (gtk_widget_translate_coordinates (src_widget, dest_widget,
                                                    0, 0,
                                                    &offset_x, &offset_y))
                {
                  coords->x += offset_x;
                  coords->y += offset_y;
                }
            }
        }

      if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &coords->pressure))
        {
          coords->pressure = gimp_device_info_map_axis (info,
                                                        GDK_AXIS_PRESSURE,
                                                        coords->pressure);
        }

      if (gdk_event_get_axis (event, GDK_AXIS_XTILT, &coords->xtilt))
        {
          coords->xtilt = gimp_device_info_map_axis (info,
                                                     GDK_AXIS_XTILT,
                                                     coords->xtilt);
        }

      if (gdk_event_get_axis (event, GDK_AXIS_YTILT, &coords->ytilt))
        {
          coords->ytilt = gimp_device_info_map_axis (info,
                                                     GDK_AXIS_YTILT,
                                                     coords->ytilt);
        }

      if (gdk_event_get_axis (event, GDK_AXIS_WHEEL, &coords->wheel))
        {
          coords->wheel = gimp_device_info_map_axis (info,
                                                     GDK_AXIS_WHEEL,
                                                     coords->wheel);
        }

      if (gimp_device_info_get_mode (info)     != GDK_MODE_DISABLED &&
          gdk_device_get_source (info->device) != GDK_SOURCE_MOUSE)
        {
          /* The event was generated by an enabled extended non-mouse device */
          coords->extended = TRUE;
        }
      else
        {
          /* The event was generated by a not extended enabled device */
          coords->extended = FALSE;
        }

      return TRUE;
    }

  gimp_device_info_get_device_coords (info, window, coords);

  return FALSE;
}

#define GIMP_AXIS_HOPEFULLY_LAST 36
/* gtk2 has a bug where gdk_device_get_state may write
   more coordinates than GDK_AXIS_LAST in a caller-provided
   array, resulting in a buffer overflow.

   This happens when the underlying X device supports more axes -- as
   of 2023, the Wacom tablet driver 1.20 supports 8 axes when
   GDK_AXIS_LAST is 7.

   To be safe we over-estimate the maximum number of axes that any
   driver may provide to gtk, to avoid buffer overflows. It would be nicer
   to fix gdk upstream (to never write more than GDK_AXIS_LAST - GDK_AXIS_X
   coordinates), but the proper upstream fix was rejected as gtk2 is EOL:
    https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/6045

   Our constant might in theory become wrong if new device drivers
   start returning much more axes than they do today -- more than 32,
   when the current maximum is 8. Hopefully, by the time this happens,
   users have migrated to gtk3 versions of gimp that use a different
   API and do not suffer from this bug.

   We chose 36 as it is also the internal limit MAX_VALUATORS on the
   number of 'valuators' used by current X server implementations.
*/

void
gimp_device_info_get_device_coords (GimpDeviceInfo *info,
                                    GdkWindow      *window,
                                    GimpCoords     *coords)
{
  gdouble axes[GIMP_AXIS_HOPEFULLY_LAST] = { 0, };

  *coords = default_coords;

  gdk_device_get_state (info->device, window, axes, NULL);

  gdk_device_get_axis (info->device, axes, GDK_AXIS_X, &coords->x);
  gdk_device_get_axis (info->device, axes, GDK_AXIS_Y, &coords->y);

  if (gdk_device_get_axis (info->device,
                           axes, GDK_AXIS_PRESSURE, &coords->pressure))
    {
      coords->pressure = gimp_device_info_map_axis (info,
                                                    GDK_AXIS_PRESSURE,
                                                    coords->pressure);
    }

  if (gdk_device_get_axis (info->device,
                           axes, GDK_AXIS_XTILT, &coords->xtilt))
    {
      coords->xtilt = gimp_device_info_map_axis (info,
                                                 GDK_AXIS_XTILT,
                                                 coords->xtilt);
    }

  if (gdk_device_get_axis (info->device,
                           axes, GDK_AXIS_YTILT, &coords->ytilt))
    {
      coords->ytilt = gimp_device_info_map_axis (info,
                                                 GDK_AXIS_YTILT,
                                                 coords->ytilt);
    }

  if (gdk_device_get_axis (info->device,
                           axes, GDK_AXIS_WHEEL, &coords->wheel))
    {
      coords->wheel = gimp_device_info_map_axis (info,
                                                 GDK_AXIS_WHEEL,
                                                 coords->wheel);
    }

  if (gimp_device_info_get_mode (info)     != GDK_MODE_DISABLED &&
      gdk_device_get_source (info->device) != GDK_SOURCE_MOUSE)
    {
      /* The event was generated by an enabled extended non-mouse device */
      coords->extended = TRUE;
    }
  else
    {
      /* The event was generated by a not extended enabled device */
      coords->extended = FALSE;
    }
}

void
gimp_device_info_get_time_coords (GimpDeviceInfo *info,
                                  GdkTimeCoord   *event,
                                  GimpCoords     *coords)
{
  *coords = default_coords;

  gdk_device_get_axis (info->device, event->axes, GDK_AXIS_X, &coords->x);
  gdk_device_get_axis (info->device, event->axes, GDK_AXIS_Y, &coords->y);

  /*  CLAMP() the return value of each *_get_axis() call to be safe
   *  against buggy XInput drivers.
   */

  if (gdk_device_get_axis (info->device,
                           event->axes, GDK_AXIS_PRESSURE, &coords->pressure))
    {
      coords->pressure = gimp_device_info_map_axis (info,
                                                    GDK_AXIS_PRESSURE,
                                                    coords->pressure);
    }

  if (gdk_device_get_axis (info->device,
                           event->axes, GDK_AXIS_XTILT, &coords->xtilt))
    {
      coords->xtilt = gimp_device_info_map_axis (info,
                                                 GDK_AXIS_XTILT,
                                                 coords->xtilt);
    }

  if (gdk_device_get_axis (info->device,
                           event->axes, GDK_AXIS_YTILT, &coords->ytilt))
    {
      coords->ytilt = gimp_device_info_map_axis (info,
                                                 GDK_AXIS_YTILT,
                                                 coords->ytilt);
    }

  if (gdk_device_get_axis (info->device,
                           event->axes, GDK_AXIS_WHEEL, &coords->wheel))
    {
      coords->wheel = gimp_device_info_map_axis (info,
                                                 GDK_AXIS_WHEEL,
                                                 coords->wheel);
    }

  if (gimp_device_info_get_mode (info)     != GDK_MODE_DISABLED &&
      gdk_device_get_source (info->device) != GDK_SOURCE_MOUSE)
    {
      /* The event was generated by an enabled extended non-mouse device */
      coords->extended = TRUE;
    }
  else
    {
      /* The event was generated by a not extended enabled device */
      coords->extended = FALSE;
    }
}

gboolean
gimp_device_info_get_event_state (GimpDeviceInfo  *info,
                                  GdkWindow       *window,
                                  const GdkEvent  *event,
                                  GdkModifierType *state)
{
  if (gdk_event_get_state (event, state))
    return TRUE;

  gimp_device_info_get_device_state (info, window, state);

  return FALSE;
}

void
gimp_device_info_get_device_state (GimpDeviceInfo  *info,
                                   GdkWindow       *window,
                                   GdkModifierType *state)
{
  gdk_device_get_state (info->device, window, NULL, state);
}