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>
44 static void gdk_broadway_display_dispose (GObject *object);
45 static void gdk_broadway_display_finalize (GObject *object);
47 G_DEFINE_TYPE (GdkBroadwayDisplay, gdk_broadway_display, GDK_TYPE_DISPLAY)
50 gdk_broadway_display_init (GdkBroadwayDisplay *display)
52 _gdk_broadway_display_manager_add_display (gdk_display_manager_get (),
53 GDK_DISPLAY_OBJECT (display));
54 display->id_ht = g_hash_table_new (NULL, NULL);
58 gdk_event_init (GdkDisplay *display)
60 GdkBroadwayDisplay *broadway_display;
62 broadway_display = GDK_BROADWAY_DISPLAY (display);
63 broadway_display->event_source = _gdk_broadway_event_source_new (display);
64 broadway_display->saved_serial = 1;
68 gdk_broadway_display_init_input (GdkDisplay *display)
70 GdkBroadwayDisplay *broadway_display;
71 GdkDeviceManager *device_manager;
75 broadway_display = GDK_BROADWAY_DISPLAY (display);
76 device_manager = gdk_display_get_device_manager (display);
78 /* For backwards compatibility, just add
79 * floating devices that are not keyboards.
81 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
83 for (l = list; l; l = l->next)
87 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
90 broadway_display->input_devices = g_list_prepend (broadway_display->input_devices,
91 g_object_ref (l->data));
96 /* Now set "core" pointer to the first
97 * master device that is a pointer.
99 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
101 for (l = list; l; l = l->next)
105 if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
108 display->core_pointer = device;
112 /* Add the core pointer to the devices list */
113 broadway_display->input_devices = g_list_prepend (broadway_display->input_devices,
114 g_object_ref (display->core_pointer));
121 GSocketConnection *connection;
122 GDataInputStream *data;
127 http_request_free (HttpRequest *request)
129 g_object_unref (request->connection);
130 g_object_unref (request->data);
131 g_string_free (request->request, TRUE);
138 set_fd_blocking (int fd)
142 if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
145 arg = arg & ~O_NONBLOCK;
147 fcntl (fd, F_SETFL, arg);
151 parse_line (char *line, char *key)
155 if (!g_str_has_prefix (line, key))
157 p = line + strlen (key);
161 /* Skip optional initial space */
168 got_input (GInputStream *stream,
169 GAsyncResult *result,
170 HttpRequest *request)
177 message = g_data_input_stream_read_upto_finish (G_DATA_INPUT_STREAM (stream), result, &len, &error);
180 GDK_BROADWAY_DISPLAY (request->display)->input = NULL;
181 http_request_free (request);
185 g_assert (message[0] == 0);
186 _gdk_broadway_events_got_input (request->display, message + 1);
188 /* Skip past ending 0xff */
189 g_data_input_stream_read_byte (request->data, NULL, NULL);
190 g_data_input_stream_read_upto_async (request->data, "\xff", 1, 0, NULL,
191 (GAsyncReadyCallback)got_input, request);
195 send_error (HttpRequest *request,
201 res = g_strdup_printf ("HTTP/1.0 %d %s\r\n\r\n"
202 "<html><head><title>%d %s</title></head>"
203 "<body>%s</body></html>",
207 /* TODO: This should really be async */
208 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
209 res, strlen (res), NULL, NULL, NULL);
211 http_request_free (request);
215 start_input (HttpRequest *request)
219 int num_key1, num_key2;
223 guint8 challenge[16];
228 GdkBroadwayDisplay *broadway_display;
230 broadway_display = GDK_BROADWAY_DISPLAY (request->display);
232 if (broadway_display->input != NULL)
234 send_error (request, 409, "Input already handled");
238 lines = g_strsplit (request->request->str, "\n", 0);
246 for (i = 0; lines[i] != NULL; i++)
248 if ((p = parse_line (lines[i], "Sec-WebSocket-Key1")))
253 if (g_ascii_isdigit (*p))
254 key1 = key1 * 10 + g_ascii_digit_value (*p);
263 else if ((p = parse_line (lines[i], "Sec-WebSocket-Key2")))
268 if (g_ascii_isdigit (*p))
269 key2 = key2 * 10 + g_ascii_digit_value (*p);
278 else if ((p = parse_line (lines[i], "Origin")))
282 else if ((p = parse_line (lines[i], "Host")))
288 if (num_key1 != 1 || num_key2 != 1 || origin == NULL || host == NULL)
291 send_error (request, 400, "Bad websocket request");
295 challenge[0] = (key1 >> 24) & 0xff;
296 challenge[1] = (key1 >> 16) & 0xff;
297 challenge[2] = (key1 >> 8) & 0xff;
298 challenge[3] = (key1 >> 0) & 0xff;
299 challenge[4] = (key2 >> 24) & 0xff;
300 challenge[5] = (key2 >> 16) & 0xff;
301 challenge[6] = (key2 >> 8) & 0xff;
302 challenge[7] = (key2 >> 0) & 0xff;
304 if (!g_input_stream_read_all (G_INPUT_STREAM (request->data), challenge+8, 8, NULL, NULL, NULL))
307 send_error (request, 400, "Bad websocket request");
311 checksum = g_checksum_new (G_CHECKSUM_MD5);
312 g_checksum_update (checksum, challenge, 16);
314 g_checksum_get_digest (checksum, challenge, &len);
315 g_checksum_free (checksum);
317 res = g_strdup_printf ("HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
318 "Upgrade: WebSocket\r\n"
319 "Connection: Upgrade\r\n"
320 "Sec-WebSocket-Origin: %s\r\n"
321 "Sec-WebSocket-Location: ws://%s/input\r\n"
322 "Sec-WebSocket-Protocol: broadway\r\n"
326 /* TODO: This should really be async */
327 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
328 res, strlen (res), NULL, NULL, NULL);
330 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
331 challenge, 16, NULL, NULL, NULL);
333 broadway_display->input = request;
335 g_data_input_stream_read_upto_async (request->data, "\xff", 1, 0, NULL,
336 (GAsyncReadyCallback)got_input, request);
342 start_output (HttpRequest *request)
345 GdkBroadwayDisplay *broadway_display;
348 socket = g_socket_connection_get_socket (request->connection);
350 broadway_display = GDK_BROADWAY_DISPLAY (request->display);
351 fd = g_socket_get_fd (socket);
352 set_fd_blocking (fd);
353 /* We dup this because otherwise it'll be closed with the request SocketConnection */
355 if (broadway_display->output)
357 broadway_display->saved_serial = broadway_output_get_next_serial (broadway_display->output);
358 broadway_output_free (broadway_display->output);
361 broadway_display->output = broadway_output_new (dup(fd), broadway_display->saved_serial);
362 _gdk_broadway_resync_windows ();
363 http_request_free (request);
367 send_data (HttpRequest *request,
368 const char *mimetype,
369 const char *data, gsize len)
373 res = g_strdup_printf ("HTTP/1.0 200 OK\r\n"
374 "Content-Type: %s\r\n"
375 "Content-Length: %"G_GSIZE_FORMAT"\r\n"
378 /* TODO: This should really be async */
379 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
380 res, strlen (res), NULL, NULL, NULL);
382 g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
383 data, len, NULL, NULL, NULL);
384 http_request_free (request);
387 #include "clienthtml.h"
388 #include "broadwayjs.h"
391 got_request (HttpRequest *request)
393 char *start, *escaped, *tmp, *version;
395 if (!g_str_has_prefix (request->request->str, "GET "))
397 send_error (request, 501, "Only GET implemented");
401 start = request->request->str + 4; /* Skip "GET " */
403 while (*start == ' ')
406 for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
408 escaped = g_strndup (start, tmp - start);
413 while (*start == ' ')
415 for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
417 version = g_strndup (start, tmp - start);
420 if (strcmp (escaped, "/client.html") == 0 || strcmp (escaped, "/") == 0)
421 send_data (request, "text/html", client_html, G_N_ELEMENTS(client_html) - 1);
422 else if (strcmp (escaped, "/broadway.js") == 0)
423 send_data (request, "text/javascript", broadway_js, G_N_ELEMENTS(broadway_js) - 1);
424 else if (strcmp (escaped, "/output") == 0)
425 start_output (request);
426 else if (strcmp (escaped, "/input") == 0)
427 start_input (request);
429 send_error (request, 404, "File not found");
433 got_http_request_line (GInputStream *stream,
434 GAsyncResult *result,
435 HttpRequest *request)
439 line = g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM (stream), result, NULL, NULL);
442 http_request_free (request);
443 g_printerr ("Error reading request lines\n");
446 if (strlen (line) == 0)
447 got_request (request);
450 /* Protect against overflow in request length */
451 if (request->request->len > 1024 * 5)
453 send_error (request, 400, "Request to long");
457 g_string_append_printf (request->request, "%s\n", line);
458 g_data_input_stream_read_line_async (request->data, 0, NULL,
459 (GAsyncReadyCallback)got_http_request_line, request);
466 handle_incoming_connection (GSocketService *service,
467 GSocketConnection *connection,
468 GObject *source_object)
470 HttpRequest *request;
473 request = g_new0 (HttpRequest, 1);
474 request->connection = g_object_ref (connection);
475 request->display = (GdkDisplay *) source_object;
476 request->request = g_string_new ("");
478 in = g_io_stream_get_input_stream (G_IO_STREAM (connection));
480 request->data = g_data_input_stream_new (in);
481 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (request->data), FALSE);
482 /* Be tolerant of input */
483 g_data_input_stream_set_newline_type (request->data, G_DATA_STREAM_NEWLINE_TYPE_ANY);
485 g_data_input_stream_read_line_async (request->data, 0, NULL,
486 (GAsyncReadyCallback)got_http_request_line, request);
491 _gdk_broadway_display_open (const gchar *display_name)
494 GdkBroadwayDisplay *broadway_display;
497 display = g_object_new (GDK_TYPE_BROADWAY_DISPLAY, NULL);
498 broadway_display = GDK_BROADWAY_DISPLAY (display);
500 broadway_display->output = NULL;
502 /* initialize the display's screens */
503 broadway_display->screens = g_new (GdkScreen *, 1);
504 broadway_display->screens[0] = _gdk_broadway_screen_new (display, 0);
506 /* We need to initialize events after we have the screen
507 * structures in places
509 _gdk_broadway_screen_events_init (broadway_display->screens[0]);
511 /*set the default screen */
512 broadway_display->default_screen = broadway_display->screens[0];
514 display->device_manager = _gdk_broadway_device_manager_new (display);
516 gdk_event_init (display);
518 gdk_broadway_display_init_input (display);
519 _gdk_broadway_display_init_dnd (display);
521 _gdk_broadway_screen_setup (broadway_display->screens[0]);
523 broadway_display->service = g_socket_service_new ();
524 if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (broadway_display->service),
529 g_printerr ("Unable to listen to port %d: %s\n", 8080, error->message);
530 g_error_free (error);
533 g_signal_connect (broadway_display->service, "incoming", G_CALLBACK (handle_incoming_connection), NULL);
535 g_signal_emit_by_name (display, "opened");
536 g_signal_emit_by_name (gdk_display_manager_get (), "display-opened", display);
542 static G_CONST_RETURN gchar *
543 gdk_broadway_display_get_name (GdkDisplay *display)
545 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
547 return (gchar *) "Broadway";
551 gdk_broadway_display_get_n_screens (GdkDisplay *display)
553 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
559 gdk_broadway_display_get_screen (GdkDisplay *display,
562 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
563 g_return_val_if_fail (screen_num == 0, NULL);
565 return GDK_BROADWAY_DISPLAY (display)->screens[screen_num];
569 gdk_broadway_display_get_default_screen (GdkDisplay *display)
571 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
573 return GDK_BROADWAY_DISPLAY (display)->default_screen;
577 gdk_broadway_display_beep (GdkDisplay *display)
579 g_return_if_fail (GDK_IS_DISPLAY (display));
583 gdk_broadway_display_sync (GdkDisplay *display)
585 g_return_if_fail (GDK_IS_DISPLAY (display));
590 gdk_broadway_display_flush (GdkDisplay *display)
592 g_return_if_fail (GDK_IS_DISPLAY (display));
597 gdk_broadway_display_has_pending (GdkDisplay *display)
603 gdk_broadway_display_get_default_group (GdkDisplay *display)
605 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
611 gdk_broadway_display_dispose (GObject *object)
613 GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (object);
615 _gdk_broadway_display_manager_remove_display (gdk_display_manager_get (),
616 GDK_DISPLAY_OBJECT (object));
618 g_list_foreach (broadway_display->input_devices, (GFunc) g_object_run_dispose, NULL);
620 _gdk_screen_close (broadway_display->screens[0]);
622 if (broadway_display->event_source)
624 g_source_destroy (broadway_display->event_source);
625 g_source_unref (broadway_display->event_source);
626 broadway_display->event_source = NULL;
629 G_OBJECT_CLASS (gdk_broadway_display_parent_class)->dispose (object);
633 gdk_broadway_display_finalize (GObject *object)
635 GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (object);
638 if (broadway_display->keymap)
639 g_object_unref (broadway_display->keymap);
641 _gdk_broadway_cursor_display_finalize (GDK_DISPLAY_OBJECT(broadway_display));
644 g_hash_table_destroy (broadway_display->atom_from_virtual);
645 g_hash_table_destroy (broadway_display->atom_to_virtual);
647 /* input GdkDevice list */
648 g_list_foreach (broadway_display->input_devices, (GFunc) g_object_unref, NULL);
649 g_list_free (broadway_display->input_devices);
650 /* Free all GdkScreens */
651 g_object_unref (broadway_display->screens[0]);
652 g_free (broadway_display->screens);
654 G_OBJECT_CLASS (gdk_broadway_display_parent_class)->finalize (object);
658 _gdk_broadway_display_make_default (GdkDisplay *display)
663 gdk_broadway_display_notify_startup_complete (GdkDisplay *display,
664 const gchar *startup_id)
669 gdk_broadway_display_supports_selection_notification (GdkDisplay *display)
675 gdk_broadway_display_request_selection_notification (GdkDisplay *display,
683 gdk_broadway_display_supports_clipboard_persistence (GdkDisplay *display)
689 gdk_broadway_display_store_clipboard (GdkDisplay *display,
690 GdkWindow *clipboard_window,
692 const GdkAtom *targets,
698 gdk_broadway_display_supports_shapes (GdkDisplay *display)
704 gdk_broadway_display_supports_input_shapes (GdkDisplay *display)
710 gdk_broadway_display_supports_composite (GdkDisplay *display)
716 gdk_broadway_display_list_devices (GdkDisplay *display)
718 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
720 return GDK_BROADWAY_DISPLAY (display)->input_devices;
724 gdk_broadway_display_get_next_serial (GdkDisplay *display)
726 GdkBroadwayDisplay *broadway_display;
727 broadway_display = GDK_BROADWAY_DISPLAY (display);
728 if (broadway_display->output)
729 return broadway_output_get_next_serial (broadway_display->output);
730 return broadway_display->saved_serial;
735 gdk_broadway_display_event_data_copy (GdkDisplay *display,
742 gdk_broadway_display_event_data_free (GdkDisplay *display,
748 gdk_broadway_display_class_init (GdkBroadwayDisplayClass * class)
750 GObjectClass *object_class = G_OBJECT_CLASS (class);
751 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
753 object_class->dispose = gdk_broadway_display_dispose;
754 object_class->finalize = gdk_broadway_display_finalize;
756 display_class->window_type = GDK_TYPE_BROADWAY_WINDOW;
758 display_class->get_name = gdk_broadway_display_get_name;
759 display_class->get_n_screens = gdk_broadway_display_get_n_screens;
760 display_class->get_screen = gdk_broadway_display_get_screen;
761 display_class->get_default_screen = gdk_broadway_display_get_default_screen;
762 display_class->beep = gdk_broadway_display_beep;
763 display_class->sync = gdk_broadway_display_sync;
764 display_class->flush = gdk_broadway_display_flush;
765 display_class->has_pending = gdk_broadway_display_has_pending;
766 display_class->queue_events = _gdk_broadway_display_queue_events;
767 display_class->get_default_group = gdk_broadway_display_get_default_group;
768 display_class->supports_selection_notification = gdk_broadway_display_supports_selection_notification;
769 display_class->request_selection_notification = gdk_broadway_display_request_selection_notification;
770 display_class->supports_clipboard_persistence = gdk_broadway_display_supports_clipboard_persistence;
771 display_class->store_clipboard = gdk_broadway_display_store_clipboard;
772 display_class->supports_shapes = gdk_broadway_display_supports_shapes;
773 display_class->supports_input_shapes = gdk_broadway_display_supports_input_shapes;
774 display_class->supports_composite = gdk_broadway_display_supports_composite;
775 display_class->list_devices = gdk_broadway_display_list_devices;
776 display_class->get_cursor_for_type = _gdk_broadway_display_get_cursor_for_type;
777 display_class->get_cursor_for_name = _gdk_broadway_display_get_cursor_for_name;
778 display_class->get_cursor_for_pixbuf = _gdk_broadway_display_get_cursor_for_pixbuf;
779 display_class->get_default_cursor_size = _gdk_broadway_display_get_default_cursor_size;
780 display_class->get_maximal_cursor_size = _gdk_broadway_display_get_maximal_cursor_size;
781 display_class->supports_cursor_alpha = _gdk_broadway_display_supports_cursor_alpha;
782 display_class->supports_cursor_color = _gdk_broadway_display_supports_cursor_color;
784 display_class->before_process_all_updates = _gdk_broadway_display_before_process_all_updates;
785 display_class->after_process_all_updates = _gdk_broadway_display_after_process_all_updates;
786 display_class->get_next_serial = gdk_broadway_display_get_next_serial;
787 display_class->notify_startup_complete = gdk_broadway_display_notify_startup_complete;
788 display_class->event_data_copy = gdk_broadway_display_event_data_copy;
789 display_class->event_data_free = gdk_broadway_display_event_data_free;
790 display_class->create_window_impl = _gdk_broadway_display_create_window_impl;
791 display_class->get_keymap = _gdk_broadway_display_get_keymap;
792 display_class->get_selection_owner = _gdk_broadway_display_get_selection_owner;
793 display_class->set_selection_owner = _gdk_broadway_display_set_selection_owner;
794 display_class->send_selection_notify = _gdk_broadway_display_send_selection_notify;
795 display_class->get_selection_property = _gdk_broadway_display_get_selection_property;
796 display_class->convert_selection = _gdk_broadway_display_convert_selection;
797 display_class->text_property_to_utf8_list = _gdk_broadway_display_text_property_to_utf8_list;
798 display_class->utf8_to_string_target = _gdk_broadway_display_utf8_to_string_target;