Posts

Showing posts from December, 2021

Offscreen rendering in GTK

Image
  GTK provides a simple OffscreenWindow API to render content offscreen and then paint a widget with that content on demand. This is important if you want to avoid flickering or don't want to show user the content till it's fully ready. The "draw" event handler of gtk_drawing_area_new widget widget draws webview widget on the cairo surface passed to the handler using gtk_widget_draw (GTK_WIDGET (webView), cr) . Keyboard, mouse and scroll events are copied and forwarded to the webview widget. We can listen to WebKitWebView 's "load-changed" event for blocklist managment. ./browser "http://www.google.com" browser.c #include <gtk/gtkx.h> #include <webkit2/webkit2.h> static gboolean refresh_widget (GtkWidget *widget, GdkEvent *event,                         gpointer xv_widget) {   if (GTK_IS_WIDGET (xv_widget))     gtk_widget_queue_dra...

Browser in GNU Emacs

Image
There are two ways to browse rich web on GNU Emacs - both of them involves xwidget. Notice the different symbols ( widget and socket ) passed to (xwidget-insert) command. Apart from the technical difference, widget is keyboard oriented and socket is mouse oriented from usability perspective. Inbuilt webkitgtk2 widget Since it's inbuilt (runs in same process as Emacs), it is better integrated with Emacs compared to the other option. There are keyboard commands to manage the browsing. Though this flickers a lot to suit my taste. Also, input handling is a bit cumbersome.   (setq w (xwidget-insert (point) ' webkit "test" 400 600)) External Surf browser Surf browser also uses webkitgtk2. It's the only browser which supports XEmbed protocol. This means you can use mouse and keyboard normally as you'd do while browsing web.   With my patch , you should be able to invoke surf as below:   (setq w (xwidget-insert (point) ' socket "test" 400 600 '(...

Build your own browser

Image
Build your own browser using GTK and webkit2. ./browser "http://www.google.com" To add content blocking, connect to the "decide-policy" signal and use webkit_policy_decision_ignore() . browser.c #include <gtk/gtkx.h> #include <webkit2/webkit2.h> int main(int argc, char* argv[]) {   // Initialize GTK+   gtk_init(&argc, &argv);   // Create an 800x600 window that will contain the browser instance   GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);   g_signal_connect(window, "delete-event", gtk_main_quit, NULL);   gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);        // Create a browser instance   WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());   gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(webView));   // Load a web page into the browser instance   webkit_web_view_load_uri(webView, argv[1]);   // Make sure the main window and all its contents are visib...

gVim in GNU Emacs

Image
  You can also embed gVim in GNU Emacs (GTK build with X11 backend). There are two things to take care of in this case - moving focus to the embedded widget and redirecting keyboard input to the widget. A GTK widget can only have focus if it's set that way (remember there are multiple widgets in a window): // Allow to receive keyboard input gtk_widget_set_can_focus (widget, TRUE); // Receive keyboard input gtk_widget_grab_focus (widget);   Emacs filters keyboard events (KeyPress) before passing it to GTK. That's how C-g always works in Emacs. When Emacs receives XEMBED_REQUEST_FOCUS (event->type == ClientMessage && event->xclient.message_type == dpyinfo->Xatom_XEMBED && event->xclient.data.l[1] == XEMBED_REQUEST_FOCUS) , we need to set input focus to the widget using XSetInputFocus() using the handle event->xclient.window . Additionally, we set a flag that a widget is active. Whenever this flag is active, we stop filtering keyboard events. T...

Embedding GTK applications via XEmbed - 2

Image
In previous post , we learnt the basics of embedding a GTK application in another application. This works fine when the embedded application supports client server architecture like gVim. Note that in this case the client mode invocations are short lived processes while server process runs throughout the lifetime of the application. In contrast, an application like VLC provides a remote control interface to run  multiple operations in the same process. In this case, we need to obtain an input channel of the process and write our commands to the same. Here's an updated version of the code. Remember to update the two bold paths with appropriate video locations. The application starts with one video and then switches to the next one when the button is clicked. embed.c #include <gtk/gtkx.h> gchar *string; GMutex mutex; void send_hello(GtkButton *btn) {   /* Note the \n at the end */   string = g_strdup_printf("add /home/anand/Downloads/test.mp4 \n"); } static vo...

Debian: No graphical display after hibernate

After a recent upgrade on debian bullseye (Debian 5.10.84-1 (2021-12-08)), I was unable to get a graphical display after resuming from hibernate. Though terminal login worked just fine. Seems like it's a problem with NetworkManager changing hostname. It will change it if the hostname variable is not set in configuration file. Following advice at https://bbs.archlinux.org/viewtopic.php?pid=684936#p684936 , I added following two lines in /etc/NetworkManager/NetworkManager.conf   [keyfile] hostname = <your hostname>        

Embedding SDL widget in GTK application

  Here's an updated version of Embedding SDL widget in GTK application using SDL2 and GTK 3.0. embed.c #include <stdio.h> #include <gtk/gtkx.h> #include <SDL.h> #include <stdbool.h> static SDL_Window *sdl_window; static SDL_Renderer *sdl_renderer; static GtkWindow *gtk_window; static GtkWidget *gtk_da; static void *gdk_window; static void *window_id; static gboolean idle(void *ud) {     if(!sdl_window) {         printf("creating SDL window for window id %p\n", window_id);         sdl_window = SDL_CreateWindowFrom(window_id);         printf("sdl_window=%p\n", sdl_window);         if(!sdl_window) {             printf("%s\n", SDL_GetError());         }         sdl_renderer = SDL_Creat...

Embedding GTK applications via XEmbed

Image
Together with GtkPlug , GtkSocket provides the ability to embed widgets from one process into another process in a fashion that is transparent to the user. One process creates a GtkSocket widget and passes that widget’s window ID to the other process, which then creates a GtkPlug with that window ID . Any widgets contained in the GtkPlug then will appear inside the first application’s window. Here's an updated version of Embedding applications via XEmbed using GTK 3.0. Reference Here are commands for running some GTK applications in embedded mode: gvim --servername %d --socketid %d #GVim server gvim --servername %d --remote-send 'GoHello, World!<Esc><C-O>' #GVim client cvlc --drawable-xid %d out.mp4     #VLC xterm -into %lu    # Terminal emulator mpv --wid=%lu url     # mpv video player surf -e %lu   # Surf web browser from Suckless based on webkitgtk2 embed.c #include <gtk/gtkx.h> #define EXPR "'GoHel...