gdkseat.c 13.2 KB
Newer Older
Carlos Garnacho's avatar
Carlos Garnacho committed
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
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 2015 Red Hat
 *
 * 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 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Carlos Garnacho <carlosg@gnome.org>
 */

#include "config.h"

#include <glib-object.h>
#include "gdkdisplay.h"
#include "gdkdevice.h"
#include "gdkseatprivate.h"
#include "gdkdeviceprivate.h"
#include "gdkintl.h"

/**
 * SECTION:gdkseat
 * @Short_description: Object representing an user seat
 * @Title: GdkSeat
 * @See_also: #GdkDisplay, #GdkDevice
 *
 * The #GdkSeat object represents a collection of input devices
Matthias Clasen's avatar
Matthias Clasen committed
36
 * that belong to a user.
Carlos Garnacho's avatar
Carlos Garnacho committed
37 38 39 40 41 42 43 44 45 46 47 48
 */

typedef struct _GdkSeatPrivate GdkSeatPrivate;

struct _GdkSeatPrivate
{
  GdkDisplay *display;
};

enum {
  DEVICE_ADDED,
  DEVICE_REMOVED,
49 50
  TOOL_ADDED,
  TOOL_REMOVED,
Carlos Garnacho's avatar
Carlos Garnacho committed
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
  N_SIGNALS
};

enum {
  PROP_0,
  PROP_DISPLAY,
  N_PROPS
};

static guint signals[N_SIGNALS] = { 0 };
static GParamSpec *props[N_PROPS] = { NULL };

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GdkSeat, gdk_seat, G_TYPE_OBJECT)

