summaryrefslogtreecommitdiff
path: root/gnome-settings-daemon/gnome-settings-locate-pointer.c
blob: 7002c1b7b3f47f9b417262bc57023c127bb012de (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
#include <gtk/gtk.h>

#include "gnome-settings-locate-pointer.h"
#include "gnome-settings-daemon.h"

#define LARGE_SIZE 101
#define SMALL_SIZE 51

typedef enum {
  STAGE_ONE,
  STAGE_TWO,
  STAGE_THREE,
  STAGE_FOUR,
  STAGE_DONE
} LocatePointerStage;

static LocatePointerStage stage;
static GdkWindow *window = NULL;
static gint cursor_x, cursor_y;
static guint locate_pointer_id = 0;

static gint
locate_pointer_expose (GtkWidget      *widget,
		       GdkEventExpose *event,
		       gpointer        data)
{
  gint size;
  GdkPoint points[4];

  if (event->window != window)
    return FALSE;

  switch (stage)
    {
    case STAGE_ONE:
    case STAGE_TWO:
      size = LARGE_SIZE;
      break;
    case STAGE_THREE:
    case STAGE_FOUR:
      size = SMALL_SIZE;
      break;
    default:
      return FALSE;
    }

  gdk_draw_rectangle (event->window,
		      widget->style->black_gc,
		      TRUE,
		      0, 0, size, size);
  switch (stage)
    {
    case STAGE_ONE:
    case STAGE_THREE:
      gdk_draw_rectangle (event->window,
			  widget->style->white_gc,
			  FALSE,
			  1, 1, size - 3, size - 3);
      break;
    case STAGE_TWO:
    case STAGE_FOUR:
      points[0].x = size/2;
      points[0].y = 0 + 1;
      points[1].x = size - 2;
      points[1].y = size/2;
      points[2].x = size/2;
      points[2].y = size - 2;
      points[3].x = 0 + 1;
      points[3].y = size/2;
      gdk_draw_polygon (event->window,
			widget->style->white_gc,
			FALSE, points, 4);
      break;
    default:
      g_assert_not_reached ();
    }

  return TRUE;
}

static void
setup_window (void)
{
  gint size;
  GdkBitmap *mask;
  GdkGC *gc;
  GdkColor col;
  GdkPoint points[4];

  gdk_window_hide (window);
  switch (stage)
    {
    case STAGE_ONE:
    case STAGE_TWO:
      size = LARGE_SIZE;
      break;
    case STAGE_THREE:
    case STAGE_FOUR:
      size = SMALL_SIZE;
      break;
    default:
      return;
    }

  gdk_window_move_resize (window,
			  cursor_x - size/2,
			  cursor_y - size/2,
			  size, size);
  mask = gdk_pixmap_new (window, size, size, 1);
  gc = gdk_gc_new (mask);
  switch (stage)
    {
    case STAGE_ONE:
    case STAGE_THREE:
      col.pixel = 1;
      gdk_gc_set_foreground (gc, &col);
      gdk_draw_rectangle (mask, gc, TRUE, 0, 0, size, size);
      col.pixel = 0;
      gdk_gc_set_foreground (gc, &col);
      gdk_draw_rectangle (mask, gc, TRUE, 3, 3, size - 6, size - 6);
      break;
    case STAGE_TWO:
    case STAGE_FOUR:
      col.pixel = 0;
      gdk_gc_set_foreground (gc, &col);
      gdk_draw_rectangle (mask, gc, TRUE, 0, 0, size, size);
      col.pixel = 1;
      gdk_gc_set_foreground (gc, &col);
      points[0].x = size/2;
      points[0].y = 0;
      points[1].x = size - 1;
      points[1].y = size/2;
      points[2].x = size/2;
      points[2].y = size - 1;
      points[3].x = 0;
      points[3].y = size/2;
      gdk_draw_polygon (mask, gc, FALSE, points, 4);
      points[0].x = size/2;
      points[0].y = 0 + 1;
      points[1].x = size - 2;
      points[1].y = size/2;
      points[2].x = size/2;
      points[2].y = size - 2;
      points[3].x = 0 + 1;
      points[3].y = size/2;
      gdk_draw_polygon (mask, gc, FALSE, points, 4);
      points[0].x = size/2;
      points[0].y = 0 + 2;
      points[1].x = size - 3;
      points[1].y = size/2;
      points[2].x = size/2;
      points[2].y = size - 3;
      points[3].x = 0 + 2;
      points[3].y = size/2;
      gdk_draw_polygon (mask, gc, FALSE, points, 4);
      break;
    default:
      g_assert_not_reached ();
    }

  gdk_window_shape_combine_mask (window, mask, 0, 0);
  g_object_unref (G_OBJECT (gc));
  g_object_unref (G_OBJECT (mask));
  gdk_window_show (window);
}

static void
create_window (void)
{
  GdkWindowAttr attributes;
  GtkWidget *invisible;

  invisible = gnome_settings_daemon_get_invisible ();

  attributes.window_type = GDK_WINDOW_TEMP;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (invisible);
  attributes.colormap = gtk_widget_get_colormap (invisible);
  attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
  attributes.width = 1;
  attributes.height = 1;
  window = gdk_window_new (gdk_get_default_root_window (),
			   &attributes,
			   GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP);
  gdk_window_set_user_data (window, invisible);
  g_signal_connect (G_OBJECT (invisible),
		    "expose_event",
		    (GCallback) locate_pointer_expose,
		    NULL);
}

static gboolean
locate_pointer_timeout (gpointer data)
{
  stage++;
  if (stage == STAGE_DONE)
    {
      gdk_window_hide (window);
      locate_pointer_id = 0;
      return FALSE;
    }
  setup_window ();
  return TRUE;
}

void
gnome_settings_locate_pointer (void)
{
  gdk_window_get_pointer (gdk_get_default_root_window (), &cursor_x, &cursor_y, NULL);

  if (locate_pointer_id)
    gtk_timeout_remove (locate_pointer_id);
  if (window == NULL)
    create_window ();

  stage = STAGE_ONE;
  setup_window ();
  gdk_window_show (window);
  locate_pointer_id = gtk_timeout_add (100, locate_pointer_timeout, NULL);
}