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