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