From e2da2259ad99e53644bebe26b12bac367d510e31 Mon Sep 17 00:00:00 2001 From: Tarnyko Date: Thu, 13 Jun 2013 18:55:40 +0200 Subject: [PATCH] broadway: Support TCP displays --- gdk/broadway/broadwayd.c | 63 +++++++++++++++++++++--------- gdk/broadway/gdkbroadway-server.c | 52 +++++++++++++++++++----- gdk/broadway/gdkbroadway-server.h | 2 +- gdk/broadway/gdkdisplay-broadway.c | 13 +----- 4 files changed, 90 insertions(+), 40 deletions(-) diff --git a/gdk/broadway/broadwayd.c b/gdk/broadway/broadwayd.c index cd6a8889ae..9dad48651e 100644 --- a/gdk/broadway/broadwayd.c +++ b/gdk/broadway/broadwayd.c @@ -437,12 +437,14 @@ main (int argc, char *argv[]) GError *error = NULL; GOptionContext *context; GMainLoop *loop; + GInetAddress *inet; GSocketAddress *address; GSocketService *listener; - char *path, *base; + char *path, *basename; char *http_address = NULL; int http_port = 0; - int display = 1; + char *display; + int port = 0; const GOptionEntry entries[] = { { "port", 'p', 0, G_OPTION_ARG_INT, &http_port, "Httpd port", "PORT" }, { "address", 'a', 0, G_OPTION_ARG_STRING, &http_address, "Ip address to bind to ", "ADDRESS" }, @@ -457,6 +459,7 @@ main (int argc, char *argv[]) exit (1); } + display = NULL; if (argc > 1) { if (*argv[1] != ':') @@ -464,16 +467,49 @@ main (int argc, char *argv[]) g_printerr ("Usage broadwayd [:DISPLAY]\n"); exit (1); } - display = strtol(argv[1]+1, NULL, 10); - if (display == 0) - { - g_printerr ("Failed to parse display num %s\n", argv[1]); - exit (1); - } + display = argv[1]; + } + + if (display == NULL) + { +#ifdef G_OS_UNIX + display = ":0"; +#else + display = ":tcp" +#endif + } + + if (g_str_has_prefix (display, ":tcp")) + { + port = strtol (display + strlen (":tcp"), NULL, 10); + + inet = g_inet_address_new_from_string ("127.0.0.1"); + g_print ("Listening on 127.0.0.1:%d\n", port + 9090); + address = g_inet_socket_address_new (inet, port + 9090); + g_object_unref (inet); + } +#ifdef G_OS_UNIX + else if (display[0] == ':' && g_ascii_isdigit(display[1])) + { + port = strtol (display + strlen (":"), NULL, 10); + basename = g_strdup_printf ("broadway%d.socket", port + 1); + path = g_build_filename (g_get_user_runtime_dir (), basename, NULL); + g_free (basename); + + g_print ("Listening on %s\n", path); + address = g_unix_socket_address_new_with_type (path, -1, + G_UNIX_SOCKET_ADDRESS_ABSTRACT); + g_free (path); + } +#endif + else + { + g_printerr ("Failed to parse display %s\n", display); + exit (1); } if (http_port == 0) - http_port = 8080 + (display - 1); + http_port = 8080 + port; server = broadway_server_new (http_address, http_port, &error); if (server == NULL) @@ -482,14 +518,6 @@ main (int argc, char *argv[]) return 1; } - base = g_strdup_printf ("broadway%d.socket", display); - path = g_build_filename (g_get_user_runtime_dir (), base, NULL); - g_free (base); - g_print ("Listening on %s\n", path); - address = g_unix_socket_address_new_with_type (path, -1, - G_UNIX_SOCKET_ADDRESS_ABSTRACT); - g_free (path); - listener = g_socket_service_new (); if (!g_socket_listener_add_address (G_SOCKET_LISTENER (listener), address, @@ -503,7 +531,6 @@ main (int argc, char *argv[]) return 1; } g_object_unref (address); - g_signal_connect (listener, "incoming", G_CALLBACK (incoming_client), NULL); g_socket_service_start (G_SOCKET_SERVICE (listener)); diff --git a/gdk/broadway/gdkbroadway-server.c b/gdk/broadway/gdkbroadway-server.c index 92d4e60f73..04565e08ae 100644 --- a/gdk/broadway/gdkbroadway-server.c +++ b/gdk/broadway/gdkbroadway-server.c @@ -20,6 +20,7 @@ #include #include #include +#include "gdkintl.h" typedef struct BroadwayInput BroadwayInput; @@ -81,31 +82,64 @@ _gdk_broadway_server_get_next_serial (GdkBroadwayServer *server) } GdkBroadwayServer * -_gdk_broadway_server_new (int port, GError **error) +_gdk_broadway_server_new (const char *display, GError **error) { GdkBroadwayServer *server; char *basename; GSocketClient *client; GSocketConnection *connection; + GInetAddress *inet; GSocketAddress *address; GPollableInputStream *pollable; GInputStream *in; GSource *source; char *path; + char *local_socket_type = NULL; + int port; - basename = g_strdup_printf ("broadway%d.socket", port); - path = g_build_filename (g_get_user_runtime_dir (), basename, NULL); - g_free (basename); + if (display == NULL) + { +#ifdef G_OS_UNIX + display = ":0"; +#else + display = ":tcp" +#endif + } - address = g_unix_socket_address_new_with_type (path, -1, - G_UNIX_SOCKET_ADDRESS_ABSTRACT); - g_free (path); + if (g_str_has_prefix (display, ":tcp")) + { + port = 9090 + strtol (display + strlen (":tcp"), NULL, 10); + + inet = g_inet_address_new_from_string ("127.0.0.1"); + address = g_inet_socket_address_new (inet, port); + g_object_unref (inet); + } +#ifdef G_OS_UNIX + else if (display[0] == ':' && g_ascii_isdigit(display[1])) + { + port = strtol (display + strlen (":"), NULL, 10); + basename = g_strdup_printf ("broadway%d.socket", port + 1); + path = g_build_filename (g_get_user_runtime_dir (), basename, NULL); + g_free (basename); + + address = g_unix_socket_address_new_with_type (path, -1, + G_UNIX_SOCKET_ADDRESS_ABSTRACT); + g_free (path); + } +#endif + else + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, + _("broadway display type not supported '%s'"), display); + return NULL; + } + + g_free (local_socket_type); client = g_socket_client_new (); - error = NULL; connection = g_socket_client_connect (client, G_SOCKET_CONNECTABLE (address), NULL, error); - + g_object_unref (address); g_object_unref (client); diff --git a/gdk/broadway/gdkbroadway-server.h b/gdk/broadway/gdkbroadway-server.h index 6c63edee73..2b93a85ad3 100644 --- a/gdk/broadway/gdkbroadway-server.h +++ b/gdk/broadway/gdkbroadway-server.h @@ -14,7 +14,7 @@ typedef struct _GdkBroadwayServerClass GdkBroadwayServerClass; #define GDK_IS_BROADWAY_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_BROADWAY_SERVER)) #define GDK_BROADWAY_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_BROADWAY_SERVER, GdkBroadwayServerClass)) -GdkBroadwayServer *_gdk_broadway_server_new (int port, +GdkBroadwayServer *_gdk_broadway_server_new (const char *display, GError **error); void _gdk_broadway_server_flush (GdkBroadwayServer *server); void _gdk_broadway_server_sync (GdkBroadwayServer *server); diff --git a/gdk/broadway/gdkdisplay-broadway.c b/gdk/broadway/gdkdisplay-broadway.c index e3d4426b31..70d5edf902 100644 --- a/gdk/broadway/gdkdisplay-broadway.c +++ b/gdk/broadway/gdkdisplay-broadway.c @@ -121,7 +121,6 @@ _gdk_broadway_display_open (const gchar *display_name) { GdkDisplay *display; GdkBroadwayDisplay *broadway_display; - int port; display = g_object_new (GDK_TYPE_BROADWAY_DISPLAY, NULL); broadway_display = GDK_BROADWAY_DISPLAY (display); @@ -150,17 +149,7 @@ _gdk_broadway_display_open (const gchar *display_name) if (display_name == NULL) display_name = g_getenv ("BROADWAY_DISPLAY"); - port = 0; - if (display_name != NULL) - { - if (*display_name == ':') - display_name++; - port = strtol(display_name, NULL, 10); - } - if (port == 0) - port = 1; - - broadway_display->server = _gdk_broadway_server_new (port, NULL); + broadway_display->server = _gdk_broadway_server_new (display_name, NULL); if (broadway_display->server == NULL) { g_printerr ("Unable to init server\n");