testsocket.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
/* testsocket.c
 * Copyright (C) 2001 Red Hat, Inc
 * Author: Owen Taylor
 *
 * 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
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19
 */

20
#include "config.h"
21
#include <gtk/gtk.h>
22
#include <gtk/gtkx.h>
23 24

#include <string.h>
25 26
#include <stdlib.h>
#include <stdio.h>
27 28

int n_children = 0;
29

30 31
GSList *sockets = NULL;

32
GtkWidget *window;
33
GtkWidget *box;
34 35 36 37 38 39 40

typedef struct 
{
  GtkWidget *box;
  GtkWidget *frame;
  GtkWidget *socket;
} Socket;
41

42 43 44
extern guint32 create_child_plug (guint32  xid,
				  gboolean local);

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static void
quit_cb (gpointer        callback_data,
	 guint           callback_action,
	 GtkWidget      *widget)
{
  GtkWidget *message_dialog = gtk_message_dialog_new (GTK_WINDOW (window), 0,
						      GTK_MESSAGE_QUESTION,
						      GTK_BUTTONS_YES_NO,
						      "Really Quit?");
  gtk_dialog_set_default_response (GTK_DIALOG (message_dialog), GTK_RESPONSE_NO);

  if (gtk_dialog_run (GTK_DIALOG (message_dialog)) == GTK_RESPONSE_YES)
    gtk_widget_destroy (window);

  gtk_widget_destroy (message_dialog);
}

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
static void
socket_destroyed (GtkWidget *widget,
		  Socket    *socket)
{
  sockets = g_slist_remove (sockets, socket);
  g_free (socket);
}

static void
plug_added (GtkWidget *widget,
	    Socket    *socket)
{
  g_print ("Plug added to socket\n");
  
  gtk_widget_show (socket->socket);
  gtk_widget_hide (socket->frame);
}

static gboolean
plug_removed (GtkWidget *widget,
	      Socket    *socket)
{
  g_print ("Plug removed from socket\n");
  
  gtk_widget_hide (socket->socket);
  gtk_widget_show (socket->frame);
  
  return TRUE;
}

static Socket *
create_socket (void)
{
  GtkWidget *label;
  
  Socket *socket = g_new (Socket, 1);
  
99
  socket->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

  socket->socket = gtk_socket_new ();
  
  gtk_box_pack_start (GTK_BOX (socket->box), socket->socket, TRUE, TRUE, 0);
  
  socket->frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (socket->frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (socket->box), socket->frame, TRUE, TRUE, 0);
  gtk_widget_show (socket->frame);
  
  label = gtk_label_new (NULL);
  gtk_label_set_markup (GTK_LABEL (label), "<span color=\"red\">Empty</span>");
  gtk_container_add (GTK_CONTAINER (socket->frame), label);
  gtk_widget_show (label);

  sockets = g_slist_prepend (sockets, socket);


Manish Singh's avatar
Manish Singh committed
118
  g_signal_connect (socket->socket, "destroy",
119
		    G_CALLBACK (socket_destroyed), socket);
Manish Singh's avatar
Manish Singh committed
120
  g_signal_connect (socket->socket, "plug_added",
121
		    G_CALLBACK (plug_added), socket);
Manish Singh's avatar
Manish Singh committed
122
  g_signal_connect (socket->socket, "plug_removed",
123 124 125 126 127
		    G_CALLBACK (plug_removed), socket);

  return socket;
}

