1 /* GDK - The GIMP Drawing Kit
2 * gdkdisplay-broadway.c
4 * Copyright 2001 Sun Microsystems Inc.
5 * Copyright (C) 2004 Nokia Corporation
7 * Erwann Chenede <erwann.chenede@sun.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
27 #include "gdkdisplay-broadway.h"
29 #include "gdkdisplay.h"
30 #include "gdkeventsource.h"
31 #include "gdkscreen.h"
32 #include "gdkscreen-broadway.h"
33 #include "gdkinternals.h"
34 #include "gdkdeviceprivate.h"
35 #include "gdkdevicemanager-broadway.h"
38 #include <glib/gprintf.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/tcp.h>
48 static void gdk_broadway_display_dispose (GObject *object);
49 static void gdk_broadway_display_finalize (GObject *object);
52 #define DEBUG_WEBSOCKETS 1
55 G_DEFINE_TYPE (GdkBroadwayDisplay, gdk_broadway_display, GDK_TYPE_DISPLAY)
58 gdk_broadway_display_init (GdkBroadwayDisplay *display)
60 _gdk_broadway_display_manager_add_display (gdk_display_manager_get (),
61 GDK_DISPLAY_OBJECT (display));
62 display->id_ht = g_hash_table_new (NULL, NULL);
66 gdk_event_init (GdkDisplay *display)
68 GdkBroadwayDisplay *broadway_display;
70 broadway_display = GDK_BROADWAY_DISPLAY (display);
71 broadway_display->event_source = _gdk_broadway_event_source_new (display);
72 broadway_display->saved_serial = 1;
73 broadway_display->last_seen_time = 1;
77 gdk_broadway_display_init_input (GdkDisplay *display)
79 GdkBroadwayDisplay *broadway_display;
80 GdkDeviceManager *device_manager;
84 broadway_display = GDK_BROADWAY_DISPLAY (display);
85 device_manager = gdk_display_get_device_manager (display);
87 /* For backwards compatibility, just add
88 * floating devices that are not keyboards.
90 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
92 for (l = list; l; l = l->next)
96 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
99 broadway_display->input_devices = g_list_prepend (broadway_display->input_devices,
100 g_object_ref (l->data));
105 /* Now set "core" pointer to the first
106 * master device that is a pointer.
108 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
110 for (l = list; l; l = l->next)
114 if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
117 display->core_pointer = device;
121 /* Add the core pointer to the devices list */
122 broadway_display->input_devices = g_list_prepend (broadway_display->input_devices,
123 g_object_ref (display->core_pointer));
128 typedef struct HttpRequest {
130 GSocketConnection *connection;
131 GDataInputStream *data;
135 static void start_output (HttpRequest *request, gboolean proto_v7_plus);
138 http_request_free (HttpRequest *request)
140 g_object_unref (request->connection);
141 g_object_unref (request->data);
142 g_string_free (request->request, TRUE);
146 struct BroadwayInput {
148 GSocketConnection *connection;
153 gboolean proto_v7_plus;
157 broadway_input_free (BroadwayInput *input)
159 g_object_unref (input->connection);
160 g_byte_array_free (input->buffer, FALSE);
161 g_source_destroy (input->source);
166 process_input_messages (GdkBroadwayDisplay *broadway_display)
168 BroadwayInputMsg *message;
170 while (broadway_display->input_messages)
172 message = broadway_display->input_messages->data;
173 broadway_display->input_messages =
174 g_list_delete_link (broadway_display->input_messages,
175 broadway_display->input_messages);
177 _gdk_broadway_events_got_input (GDK_DISPLAY (broadway_display), message);
183 parse_pointer_data (char *p, BroadwayInputPointerMsg *data)
185 data->mouse_window_id = strtol (p, &p, 10);
187 data->event_window_id = strtol (p, &p, 10);
189 data->root_x = strtol (p, &p, 10);
191 data->root_y = strtol (p, &p, 10);
193 data->win_x = strtol (p, &p, 10);
195 data->win_y = strtol (p, &p, 10);
197 data->state = strtol (p, &p, 10);
203 update_future_pointer_info (GdkBroadwayDisplay *broadway_display, BroadwayInputPointerMsg *data)
205 broadway_display->future_root_x = data->root_x;
206 broadway_display->future_root_y = data->root_y;
207 broadway_display->future_state = data->state;
208 broadway_display->future_mouse_in_toplevel = data->mouse_window_id;
212 parse_input_message (BroadwayInput *input, const char *message)
214 GdkBroadwayDisplay *broadway_display;
215 BroadwayInputMsg msg;
219 broadway_display = GDK_BROADWAY_DISPLAY (input->display);
222 msg.base.type = *p++;
223 msg.base.serial = (guint32)strtol (p, &p, 10);
225 time_ = strtol(p, &p, 10);
229 time_ = broadway_display->last_seen_time;
231 if (!input->seen_time) {
232 input->seen_time = TRUE;
233 /* Calculate time base so that any following times are normalized to start
234 5 seconds after last_seen_time, to avoid issues that could appear when
235 a long hiatus due to a reconnect seems to be instant */
236 input->time_base = time_ - (broadway_display->last_seen_time + 5000);
238 time_ = time_ - input->time_base;
241 broadway_display->last_seen_time = time_;
243 msg.base.time = time_;
245 switch (msg.base.type) {
246 case 'e': /* Enter */
247 case 'l': /* Leave */
248 p = parse_pointer_data (p, &msg.pointer);
249 update_future_pointer_info (broadway_display, &msg.pointer);
251 msg.crossing.mode = strtol(p, &p, 10);
254 case 'm': /* Mouse move */
255 p = parse_pointer_data (p, &msg.pointer);
256 update_future_pointer_info (broadway_display, &msg.pointer);
261 p = parse_pointer_data (p, &msg.pointer);
262 update_future_pointer_info (broadway_display, &msg.pointer);
264 msg.button.button = strtol(p, &p, 10);
268 p = parse_pointer_data (p, &msg.pointer);
269 update_future_pointer_info (broadway_display, &msg.pointer);
271 msg.scroll.dir = strtol(p, &p, 10);
276 msg.key.key = strtol(p, &p, 10);
278 msg.key.state = strtol(p, &p, 10);
283 msg.grab_reply.res = strtol(p, &p, 10);
287 msg.configure_notify.id = strtol(p, &p, 10);
289 msg.configure_notify.x = strtol (p, &p, 10);
291 msg.configure_notify.y = strtol (p, &p, 10);
293 msg.configure_notify.width = strtol (p, &p, 10);
295 msg.configure_notify.height = strtol (p, &p, 10);
299 msg.delete_notify.id = strtol(p, &p, 10);
303 msg.screen_resize_notify.width = strtol (p, &p, 10);
305 msg.screen_resize_notify.height = strtol (p, &p, 10);
309 g_printerr ("Unknown input command %s\n", message);
313 broadway_display->input_messages = g_list_append (broadway_display->input_messages, g_memdup (&msg, sizeof (msg)));
318 hex_dump (guchar *data, gsize len)
320 #ifdef DEBUG_WEBSOCKETS
322 for (j = 0; j < len + 15; j += 16)
324 fprintf (stderr, "0x%.4x ", j);
325 for (i = 0; i < 16; i++)
328 fprintf (stderr, "%.2x ", data[j+i]);
330 fprintf (stderr, " ");
332 fprintf (stderr, " ");
334 fprintf (stderr, " | ");
336 for (i = 0; i < 16; i++)
337 if ((j + i) < len && g_ascii_isalnum(data[j+i]))
338 fprintf (stderr, "%c", data[j+i]);
340 fprintf (stderr, ".");
341 fprintf (stderr, "\n");
347 parse_input (BroadwayInput *input)
349 GdkBroadwayDisplay *broadway_display;
351 broadway_display = GDK_BROADWAY_DISPLAY (input->display);
353 if (!input->buffer->len)
356 if (input->proto_v7_plus)
358 hex_dump (input->buffer->data, input->buffer->len);
360 while (input->buffer->len > 2)
362 gsize len, payload_len;
363 BroadwayWSOpCode code;
364 gboolean is_mask, fin;
365 guchar *buf, *data, *mask;
367 buf = input->buffer->data;
368 len = input->buffer->len;
370 #ifdef DEBUG_WEBSOCKETS
371 g_print ("Parse input first byte 0x%2x 0x%2x\n", buf[0], buf[1]);
375 code = buf[0] & 0x0f;
376 payload_len = buf[1] & 0x7f;
377 is_mask = buf[1] & 0x80;
380 if (payload_len > 125)
384 payload_len = GUINT16_FROM_BE( *(guint16 *) data );
387 else if (payload_len > 126)
391 payload_len = GUINT64_FROM_BE( *(guint64 *) data );
398 if (data - buf + 4 > len)
404 if (data - buf + payload_len > len)
405 return; /* wait to accumulate more */
410 for (i = 0; i < payload_len; i++)
411 data[i] ^= mask[i%4];
415 case BROADWAY_WS_CNX_CLOSE:
416 break; /* hang around anyway */
417 case BROADWAY_WS_TEXT:
420 #ifdef DEBUG_WEBSOCKETS
421 g_warning ("can't yet accept fragmented input");
426 char *terminated = g_strndup((char *)data, payload_len);
427 parse_input_message (input, terminated);
431 case BROADWAY_WS_CNX_PING:
432 broadway_output_pong (broadway_display->output);
434 case BROADWAY_WS_CNX_PONG:
435 break; /* we never send pings, but tolerate pongs */
436 case BROADWAY_WS_BINARY:
437 case BROADWAY_WS_CONTINUATION:
440 g_warning ("fragmented or unknown input code 0x%2x with fin set", code);
445 g_byte_array_remove_range (input->buffer, 0, data - buf + payload_len);
448 else /* old style protocol */
453 buf = (char *)input->buffer->data;
454 len = input->buffer->len;
458 broadway_display->input = NULL;
459 broadway_input_free (input);
463 while ((ptr = memchr (buf, 0xff, len)) != NULL)
468 parse_input_message (input, buf + 1);
473 if (len > 0 && buf[0] != 0)
475 broadway_display->input = NULL;
476 broadway_input_free (input);
480 g_byte_array_remove_range (input->buffer, 0, buf - (char *)input->buffer->data);
486 process_input_idle_cb (GdkBroadwayDisplay *display)
488 display->process_input_idle = 0;
489 process_input_messages (display);
490 return G_SOURCE_REMOVE;
494 queue_process_input_at_idle (GdkBroadwayDisplay *broadway_display)
496 if (broadway_display->process_input_idle == 0)
497 broadway_display->process_input_idle =
498 g_idle_add_full (GDK_PRIORITY_EVENTS, (GSourceFunc)process_input_idle_cb, broadway_display, NULL);
502 _gdk_broadway_display_read_all_input_nonblocking (GdkDisplay *display)
504 GdkBroadwayDisplay *broadway_display;
509 BroadwayInput *input;
511 broadway_display = GDK_BROADWAY_DISPLAY (display);
512 if (broadway_display->input == NULL)
515 input = broadway_display->input;
517 in = g_io_stream_get_input_stream (G_IO_STREAM (input->connection));
520 res = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (in),
521 buffer, sizeof (buffer), NULL, &error);
526 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
528 g_error_free (error);
532 broadway_display->input = NULL;
533 broadway_input_free (input);
536 g_print ("input error %s\n", error->message);
537 g_error_free (error);
542 g_byte_array_append (input->buffer, buffer, res);
548 _gdk_broadway_display_consume_all_input (GdkDisplay *display)
550 GdkBroadwayDisplay *broadway_display;
552 broadway_display = GDK_BROADWAY_DISPLAY (display);
553 _gdk_broadway_display_read_all_input_nonblocking (display);
555 /* Since we're parsing input but not processing the resulting messages
556 we might not get a readable callback on the stream, so queue an idle to
557 process the messages */
558 queue_process_input_at_idle (broadway_display);
563 input_data_cb (GObject *stream,
564 BroadwayInput *input)
566 GdkBroadwayDisplay *broadway_display;
568 broadway_display = GDK_BROADWAY_DISPLAY (input->display);
569 _gdk_broadway_display_read_all_input_nonblocking (input->display);
571 process_input_messages (broadway_display);
576 /* Note: This may be called while handling a message (i.e. sorta recursively) */
578 _gdk_broadway_display_block_for_input (GdkDisplay *display, char op,
579 guint32 serial, gboolean remove_message)
581 GdkBroadwayDisplay *broadway_display;
582 BroadwayInputMsg *message;
585 BroadwayInput *input;
589 gdk_display_flush (display);
591 broadway_display = GDK_BROADWAY_DISPLAY (display);
592 if (broadway_display->input == NULL)
595 input = broadway_display->input;
598 /* Check for existing reply in queue */
600 for (l = broadway_display->input_messages; l != NULL; l = l->next)
604 if (message->base.type == op)
606 if (message->base.serial == serial)
609 broadway_display->input_messages =
610 g_list_delete_link (broadway_display->input_messages, l);
616 /* Not found, read more, blocking */
618 in = g_io_stream_get_input_stream (G_IO_STREAM (input->connection));
619 res = g_input_stream_read (in, buffer, sizeof (buffer), NULL, NULL);
622 g_byte_array_append (input->buffer, buffer, res);
626 /* Since we're parsing input but not processing the resulting messages
627 we might not get a readable callback on the stream, so queue an idle to
628 process the messages */
629 queue_process_input_at_idle (broadway_display);
634 parse_line (char *line, char *key)
638 if (!g_str_has_prefix (line, key))
640 p = line + strlen (key);
644 /* Skip optional initial space */
650 send_error (HttpRequest *request,
656 res = g_strdup_printf ("HTTP/1.0 %d %s\r\n\r\n"
657 "<html><head><title>%d %s</title></head>"
658 "<body>%s</body></html>",
662 /* TODO: This should really be async */
663 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
664 res, strlen (res), NULL, NULL, NULL);
666 http_request_free (request);
669 /* magic from: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17 */
670 #define SEC_WEB_SOCKET_KEY_MAGIC "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
672 /* 'x3JJHMbDL1EzLkh9GBhXDw==' generates 'HSmrc0sMlYUkAGmm5OPpG2HaGWk=' */
674 generate_handshake_response_wsietf_v7 (const gchar *key)
676 gsize digest_len = 20;
677 guchar digest[digest_len];
680 checksum = g_checksum_new (G_CHECKSUM_SHA1);
684 g_checksum_update (checksum, (guchar *)key, -1);
685 g_checksum_update (checksum, (guchar *)SEC_WEB_SOCKET_KEY_MAGIC, -1);
687 g_checksum_get_digest (checksum, digest, &digest_len);
688 g_checksum_free (checksum);
690 g_assert (digest_len == 20);
692 return g_base64_encode (digest, digest_len);
696 start_input (HttpRequest *request)
700 int num_key1, num_key2;
704 guint8 challenge[16];
709 GdkBroadwayDisplay *broadway_display;
710 BroadwayInput *input;
711 const void *data_buffer;
712 gsize data_buffer_size;
715 gboolean proto_v7_plus;
717 broadway_display = GDK_BROADWAY_DISPLAY (request->display);
719 if (broadway_display->input != NULL)
721 send_error (request, 409, "Input already handled");
725 #ifdef DEBUG_WEBSOCKETS
726 g_print ("incoming request:\n%s\n", request->request->str);
728 lines = g_strsplit (request->request->str, "\n", 0);
737 for (i = 0; lines[i] != NULL; i++)
739 if ((p = parse_line (lines[i], "Sec-WebSocket-Key1")))
744 if (g_ascii_isdigit (*p))
745 key1 = key1 * 10 + g_ascii_digit_value (*p);
754 else if ((p = parse_line (lines[i], "Sec-WebSocket-Key2")))
759 if (g_ascii_isdigit (*p))
760 key2 = key2 * 10 + g_ascii_digit_value (*p);
769 else if ((p = parse_line (lines[i], "Sec-WebSocket-Key")))
773 else if ((p = parse_line (lines[i], "Origin")))
777 else if ((p = parse_line (lines[i], "Host")))
781 else if ((p = parse_line (lines[i], "Sec-WebSocket-Origin")))
787 if (origin == NULL || host == NULL)
790 send_error (request, 400, "Bad websocket request");
796 char* accept = generate_handshake_response_wsietf_v7 (key_v7);
797 res = g_strdup_printf ("HTTP/1.1 101 Switching Protocols\r\n"
798 "Upgrade: websocket\r\n"
799 "Connection: Upgrade\r\n"
800 "Sec-WebSocket-Accept: %s\r\n"
801 "Sec-WebSocket-Origin: %s\r\n"
802 "Sec-WebSocket-Location: ws://%s/socket\r\n"
803 "Sec-WebSocket-Protocol: broadway\r\n"
804 "\r\n", accept, origin, host);
807 #ifdef DEBUG_WEBSOCKETS
808 g_print ("v7 proto response:\n%s", res);
811 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
812 res, strlen (res), NULL, NULL, NULL);
814 proto_v7_plus = TRUE;
818 if (num_key1 != 1 || num_key2 != 1)
821 send_error (request, 400, "Bad websocket request");
825 challenge[0] = (key1 >> 24) & 0xff;
826 challenge[1] = (key1 >> 16) & 0xff;
827 challenge[2] = (key1 >> 8) & 0xff;
828 challenge[3] = (key1 >> 0) & 0xff;
829 challenge[4] = (key2 >> 24) & 0xff;
830 challenge[5] = (key2 >> 16) & 0xff;
831 challenge[6] = (key2 >> 8) & 0xff;
832 challenge[7] = (key2 >> 0) & 0xff;
834 if (!g_input_stream_read_all (G_INPUT_STREAM (request->data), challenge+8, 8, NULL, NULL, NULL))
837 send_error (request, 400, "Bad websocket request");
841 checksum = g_checksum_new (G_CHECKSUM_MD5);
842 g_checksum_update (checksum, challenge, 16);
844 g_checksum_get_digest (checksum, challenge, &len);
845 g_checksum_free (checksum);
847 res = g_strdup_printf ("HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
848 "Upgrade: WebSocket\r\n"
849 "Connection: Upgrade\r\n"
850 "Sec-WebSocket-Origin: %s\r\n"
851 "Sec-WebSocket-Location: ws://%s/socket\r\n"
852 "Sec-WebSocket-Protocol: broadway\r\n"
856 #ifdef DEBUG_WEBSOCKETS
857 g_print ("legacy response:\n%s", res);
859 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
860 res, strlen (res), NULL, NULL, NULL);
862 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
863 challenge, 16, NULL, NULL, NULL);
864 proto_v7_plus = FALSE;
867 input = g_new0 (BroadwayInput, 1);
869 input->display = request->display;
870 input->connection = g_object_ref (request->connection);
871 input->proto_v7_plus = proto_v7_plus;
873 data_buffer = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (request->data), &data_buffer_size);
874 input->buffer = g_byte_array_sized_new (data_buffer_size);
875 g_byte_array_append (input->buffer, data_buffer, data_buffer_size);
877 broadway_display->input = input;
879 start_output (request, proto_v7_plus);
881 /* This will free and close the data input stream, but we got all the buffered content already */
882 http_request_free (request);
884 in = g_io_stream_get_input_stream (G_IO_STREAM (input->connection));
885 input->source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (in), NULL);
886 g_source_set_callback (input->source, (GSourceFunc)input_data_cb, input, NULL);
887 g_source_attach (input->source, NULL);
889 /* Process any data in the pipe already */
891 process_input_messages (broadway_display);
897 start_output (HttpRequest *request, gboolean proto_v7_plus)
900 GdkBroadwayDisplay *broadway_display;
903 socket = g_socket_connection_get_socket (request->connection);
904 setsockopt(g_socket_get_fd (socket), IPPROTO_TCP,
905 TCP_NODELAY, (char *) &flag, sizeof(int));
907 broadway_display = GDK_BROADWAY_DISPLAY (request->display);
909 if (broadway_display->output)
911 broadway_display->saved_serial = broadway_output_get_next_serial (broadway_display->output);
912 broadway_output_free (broadway_display->output);
915 broadway_display->output =
916 broadway_output_new (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
917 broadway_display->saved_serial, proto_v7_plus);
919 _gdk_broadway_resync_windows ();
921 if (broadway_display->pointer_grab_window)
922 broadway_output_grab_pointer (broadway_display->output,
923 GDK_WINDOW_IMPL_BROADWAY (broadway_display->pointer_grab_window->impl)->id,
924 broadway_display->pointer_grab_owner_events);
928 send_data (HttpRequest *request,
929 const char *mimetype,
930 const char *data, gsize len)
934 res = g_strdup_printf ("HTTP/1.0 200 OK\r\n"
935 "Content-Type: %s\r\n"
936 "Content-Length: %"G_GSIZE_FORMAT"\r\n"
939 /* TODO: This should really be async */
940 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
941 res, strlen (res), NULL, NULL, NULL);
943 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
944 data, len, NULL, NULL, NULL);
945 http_request_free (request);
948 #include "clienthtml.h"
949 #include "broadwayjs.h"
952 got_request (HttpRequest *request)
954 char *start, *escaped, *tmp, *version, *query;
956 if (!g_str_has_prefix (request->request->str, "GET "))
958 send_error (request, 501, "Only GET implemented");
962 start = request->request->str + 4; /* Skip "GET " */
964 while (*start == ' ')
967 for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
969 escaped = g_strndup (start, tmp - start);
974 while (*start == ' ')
976 for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
978 version = g_strndup (start, tmp - start);
981 query = strchr (escaped, '?');
985 if (strcmp (escaped, "/client.html") == 0 || strcmp (escaped, "/") == 0)
986 send_data (request, "text/html", client_html, G_N_ELEMENTS(client_html) - 1);
987 else if (strcmp (escaped, "/broadway.js") == 0)
988 send_data (request, "text/javascript", broadway_js, G_N_ELEMENTS(broadway_js) - 1);
989 else if (strcmp (escaped, "/socket") == 0)
990 start_input (request);
992 send_error (request, 404, "File not found");
999 got_http_request_line (GInputStream *stream,
1000 GAsyncResult *result,
1001 HttpRequest *request)
1005 line = g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM (stream), result, NULL, NULL);
1008 http_request_free (request);
1009 g_printerr ("Error reading request lines\n");
1012 if (strlen (line) == 0)
1013 got_request (request);
1016 /* Protect against overflow in request length */
1017 if (request->request->len > 1024 * 5)
1019 send_error (request, 400, "Request too long");
1023 g_string_append_printf (request->request, "%s\n", line);
1024 g_data_input_stream_read_line_async (request->data, 0, NULL,
1025 (GAsyncReadyCallback)got_http_request_line, request);
1032 handle_incoming_connection (GSocketService *service,
1033 GSocketConnection *connection,
1034 GObject *source_object)
1036 HttpRequest *request;
1039 request = g_new0 (HttpRequest, 1);
1040 request->connection = g_object_ref (connection);
1041 request->display = (GdkDisplay *) source_object;
1042 request->request = g_string_new ("");
1044 in = g_io_stream_get_input_stream (G_IO_STREAM (connection));
1046 request->data = g_data_input_stream_new (in);
1047 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (request->data), FALSE);
1048 /* Be tolerant of input */
1049 g_data_input_stream_set_newline_type (request->data, G_DATA_STREAM_NEWLINE_TYPE_ANY);
1051 g_data_input_stream_read_line_async (request->data, 0, NULL,
1052 (GAsyncReadyCallback)got_http_request_line, request);
1057 _gdk_broadway_display_open (const gchar *display_name)
1059 GdkDisplay *display;
1060 GdkBroadwayDisplay *broadway_display;
1064 display = g_object_new (GDK_TYPE_BROADWAY_DISPLAY, NULL);
1065 broadway_display = GDK_BROADWAY_DISPLAY (display);
1067 broadway_display->output = NULL;
1069 /* initialize the display's screens */
1070 broadway_display->screens = g_new (GdkScreen *, 1);
1071 broadway_display->screens[0] = _gdk_broadway_screen_new (display, 0);
1073 /* We need to initialize events after we have the screen
1074 * structures in places
1076 _gdk_broadway_screen_events_init (broadway_display->screens[0]);
1078 /*set the default screen */
1079 broadway_display->default_screen = broadway_display->screens[0];
1081 display->device_manager = _gdk_broadway_device_manager_new (display);
1083 gdk_event_init (display);
1085 gdk_broadway_display_init_input (display);
1086 _gdk_broadway_display_init_dnd (display);
1088 _gdk_broadway_screen_setup (broadway_display->screens[0]);
1090 if (display_name == NULL)
1091 display_name = g_getenv ("BROADWAY_DISPLAY");
1094 if (display_name != NULL)
1095 port = strtol(display_name, NULL, 10);
1099 broadway_display->service = g_socket_service_new ();
1100 if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (broadway_display->service),
1105 g_printerr ("Unable to listen to port %d: %s\n", 8080, error->message);
1106 g_error_free (error);
1109 g_signal_connect (broadway_display->service, "incoming", G_CALLBACK (handle_incoming_connection), NULL);
1111 g_signal_emit_by_name (display, "opened");
1112 g_signal_emit_by_name (gdk_display_manager_get (), "display-opened", display);
1118 static const gchar *
1119 gdk_broadway_display_get_name (GdkDisplay *display)
1121 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1123 return (gchar *) "Broadway";
1127 gdk_broadway_display_get_n_screens (GdkDisplay *display)
1129 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1135 gdk_broadway_display_get_screen (GdkDisplay *display,
1138 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1139 g_return_val_if_fail (screen_num == 0, NULL);
1141 return GDK_BROADWAY_DISPLAY (display)->screens[screen_num];
1145 gdk_broadway_display_get_default_screen (GdkDisplay *display)
1147 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1149 return GDK_BROADWAY_DISPLAY (display)->default_screen;
1153 gdk_broadway_display_beep (GdkDisplay *display)
1155 g_return_if_fail (GDK_IS_DISPLAY (display));
1159 gdk_broadway_display_sync (GdkDisplay *display)
1161 g_return_if_fail (GDK_IS_DISPLAY (display));
1166 gdk_broadway_display_flush (GdkDisplay *display)
1168 GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (display);
1170 g_return_if_fail (GDK_IS_DISPLAY (display));
1172 if (broadway_display->output &&
1173 !broadway_output_flush (broadway_display->output))
1175 broadway_display->saved_serial = broadway_output_get_next_serial (broadway_display->output);
1176 broadway_output_free (broadway_display->output);
1177 broadway_display->output = NULL;
1182 gdk_broadway_display_has_pending (GdkDisplay *display)
1188 gdk_broadway_display_get_default_group (GdkDisplay *display)
1190 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1196 gdk_broadway_display_dispose (GObject *object)
1198 GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (object);
1200 _gdk_broadway_display_manager_remove_display (gdk_display_manager_get (),
1201 GDK_DISPLAY_OBJECT (object));
1203 g_list_foreach (broadway_display->input_devices, (GFunc) g_object_run_dispose, NULL);
1205 _gdk_screen_close (broadway_display->screens[0]);
1207 if (broadway_display->event_source)
1209 g_source_destroy (broadway_display->event_source);
1210 g_source_unref (broadway_display->event_source);
1211 broadway_display->event_source = NULL;
1214 G_OBJECT_CLASS (gdk_broadway_display_parent_class)->dispose (object);
1218 gdk_broadway_display_finalize (GObject *object)
1220 GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (object);
1223 if (broadway_display->keymap)
1224 g_object_unref (broadway_display->keymap);
1226 _gdk_broadway_cursor_display_finalize (GDK_DISPLAY_OBJECT(broadway_display));
1228 /* input GdkDevice list */
1229 g_list_free_full (broadway_display->input_devices, g_object_unref);
1230 /* Free all GdkScreens */
1231 g_object_unref (broadway_display->screens[0]);
1232 g_free (broadway_display->screens);
1234 G_OBJECT_CLASS (gdk_broadway_display_parent_class)->finalize (object);
1238 _gdk_broadway_display_make_default (GdkDisplay *display)
1243 gdk_broadway_display_notify_startup_complete (GdkDisplay *display,
1244 const gchar *startup_id)
1249 gdk_broadway_display_supports_selection_notification (GdkDisplay *display)
1255 gdk_broadway_display_request_selection_notification (GdkDisplay *display,
1263 gdk_broadway_display_supports_clipboard_persistence (GdkDisplay *display)
1269 gdk_broadway_display_store_clipboard (GdkDisplay *display,
1270 GdkWindow *clipboard_window,
1272 const GdkAtom *targets,
1278 gdk_broadway_display_supports_shapes (GdkDisplay *display)
1284 gdk_broadway_display_supports_input_shapes (GdkDisplay *display)
1290 gdk_broadway_display_supports_composite (GdkDisplay *display)
1296 gdk_broadway_display_list_devices (GdkDisplay *display)
1298 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1300 return GDK_BROADWAY_DISPLAY (display)->input_devices;
1304 gdk_broadway_display_get_next_serial (GdkDisplay *display)
1306 GdkBroadwayDisplay *broadway_display;
1307 broadway_display = GDK_BROADWAY_DISPLAY (display);
1308 if (broadway_display->output)
1309 return broadway_output_get_next_serial (broadway_display->output);
1310 return broadway_display->saved_serial;
1315 gdk_broadway_display_event_data_copy (GdkDisplay *display,
1316 const GdkEvent *src,
1322 gdk_broadway_display_event_data_free (GdkDisplay *display,
1328 gdk_broadway_display_class_init (GdkBroadwayDisplayClass * class)
1330 GObjectClass *object_class = G_OBJECT_CLASS (class);
1331 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
1333 object_class->dispose = gdk_broadway_display_dispose;
1334 object_class->finalize = gdk_broadway_display_finalize;
1336 display_class->window_type = GDK_TYPE_BROADWAY_WINDOW;
1338 display_class->get_name = gdk_broadway_display_get_name;
1339 display_class->get_n_screens = gdk_broadway_display_get_n_screens;
1340 display_class->get_screen = gdk_broadway_display_get_screen;
1341 display_class->get_default_screen = gdk_broadway_display_get_default_screen;
1342 display_class->beep = gdk_broadway_display_beep;
1343 display_class->sync = gdk_broadway_display_sync;
1344 display_class->flush = gdk_broadway_display_flush;
1345 display_class->has_pending = gdk_broadway_display_has_pending;
1346 display_class->queue_events = _gdk_broadway_display_queue_events;
1347 display_class->get_default_group = gdk_broadway_display_get_default_group;
1348 display_class->supports_selection_notification = gdk_broadway_display_supports_selection_notification;
1349 display_class->request_selection_notification = gdk_broadway_display_request_selection_notification;
1350 display_class->supports_clipboard_persistence = gdk_broadway_display_supports_clipboard_persistence;
1351 display_class->store_clipboard = gdk_broadway_display_store_clipboard;
1352 display_class->supports_shapes = gdk_broadway_display_supports_shapes;
1353 display_class->supports_input_shapes = gdk_broadway_display_supports_input_shapes;
1354 display_class->supports_composite = gdk_broadway_display_supports_composite;
1355 display_class->list_devices = gdk_broadway_display_list_devices;
1356 display_class->get_cursor_for_type = _gdk_broadway_display_get_cursor_for_type;
1357 display_class->get_cursor_for_name = _gdk_broadway_display_get_cursor_for_name;
1358 display_class->get_cursor_for_pixbuf = _gdk_broadway_display_get_cursor_for_pixbuf;
1359 display_class->get_default_cursor_size = _gdk_broadway_display_get_default_cursor_size;
1360 display_class->get_maximal_cursor_size = _gdk_broadway_display_get_maximal_cursor_size;
1361 display_class->supports_cursor_alpha = _gdk_broadway_display_supports_cursor_alpha;
1362 display_class->supports_cursor_color = _gdk_broadway_display_supports_cursor_color;
1364 display_class->before_process_all_updates = _gdk_broadway_display_before_process_all_updates;
1365 display_class->after_process_all_updates = _gdk_broadway_display_after_process_all_updates;
1366 display_class->get_next_serial = gdk_broadway_display_get_next_serial;
1367 display_class->notify_startup_complete = gdk_broadway_display_notify_startup_complete;
1368 display_class->event_data_copy = gdk_broadway_display_event_data_copy;
1369 display_class->event_data_free = gdk_broadway_display_event_data_free;
1370 display_class->create_window_impl = _gdk_broadway_display_create_window_impl;
1371 display_class->get_keymap = _gdk_broadway_display_get_keymap;
1372 display_class->get_selection_owner = _gdk_broadway_display_get_selection_owner;
1373 display_class->set_selection_owner = _gdk_broadway_display_set_selection_owner;
1374 display_class->send_selection_notify = _gdk_broadway_display_send_selection_notify;
1375 display_class->get_selection_property = _gdk_broadway_display_get_selection_property;
1376 display_class->convert_selection = _gdk_broadway_display_convert_selection;
1377 display_class->text_property_to_utf8_list = _gdk_broadway_display_text_property_to_utf8_list;
1378 display_class->utf8_to_string_target = _gdk_broadway_display_utf8_to_string_target;