Skip to content

Commit

Permalink
Inject monitors' geometries to page JS
Browse files Browse the repository at this point in the history
...before loading user code.

This way user JS can draw whatever they want, knowing what areas
correspond to monitors.
  • Loading branch information
anko committed Sep 15, 2018
1 parent 3b8c498 commit 9474ebd
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <gtk/gtk.h> // windowing
#include <webkit2/webkit2.h> // web view
#include <stdlib.h> // exit
#include <stdio.h> // files

static void screen_changed(GtkWidget *widget, GdkScreen *old_screen,
gpointer user_data);
Expand All @@ -29,6 +30,22 @@ static int get_monitor_rects(GdkDisplay *display, GdkRectangle **rectangles) {
return n;
}

static void web_view_javascript_finished(GObject *object, GAsyncResult *result,
gpointer user_data) {
WebKitJavascriptResult *js_result;
JSValueRef value;
JSGlobalContextRef context;
GError *error = NULL;

js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &error);
if (!js_result) {
g_warning("Error running JavaScript: %s", error->message);
g_error_free(error);
return;
}
webkit_javascript_result_unref(js_result);
}

int main(int argc, char **argv) {
gtk_init(&argc, &argv);
if (argc < 2) {
Expand Down Expand Up @@ -70,6 +87,35 @@ int main(int argc, char **argv) {
// Initialise the window and make it active. We need this so it can
// fullscreen to the correct size.
screen_changed(window, NULL, NULL);

GdkDisplay *display = gdk_display_get_default();
GdkRectangle *rectangles = NULL;
int nRectangles = get_monitor_rects(display, &rectangles);

// snprintf-ing and then strncat-ing strings safely in C is hard, and it's
// 3am, so let's write to a temporary file and read the result back.
char filename[] = "/tmp/hudkit_js_init_XXXXXX";
int fd = mkstemp(filename);
FILE *fp = fdopen(fd, "w+");
if (fp == NULL) {
fprintf(stderr, "Error opening temp file\n");
exit(1);
}
fprintf(fp, "window.Hudkit = {monitors:[");
for (int i = 0; i < nRectangles; ++i) {
GdkRectangle rect = rectangles[i];
fprintf(fp, "{x:%i,y:%i,width:%i,height:%i},\n", rect.x, rect.y, rect.width, rect.height);
}
fprintf(fp, "]};");
fseek(fp, 0, SEEK_SET);

char buffer[1000];
int nRead = fread(buffer, 1, 1000, fp);
buffer[nRead] = '\0';
fclose(fp);

webkit_web_view_run_javascript(web_view, buffer, NULL, web_view_javascript_finished, NULL);

gtk_widget_show_all(window);

// Hide the window, so we can get our properties ready without the window
Expand Down

0 comments on commit 9474ebd

Please sign in to comment.