[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
From: |
Gavin D. Smith |
Date: |
Sun, 20 Mar 2022 04:54:55 -0400 (EDT) |
branch: master
commit d2d734c03e274bda471238fe855155baa8fad3e1
Author: Gavin Smith <gavinsmith0123@gmail.com>
AuthorDate: Sun May 5 17:51:12 2019 +0100
get socket events with g_socket_create_source from GIO
---
js/wkinfo/extension.c | 19 +++++++---
js/wkinfo/main.c | 100 +++++++++++++++++++++++++++++++-------------------
2 files changed, 76 insertions(+), 43 deletions(-)
diff --git a/js/wkinfo/extension.c b/js/wkinfo/extension.c
index d39bea5ed8..ff5460e5d1 100644
--- a/js/wkinfo/extension.c
+++ b/js/wkinfo/extension.c
@@ -139,10 +139,17 @@ document_loaded_callback (WebKitWebPage *web_page,
strcpy (next_link + (p - current_uri), href);
g_print ("saved ref |%s|\n", next_link);
- /*
- sendto (socket_id, next_link, strlen (next_link), 0,
- (struct sockaddr *) &main_name, main_name_size);
- */
+
+ ssize_t result;
+ result = sendto (socket_id, next_link,
+ strlen (next_link), 0,
+ (struct sockaddr *) &main_name, main_name_size);
+
+ if (result == -1)
+ {
+ g_print ("socket write failed: %s\n",
+ strerror(errno));
+ }
}
}
}
@@ -171,7 +178,7 @@ web_page_created_callback (WebKitWebExtension *extension,
NULL, 0);
}
-static int
+static void
make_named_socket (const char *filename)
{
struct sockaddr_un name;
@@ -215,7 +222,7 @@ initialize_socket (const char *main_socket_file)
if (!our_socket_file)
{
our_socket_file = tmpnam (0);
- socket_id = make_named_socket (our_socket_file);
+ make_named_socket (our_socket_file);
}
else
g_print ("bug: web process initialized twice\n");
diff --git a/js/wkinfo/main.c b/js/wkinfo/main.c
index f5e6dabf62..1c52e99ce9 100644
--- a/js/wkinfo/main.c
+++ b/js/wkinfo/main.c
@@ -7,6 +7,8 @@
#include <sys/un.h>
#include <gtk/gtk.h>
+#include <gio/gio.h>
+#include <gio/gunixsocketaddress.h>
#include <webkit2/webkit2.h>
static void destroyWindowCb(GtkWidget* widget, GtkWidget* window);
@@ -20,6 +22,7 @@ static gboolean onkeypress(GtkWidget *webView,
static char *socket_file;
int socket_id;
+/* FIXME - not removed if program killed with C-c. */
static void
remove_socket (void)
{
@@ -27,44 +30,39 @@ remove_socket (void)
unlink (socket_file);
}
-static int
-make_named_socket (const char *filename)
+gboolean
+socket_cb (GSocket *socket,
+ GIOCondition condition,
+ gpointer user_data)
{
- struct sockaddr_un name;
- int sock;
- size_t size;
+ static char buffer[256];
+ GError *err = 0;
+ gssize result;
- /* Create the socket. */
- sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
- if (sock < 0)
+ switch (condition)
{
- perror ("socket");
- exit (EXIT_FAILURE);
- }
-
- /* Bind a name to the socket. */
- name.sun_family = AF_LOCAL;
- strncpy (name.sun_path, filename, sizeof (name.sun_path));
- name.sun_path[sizeof (name.sun_path) - 1] = '\0';
-
- /* The size of the address is
- the offset of the start of the filename,
- plus its length (not including the terminating null byte).
- Alternatively you can just do:
- size = SUN_LEN (&name);
- */
- size = (offsetof (struct sockaddr_un, sun_path)
- + strlen (name.sun_path));
-
- if (bind (sock, (struct sockaddr *) &name, size) < 0)
- {
- perror ("bind");
- exit (EXIT_FAILURE);
- }
+ case G_IO_IN:
+ result = g_socket_receive (socket, buffer, sizeof buffer, NULL, &err);
+ if (result <= 0)
+ {
+ g_print ("socket receive error: %s\n", err->message);
+ gtk_main_quit ();
+ }
- atexit (&remove_socket);
+ g_print ("Received le data: <%s>\n", buffer);
- return sock;
+ break;
+ case G_IO_ERR:
+ case G_IO_HUP:
+ g_print ("socket error\n");
+ gtk_main_quit ();
+ break;
+ default:
+ g_print ("unhandled socket connection\n");
+ gtk_main_quit ();
+ break;
+ }
+ return false;
}
static void
@@ -78,13 +76,39 @@ initialize_web_extensions (WebKitWebContext *context,
Some example code and documentation for WebKitGTK uses dbus instead. */
if (!socket_file)
- {
- socket_file = tmpnam (0);
- socket_id = make_named_socket (socket_file);
- }
+ socket_file = tmpnam (0);
else
g_print ("bug: more than one web process started\n");
+ GError *err = 0;
+
+ err = 0;
+ GSocket *gsocket = g_socket_new (G_SOCKET_FAMILY_UNIX,
+ G_SOCKET_TYPE_DATAGRAM,
+ 0,
+ &err);
+ if (!gsocket)
+ {
+ g_print ("no socket: %s\n", err->message);
+ gtk_main_quit ();
+ }
+
+ err = 0;
+ GSocketAddress *address = g_unix_socket_address_new (socket_file);
+ g_socket_bind (gsocket, address, FALSE, &err);
+ if (!gsocket)
+ {
+ g_print ("bind socket: %s\n", err->message);
+ gtk_main_quit ();
+ }
+ err = 0;
+
+ GSource *gsource = g_socket_create_source (gsocket, G_IO_IN, NULL);
+ g_source_set_callback (gsource, (GSourceFunc)(socket_cb), NULL, NULL);
+ g_source_attach (gsource, NULL);
+
+ atexit (&remove_socket);
+
webkit_web_context_set_web_extensions_directory (
context, WEB_EXTENSIONS_DIRECTORY);
webkit_web_context_set_web_extensions_initialization_user_data (
@@ -161,6 +185,8 @@ static gboolean onkeypress(GtkWidget *webView,
;
}
+ return false;
+
}
- master updated (cd0de30b76 -> ca0de032c7), Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject],
Gavin D. Smith <=
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20