summaryrefslogtreecommitdiff
path: root/tests/testclientmessage.c
blob: bd1a0971089b78b79beb461589b36ddd8e7b4915 (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
/* testclientmessage.c
 * Copyright (C) 2008  Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gtk/gtk.h>

static GdkAtom my_type;
static GdkAtom random_type;

static void
send_known (void)
{
  GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
  static int counter = 42;
  int i;
  
  event->client.window = NULL;
  event->client.message_type = my_type;
  event->client.data_format = 32;
  event->client.data.l[0] = counter++;
  for (i = 1; i < 5; i++)
    event->client.data.l[i] = 0;

  gdk_screen_broadcast_client_message (gdk_display_get_default_screen (gdk_display_get_default ()), event);
  
  gdk_event_free (event);
}

void
send_random (void)
{
  GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
  static int counter = 1;
  int i;
  
  event->client.window = NULL;
  event->client.message_type = random_type;
  event->client.data_format = 32;
  event->client.data.l[0] = counter++;
  for (i = 1; i < 5; i++)
    event->client.data.l[i] = 0;

  gdk_screen_broadcast_client_message (gdk_display_get_default_screen (gdk_display_get_default ()), event);
  
  gdk_event_free (event);
}

static GdkFilterReturn
filter_func (GdkXEvent *xevent,
	     GdkEvent  *event,
	     gpointer   data)
{
  g_print ("Got matching client message!\n");
  return GDK_FILTER_REMOVE;
}

int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *button;

  gtk_init (&argc, &argv);

  my_type = gdk_atom_intern ("GtkTestClientMessage", FALSE);
  random_type = gdk_atom_intern (g_strdup_printf ("GtkTestClientMessage-%d",
						  g_rand_int_range (g_rand_new (), 1, 99)),
				 FALSE);

  g_print ("using random client message type %s\n", gdk_atom_name (random_type));

  window = g_object_connect (g_object_new (gtk_window_get_type (),
					   "type", GTK_WINDOW_TOPLEVEL,
					   "title", "testclientmessage",
					   "border_width", 10,
					   NULL),
			     "signal::destroy", gtk_main_quit, NULL,
			     NULL);
  vbox = g_object_new (gtk_vbox_get_type (),
		       "GtkWidget::parent", window,
		       "GtkWidget::visible", TRUE,
		       NULL);
  button = g_object_connect (g_object_new (gtk_button_get_type (),
					   "GtkButton::label", "send known client message",
					   "GtkWidget::parent", vbox,
					   "GtkWidget::visible", TRUE,
					   NULL),
			     "signal::clicked", send_known, NULL,
			     NULL);
  button = g_object_connect (g_object_new (gtk_button_get_type (),
					   "GtkButton::label", "send random client message",
					   "GtkWidget::parent", vbox,
					   "GtkWidget::visible", TRUE,
					   NULL),
			     "signal::clicked", send_random, NULL,
			     NULL);
  gdk_display_add_client_message_filter (gdk_display_get_default (),
					 my_type,
					 filter_func,
					 NULL);
  gtk_widget_show (window);

  gtk_main ();

  return 0;
}