Implementing GTK4 in highgui

Hi everyone,

I’ve been working on a GTK4 backend for HighGUI and wanted to get some feedback before turning this into a formal PR, mainly around scope and whether some of the changes cross lines that shouldn’t be crossed.

the basic part is straightforward. detect gtk4 with pkg config add HAVE_GTK4 register a GTK4 backend and add a new window_gtk4.cpp that implements things natively using gtk4 apis drawing area snapshot gdktexture event controllers etc. that part feels pretty mechanical and similar to how gtk3 or qt were added in the past just without reusing any old gtk code

Here are where things get opinionated and require some input from people working on opencv: i also changed cvWaitKey so that it first goes through getCurrentUIBackend()->waitKeyEx() and only falls back to the old per-backend functions if no backend is active. this is what allows the gtk4 backend to own its event loop without adding a separate cvWaitKey_GTK4 path

inside window_gtk4.cpp i ended up routing the legacy C API entry points like cvNamedWindow cvShowImage cvDestroyWindow etc directly to the gtk4 backend instead of relying on the old gtk code. this avoids touching window_gtk.cpp at all for gtk4 but it does mean gtk4 implements those symbols itself

the gtk4 backend pumps the glib main context inside waitKeyEx using g_main_context_iteration to preserve the usual highgui blocking behavior

none of this changes behavior for existing backends but it does change how gtk4 integrates with the highgui dispatch logic so i wanted to check if this direction is acceptable or if some of these things should be done differently before submitting anything upstream. Thanks for reading

The changes i made :- 1. window.cpp
#elif defined(HAVE_GTK) to #elif defined(HAVE_GTK) && !defined(HAVE_GTK4)

2.changed cvWaitKey to prefer the backend path first
if (auto backend = cv::highgui_backend::getCurrentUIBackend())
return backend->waitKeyEx(delay);

3.inside window_gtk4.cpp i implemented the legacy C API entry points directly, for example
CV_IMPL int cvNamedWindow(…)
CV_IMPL void cvShowImage(…)
CV_IMPL void cvDestroyWindow(…)
and routed them to the gtk4 backend instead of relying on window_gtk.cpp

the gtk4 backend processes events by explicitly pumping the glib main context in waitKeyEx

4.i also explicitly disable cvStartWindowThread for gtk4

CV_IMPL int cvStartWindowThread() { return 0; }

I am just looking for validation or critique i guess to make appropriate changes here.

my recommendation: start an issue so you can get feedback on the plan now.

PRs out of the blue are not going to sidestep the planning and review.

It’s already an open issue: Implement GTK 4.0 support in highgui · Issue #27135 · opencv/opencv · GitHub

And wanted some direction on opinionated stuff..

Excellent! That issue is the place to discuss.

The forum is for user support. The developers rarely take a look in here.