File: gtkmouse.c

package info (click to toggle)
fuse-emulator 1.5.7%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,024 kB
  • sloc: ansic: 87,196; sh: 11,456; perl: 3,916; makefile: 991; yacc: 245; lex: 139
file content (182 lines) | stat: -rw-r--r-- 5,136 bytes parent folder | download | duplicates (2)
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
/* gtkmouse.c: GTK+ routines for emulating Spectrum mice
   Copyright (c) 2004 Darren Salt
   Copyright (c) 2015 Sergio BaldovĂ­

   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 2 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, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: linux@youmustbejoking.demon.co.uk

*/

#include <config.h>

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

#include "gtkinternals.h"
#include "ui/ui.h"

#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif

/* For XWarpPointer *only* - see below */
#include <gdk/gdkx.h>
#include <X11/Xlib.h>

static GdkCursor *nullpointer = NULL;

/* The widget we base our events, grabs, warping etc on */
static GtkWidget *mouse_widget = NULL;

static void
gtkmouse_reset_pointer( void )
{
  /* Ugh. GDK doesn't have its own move-pointer function :-|

     The logic here is a bit hairy:

     * On GTK+ 2.x, we warp relative to the drawing area
     * On GTK+ 3.x on X11, we warp relative to the top-level window
     * On GTK+ 3.x on Wayland, we don't warp at all because it causes a
       segfault (see bug #435)
   */

  GdkWindow *window;

#ifdef GDK_WINDOWING_WAYLAND
  GdkDisplay *display = gdk_display_get_default();
  if( GDK_IS_WAYLAND_DISPLAY( display ) ) return;
#endif                /* #ifdef GDK_WINDOWING_WAYLAND */

  window = gtk_widget_get_window( mouse_widget );
  XWarpPointer( GDK_WINDOW_XDISPLAY( window ), None, 
                GDK_WINDOW_XID( window ), 0, 0, 0, 0, 128, 128 );
}

static gboolean
motion_event( GtkWidget *widget GCC_UNUSED, GdkEventMotion *event,
              gpointer data GCC_UNUSED )
{
  if( !ui_mouse_grabbed ) return TRUE;

  if( event->x != 128 || event->y != 128 )
    gtkmouse_reset_pointer();
  ui_mouse_motion( event->x - 128, event->y - 128 );
  return TRUE;
}

static gboolean
button_event( GtkWidget *widget GCC_UNUSED, GdkEventButton *event,
	      gpointer data GCC_UNUSED )
{
  if( event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS
      || event->type == GDK_3BUTTON_PRESS )
    ui_mouse_button( event->button, 1 );
  else
    ui_mouse_button( event->button, 0 );
  return TRUE;
}

void
gtkmouse_init( void )
{
#if GTK_CHECK_VERSION( 3, 0, 0 )
  mouse_widget = gtkui_window;
#else                 /* #if GTK_CHECK_VERSION( 3, 0, 0 ) */
  mouse_widget = gtkui_drawing_area;
#endif                /* #if GTK_CHECK_VERSION( 3, 0, 0 ) */

  gtk_widget_add_events( GTK_WIDGET( mouse_widget ),
    GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
  g_signal_connect( G_OBJECT( mouse_widget ), "motion-notify-event",
		    G_CALLBACK( motion_event ), NULL );
  g_signal_connect( G_OBJECT( mouse_widget ), "button-press-event",
		    G_CALLBACK( button_event ), NULL );
  g_signal_connect( G_OBJECT( mouse_widget ), "button-release-event",
		    G_CALLBACK( button_event ), NULL );
}

int
ui_mouse_grab( int startup )
{
  GdkWindow *window;
  GdkGrabStatus status;

  if( startup ) return 0;

  window = gtk_widget_get_window( mouse_widget );

#if !GTK_CHECK_VERSION( 3, 0, 0 )

  if( !nullpointer ) {
    nullpointer = gdk_cursor_new( GDK_BLANK_CURSOR );
  }

  status = gdk_pointer_grab( window, FALSE,
                             GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
                             GDK_BUTTON_RELEASE_MASK,
                             window, nullpointer, GDK_CURRENT_TIME );

#else

  GdkDisplay *display;
  GdkSeat *seat;

  display = gdk_window_get_display( window );

  if( !nullpointer ) {
    nullpointer = gdk_cursor_new_for_display( display, GDK_BLANK_CURSOR );
  }

  seat = gdk_display_get_default_seat( display );
  status = gdk_seat_grab( seat, window, GDK_SEAT_CAPABILITY_ALL_POINTING,
                          FALSE, nullpointer, NULL, NULL, NULL );

#endif                /* #if !GTK_CHECK_VERSION( 3, 0, 0 ) */

  if( status == GDK_GRAB_SUCCESS ) {
    gtkmouse_reset_pointer();
    ui_statusbar_update( UI_STATUSBAR_ITEM_MOUSE, UI_STATUSBAR_STATE_ACTIVE );
    return 1;
  }

  ui_error( UI_ERROR_WARNING, "Mouse grab failed" );
  return 0;
}

int
ui_mouse_release( int suspend GCC_UNUSED )
{
#if !GTK_CHECK_VERSION( 3, 0, 0 )

  gdk_pointer_ungrab( GDK_CURRENT_TIME );

#else

  GdkDisplay *display;
  GdkSeat *seat;

  display = gtk_widget_get_display( mouse_widget );
  seat = gdk_display_get_default_seat( display );
  gdk_seat_ungrab( seat );

#endif                /* #if !GTK_CHECK_VERSION( 3, 0, 0 ) */ 

  ui_statusbar_update( UI_STATUSBAR_ITEM_MOUSE, UI_STATUSBAR_STATE_INACTIVE );
  return 0;
}