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