static void
gdk_seat_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  GdkSeatPrivate *priv = gdk_seat_get_instance_private (GDK_SEAT (object));

  switch (prop_id)
    {
    case PROP_DISPLAY:
      priv->display = g_value_get_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdk_seat_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
  GdkSeatPrivate *priv = gdk_seat_get_instance_private (GDK_SEAT (object));

  switch (prop_id)
    {
    case PROP_DISPLAY:
      g_value_set_object (value, priv->display);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdk_seat_class_init (GdkSeatClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = gdk_seat_set_property;
  object_class->get_property = gdk_seat_get_property;

  /**
   * GdkSeat::device-added:
   * @seat: the object on which the signal is emitted
   * @device: the newly added #GdkDevice.
   *
116
   * The ::device-added signal is emitted when a new input
Carlos Garnacho's avatar
Carlos Garnacho committed
117 118 119 120 121
   * device is related to this seat.
   *
   * Since: 3.20
   */
  signals [DEVICE_ADDED] =
122
    g_signal_new (g_intern_static_string ("device-added"),
Carlos Garnacho's avatar
Carlos Garnacho committed
123 124 125 126 127 128 129 130 131 132 133 134 135
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GdkSeatClass, device_added),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  GDK_TYPE_DEVICE);

  /**
   * GdkSeat::device-removed:
   * @seat: the object on which the signal is emitted
   * @device: the just removed #GdkDevice.
   *
136
   * The ::device-removed signal is emitted when an
Carlos Garnacho's avatar
Carlos Garnacho committed
137 138 139 140 141
   * input device is removed (e.g. unplugged).
   *
   * Since: 3.20
   */
  signals [DEVICE_REMOVED] =
142
    g_signal_new (g_intern_static_string ("device-removed"),
Carlos Garnacho's avatar
Carlos Garnacho committed
143 144 145 146 147 148 149 150
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GdkSeatClass, device_removed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  GDK_TYPE_DEVICE);

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
  /**
   * GdkSeat::tool-added:
   * @seat: the object on which the signal is emitted
   * @tool: the new #GdkDeviceTool known to the seat
   *
   * The ::tool-added signal is emitted whenever a new tool
   * is made known to the seat. The tool may later be assigned
   * to a device (i.e. on proximity with a tablet). The device
   * will emit the #GdkDevice::tool-changed signal accordingly.
   *
   * A same tool may be used by several devices.
   *
   * Since: 3.22
   */
  signals [TOOL_ADDED] =
    g_signal_new (g_intern_static_string ("tool-added"),
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  g_cclosure_marshal_VOID__BOXED,
                  G_TYPE_NONE, 1,
                  GDK_TYPE_DEVICE_TOOL);

  /**
   * GdkSeat::tool-removed:
   * @seat: the object on which the signal is emitted
   * @tool: the just removed #GdkDeviceTool
   *
   * This signal is emitted whenever a tool is no longer known
   * to this @seat.
   *
   * Since: 3.22
   */
  signals [TOOL_REMOVED] =
    g_signal_new (g_intern_static_string ("tool-removed"),
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  g_cclosure_marshal_VOID__BOXED,
                  G_TYPE_NONE, 1,
                  GDK_TYPE_DEVICE_TOOL);

Carlos Garnacho's avatar
Carlos Garnacho committed
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
  /**
   * GdkSeat:display:
   *
   * #GdkDisplay of this seat.
   *
   * Since: 3.20
   */
  props[PROP_DISPLAY] =
    g_param_spec_object ("display",
                         P_("Display"),
                         P_("Display"),
                         GDK_TYPE_DISPLAY,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, N_PROPS, props);
}

static void
gdk_seat_init (GdkSeat *seat)
{
}

/**
 * gdk_seat_get_capabilities:
 * @seat: a #GdkSeat
 *
 * Returns the capabilities this #GdkSeat currently has.
 *
 * Returns: the seat capabilities
 *
 * Since: 3.20
 **/
GdkSeatCapabilities
gdk_seat_get_capabilities (GdkSeat *seat)
{
  GdkSeatClass *seat_class;

  g_return_val_if_fail (GDK_IS_SEAT (seat), GDK_SEAT_CAPABILITY_NONE);

  seat_class = GDK_SEAT_GET_CLASS (seat);
  return seat_class->get_capabilities (seat);
}

/**
 * gdk_seat_grab:
 * @seat: a #GdkSeat
 * @window: the #GdkWindow which will own the grab
 * @capabilities: capabilities that will be grabbed
 * @owner_events: if %FALSE then all device events are reported with respect to
 *                @window and are only reported if selected by @event_mask. If
 *                %TRUE then pointer events for this application are reported
 *                as normal, but pointer events outside this application are
 *                reported with respect to @window and only if selected by
 *                @event_mask. In either mode, unreported events are discarded.
 * @cursor: (nullable): the cursor to display while the grab is active. If
 *          this is %NULL then the normal cursors are used for
 *          @window and its descendants, and the cursor for @window is used
 *          elsewhere.
 * @event: (nullable): the event that is triggering the grab, or %NULL if none
 *         is available.
Matthias Clasen's avatar
Matthias Clasen committed
255 256 257
 * @prepare_func: (nullable) (scope call) (closure prepare_func_data): function to
 *                prepare the window to be grabbed, it can be %NULL if @window is
 *                visible before this call.
258
 * @prepare_func_data: user data to pass to @prepare_func
Carlos Garnacho's avatar
Carlos Garnacho committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
 *
 * Grabs the seat so that all events corresponding to the given @capabilities
 * are passed to this application until the seat is ungrabbed with gdk_seat_ungrab(),
 * or the window becomes hidden. This overrides any previous grab on the
 * seat by this client.
 *
 * As a rule of thumb, if a grab is desired over %GDK_SEAT_CAPABILITY_POINTER,
 * all other "pointing" capabilities (eg. %GDK_SEAT_CAPABILITY_TOUCH) should
 * be grabbed too, so the user is able to interact with all of those while
 * the grab holds, you should thus use %GDK_SEAT_CAPABILITY_ALL_POINTING most
 * commonly.
 *
 * Grabs are used for operations which need complete control over the
 * events corresponding to the given capabilities. For example in GTK+ this
 * is used for Drag and Drop operations, popup menus and such.
 *
 * Note that if the event mask of a #GdkWindow has selected both button press
 * and button release events, or touch begin and touch end, then a press event
 * will cause an automatic grab until the button is released, equivalent to a
Matthias Clasen's avatar
Matthias Clasen committed
278 279
 * grab on the window with @owner_events set to %TRUE. This is done because most
 * applications expect to receive paired press and release events.
Carlos Garnacho's avatar
Carlos Garnacho committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
 *
 * If you set up anything at the time you take the grab that needs to be
 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
 * events that are emitted when the grab ends unvoluntarily.
 *
 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
 *
 * Since: 3.20
 **/
GdkGrabStatus
gdk_seat_grab (GdkSeat                *seat,
               GdkWindow              *window,
               GdkSeatCapabilities     capabilities,
               gboolean                owner_events,
               GdkCursor              *cursor,
               const GdkEvent         *event,
               GdkSeatGrabPrepareFunc  prepare_func,
               gpointer                prepare_func_data)
{
  GdkSeatClass *seat_class;

  g_return_val_if_fail (GDK_IS_SEAT (seat), GDK_GRAB_FAILED);
  g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_FAILED);

  capabilities &= GDK_SEAT_CAPABILITY_ALL;
  g_return_val_if_fail (capabilities != GDK_SEAT_CAPABILITY_NONE, GDK_GRAB_FAILED);

  seat_class = GDK_SEAT_GET_CLASS (seat);

  return seat_class->grab (seat, window, capabilities, owner_events, cursor,
                           event, prepare_func, prepare_func_data);
}

/**
 * gdk_seat_ungrab:
 * @seat: a #GdkSeat
 *
 * Releases a grab added through gdk_seat_grab().
 *
 * Since: 3.20
 **/
void
gdk_seat_ungrab (GdkSeat *seat)
{
  GdkSeatClass *seat_class;

  g_return_if_fail (GDK_IS_SEAT (seat));

  seat_class = GDK_SEAT_GET_CLASS (seat);
329
  seat_class->ungrab (seat);
Carlos Garnacho's avatar
Carlos Garnacho committed
330 331 332 333 334 335 336 337 338
}

/**
 * gdk_seat_get_slaves:
 * @seat: a #GdkSeat
 * @capabilities: capabilities to get devices for
 *
 * Returns the slave devices that match the given capabilities.
 *
Matthias Clasen's avatar
Matthias Clasen committed
339 340
 * Returns: (transfer container) (element-type GdkDevice): A list of #GdkDevices.
 *          The list must be freed with g_list_free(), the elements are owned
Carlos Garnacho's avatar
Carlos Garnacho committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
 *          by GDK and must not be freed.
 *
 * Since: 3.20
 **/
GList *
gdk_seat_get_slaves (GdkSeat             *seat,
                     GdkSeatCapabilities  capabilities)
{
  GdkSeatClass *seat_class;

  g_return_val_if_fail (GDK_IS_SEAT (seat), NULL);

  seat_class = GDK_SEAT_GET_CLASS (seat);
  return seat_class->get_slaves (seat, capabilities);
}

/**
 * gdk_seat_get_pointer:
 * @seat: a #GdkSeat
 *
 * Returns the master device that routes pointer events.
 *
 * Returns: (transfer none) (nullable): a master #GdkDevice with pointer
Matthias Clasen's avatar
Matthias Clasen committed
364
 *          capabilities. This object is owned by GTK+ and must not be freed.
Carlos Garnacho's avatar
Carlos Garnacho committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
 *
 * Since: 3.20
 **/
GdkDevice *
gdk_seat_get_pointer (GdkSeat *seat)
{
  GdkSeatClass *seat_class;

  g_return_val_if_fail (GDK_IS_SEAT (seat), NULL);

  seat_class = GDK_SEAT_GET_CLASS (seat);
  return seat_class->get_master (seat, GDK_SEAT_CAPABILITY_POINTER);
}

/**
 * gdk_seat_get_keyboard:
 * @seat: a #GdkSeat
 *
 * Returns the master device that routes keyboard events.
 *
 * Returns: (transfer none) (nullable): a master #GdkDevice with keyboard
Matthias Clasen's avatar
Matthias Clasen committed
386
 *          capabilities. This object is owned by GTK+ and must not be freed.
Carlos Garnacho's avatar
Carlos Garnacho committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
 *
 * Since: 3.20
 **/
GdkDevice *
gdk_seat_get_keyboard (GdkSeat *seat)
{
  GdkSeatClass *seat_class;

  g_return_val_if_fail (GDK_IS_SEAT (seat), NULL);

  seat_class = GDK_SEAT_GET_CLASS (seat);
  return seat_class->get_master (seat, GDK_SEAT_CAPABILITY_KEYBOARD);
}

void
gdk_seat_device_added (GdkSeat   *seat,
                       GdkDevice *device)
{
  gdk_device_set_seat (device, seat);
  g_signal_emit (seat, signals[DEVICE_ADDED], 0, device);
}

void
gdk_seat_device_removed (GdkSeat   *seat,
                         GdkDevice *device)
{
  gdk_device_set_seat (device, NULL);
  g_signal_emit (seat, signals[DEVICE_REMOVED], 0, device);
}

/**
 * gdk_seat_get_display:
 * @seat: a #GdkSeat
 *
 * Returns the #GdkDisplay this seat belongs to.
 *
Matthias Clasen's avatar
Matthias Clasen committed
423 424
 * Returns: (transfer none): a #GdkDisplay. This object is owned by GTK+
 *          and must not be freed.
Carlos Garnacho's avatar
Carlos Garnacho committed
425 426 427 428 429 430 431 432 433 434
 **/
GdkDisplay *
gdk_seat_get_display (GdkSeat *seat)
{
  GdkSeatPrivate *priv = gdk_seat_get_instance_private (seat);

  g_return_val_if_fail (GDK_IS_SEAT (seat), NULL);

  return priv->display;
}
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

void
gdk_seat_tool_added (GdkSeat       *seat,
                     GdkDeviceTool *tool)
{
  g_signal_emit (seat, signals[TOOL_ADDED], 0, tool);
}

void
gdk_seat_tool_removed (GdkSeat       *seat,
                       GdkDeviceTool *tool)
{
  g_signal_emit (seat, signals[TOOL_REMOVED], 0, tool);
}

GdkDeviceTool *
gdk_seat_get_tool (GdkSeat *seat,
452 453
                   guint64  serial,
                   guint64  hw_id)
454 455 456 457 458 459
{
  GdkSeatClass *seat_class;

  g_return_val_if_fail (GDK_IS_SEAT (seat), NULL);

  seat_class = GDK_SEAT_GET_CLASS (seat);
460
  return seat_class->get_tool (seat, serial, hw_id);
461
}