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