Skip to content

Instantly share code, notes, and snippets.

@vain

vain/Makefile Secret

Created May 14, 2017 10:27
Show Gist options
  • Save vain/e861bce9536410aa10d46948d6bd48c8 to your computer and use it in GitHub Desktop.
Save vain/e861bce9536410aa10d46948d6bd48c8 to your computer and use it in GitHub Desktop.
webkit2gtk mouse gestures
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>
struct Client
{
GtkWidget *web_view;
GtkWidget *win;
gdouble mouse_down_x, mouse_down_y;
};
gboolean
key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
{
struct Client *c = (struct Client *)data;
gdouble dx, dy;
gdouble rx, ry;
if (event->type == GDK_BUTTON_PRESS)
{
switch (((GdkEventButton *)event)->button)
{
case 3:
c->mouse_down_x = ((GdkEventButton *)event)->x;
c->mouse_down_y = ((GdkEventButton *)event)->y;
/* XXX Returning TRUE prevents the context menu from
* showing up. */
return TRUE;
}
}
else if (event->type == GDK_BUTTON_RELEASE)
{
if ((((GdkEventButton *)event)->button) == 3)
{
rx = ((GdkEventButton *)event)->x;
ry = ((GdkEventButton *)event)->y;
dx = c->mouse_down_x - rx;
dy = c->mouse_down_y - ry;
if (abs(dx) > abs(dy))
{
if (dx > 30) /* navigate backward */
{
webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
return TRUE;
}
else if (dx < -30) /* navigate forward */
{
webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
return TRUE;
}
}
else
{
if (dy > 30) /* reload */
{
webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->web_view));
return TRUE;
}
else if (dy < -30) /* new tab */
{
printf("opening new tab, not implemented here\n");
return TRUE;
}
}
/* XXX If we end up here, we should open webkit's context
* menu, because the right mouse button was released but it
* was not a valid/known gesture. */
}
}
return FALSE;
}
int
main(int argc, char **argv)
{
struct Client c;
gtk_init(&argc, &argv);
c.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
c.web_view = webkit_web_view_new();
g_signal_connect(G_OBJECT(c.win), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(c.web_view), "close",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(c.web_view), "button-press-event",
G_CALLBACK(key_web_view), &c);
g_signal_connect(G_OBJECT(c.web_view), "button-release-event",
G_CALLBACK(key_web_view), &c);
gtk_container_add(GTK_CONTAINER(c.win), c.web_view);
gtk_widget_show_all(c.win);
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c.web_view),
"https://www.google.com");
gtk_main();
exit(EXIT_SUCCESS);
}
CFLAGS += -Wall -Wextra -Wno-unused-parameter -O3
browser: browser.c
$(CC) $(CFLAGS) $(LDFLAGS) \
-o $@ $< \
`pkg-config --cflags --libs gtk+-3.0 glib-2.0 webkit2gtk-4.0`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment