File: test-events.c

package info (click to toggle)
clutter-gesture 0.0.2.1-7
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 492 kB
  • ctags: 590
  • sloc: ansic: 3,492; makefile: 43; sh: 8
file content (384 lines) | stat: -rw-r--r-- 11,564 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
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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/***
 *	This file is part of Clutter-Gesture.
 *
 *	Portions Copyright 2009 (c) Intel Corp.
 *	Author: Long Bu		(long.bu@intel.com)
 *
 *	This file is modified based on test-events of Clutter/tests
 * 	Copyright (C) 2006 OpenedHand
 *
 *	Clutter-Gesture 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.1 of the License,
 *	or (at your option) any later version.
 *
 *	Clutter-Gesture 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 Lesser General Public License
 *	along with ClutterGesture; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *	USA.
 ***/
#include <gmodule.h>
#include <clutter/clutter.h>
#include <clutter/x11/clutter-x11.h>
#include <string.h>
#include "clutter-gesture.h"

gboolean IsFullScreen = FALSE, IsMotion = TRUE;

#if 0
static void
stage_state_cb (ClutterStage    *stage,
		gpointer         data)
{
  gchar *detail = (gchar*)data;

  printf("[stage signal] %s\n", detail);
}

static gboolean
blue_button_cb (ClutterActor    *actor,
		ClutterEvent    *event,
		gpointer         data)
{
  g_print ("blue_button_cb is called\n");

  return FALSE;
}
static gboolean
red_button_cb (ClutterActor    *actor,
		ClutterEvent    *event,
		gpointer         data)
{

  if (IsMotion)
    IsMotion = FALSE;
  else
    IsMotion = TRUE;

  clutter_set_motion_events_enabled (IsMotion);

  g_print ("*** Per actor motion events %s ***\n",
           IsMotion ? "enabled" : "disabled");

  return FALSE;
}
#endif
#if 0
static gboolean
capture_cb (ClutterActor *actor,
	    ClutterEvent *event,
	    gpointer      data)
{
  g_print ("* captured event for type '%s' *\n",
           G_OBJECT_TYPE_NAME (actor));

  return FALSE;
}
#endif
/*
static void
key_focus_in_cb (ClutterActor    *actor,
		 gpointer         data)
{
  ClutterActor *focus_box = CLUTTER_ACTOR(data);  

  if (actor == clutter_stage_get_default ())
    clutter_actor_hide (focus_box);
  else
    {
      clutter_actor_set_position (focus_box,
				  clutter_actor_get_x (actor) - 5,
				  clutter_actor_get_y (actor) - 5);

      clutter_actor_set_size (focus_box,
			      clutter_actor_get_width (actor) + 10,
			      clutter_actor_get_height (actor) + 10);
      clutter_actor_show (focus_box);
    }
}
*/

static void
fill_keybuf (char *keybuf, ClutterKeyEvent *event)
{
  char utf8[6];
  int len;

  /* printable character, if any (ß, ∑) */
  len = g_unichar_to_utf8 (event->unicode_value, utf8);
  utf8[len] = '\0';
  sprintf(keybuf, "'%s' ", utf8);

  /* key combination (<Mod1>s, <Shift><Mod1>S, <Ctrl><Mod1>Delete) */
  len = g_unichar_to_utf8 (clutter_keysym_to_unicode (event->keyval),
                           utf8);
  utf8[len] = '\0';

  if (event->modifier_state & CLUTTER_SHIFT_MASK)
    strcat (keybuf, "<Shift>");
  if (event->modifier_state & CLUTTER_LOCK_MASK)
    strcat (keybuf, "<Lock>");
  if (event->modifier_state & CLUTTER_CONTROL_MASK)
    strcat (keybuf, "<Control>");
  if (event->modifier_state & CLUTTER_MOD1_MASK)
    strcat (keybuf, "<Mod1>");
  if (event->modifier_state & CLUTTER_MOD2_MASK)
    strcat (keybuf, "<Mod2>");
  if (event->modifier_state & CLUTTER_MOD3_MASK)
    strcat (keybuf, "<Mod3>");
  if (event->modifier_state & CLUTTER_MOD4_MASK)
    strcat (keybuf, "<Mod4>");
  if (event->modifier_state & CLUTTER_MOD5_MASK)
    strcat (keybuf, "<Mod5>");
  strcat (keybuf, utf8);
}

static gboolean
gesture_cb (ClutterGesture    *gesture,
	  ClutterGestureEvent    *event,
	  gpointer         data)
{
	const char *gesture_name[] = {"dummy", 
		"SLIDE", "HOLD", "PINCH", "WINDOW", "ANY"};

	const char *slide_dir_name [] = {
		"dummy", 
		 "SLIDE_UP", "SLIDE_DOWN", "SLIDE_LEFT", "SLIDE_RIGHT"};


	printf("gesture_cb: event pointer %p\n", event);
	if (event) {
		printf("gesture_cb: event %s\n", gesture_name[event->type]);
		switch (event->type) {
			case GESTURE_SLIDE: 
				{
					ClutterGestureSlideEvent *slide = (ClutterGestureSlideEvent *)event;
					printf("slide direction :%s\n", slide_dir_name[slide->direction]);
					break;
				}
                        case GESTURE_HOLD:
                                {
                                        printf("GESTURE_HOLD is received\n");
                                        break;
                                }

                        case GESTURE_PINCH:
                                {
                                  printf("GESTURE_PINCH event is received\n");
                                  break;
                                }
                        case GESTURE_ROTATE:
                                {
                                  printf("GESTURE_ROTATE event is received\n");
                                  break;
                                }
			default:
				break;

		}
	}
	return TRUE;

}

static gboolean
input_cb (ClutterActor    *actor,
	  ClutterEvent    *event,
	  gpointer         data)
{
  ClutterStage *stage = CLUTTER_STAGE (clutter_stage_get_default ());
//  gchar keybuf[128], *source = (gchar*)data;
  gchar keybuf[128];
  gfloat x, y;

  switch (event->type)
    {
    case CLUTTER_KEY_PRESS:
      fill_keybuf (keybuf, &event->key);
//      printf ("[%s] KEY PRESS %s", source, keybuf);
      break;
    case CLUTTER_KEY_RELEASE:
      fill_keybuf (keybuf, &event->key);
//      printf ("[%s] KEY RELEASE %s", source, keybuf);
      break;
    case CLUTTER_MOTION:
//      g_print ("[%s] MOTION", source);
      break;
    case CLUTTER_ENTER:
//      g_print ("[%s] ENTER", source);
      break;
    case CLUTTER_LEAVE:
//      g_print ("[%s] LEAVE", source);
      break;
    case CLUTTER_BUTTON_PRESS:
//      g_print ("[%s] BUTTON PRESS (click count:%i)", 
//	       source, event->button.click_count);
      clutter_event_get_coords(event, &x, &y);
//      g_print("x, y is %d, %d\n", x, y);
      break;
    case CLUTTER_BUTTON_RELEASE:
//      g_print ("[%s] BUTTON RELEASE (click count:%i)", 
//	       source, event->button.click_count);

      if (clutter_event_get_source (event) == CLUTTER_ACTOR (stage))
        clutter_stage_set_key_focus (stage, NULL);
      else if (clutter_event_get_source (event) == actor
	       && clutter_actor_get_parent (actor) == CLUTTER_ACTOR (stage))
	clutter_stage_set_key_focus (stage, actor);
      break;
    case CLUTTER_SCROLL:
//      g_print ("[%s] BUTTON SCROLL (click count:%i)",
//	       source, event->button.click_count);
      break;
    case CLUTTER_STAGE_STATE:
//      g_print ("[%s] STAGE STATE", source);
      break;
    case CLUTTER_DESTROY_NOTIFY:
//      g_print ("[%s] DESTROY NOTIFY", source);
      break;
    case CLUTTER_CLIENT_MESSAGE:
//      g_print ("[%s] CLIENT MESSAGE", source);
      break;
    case CLUTTER_DELETE:
//      g_print ("[%s] DELETE", source);
      break;
    default:
      return FALSE;
    }

  if (clutter_event_get_source (event) == actor)
    {
      ;
    }
//    g_print (" *source*");
  
//  g_print ("\n");

  return FALSE;
}

int
main (int argc, char *argv[])
{
  ClutterActor    *stage, *actor, *actor1, *group;
  ClutterColor    rcol = { 0xff, 0, 0, 0xff}, 
                  bcol = { 0, 0, 0xff, 0xff },
		  gcol = { 0, 0xff, 0, 0xff };
		  /* ycol = { 0xff, 0xff, 0, 0xff }; */
		  /*ncol = { 0, 0, 0, 0xff }; */
  ClutterGesture *gesture = NULL;
  ClutterInitError clutter_err;

  g_type_init();

  clutter_x11_enable_xinput();

  clutter_err = clutter_init (&argc, &argv);
  if (clutter_err != CLUTTER_INIT_SUCCESS) {
      g_error ("clutter_init() error\n");
  }

//  clutter_set_motion_events_frequency(120);

  stage = clutter_stage_get_default ();
  clutter_stage_set_throttle_motion_events(CLUTTER_STAGE(stage), FALSE);
  clutter_set_default_frame_rate(60);
//  g_signal_connect (stage, "event", G_CALLBACK (input_cb), "stage");
#if 0
  g_signal_connect (stage, "fullscreen", 
		    G_CALLBACK (stage_state_cb), "fullscreen");
  g_signal_connect (stage, "unfullscreen", 
		    G_CALLBACK (stage_state_cb), "unfullscreen");
  g_signal_connect (stage, "activate", 
		    G_CALLBACK (stage_state_cb), "activate");
  g_signal_connect (stage, "deactivate", 
		    G_CALLBACK (stage_state_cb), "deactivate");
#endif
  group = clutter_group_new ();
  clutter_container_add (CLUTTER_CONTAINER (stage), group, NULL);
//  clutter_actor_set_position (group, 100, 350);
//  g_signal_connect (stage, "captured-event", G_CALLBACK (capture_cb), NULL);

//  focus_box = clutter_rectangle_new_with_color (&ncol);
//  clutter_container_add (CLUTTER_CONTAINER(stage), focus_box, NULL);
    actor = clutter_rectangle_new_with_color (&rcol);
  clutter_actor_set_size (actor, 200, 200);
  clutter_actor_set_position (actor, 100, 100);

  clutter_actor_set_reactive (actor, TRUE);

  clutter_container_add (CLUTTER_CONTAINER (group), actor, NULL);

  g_signal_connect (actor, "event", G_CALLBACK (input_cb), "red box");
  /* g_signal_connect (actor, "focus-in", G_CALLBACK (key_focus_in_cb), 
		    focus_box);	*/

    clutter_stage_set_key_focus (CLUTTER_STAGE (stage), actor);

  /* g_signal_connect (actor, "focus-in", G_CALLBACK (key_focus_in_cb), 
		    focus_box);	*/

  actor1 = clutter_rectangle_new_with_color (&gcol);
  clutter_actor_set_size (actor1, 50, 50);
  clutter_actor_set_position (actor1, 300, 300);
  //clutter_actor_set_depth (actor1, 10);

  clutter_actor_set_reactive (actor1, TRUE);

  clutter_container_add (CLUTTER_CONTAINER (group), actor1, NULL);
  clutter_actor_set_reactive (group, TRUE);

//  g_signal_connect (group, "event", G_CALLBACK (input_cb), "group");

  clutter_actor_show(actor1);

  actor = clutter_rectangle_new_with_color (&bcol);
  clutter_actor_set_size (actor, 50, 50);
  clutter_actor_set_position (actor, 400, 400);

  clutter_actor_set_reactive (actor, TRUE);

  clutter_container_add (CLUTTER_CONTAINER(stage), actor, NULL);

  g_signal_connect (actor, "event", G_CALLBACK (input_cb), "blue box");
  /* g_signal_connect (actor, "focus-in", G_CALLBACK (key_focus_in_cb), 
		    focus_box);	*/
#if 0
  /* non reactive */
  actor = clutter_rectangle_new_with_color (&ncol);
  clutter_actor_set_size (actor, 400, 50);
  clutter_actor_set_position (actor, 100, 250);

  clutter_container_add (CLUTTER_CONTAINER(stage), actor, NULL);

  g_signal_connect (actor, "event", G_CALLBACK (input_cb), "blue box");
  /* g_signal_connect (actor, "focus-in", G_CALLBACK (key_focus_in_cb), 
		    focus_box);
  g_signal_connect (stage, "focus-in", G_CALLBACK (key_focus_in_cb), 
		    focus_box);
	*/
  /* non reactive group, with reactive child */
  actor = clutter_rectangle_new_with_color (&ycol);
  clutter_actor_set_size (actor, 100, 100);
  clutter_actor_set_reactive (actor, TRUE);

  g_signal_connect (actor, "event", G_CALLBACK (input_cb), "yellow box");
#endif
  /* note group not reactive */
  clutter_actor_show_all (group);
  clutter_actor_show_all (CLUTTER_ACTOR (stage));

  gesture = clutter_gesture_new(CLUTTER_ACTOR(stage));
  clutter_gesture_set_gesture_mask(gesture, group, GESTURE_MASK_SLIDE | GESTURE_MASK_HOLD);
  g_signal_connect (gesture, "gesture-slide-event", G_CALLBACK (gesture_cb), (gpointer)0x11223344);
  g_signal_connect (gesture, "gesture-hold-event", G_CALLBACK (gesture_cb), (gpointer)0x11223344);

  clutter_main();

  return 0;
}