Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C library for GUIs that does not require its own event loop to be used?

I am looking for a GUI toolkit that I can use from plain C, that works at least on Linux and that does not force me to use its own eventloop – I want to use libev for the main loop and have it notify the toolkit library when X events come in or so.

I have not found anything like that – do I really have to patch a toolkit library to get what I want?

like image 482
thejh Avatar asked Nov 06 '13 15:11

thejh


2 Answers

Unfortunately this demand probably seriously restricts what GUI toolkits you can choose from, since they're all so bad in this regard (among many others). I don't know if this is quite fair as an answer, but I'd like to propose to you a different solution: let the GUI toolkit run whatever event loop it wants to run in its own thread or process. Since GUI libraries are notoriously bad (crashing or exiting without warning), the "own process" version might actually be the best idea -- you could communicate with your UI via a pipe, and roll your own event loop like you want to in the main process. Threads of course have their own benefits: no need to serialize data shared with the GUI, and no need to worry about the case where the user kills the main program without killing the GUI or vice versa (since threads cannot be killed individually).

like image 130
R.. GitHub STOP HELPING ICE Avatar answered Oct 18 '22 03:10

R.. GitHub STOP HELPING ICE


Nuklear.

https://github.com/Immediate-Mode-UI/Nuklear

Nuklear is a GUI toolkit that only creates widgets, buttons, labels and similar but does not use a render backend on its own. You have to provide the render backend for it. You can use Xlib/X11 for the rendering. Xlib does not need a main loop. You can do something like this:

  1. Create a connection to the X11-server and create a windows
  2. Init the windows (set up an event mask, load fonts and so on)
  3. Init a Nuklear context
  4. Check for events from the X11-socket and send them to Nuklear
  5. Draw your GUI to Nuklear
  6. Send the graphic primitives from Nuklear to the X11-Server.
  7. Do all the other things you need to do
  8. Use XConnectionNumber() to get the file descriptor for the X11-Socket
  9. Use pselect() to wait on this file descriptor for reading and everything else you wait on.
  10. Repeat at step 4.

Nuklear has an example header file that provides the functions needed to combine Nuklear and Xlib, that can help you with steps 4 and 6: https://github.com/Immediate-Mode-UI/Nuklear/blob/master/demo/x11/nuklear_xlib.h

Nuklear has this features and disadvantages:

  • You can have complete control over it.
  • It is written in C89
  • It does not need any external library by itself, not even the standard headers
  • single header implementation
  • You can use the render backend you want.
  • No unrelated functions like file handling or image loading. You need other libraries or write your own functions for that.
  • Graphical possibilities limited compared to toolkits like Qt
like image 38
12431234123412341234123 Avatar answered Oct 18 '22 01:10

12431234123412341234123