loop.c 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include <sys/select.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

void gui_loop(gui *_gui)
{
  struct gui *g = _gui;
  int xfd;
  int eventfd;
  int maxfd;
  fd_set rd;

19 20 21 22
  /* lock not necessary but there for consistency (when instrumenting x.c
   * we need the gui to be locked when calling any function in x.c)
   */
  glock(g);
23
  xfd = x_connection_fd(g->x);
24
  gunlock(g);
25 26 27 28 29 30
  eventfd = g->event_pipe[0];

  if (eventfd > xfd) maxfd = eventfd;
  else               maxfd = xfd;

  while (1) {
31
    glock(g);
32
    x_flush(g->x);
33
    gunlock(g);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    FD_ZERO(&rd);
    FD_SET(xfd, &rd);
    FD_SET(eventfd, &rd);
    if (select(maxfd+1, &rd, NULL, NULL, NULL) == -1)
      ERR("select: %s\n", strerror(errno));

    glock(g);

    if (FD_ISSET(xfd, &rd))
      x_events(g);

    if (FD_ISSET(eventfd, &rd)) {
      char c[256];
      if (read(eventfd, c, 256)); /* for no gcc warnings */
    }

    gui_events(g);

    if (g->repainted) {
      struct widget_list *cur;
      g->repainted = 0;
      cur = g->toplevel;
      while (cur) {
        struct toplevel_window_widget *w =
            (struct toplevel_window_widget *)cur->item;
        x_draw(g->x, w->x);
        cur = cur->next;
      }
    }

    gunlock(g);
  }
}