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