<commit_before>#include <ctype.h>
#include <fcntl.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <webkit/webkit.h>
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>

#include "wkline.h"
#include "config.h"

static void
fifo_monitor (gpointer data) {
	char buf[256];
	int num, fd;
	WklineThreadData *thread_data = (WklineThreadData *)data;

	if (mkfifo(wkline_fifo_path, 0666) < 0) {
		perror("mkfifo");
	}

	while (1) {
		if ((fd = open(wkline_fifo_path, O_RDONLY)) < 0) {
			perror("FIFO open");
		}
		do {
			if ((num = read(fd, buf, sizeof(buf))) < 0) {
				perror("FIFO read");
			}
			else {
				buf[num] = '\0';
				if (num > 0) {
					thread_data->buf = buf;
					g_idle_add((GSourceFunc)fifo_monitor_inject_data, data);
				}
			}
		} while (num > 0);

		close(fd);
	}
}

static gboolean
fifo_monitor_inject_data(gpointer data) {
	WklineThreadData *thread_data = (WklineThreadData *)data;

	web_view_inject(thread_data->web_view, thread_data->buf);

	return FALSE;
}

static int
get_intern_atom (xcb_connection_t *conn, char *atom) {
	xcb_intern_atom_cookie_t cookie;
	xcb_intern_atom_reply_t *reply;

	cookie = xcb_intern_atom(conn, 0, strlen(atom), atom);
	reply = xcb_intern_atom_reply(conn, cookie, NULL);

	if (reply) {
		return reply->atom;
	}

	return -1;
}

static void
web_view_inject (WebKitWebView *web_view, char *payload) {
	char script[256];

	fprintf(stderr, "Injecting data into web view: %s\n", payload);
	sprintf(script, "try{wkInject(%s)}catch(e){console.log(e)}", payload);

	webkit_web_view_execute_script(web_view, script);
}

static gboolean
wk_context_menu_cb (WebKitWebView *web_view, GtkWidget *window) {
	// Disable context menu
	return TRUE;
}

static void
wk_notify_load_status_cb (WebKitWebView *web_view, GParamSpec *pspec, GtkWidget *window) {
	char init_data[256];
	WebKitLoadStatus status = webkit_web_view_get_load_status(web_view);

	if (status == WEBKIT_LOAD_FINISHED) {
		sprintf(init_data, "['init',{height:%i}]", wkline_height);
		web_view_inject(web_view, init_data);
		// TODO move thread check and creation here to avoid script errors when the page hasn't been loaded yet<commit_msg>Remove note about moving thread code<commit_after>#include <ctype.h>
#include <fcntl.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <webkit/webkit.h>
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>

#include "wkline.h"
#include "config.h"

static void
fifo_monitor (gpointer data) {
	char buf[256];
	int num, fd;
	WklineThreadData *thread_data = (WklineThreadData *)data;

	if (mkfifo(wkline_fifo_path, 0666) < 0) {
		perror("mkfifo");
	}

	while (1) {
		if ((fd = open(wkline_fifo_path, O_RDONLY)) < 0) {
			perror("FIFO open");
		}
		do {
			if ((num = read(fd, buf, sizeof(buf))) < 0) {
				perror("FIFO read");
			}
			else {
				buf[num] = '\0';
				if (num > 0) {
					thread_data->buf = buf;
					g_idle_add((GSourceFunc)fifo_monitor_inject_data, data);
				}
			}
		} while (num > 0);

		close(fd);
	}
}

static gboolean
fifo_monitor_inject_data(gpointer data) {
	WklineThreadData *thread_data = (WklineThreadData *)data;

	web_view_inject(thread_data->web_view, thread_data->buf);

	return FALSE;
}

static int
get_intern_atom (xcb_connection_t *conn, char *atom) {
	xcb_intern_atom_cookie_t cookie;
	xcb_intern_atom_reply_t *reply;

	cookie = xcb_intern_atom(conn, 0, strlen(atom), atom);
	reply = xcb_intern_atom_reply(conn, cookie, NULL);

	if (reply) {
		return reply->atom;
	}

	return -1;
}

static void
web_view_inject (WebKitWebView *web_view, char *payload) {
	char script[256];

	fprintf(stderr, "Injecting data into web view: %s\n", payload);
	sprintf(script, "try{wkInject(%s)}catch(e){console.log(e)}", payload);

	webkit_web_view_execute_script(web_view, script);
}

static gboolean
wk_context_menu_cb (WebKitWebView *web_view, GtkWidget *window) {
	// Disable context menu
	return TRUE;
}

static void
wk_notify_load_status_cb (WebKitWebView *web_view, GParamSpec *pspec, GtkWidget *window) {
	char init_data[256];
	WebKitLoadStatus status = webkit_web_view_get_load_status(web_view);

	if (status == WEBKIT_LOAD_FINISHED) {
		sprintf(init_data, "['init',{height:%i}]", wkline_height);
		web_view_inject(web_view, init_data);
{'fraction_non_alphanumeric': 0.14317925591882752, 'fraction_numerical': 0.00992108229988726, 'mean_word_length': 5.035374149659864, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6761276', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1767, 'n_words': 372}