1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2000 Tor Lillqvist
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* A test program for the main loop and IO channel code.
21 * Just run it. Optional parameter is number of sub-processes.
24 #undef G_DISABLE_ASSERT
42 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
50 static GMainLoop
*main_loop
;
52 #define BUFSIZE 5000 /* Larger than the circular buffer in
53 * giowin32.c on purpose.
72 GIOError error
= G_IO_ERROR_NONE
;
75 /* g_io_channel_read() doesn't necessarily return all the
76 * data we want at once.
81 error
= g_io_channel_read (channel
, bufp
, left
, &nb
);
83 if (error
!= G_IO_ERROR_NONE
)
85 g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd
,
86 (error
== G_IO_ERROR_AGAIN
? "AGAIN" :
87 (error
== G_IO_ERROR_INVAL
? "INVAL" :
88 (error
== G_IO_ERROR_UNKNOWN
? "UNKNOWN" : "???"))));
89 if (error
== G_IO_ERROR_AGAIN
)
103 shutdown_source (gpointer data
)
105 if (g_source_remove (*(guint
*) data
))
109 g_main_loop_quit (main_loop
);
114 recv_message (GIOChannel
*channel
,
118 gint fd
= g_io_channel_unix_get_fd (channel
);
119 gboolean retval
= TRUE
;
122 g_print ("gio-test: ...from %d:%s%s%s%s\n", fd
,
123 (cond
& G_IO_ERR
) ? " ERR" : "",
124 (cond
& G_IO_HUP
) ? " HUP" : "",
125 (cond
& G_IO_IN
) ? " IN" : "",
126 (cond
& G_IO_PRI
) ? " PRI" : "");
129 if (cond
& (G_IO_ERR
| G_IO_HUP
))
131 shutdown_source (data
);
143 error
= read_all (fd
, channel
, (gchar
*) &seq
, sizeof (seq
), &nb
);
144 if (error
== G_IO_ERROR_NONE
)
149 g_print ("gio-test: ...from %d: EOF\n", fd
);
151 shutdown_source (data
);
155 g_assert (nb
== sizeof (nbytes
));
157 for (i
= 0; i
< nkiddies
; i
++)
158 if (seqtab
[i
].fd
== fd
)
160 if (seq
!= seqtab
[i
].seq
)
162 g_print ("gio-test: ...from %d: invalid sequence number %d, expected %d\n",
163 fd
, seq
, seqtab
[i
].seq
);
164 g_assert_not_reached ();
170 error
= read_all (fd
, channel
, (gchar
*) &nbytes
, sizeof (nbytes
), &nb
);
173 if (error
!= G_IO_ERROR_NONE
)
179 g_print ("gio-test: ...from %d: EOF\n", fd
);
181 shutdown_source (data
);
185 g_assert (nb
== sizeof (nbytes
));
187 if (nbytes
>= BUFSIZE
)
189 g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd
, nbytes
, nbytes
);
190 g_assert_not_reached ();
192 g_assert (nbytes
>= 0 && nbytes
< BUFSIZE
);
194 g_print ("gio-test: ...from %d: %d bytes\n", fd
, nbytes
);
198 error
= read_all (fd
, channel
, buf
, nbytes
, &nb
);
200 if (error
!= G_IO_ERROR_NONE
)
206 g_print ("gio-test: ...from %d: EOF\n", fd
);
208 shutdown_source (data
);
212 for (j
= 0; j
< nbytes
; j
++)
213 if (buf
[j
] != ' ' + ((nbytes
+ j
) % 95))
215 g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
216 fd
, j
, buf
[j
], 'a' + ((nbytes
+ j
) % 32));
217 g_assert_not_reached ();
220 g_print ("gio-test: ...from %d: OK\n", fd
);
230 recv_windows_message (GIOChannel
*channel
,
240 error
= g_io_channel_read (channel
, &msg
, sizeof (MSG
), &nb
);
242 if (error
!= G_IO_ERROR_NONE
)
244 g_print ("gio-test: ...reading Windows message: G_IO_ERROR_%s\n",
245 (error
== G_IO_ERROR_AGAIN
? "AGAIN" :
246 (error
== G_IO_ERROR_INVAL
? "INVAL" :
247 (error
== G_IO_ERROR_UNKNOWN
? "UNKNOWN" : "???"))));
248 if (error
== G_IO_ERROR_AGAIN
)
254 g_print ("gio-test: ...Windows message for %#x: %d,%d,%d\n",
255 msg
.hwnd
, msg
.message
, msg
.wParam
, msg
.lParam
);
261 window_procedure (HWND hwnd
,
266 g_print ("gio-test: window_procedure for %#x: %d,%d,%d\n",
267 hwnd
, message
, wparam
, lparam
);
268 return DefWindowProc (hwnd
, message
, wparam
, lparam
);
281 GIOChannel
*my_read_channel
;
292 GIOChannel
*windows_messages_channel
;
295 nkiddies
= (argc
== 1 ? 1 : atoi(argv
[1]));
296 seqtab
= g_malloc (nkiddies
* 2 * sizeof (int));
300 wcl
.lpfnWndProc
= window_procedure
;
303 wcl
.hInstance
= GetModuleHandle (NULL
);
306 wcl
.hbrBackground
= NULL
;
307 wcl
.lpszMenuName
= NULL
;
308 wcl
.lpszClassName
= "gio-test";
310 klass
= RegisterClass (&wcl
);
314 g_print ("gio-test: RegisterClass failed\n");
318 hwnd
= CreateWindow (MAKEINTATOM(klass
), "gio-test", 0, 0, 0, 10, 10,
319 NULL
, NULL
, wcl
.hInstance
, NULL
);
322 g_print ("gio-test: CreateWindow failed\n");
326 windows_messages_channel
= g_io_channel_win32_new_messages ((guint
)hwnd
);
327 g_io_add_watch (windows_messages_channel
, G_IO_IN
, recv_windows_message
, 0);
330 for (i
= 0; i
< nkiddies
; i
++)
332 int pipe_to_sub
[2], pipe_from_sub
[2];
334 if (pipe (pipe_to_sub
) == -1 ||
335 pipe (pipe_from_sub
) == -1)
336 perror ("pipe"), exit (1);
338 seqtab
[i
].fd
= pipe_from_sub
[0];
341 my_read_channel
= g_io_channel_unix_new (pipe_from_sub
[0]);
343 id
= g_new (guint
, 1);
345 g_io_add_watch (my_read_channel
,
346 G_IO_IN
| G_IO_PRI
| G_IO_ERR
| G_IO_HUP
,
353 cmdline
= g_strdup_printf ("%d:%d:%d",
357 _spawnl (_P_NOWAIT
, argv
[0], argv
[0], "--child", cmdline
, NULL
);
359 cmdline
= g_strdup_printf ("%s --child %d:%d &", argv
[0],
360 pipe_to_sub
[0], pipe_from_sub
[1]);
364 close (pipe_to_sub
[0]);
365 close (pipe_from_sub
[1]);
368 g_get_current_time (&start
);
369 g_io_channel_win32_make_pollfd (my_read_channel
, G_IO_IN
, &pollfd
);
370 pollresult
= g_io_channel_win32_poll (&pollfd
, 1, 100);
371 g_get_current_time (&end
);
372 if (end
.tv_usec
< start
.tv_usec
)
373 end
.tv_sec
--, end
.tv_usec
+= 1000000;
374 g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
375 end
.tv_sec
- start
.tv_sec
,
376 (end
.tv_usec
- start
.tv_usec
) / 1000,
381 main_loop
= g_main_loop_new (NULL
, FALSE
);
383 g_main_loop_run (main_loop
);
399 g_get_current_time (&tv
);
401 sscanf (argv
[2], "%d:%d%n", &readfd
, &writefd
, &n
);
404 sscanf (argv
[2] + n
, ":%d", &hwnd
);
407 srand (tv
.tv_sec
^ (tv
.tv_usec
/ 1000) ^ readfd
^ (writefd
<< 4));
409 for (i
= 0; i
< 20 + rand() % 20; i
++)
411 g_usleep (100 + (rand() % 10) * 5000);
412 buflen
= rand() % BUFSIZE
;
413 for (j
= 0; j
< buflen
; j
++)
414 buf
[j
] = ' ' + ((buflen
+ j
) % 95);
416 g_print ("gio-test: child writing %d+%d bytes to %d\n",
417 (int)(sizeof(i
) + sizeof(buflen
)), buflen
, writefd
);
419 write (writefd
, &i
, sizeof (i
));
420 write (writefd
, &buflen
, sizeof (buflen
));
421 write (writefd
, buf
, buflen
);
424 if (rand() % 100 < 5)
426 int msg
= WM_USER
+ (rand() % 100);
427 WPARAM wparam
= rand ();
428 LPARAM lparam
= rand ();
429 g_print ("gio-test: child posting message %d,%d,%d to %#x\n",
430 msg
, wparam
, lparam
, hwnd
);
431 PostMessage (hwnd
, msg
, wparam
, lparam
);
436 g_print ("gio-test: child exiting, closing %d\n", writefd
);