]> Pileus Git - ~andy/gtk/blob - gdk/broadway/gdkdisplay-broadway.c
[broadway] Serialize event times
[~andy/gtk] / gdk / broadway / gdkdisplay-broadway.c
1 /* GDK - The GIMP Drawing Kit
2  * gdkdisplay-broadway.c
3  * 
4  * Copyright 2001 Sun Microsystems Inc.
5  * Copyright (C) 2004 Nokia Corporation
6  *
7  * Erwann Chenede <erwann.chenede@sun.com>
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include "config.h"
26
27 #include "gdkdisplay-broadway.h"
28
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"
36
37 #include <glib.h>
38 #include <glib/gprintf.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <unistd.h>
43
44 static void   gdk_broadway_display_dispose            (GObject            *object);
45 static void   gdk_broadway_display_finalize           (GObject            *object);
46
47 G_DEFINE_TYPE (GdkBroadwayDisplay, gdk_broadway_display, GDK_TYPE_DISPLAY)
48
49 static void
50 gdk_broadway_display_init (GdkBroadwayDisplay *display)
51 {
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);
55 }
56
57 static void
58 gdk_event_init (GdkDisplay *display)
59 {
60   GdkBroadwayDisplay *broadway_display;
61
62   broadway_display = GDK_BROADWAY_DISPLAY (display);
63   broadway_display->event_source = _gdk_broadway_event_source_new (display);
64   broadway_display->saved_serial = 1;
65   broadway_display->last_seen_time = 1;
66 }
67
68 static void
69 gdk_broadway_display_init_input (GdkDisplay *display)
70 {
71   GdkBroadwayDisplay *broadway_display;
72   GdkDeviceManager *device_manager;
73   GdkDevice *device;
74   GList *list, *l;
75
76   broadway_display = GDK_BROADWAY_DISPLAY (display);
77   device_manager = gdk_display_get_device_manager (display);
78
79   /* For backwards compatibility, just add
80    * floating devices that are not keyboards.
81    */
82   list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
83
84   for (l = list; l; l = l->next)
85     {
86       device = l->data;
87
88       if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
89         continue;
90
91       broadway_display->input_devices = g_list_prepend (broadway_display->input_devices,
92                                                    g_object_ref (l->data));
93     }
94
95   g_list_free (list);
96
97   /* Now set "core" pointer to the first
98    * master device that is a pointer.
99    */
100   list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
101
102   for (l = list; l; l = l->next)
103     {
104       device = list->data;
105
106       if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
107         continue;
108
109       display->core_pointer = device;
110       break;
111     }
112
113   /* Add the core pointer to the devices list */
114   broadway_display->input_devices = g_list_prepend (broadway_display->input_devices,
115                                                g_object_ref (display->core_pointer));
116
117   g_list_free (list);
118 }
119
120 typedef struct HttpRequest {
121   GdkDisplay *display;
122   GSocketConnection *connection;
123   GDataInputStream *data;
124   GString *request;
125 }  HttpRequest;
126
127 static void
128 http_request_free (HttpRequest *request)
129 {
130   g_object_unref (request->connection);
131   g_object_unref (request->data);
132   g_string_free (request->request, TRUE);
133   g_free (request);
134 }
135
136 struct BroadwayInput {
137   GdkDisplay *display;
138   GSocketConnection *connection;
139   GByteArray *buffer;
140   GSource *source;
141   gboolean seen_time;
142   gint64 time_base;
143 };
144
145 static void
146 broadway_input_free (BroadwayInput *input)
147 {
148   g_object_unref (input->connection);
149   g_byte_array_free (input->buffer, FALSE);
150   g_source_destroy (input->source);
151   g_free (input);
152 }
153
154 static void
155 process_input_messages (GdkBroadwayDisplay *broadway_display)
156 {
157   BroadwayInputMsg *message;
158
159   while (broadway_display->input_messages)
160     {
161       message = broadway_display->input_messages->data;
162       broadway_display->input_messages =
163         g_list_delete_link (broadway_display->input_messages,
164                             broadway_display->input_messages);
165
166       _gdk_broadway_events_got_input (GDK_DISPLAY (broadway_display), message);
167       g_free (message);
168     }
169 }
170
171 static char *
172 parse_pointer_data (char *p, BroadwayInputPointerMsg *data)
173 {
174   data->mouse_window_id = strtol (p, &p, 10);
175   p++; /* Skip , */
176   data->event_window_id = strtol (p, &p, 10);
177   p++; /* Skip , */
178   data->root_x = strtol (p, &p, 10);
179   p++; /* Skip , */
180   data->root_y = strtol (p, &p, 10);
181   p++; /* Skip , */
182   data->win_x = strtol (p, &p, 10);
183   p++; /* Skip , */
184   data->win_y = strtol (p, &p, 10);
185   p++; /* Skip , */
186   data->state = strtol (p, &p, 10);
187
188   return p;
189 }
190
191 static void
192 update_future_pointer_info (GdkBroadwayDisplay *broadway_display, BroadwayInputPointerMsg *data)
193 {
194   broadway_display->future_root_x = data->root_x;
195   broadway_display->future_root_y = data->root_y;
196   broadway_display->future_state = data->state;
197   broadway_display->future_mouse_in_toplevel = data->mouse_window_id;
198 }
199
200 static void
201 parse_input_message (BroadwayInput *input, const char *message)
202 {
203   GdkBroadwayDisplay *broadway_display;
204   BroadwayInputMsg msg;
205   char *p;
206   gint64 time_;
207
208   broadway_display = GDK_BROADWAY_DISPLAY (input->display);
209
210   p = (char *)message;
211   msg.base.type = *p++;
212   msg.base.serial = (guint32)strtol (p, &p, 10);
213   p++; /* Skip , */
214   time_ = strtol(p, &p, 10);
215   p++; /* Skip , */
216
217   if (time_ == 0) {
218     time_ = broadway_display->last_seen_time;
219   } else {
220     if (!input->seen_time) {
221       input->seen_time = TRUE;
222       /* Calculate time base so that any following times are normalized to start
223          5 seconds after last_seen_time, to avoid issues that could appear when
224          a long hiatus due to a reconnect seems to be instant */
225       input->time_base = time_ - (broadway_display->last_seen_time + 5000);
226     } 
227     time_ = time_ - input->time_base;
228   }
229
230   broadway_display->last_seen_time = time_;
231
232   msg.base.time = time_;
233
234   switch (msg.base.type) {
235   case 'e': /* Enter */
236   case 'l': /* Leave */
237     p = parse_pointer_data (p, &msg.pointer);
238     update_future_pointer_info (broadway_display, &msg.pointer);
239     p++; /* Skip , */
240     msg.crossing.mode = strtol(p, &p, 10);
241     break;
242
243   case 'm': /* Mouse move */
244     p = parse_pointer_data (p, &msg.pointer);
245     update_future_pointer_info (broadway_display, &msg.pointer);
246     break;
247
248   case 'b':
249   case 'B':
250     p = parse_pointer_data (p, &msg.pointer);
251     update_future_pointer_info (broadway_display, &msg.pointer);
252     p++; /* Skip , */
253     msg.button.button = strtol(p, &p, 10);
254     break;
255
256   case 's':
257     p = parse_pointer_data (p, &msg.pointer);
258     update_future_pointer_info (broadway_display, &msg.pointer);
259     p++; /* Skip , */
260     msg.scroll.dir = strtol(p, &p, 10);
261     break;
262
263   case 'k':
264   case 'K':
265     msg.key.key = strtol(p, &p, 10);
266     break;
267
268   case 'g':
269   case 'u':
270     msg.grab_reply.res = strtol(p, &p, 10);
271     break;
272
273   case 'w':
274     msg.configure_notify.id = strtol(p, &p, 10);
275     p++; /* Skip , */
276     msg.configure_notify.x = strtol (p, &p, 10);
277     p++; /* Skip , */
278     msg.configure_notify.y = strtol (p, &p, 10);
279     p++; /* Skip , */
280     msg.configure_notify.width = strtol (p, &p, 10);
281     p++; /* Skip , */
282     msg.configure_notify.height = strtol (p, &p, 10);
283     break;
284
285   case 'W':
286     msg.delete_notify.id = strtol(p, &p, 10);
287     break;
288
289   case 'd':
290     msg.screen_resize_notify.width = strtol (p, &p, 10);
291     p++; /* Skip , */
292     msg.screen_resize_notify.height = strtol (p, &p, 10);
293     break;
294
295   default:
296     g_printerr ("Unknown input command %s\n", message);
297     break;
298   }
299
300   broadway_display->input_messages = g_list_append (broadway_display->input_messages, g_memdup (&msg, sizeof (msg)));
301
302 }
303
304 static void
305 parse_input (BroadwayInput *input)
306 {
307   GdkBroadwayDisplay *broadway_display;
308   char *buf, *ptr;
309   gsize len;
310
311   broadway_display = GDK_BROADWAY_DISPLAY (input->display);
312
313   buf = (char *)input->buffer->data;
314   len = input->buffer->len;
315
316   if (len == 0)
317     return;
318
319   if (buf[0] != 0)
320     {
321       broadway_display->input = NULL;
322       broadway_input_free (input);
323       return;
324     }
325
326   while ((ptr = memchr (buf, 0xff, len)) != NULL)
327     {
328       *ptr = 0;
329       ptr++;
330
331       parse_input_message (input, buf + 1);
332
333       len -= ptr - buf;
334       buf = ptr;
335
336       if (len > 0 && buf[0] != 0)
337         {
338           broadway_display->input = NULL;
339           broadway_input_free (input);
340           break;
341         }
342     }
343
344   g_byte_array_remove_range (input->buffer, 0, buf - (char *)input->buffer->data);
345 }
346
347
348 static gboolean
349 process_input_idle_cb (GdkBroadwayDisplay *display)
350 {
351   display->process_input_idle = 0;
352   process_input_messages (display);
353   return FALSE;
354 }
355
356 static void
357 queue_process_input_at_idle (GdkBroadwayDisplay *broadway_display)
358 {
359   if (broadway_display->process_input_idle == 0)
360     broadway_display->process_input_idle =
361       g_idle_add_full (GDK_PRIORITY_EVENTS, (GSourceFunc)process_input_idle_cb, broadway_display, NULL);
362 }
363
364 static void
365 _gdk_broadway_display_read_all_input_nonblocking (GdkDisplay *display)
366 {
367   GdkBroadwayDisplay *broadway_display;
368   GInputStream *in;
369   gssize res;
370   guint8 buffer[1024];
371   GError *error;
372   BroadwayInput *input;
373
374   broadway_display = GDK_BROADWAY_DISPLAY (display);
375   if (broadway_display->input == NULL)
376     return;
377
378   input = broadway_display->input;
379
380   in = g_io_stream_get_input_stream (G_IO_STREAM (input->connection));
381
382   error = NULL;
383   res = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (in),
384                                                   buffer, sizeof (buffer), NULL, &error);
385
386   if (res <= 0)
387     {
388       if (res < 0 &&
389           g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
390         {
391           g_error_free (error);
392           return;
393         }
394
395       broadway_display->input = NULL;
396       broadway_input_free (input);
397       if (res < 0)
398         {
399           g_print ("input error %s", error->message);
400           g_error_free (error);
401         }
402       return;
403     }
404
405   g_byte_array_append (input->buffer, buffer, res);
406
407   parse_input (input);
408 }
409
410 void
411 _gdk_broadway_display_consume_all_input (GdkDisplay *display)
412 {
413   GdkBroadwayDisplay *broadway_display;
414
415   broadway_display = GDK_BROADWAY_DISPLAY (display);
416   _gdk_broadway_display_read_all_input_nonblocking (display);
417
418   /* Since we're parsing input but not processing the resulting messages
419      we might not get a readable callback on the stream, so queue an idle to
420      process the messages */
421   queue_process_input_at_idle (broadway_display);
422 }
423
424
425 static gboolean
426 input_data_cb (GObject  *stream,
427                BroadwayInput *input)
428 {
429   GdkBroadwayDisplay *broadway_display;
430
431   broadway_display = GDK_BROADWAY_DISPLAY (input->display);
432   _gdk_broadway_display_read_all_input_nonblocking (input->display);
433
434   process_input_messages (broadway_display);
435
436   return TRUE;
437 }
438
439 /* Note: This may be called while handling a message (i.e. sorta recursively) */
440 BroadwayInputMsg *
441 _gdk_broadway_display_block_for_input (GdkDisplay *display, char op,
442                                        guint32 serial, gboolean remove_message)
443 {
444   GdkBroadwayDisplay *broadway_display;
445   BroadwayInputMsg *message;
446   gssize res;
447   guint8 buffer[1024];
448   BroadwayInput *input;
449   GInputStream *in;
450   GList *l;
451
452   gdk_display_flush (display);
453
454   broadway_display = GDK_BROADWAY_DISPLAY (display);
455   if (broadway_display->input == NULL)
456     return NULL;
457
458   input = broadway_display->input;
459
460   while (TRUE) {
461     /* Check for existing reply in queue */
462
463     for (l = broadway_display->input_messages; l != NULL; l = l->next)
464       {
465         message = l->data;
466
467         if (message->base.type == op)
468           {
469             if (message->base.serial == serial)
470               {
471                 if (remove_message)
472                   broadway_display->input_messages =
473                     g_list_delete_link (broadway_display->input_messages, l);
474                 return message;
475               }
476           }
477       }
478
479     /* Not found, read more, blocking */
480
481     in = g_io_stream_get_input_stream (G_IO_STREAM (input->connection));
482     res = g_input_stream_read (in, buffer, sizeof (buffer), NULL, NULL);
483     if (res <= 0)
484       return NULL;
485     g_byte_array_append (input->buffer, buffer, res);
486
487     parse_input (input);
488
489     /* Since we're parsing input but not processing the resulting messages
490        we might not get a readable callback on the stream, so queue an idle to
491        process the messages */
492     queue_process_input_at_idle (broadway_display);
493   }
494 }
495
496 #include <unistd.h>
497 #include <fcntl.h>
498 static void
499 set_fd_blocking (int fd)
500 {
501   glong arg;
502
503   if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
504     arg = 0;
505
506   arg = arg & ~O_NONBLOCK;
507
508   fcntl (fd, F_SETFL, arg);
509 }
510
511 static char *
512 parse_line (char *line, char *key)
513 {
514   char *p;
515
516   if (!g_str_has_prefix (line, key))
517     return NULL;
518   p = line + strlen (key);
519   if (*p != ':')
520     return NULL;
521   p++;
522   /* Skip optional initial space */
523   if (*p == ' ')
524     p++;
525   return p;
526 }
527 static void
528 send_error (HttpRequest *request,
529             int error_code,
530             const char *reason)
531 {
532   char *res;
533
534   res = g_strdup_printf ("HTTP/1.0 %d %s\r\n\r\n"
535                          "<html><head><title>%d %s</title></head>"
536                          "<body>%s</body></html>",
537                          error_code, reason,
538                          error_code, reason,
539                          reason);
540   /* TODO: This should really be async */
541   g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
542                              res, strlen (res), NULL, NULL, NULL);
543   g_free (res);
544   http_request_free (request);
545 }
546
547 static void
548 start_input (HttpRequest *request)
549 {
550   char **lines;
551   char *p;
552   int num_key1, num_key2;
553   guint64 key1, key2;
554   int num_space;
555   int i;
556   guint8 challenge[16];
557   char *res;
558   gsize len;
559   GChecksum *checksum;
560   char *origin, *host;
561   GdkBroadwayDisplay *broadway_display;
562   BroadwayInput *input;
563   const void *data_buffer;
564   gsize data_buffer_size;
565   GInputStream *in;
566
567   broadway_display = GDK_BROADWAY_DISPLAY (request->display);
568
569   if (broadway_display->input != NULL)
570     {
571       send_error (request, 409, "Input already handled");
572       return;
573     }
574
575   lines = g_strsplit (request->request->str, "\n", 0);
576
577   num_key1 = 0;
578   num_key2 = 0;
579   key1 = 0;
580   key2 = 0;
581   origin = NULL;
582   host = NULL;
583   for (i = 0; lines[i] != NULL; i++)
584     {
585       if ((p = parse_line (lines[i], "Sec-WebSocket-Key1")))
586         {
587           num_space = 0;
588           while (*p != 0)
589             {
590               if (g_ascii_isdigit (*p))
591                 key1 = key1 * 10 + g_ascii_digit_value (*p);
592               else if (*p == ' ')
593                 num_space++;
594
595               p++;
596             }
597           key1 /= num_space;
598           num_key1++;
599         }
600       else if ((p = parse_line (lines[i], "Sec-WebSocket-Key2")))
601         {
602           num_space = 0;
603           while (*p != 0)
604             {
605               if (g_ascii_isdigit (*p))
606                 key2 = key2 * 10 + g_ascii_digit_value (*p);
607               else if (*p == ' ')
608                 num_space++;
609
610               p++;
611             }
612           key2 /= num_space;
613           num_key2++;
614         }
615       else if ((p = parse_line (lines[i], "Origin")))
616         {
617           origin = p;
618         }
619       else if ((p = parse_line (lines[i], "Host")))
620         {
621           host = p;
622         }
623     }
624
625   if (num_key1 != 1 || num_key2 != 1 || origin == NULL || host == NULL)
626     {
627       g_strfreev (lines);
628       send_error (request, 400, "Bad websocket request");
629       return;
630     }
631
632   challenge[0] = (key1 >> 24) & 0xff;
633   challenge[1] = (key1 >> 16) & 0xff;
634   challenge[2] = (key1 >>  8) & 0xff;
635   challenge[3] = (key1 >>  0) & 0xff;
636   challenge[4] = (key2 >> 24) & 0xff;
637   challenge[5] = (key2 >> 16) & 0xff;
638   challenge[6] = (key2 >>  8) & 0xff;
639   challenge[7] = (key2 >>  0) & 0xff;
640
641   if (!g_input_stream_read_all (G_INPUT_STREAM (request->data), challenge+8, 8, NULL, NULL, NULL))
642     {
643       g_strfreev (lines);
644       send_error (request, 400, "Bad websocket request");
645       return;
646     }
647
648   checksum = g_checksum_new (G_CHECKSUM_MD5);
649   g_checksum_update (checksum, challenge, 16);
650   len = 16;
651   g_checksum_get_digest (checksum, challenge, &len);
652   g_checksum_free (checksum);
653
654   res = g_strdup_printf ("HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
655                          "Upgrade: WebSocket\r\n"
656                          "Connection: Upgrade\r\n"
657                          "Sec-WebSocket-Origin: %s\r\n"
658                          "Sec-WebSocket-Location: ws://%s/input\r\n"
659                          "Sec-WebSocket-Protocol: broadway\r\n"
660                          "\r\n",
661                          origin, host);
662
663   g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
664                              res, strlen (res), NULL, NULL, NULL);
665   g_free (res);
666   g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
667                              challenge, 16, NULL, NULL, NULL);
668
669   input = g_new0 (BroadwayInput, 1);
670
671   input->display = request->display;
672   input->connection = g_object_ref (request->connection);
673
674   data_buffer = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (request->data), &data_buffer_size);
675   input->buffer = g_byte_array_sized_new (data_buffer_size);
676   g_byte_array_append (input->buffer, data_buffer, data_buffer_size);
677
678   broadway_display->input = input;
679
680   /* This will free and close the data input stream, but we got all the buffered content already */
681   http_request_free (request);
682
683   in = g_io_stream_get_input_stream (G_IO_STREAM (input->connection));
684   input->source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (in), NULL);
685   g_source_set_callback (input->source, (GSourceFunc)input_data_cb, input, NULL);
686   g_source_attach (input->source, NULL);
687
688   /* Process any data in the pipe already */
689   parse_input (input);
690   process_input_messages (broadway_display);
691
692   g_strfreev (lines);
693 }
694
695 static void
696 start_output (HttpRequest *request)
697 {
698   GSocket *socket;
699   GdkBroadwayDisplay *broadway_display;
700   int fd;
701
702   socket = g_socket_connection_get_socket (request->connection);
703
704   broadway_display = GDK_BROADWAY_DISPLAY (request->display);
705   fd = g_socket_get_fd (socket);
706   set_fd_blocking (fd);
707   /* We dup this because otherwise it'll be closed with the request SocketConnection */
708
709   if (broadway_display->output)
710     {
711       broadway_display->saved_serial = broadway_output_get_next_serial (broadway_display->output);
712       broadway_output_free (broadway_display->output);
713     }
714
715   broadway_display->output = broadway_output_new (dup(fd), broadway_display->saved_serial);
716   _gdk_broadway_resync_windows ();
717
718   if (broadway_display->pointer_grab_window)
719     broadway_output_grab_pointer (broadway_display->output,
720                                   GDK_WINDOW_IMPL_BROADWAY (broadway_display->pointer_grab_window->impl)->id,
721                                   broadway_display->pointer_grab_owner_events);
722
723   http_request_free (request);
724 }
725
726 static void
727 send_data (HttpRequest *request,
728              const char *mimetype,
729              const char *data, gsize len)
730 {
731   char *res;
732
733   res = g_strdup_printf ("HTTP/1.0 200 OK\r\n"
734                          "Content-Type: %s\r\n"
735                          "Content-Length: %"G_GSIZE_FORMAT"\r\n"
736                          "\r\n",
737                          mimetype, len);
738   /* TODO: This should really be async */
739   g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
740                              res, strlen (res), NULL, NULL, NULL);
741   g_free (res);
742   g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (request->connection)),
743                              data, len, NULL, NULL, NULL);
744   http_request_free (request);
745 }
746
747 #include "clienthtml.h"
748 #include "broadwayjs.h"
749
750 static void
751 got_request (HttpRequest *request)
752 {
753   char *start, *escaped, *tmp, *version;
754
755   if (!g_str_has_prefix (request->request->str, "GET "))
756     {
757       send_error (request, 501, "Only GET implemented");
758       return;
759     }
760
761   start = request->request->str + 4; /* Skip "GET " */
762
763   while (*start == ' ')
764     start++;
765
766   for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
767     ;
768   escaped = g_strndup (start, tmp - start);
769   version = NULL;
770   if (*tmp == ' ')
771     {
772       start = tmp;
773       while (*start == ' ')
774         start++;
775       for (tmp = start; *tmp != 0 && *tmp != ' ' && *tmp != '\n'; tmp++)
776         ;
777       version = g_strndup (start, tmp - start);
778     }
779
780   if (strcmp (escaped, "/client.html") == 0 || strcmp (escaped, "/") == 0)
781     send_data (request, "text/html", client_html, G_N_ELEMENTS(client_html) - 1);
782   else if (strcmp (escaped, "/broadway.js") == 0)
783     send_data (request, "text/javascript", broadway_js, G_N_ELEMENTS(broadway_js) - 1);
784   else if (strcmp (escaped, "/output") == 0)
785     start_output (request);
786   else if (strcmp (escaped, "/input") == 0)
787     start_input (request);
788   else
789     send_error (request, 404, "File not found");
790
791   g_free (escaped);
792   g_free (version);
793 }
794
795 static void
796 got_http_request_line (GInputStream *stream,
797                        GAsyncResult *result,
798                        HttpRequest *request)
799 {
800   char *line;
801
802   line = g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM (stream), result, NULL, NULL);
803   if (line == NULL)
804     {
805       http_request_free (request);
806       g_printerr ("Error reading request lines\n");
807       return;
808     }
809   if (strlen (line) == 0)
810     got_request (request);
811   else
812     {
813       /* Protect against overflow in request length */
814       if (request->request->len > 1024 * 5)
815         {
816           send_error (request, 400, "Request to long");
817         }
818       else
819         {
820           g_string_append_printf (request->request, "%s\n", line);
821           g_data_input_stream_read_line_async (request->data, 0, NULL,
822                                                (GAsyncReadyCallback)got_http_request_line, request);
823         }
824     }
825   g_free (line);
826 }
827
828 static gboolean
829 handle_incoming_connection (GSocketService    *service,
830                             GSocketConnection *connection,
831                             GObject           *source_object)
832 {
833   HttpRequest *request;
834   GInputStream *in;
835
836   request = g_new0 (HttpRequest, 1);
837   request->connection = g_object_ref (connection);
838   request->display = (GdkDisplay *) source_object;
839   request->request = g_string_new ("");
840
841   in = g_io_stream_get_input_stream (G_IO_STREAM (connection));
842
843   request->data = g_data_input_stream_new (in);
844   g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (request->data), FALSE);
845   /* Be tolerant of input */
846   g_data_input_stream_set_newline_type (request->data, G_DATA_STREAM_NEWLINE_TYPE_ANY);
847
848   g_data_input_stream_read_line_async (request->data, 0, NULL,
849                                        (GAsyncReadyCallback)got_http_request_line, request);
850   return TRUE;
851 }
852
853 GdkDisplay *
854 _gdk_broadway_display_open (const gchar *display_name)
855 {
856   GdkDisplay *display;
857   GdkBroadwayDisplay *broadway_display;
858   GError *error;
859   int port;
860
861   display = g_object_new (GDK_TYPE_BROADWAY_DISPLAY, NULL);
862   broadway_display = GDK_BROADWAY_DISPLAY (display);
863
864   broadway_display->output = NULL;
865
866   /* initialize the display's screens */
867   broadway_display->screens = g_new (GdkScreen *, 1);
868   broadway_display->screens[0] = _gdk_broadway_screen_new (display, 0);
869
870   /* We need to initialize events after we have the screen
871    * structures in places
872    */
873   _gdk_broadway_screen_events_init (broadway_display->screens[0]);
874
875   /*set the default screen */
876   broadway_display->default_screen = broadway_display->screens[0];
877
878   display->device_manager = _gdk_broadway_device_manager_new (display);
879
880   gdk_event_init (display);
881
882   gdk_broadway_display_init_input (display);
883   _gdk_broadway_display_init_dnd (display);
884
885   _gdk_broadway_screen_setup (broadway_display->screens[0]);
886
887   if (display_name == NULL)
888     display_name = g_getenv ("BROADWAY_DISPLAY");
889
890   port = 0;
891   if (display_name != NULL)
892     port = strtol(display_name, NULL, 10);
893   if (port == 0)
894     port = 8080;
895
896   broadway_display->service = g_socket_service_new ();
897   if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (broadway_display->service),
898                                         port,
899                                         G_OBJECT (display),
900                                         &error))
901     {
902       g_printerr ("Unable to listen to port %d: %s\n", 8080, error->message);
903       g_error_free (error);
904       return NULL;
905     }
906   g_signal_connect (broadway_display->service, "incoming", G_CALLBACK (handle_incoming_connection), NULL);
907
908   g_signal_emit_by_name (display, "opened");
909   g_signal_emit_by_name (gdk_display_manager_get (), "display-opened", display);
910
911   return display;
912 }
913
914
915 static G_CONST_RETURN gchar *
916 gdk_broadway_display_get_name (GdkDisplay *display)
917 {
918   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
919
920   return (gchar *) "Broadway";
921 }
922
923 static gint
924 gdk_broadway_display_get_n_screens (GdkDisplay *display)
925 {
926   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
927
928   return 1;
929 }
930
931 static GdkScreen *
932 gdk_broadway_display_get_screen (GdkDisplay *display,
933                                  gint        screen_num)
934 {
935   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
936   g_return_val_if_fail (screen_num == 0, NULL);
937
938   return GDK_BROADWAY_DISPLAY (display)->screens[screen_num];
939 }
940
941 static GdkScreen *
942 gdk_broadway_display_get_default_screen (GdkDisplay *display)
943 {
944   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
945
946   return GDK_BROADWAY_DISPLAY (display)->default_screen;
947 }
948
949 static void
950 gdk_broadway_display_beep (GdkDisplay *display)
951 {
952   g_return_if_fail (GDK_IS_DISPLAY (display));
953 }
954
955 static void
956 gdk_broadway_display_sync (GdkDisplay *display)
957 {
958   g_return_if_fail (GDK_IS_DISPLAY (display));
959
960 }
961
962 static void
963 gdk_broadway_display_flush (GdkDisplay *display)
964 {
965   GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (display);
966
967   g_return_if_fail (GDK_IS_DISPLAY (display));
968
969   if (broadway_display->output &&
970       !broadway_output_flush (broadway_display->output))
971     {
972       broadway_display->saved_serial = broadway_output_get_next_serial (broadway_display->output);
973       broadway_output_free (broadway_display->output);
974       broadway_display->output = NULL;
975     }
976 }
977
978 static gboolean
979 gdk_broadway_display_has_pending (GdkDisplay *display)
980 {
981   return FALSE;
982 }
983
984 static GdkWindow *
985 gdk_broadway_display_get_default_group (GdkDisplay *display)
986 {
987   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
988
989   return NULL;
990 }
991
992 static void
993 gdk_broadway_display_dispose (GObject *object)
994 {
995   GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (object);
996
997   _gdk_broadway_display_manager_remove_display (gdk_display_manager_get (),
998                                                 GDK_DISPLAY_OBJECT (object));
999
1000   g_list_foreach (broadway_display->input_devices, (GFunc) g_object_run_dispose, NULL);
1001
1002   _gdk_screen_close (broadway_display->screens[0]);
1003
1004   if (broadway_display->event_source)
1005     {
1006       g_source_destroy (broadway_display->event_source);
1007       g_source_unref (broadway_display->event_source);
1008       broadway_display->event_source = NULL;
1009     }
1010
1011   G_OBJECT_CLASS (gdk_broadway_display_parent_class)->dispose (object);
1012 }
1013
1014 static void
1015 gdk_broadway_display_finalize (GObject *object)
1016 {
1017   GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (object);
1018
1019   /* Keymap */
1020   if (broadway_display->keymap)
1021     g_object_unref (broadway_display->keymap);
1022
1023   _gdk_broadway_cursor_display_finalize (GDK_DISPLAY_OBJECT(broadway_display));
1024
1025   /* Atom Hashtable */
1026   g_hash_table_destroy (broadway_display->atom_from_virtual);
1027   g_hash_table_destroy (broadway_display->atom_to_virtual);
1028
1029   /* input GdkDevice list */
1030   g_list_foreach (broadway_display->input_devices, (GFunc) g_object_unref, NULL);
1031   g_list_free (broadway_display->input_devices);
1032   /* Free all GdkScreens */
1033   g_object_unref (broadway_display->screens[0]);
1034   g_free (broadway_display->screens);
1035
1036   G_OBJECT_CLASS (gdk_broadway_display_parent_class)->finalize (object);
1037 }
1038
1039 void
1040 _gdk_broadway_display_make_default (GdkDisplay *display)
1041 {
1042 }
1043
1044 static void
1045 gdk_broadway_display_notify_startup_complete (GdkDisplay  *display,
1046                                               const gchar *startup_id)
1047 {
1048 }
1049
1050 static gboolean
1051 gdk_broadway_display_supports_selection_notification (GdkDisplay *display)
1052 {
1053   return FALSE;
1054 }
1055
1056 static gboolean
1057 gdk_broadway_display_request_selection_notification (GdkDisplay *display,
1058                                                      GdkAtom     selection)
1059
1060 {
1061     return FALSE;
1062 }
1063
1064 static gboolean
1065 gdk_broadway_display_supports_clipboard_persistence (GdkDisplay *display)
1066 {
1067   return FALSE;
1068 }
1069
1070 static void
1071 gdk_broadway_display_store_clipboard (GdkDisplay    *display,
1072                                       GdkWindow     *clipboard_window,
1073                                       guint32        time_,
1074                                       const GdkAtom *targets,
1075                                       gint           n_targets)
1076 {
1077 }
1078
1079 static gboolean
1080 gdk_broadway_display_supports_shapes (GdkDisplay *display)
1081 {
1082   return FALSE;
1083 }
1084
1085 static gboolean
1086 gdk_broadway_display_supports_input_shapes (GdkDisplay *display)
1087 {
1088   return FALSE;
1089 }
1090
1091 static gboolean
1092 gdk_broadway_display_supports_composite (GdkDisplay *display)
1093 {
1094   return FALSE;
1095 }
1096
1097 static GList *
1098 gdk_broadway_display_list_devices (GdkDisplay *display)
1099 {
1100   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1101
1102   return GDK_BROADWAY_DISPLAY (display)->input_devices;
1103 }
1104
1105 static gulong
1106 gdk_broadway_display_get_next_serial (GdkDisplay *display)
1107 {
1108   GdkBroadwayDisplay *broadway_display;
1109   broadway_display = GDK_BROADWAY_DISPLAY (display);
1110   if (broadway_display->output)
1111     return broadway_output_get_next_serial (broadway_display->output);
1112   return broadway_display->saved_serial;
1113 }
1114
1115
1116 static void
1117 gdk_broadway_display_event_data_copy (GdkDisplay    *display,
1118                                       const GdkEvent *src,
1119                                       GdkEvent       *dst)
1120 {
1121 }
1122
1123 static void
1124 gdk_broadway_display_event_data_free (GdkDisplay    *display,
1125                                       GdkEvent *event)
1126 {
1127 }
1128
1129 static void
1130 gdk_broadway_display_class_init (GdkBroadwayDisplayClass * class)
1131 {
1132   GObjectClass *object_class = G_OBJECT_CLASS (class);
1133   GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
1134
1135   object_class->dispose = gdk_broadway_display_dispose;
1136   object_class->finalize = gdk_broadway_display_finalize;
1137
1138   display_class->window_type = GDK_TYPE_BROADWAY_WINDOW;
1139
1140   display_class->get_name = gdk_broadway_display_get_name;
1141   display_class->get_n_screens = gdk_broadway_display_get_n_screens;
1142   display_class->get_screen = gdk_broadway_display_get_screen;
1143   display_class->get_default_screen = gdk_broadway_display_get_default_screen;
1144   display_class->beep = gdk_broadway_display_beep;
1145   display_class->sync = gdk_broadway_display_sync;
1146   display_class->flush = gdk_broadway_display_flush;
1147   display_class->has_pending = gdk_broadway_display_has_pending;
1148   display_class->queue_events = _gdk_broadway_display_queue_events;
1149   display_class->get_default_group = gdk_broadway_display_get_default_group;
1150   display_class->supports_selection_notification = gdk_broadway_display_supports_selection_notification;
1151   display_class->request_selection_notification = gdk_broadway_display_request_selection_notification;
1152   display_class->supports_clipboard_persistence = gdk_broadway_display_supports_clipboard_persistence;
1153   display_class->store_clipboard = gdk_broadway_display_store_clipboard;
1154   display_class->supports_shapes = gdk_broadway_display_supports_shapes;
1155   display_class->supports_input_shapes = gdk_broadway_display_supports_input_shapes;
1156   display_class->supports_composite = gdk_broadway_display_supports_composite;
1157   display_class->list_devices = gdk_broadway_display_list_devices;
1158   display_class->get_cursor_for_type = _gdk_broadway_display_get_cursor_for_type;
1159   display_class->get_cursor_for_name = _gdk_broadway_display_get_cursor_for_name;
1160   display_class->get_cursor_for_pixbuf = _gdk_broadway_display_get_cursor_for_pixbuf;
1161   display_class->get_default_cursor_size = _gdk_broadway_display_get_default_cursor_size;
1162   display_class->get_maximal_cursor_size = _gdk_broadway_display_get_maximal_cursor_size;
1163   display_class->supports_cursor_alpha = _gdk_broadway_display_supports_cursor_alpha;
1164   display_class->supports_cursor_color = _gdk_broadway_display_supports_cursor_color;
1165
1166   display_class->before_process_all_updates = _gdk_broadway_display_before_process_all_updates;
1167   display_class->after_process_all_updates = _gdk_broadway_display_after_process_all_updates;
1168   display_class->get_next_serial = gdk_broadway_display_get_next_serial;
1169   display_class->notify_startup_complete = gdk_broadway_display_notify_startup_complete;
1170   display_class->event_data_copy = gdk_broadway_display_event_data_copy;
1171   display_class->event_data_free = gdk_broadway_display_event_data_free;
1172   display_class->create_window_impl = _gdk_broadway_display_create_window_impl;
1173   display_class->get_keymap = _gdk_broadway_display_get_keymap;
1174   display_class->get_selection_owner = _gdk_broadway_display_get_selection_owner;
1175   display_class->set_selection_owner = _gdk_broadway_display_set_selection_owner;
1176   display_class->send_selection_notify = _gdk_broadway_display_send_selection_notify;
1177   display_class->get_selection_property = _gdk_broadway_display_get_selection_property;
1178   display_class->convert_selection = _gdk_broadway_display_convert_selection;
1179   display_class->text_property_to_utf8_list = _gdk_broadway_display_text_property_to_utf8_list;
1180   display_class->utf8_to_string_target = _gdk_broadway_display_utf8_to_string_target;
1181 }
1182