128 129 130
void
remove_child (GtkWidget *window)
{
131 132 133 134 135
  if (sockets)
    {
      Socket *socket = sockets->data;
      gtk_widget_destroy (socket->box);
    }
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
static gboolean
child_read_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
{
  GIOStatus status;
  GError *error = NULL;
  char *line;
  gsize term;
  int xid;
  
  status = g_io_channel_read_line (channel, &line, NULL, &term, &error);
  switch (status)
    {
    case G_IO_STATUS_NORMAL:
      line[term] = '\0';
      xid = strtol (line, NULL, 0);
      if (xid == 0)
	{
	  fprintf (stderr, "Invalid window id '%s'\n", line);
	}
      else
	{
159
	  Socket *socket = create_socket ();
160
	  gtk_box_pack_start (GTK_BOX (box), socket->box, TRUE, TRUE, 0);
161
	  gtk_widget_show (socket->box);
162
	  
163
	  gtk_socket_add_id (GTK_SOCKET (socket->socket), xid);
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	}
      g_free (line);
      return TRUE;
    case G_IO_STATUS_AGAIN:
      return TRUE;
    case G_IO_STATUS_EOF:
      n_children--;
      return FALSE;
    case G_IO_STATUS_ERROR:
      fprintf (stderr, "Error reading fd from child: %s\n", error->message);
      exit (1);
      return FALSE;
    default:
      g_assert_not_reached ();
      return FALSE;
    }
  
}

183
void
184 185
add_child (GtkWidget *window,
	   gboolean   active)
186
{
187
  Socket *socket;
188 189
  char *argv[3] = { "./testsocket_child", NULL, NULL };
  char buffer[20];
190 191
  int out_fd;
  GIOChannel *channel;
192 193
  GError *error = NULL;

194 195
  if (active)
    {
196
      socket = create_socket ();
197
      gtk_box_pack_start (GTK_BOX (box), socket->box, TRUE, TRUE, 0);
198
      gtk_widget_show (socket->box);
199
      sprintf(buffer, "%#lx", (gulong) gtk_socket_get_id (GTK_SOCKET (socket->socket)));
200 201
      argv[1] = buffer;
    }
202
  
203
  if (!g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, &out_fd, NULL, &error))
204 205 206 207
    {
      fprintf (stderr, "Can't exec testsocket_child: %s\n", error->message);
      exit (1);
    }
208 209 210

  n_children++;
  channel = g_io_channel_unix_new (out_fd);
Manish Singh's avatar
Manish Singh committed
211
  g_io_channel_set_close_on_unref (channel, TRUE);
212 213 214 215 216 217 218 219
  g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error);
  if (error)
    {
      fprintf (stderr, "Error making channel non-blocking: %s\n", error->message);
      exit (1);
    }
  
  g_io_add_watch (channel, G_IO_IN | G_IO_HUP, child_read_watch, NULL);
220
  g_io_channel_unref (channel);
221 222
}

223 224 225 226 227 228 229 230 231 232 233 234 235
void
add_active_child (GtkWidget *window)
{
  add_child (window, TRUE);
}

void
add_passive_child (GtkWidget *window)
{
  add_child (window, FALSE);
}

void
236
add_local_active_child (GtkWidget *window)
237
{
238
  Socket *socket;
239

240
  socket = create_socket ();
241
  gtk_box_pack_start (GTK_BOX (box), socket->box, TRUE, TRUE, 0);
242
  gtk_widget_show (socket->box);
243

244
  create_child_plug (gtk_socket_get_id (GTK_SOCKET (socket->socket)), TRUE);
245 246
}

247 248 249 250
void
add_local_passive_child (GtkWidget *window)
{
  Socket *socket;
251
  Window xid;
252 253

  socket = create_socket ();
254
  gtk_box_pack_start (GTK_BOX (box), socket->box, TRUE, TRUE, 0);
255 256 257 258 259 260
  gtk_widget_show (socket->box);

  xid = create_child_plug (0, TRUE);
  gtk_socket_add_id (GTK_SOCKET (socket->socket), xid);
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static const char *
grab_string (int status)
{
  switch (status) {
  case GDK_GRAB_SUCCESS:          return "GrabSuccess";
  case GDK_GRAB_ALREADY_GRABBED:  return "AlreadyGrabbed";
  case GDK_GRAB_INVALID_TIME:     return "GrabInvalidTime";
  case GDK_GRAB_NOT_VIEWABLE:     return "GrabNotViewable";
  case GDK_GRAB_FROZEN:           return "GrabFrozen";
  default:
    {
      static char foo [255];
      sprintf (foo, "unknown status: %d", status);
      return foo;
    }
  }
}

static void
grab_window_toggled (GtkToggleButton *button,
		     GtkWidget       *widget)
{
283
  GdkDevice *device = gtk_get_current_event_device ();
284
  GdkSeat *seat = gdk_device_get_seat (device);
285 286 287 288 289

  if (gtk_toggle_button_get_active (button))
    {
      int status;

290 291 292
      status = gdk_seat_grab (seat, gtk_widget_get_window (widget),
                              GDK_SEAT_CAPABILITY_KEYBOARD,
                              FALSE, NULL, NULL, NULL, NULL);
293 294 295 296

      if (status != GDK_GRAB_SUCCESS)
	g_warning ("Could not grab keyboard!  (%s)", grab_string (status));

297 298
    }
  else
299
    {
300
      gdk_seat_ungrab (seat);
301 302 303
    }
}

304 305 306 307 308
int
main (int argc, char *argv[])
{
  GtkWidget *button;
  GtkWidget *hbox;
309
  GtkWidget *vbox;
310 311 312
  GtkWidget *menubar;
  GtkWidget *menuitem;
  GtkWidget *menu;
313
  GtkWidget *entry;
314
  GtkWidget *checkbutton;
315
  GtkAccelGroup *accel_group;
316 317 318 319

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
Manish Singh's avatar
Manish Singh committed
320 321
  g_signal_connect (window, "destroy",
		    G_CALLBACK (gtk_main_quit), NULL);
322 323 324 325
  
  gtk_window_set_title (GTK_WINDOW (window), "Socket Test");
  gtk_container_set_border_width (GTK_CONTAINER (window), 0);

326
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
327 328
  gtk_container_add (GTK_CONTAINER (window), vbox);

329 330
  menubar = gtk_menu_bar_new ();
  menuitem = gtk_menu_item_new_with_mnemonic ("_File");
331 332
  gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);

333 334 335
  menu = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
  menuitem = gtk_menu_item_new_with_mnemonic ("_Quit");
336
  g_signal_connect (menuitem, "activate", G_CALLBACK (quit_cb), window);
337 338
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

339 340
  accel_group = gtk_accel_group_new ();
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
341
  gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
342

343
  button = gtk_button_new_with_label ("Add Active Child");
344 345
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);

Manish Singh's avatar
Manish Singh committed
346 347
  g_signal_connect_swapped (button, "clicked",
			    G_CALLBACK (add_active_child), vbox);
348 349 350 351

  button = gtk_button_new_with_label ("Add Passive Child");
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);

Manish Singh's avatar
Manish Singh committed
352 353
  g_signal_connect_swapped (button, "clicked",
			    G_CALLBACK (add_passive_child), vbox);
354

355 356 357
  button = gtk_button_new_with_label ("Add Local Active Child");
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);

Manish Singh's avatar
Manish Singh committed
358 359
  g_signal_connect_swapped (button, "clicked",
			    G_CALLBACK (add_local_active_child), vbox);
360 361

  button = gtk_button_new_with_label ("Add Local Passive Child");
362 363
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);

Manish Singh's avatar
Manish Singh committed
364 365
  g_signal_connect_swapped (button, "clicked",
			    G_CALLBACK (add_local_passive_child), vbox);
366 367 368 369

  button = gtk_button_new_with_label ("Remove Last Child");
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);

Manish Singh's avatar
Manish Singh committed
370 371
  g_signal_connect_swapped (button, "clicked",
			    G_CALLBACK (remove_child), vbox);
372

373 374 375 376 377 378 379
  checkbutton = gtk_check_button_new_with_label ("Grab keyboard");
  gtk_box_pack_start (GTK_BOX (vbox), checkbutton, FALSE, FALSE, 0);

  g_signal_connect (checkbutton, "toggled",
		    G_CALLBACK (grab_window_toggled),
		    window);

380
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
381 382 383 384 385
  gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX(hbox), entry, FALSE, FALSE, 0);

386
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
387 388 389 390
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  box = hbox;
  
391 392 393 394
  gtk_widget_show_all (window);

  gtk_main ();

395 396 397
  if (n_children)
    {
      g_print ("Waiting for children to exit\n");
398

399
      while (n_children)
Manish Singh's avatar
Manish Singh committed
400
	g_main_context_iteration (NULL, TRUE);
401
    }
402

403 404
  return 0;
}