GtkGLArea issues: context type, getProcAddress and key events

I’m replace native window with GtkGLArea, but there are some problems:

  1. how do i knows the type of gl context created by gtk, EGL or GLX?

  2. is there a api like eglGetProcAddress and glXGetProcAddress?

  3. gl area couldn’t receive key press/release events even if i add related event masks to the widget, test code is here:

#include <GL/gl.h>
#include <gtk/gtk.h>

static gboolean key_press(GtkGLArea *area, GdkEvent *event, gpointer data) {
  printf("key press: %d\n", ((GdkEventKey *)event)->keyval);
  return TRUE;
}

static gboolean key_release(GtkGLArea *area, GdkEvent *event, gpointer data) {
  printf("key release: %d\n", ((GdkEventKey *)event)->keyval);
  return TRUE;
}

static gboolean render(GtkGLArea *area, GdkGLContext *context, gpointer data) {
  printf("draw\n");
  GtkAllocation alloc;
  gtk_widget_get_allocation(GTK_WIDGET(area), &alloc);
  glViewport(0, 0, alloc.width, alloc.height);
  glClear(GL_COLOR_BUFFER_BIT);
  glClearColor(0.3, 0.3, 0.0, 1.0);
  glFlush();

  return TRUE;
}

int main(int argc, char **argv) {
  gtk_init(&argc, &argv);

  GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL),
            *gl_area = gtk_gl_area_new();
  gtk_widget_add_events(gl_area, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
  g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
  g_signal_connect(gl_area, "render", G_CALLBACK(render), NULL);
  g_signal_connect(gl_area, "key-press-event", G_CALLBACK(key_press), NULL);
  g_signal_connect(gl_area, "key-release-event", G_CALLBACK(key_release), NULL);

  gtk_container_add(GTK_CONTAINER(window), gl_area);
  gtk_widget_show_all(window);
  gtk_widget_grab_focus(GTK_WIDGET(gl_area)); 
  gtk_main();
  return 0;
}
``

i have solved problem 3: call gtk_widget_set_can_focus on the gl area widget.

1 Like

I believe the answer is: if you’re using X, its GLX, if you are using Wayland, it is EGL.

GTK uses libepoxy, so you can use the libepoxy API, like epoxy_has_glx(), epoxy_has_egl(), etc.

In general, GTK will create GLX contexts under X11, and EGL contexts under Wayland.

Use libepoxy.

To be fair, though, I have no idea what you’re trying to achieve. You should not ever know what kind of underlying windowing system API created a GL context; if you want to check for windowing system-specific extensions, you can use epoxy_has_glx_extension(), epoxy_has_egl_extension(), or epoxy_has_wgl_extension() before using them, just like you’d use epoxy_has_gl_extension().

@matthiasc @ebassi , i’m using a third party library to do opengl rendering, it requires a function likes eglGetProcAddress and glXGetProcAddress, so i wants a api such as gdk_gl_context_get_proc_address or a api to knows the context type.

I believe the answer is: if you’re using X, its GLX, if you are using Wayland, it is EGL.

thank you, this answer could solve my problem.

Sorry, we are not going to add that API to GDK: we have a hard dependency on libepoxy, and libepoxy already provides that API.