Blob Blame Raw

#include "app.h"

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>



int appInit(App *app) {
  LOGDBG("app: init");
  app->run = 0;
  app->x = app->y = app->w = app->h = 0;
  app->irx = app->iry = app->irw = app->irh = 0;
  
  LOGDBG("app: init: connect to xcb");
  app->xcb = xcb_connect(NULL, NULL);
  if (!app->xcb)
    return LOGERR("app: init: cannot connect to xcb");
  
  LOGDBG("app: init: get screen");
  app->screen = xcb_setup_roots_iterator(xcb_get_setup(app->xcb)).data;
  if (app->screen) {
    LOGDBG("app: init: create window");
    
    app->x = 0;
    app->w = app->screen->width_in_pixels;
    app->h = app->screen->height_in_pixels/4;
    app->y = app->screen->height_in_pixels - app->h;
    
    #ifdef NOBORDER
    int noborder = 1;
    #else
    int noborder = 0;
    #endif
    
    app->win = xcb_generate_id(app->xcb);
    uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
    uint32_t values[] = {
      noborder,                         // override redirect
        XCB_EVENT_MASK_EXPOSURE         // event mask
      | XCB_EVENT_MASK_STRUCTURE_NOTIFY
      | XCB_EVENT_MASK_BUTTON_PRESS
      | XCB_EVENT_MASK_BUTTON_1_MOTION
      | XCB_EVENT_MASK_BUTTON_RELEASE };
    xcb_create_window(
      app->xcb,                      // connection
      XCB_COPY_FROM_PARENT,          // depth (same as root)
      app->win,                      // window id
      app->screen->root,             // parent window
      app->x, app->y,                // origin
      app->w, app->h,                // size
      10,                            // border_width
      XCB_WINDOW_CLASS_INPUT_OUTPUT, // class
      app->screen->root_visual,      // visual
      mask,                          // additional options mask
      values );                      // additional options
    
    uint32_t hints[9] = {
      1, // mask for input hints
      0, // value of input hint
    };
    xcb_change_property_checked(
      app->xcb,
      XCB_PROP_MODE_REPLACE,
      app->win,
      XCB_ATOM_WM_HINTS,
      XCB_ATOM_WM_HINTS,
      32,
      sizeof(hints)/sizeof(hints[0]), hints );
    
    // init submodules
    if (graphInit(&app->graph, app)) {
      graphResize(&app->graph);
      if (inputInit(&app->input, app)) {
        if (keyboardInit(&app->keyboard, app)) {
          return 1;
        }
        inputDeinit(&app->input);
      }
      graphDeinit(&app->graph);
    }
  } else {
    LOGERR("app: init: no screen found");
  }
  
  xcb_disconnect(app->xcb);
  return 0;
}


void appDeinit(App *app) {
  LOGDBG("app: deinit");
  keyboardDeinit(&app->keyboard);
  inputDeinit(&app->input);
  graphDeinit(&app->graph);
  xcb_disconnect(app->xcb);
}


int appRun(App *app) {
  LOGDBG("app: run");

  if (app->run)
    return LOGERR("app: run: seems already run");
  app->run = 1;
  
  LOGDBG("app: run: show window");
  xcb_map_window(app->xcb, app->win);

  LOGDBG("app: run: event loop");

  int fd = xcb_get_file_descriptor(app->xcb);
  if (fd < 0)
    LOGERR("app: run: cannot get xcb file descruptor");
    
  int buttonDown = 0;
  unsigned int buttonDownMs = 0;
  while(1) {
    xcb_generic_event_t *event = NULL;
    xcb_flush(app->xcb);
    
    if (buttonDown == 1) {
      event = xcb_poll_for_event(app->xcb);
      if (!event) {
        // manually call select for the X-socket to use timeout
        unsigned int dt = monotonicMs() - buttonDownMs;
        if (dt < LONGPRESS_MS) {
          dt = LONGPRESS_MS - dt;
          struct timeval t = {};
          t.tv_sec = dt/1000;
          t.tv_usec = dt%1000*1000;
 
          LOGDBG("app: run: use select fd=%d, timeout=%ums, sec=%ld, usec=%ld", fd, dt, t.tv_sec, t.tv_usec);

          xcb_flush(app->xcb);
          
          fd_set fds = {};
          FD_ZERO(&fds);
          FD_SET(fd, &fds);
          select(fd + 1, &fds, NULL, NULL, &t);
          
          event = xcb_poll_for_event(app->xcb);
        }
      }
    } else {
      event = xcb_wait_for_event(app->xcb);
      if (!event) break;
    }
    
    if (event) {
      switch(event->response_type & ~0x80) {
      case XCB_EXPOSE: {
        xcb_expose_event_t *e = (xcb_expose_event_t*)event;
        LOGDBG("app: expose: x=%d, y=%d, w=%d, h=%d", e->x, e->y, e->width, e->height);
        appInvalidateRect(app, e->x, e->y, e->width, e->height);
        break;
      }
      case XCB_CONFIGURE_NOTIFY: {
        xcb_configure_notify_event_t *e = (xcb_configure_notify_event_t*)event;
        if ( e->width
          && e->height
          && ( app->x != e->x
            || app->y != e->y
            || app->w != e->width
            || app->h != e->height ))
        {
          LOGDBG("app: moved: x=%d y=%d w=%d, h=%d", e->x, e->y, e->width, e->height);
          int resized = app->w != e->width || app->h != e->height;
          app->x = e->x;
          app->y = e->y;
          app->w = e->width;
          app->h = e->height;
          if (resized) graphResize(&app->graph);
        }
        break;
      }
      case XCB_BUTTON_PRESS: {
        xcb_button_press_event_t *e = (xcb_button_press_event_t*)event;
        if (e->detail == 1) {
          LOGDBG("app: mouse down: x=%d, y=%d", e->event_x, e->event_y);
          if (!buttonDown) { buttonDown = 1; buttonDownMs = monotonicMs(); }
          keyboardMouseDown(&app->keyboard, e->event_x, e->event_y);
        }
        break;
      }
      case XCB_BUTTON_RELEASE: {
        xcb_button_press_event_t *e = (xcb_button_press_event_t*)event;
        if (e->detail == 1) {
          LOGDBG("app: mouse up");
          buttonDown = 0;
          keyboardMouseUp(&app->keyboard);
        }
        break;
      }
      case XCB_MOTION_NOTIFY: {
        xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t*)event;
        if (e->state & Button1Mask) {
          LOGDBG("app: mouse motion: x=%d, y=%d", e->event_x, e->event_y);
          keyboardMouseMotion(&app->keyboard, e->event_x, e->event_y);
        }
        break;
      }
      default: 
          break;
      }
      free(event);
    }
    
    if (buttonDown == 1 && monotonicMs() - buttonDownMs >= LONGPRESS_MS) {
        LOGDBG("app: long press");
        buttonDown = 2;
        keyboardMouseLongDown(&app->keyboard);
    }
    
    if (app->run != 1) break;
    
    inputUpdateModifiers(&app->input);
    keyboardResize(&app->keyboard);
    keyboardUpdateModifiers(&app->keyboard);
    if (app->irw > 0 && app->irh > 0) {
      LOGDBG("app: draw: x=%d, y=%d, w=%d, h=%d", app->irx, app->iry, app->irw, app->irh);
      graphDrawBackgound(&app->graph, app->irx, app->iry, app->irw, app->irh);
      keyboardDraw(&app->keyboard, app->irx, app->iry, app->irw, app->irh);
      app->irw = 0;
    }
  }
  
  int err = app->run == -2;
  app->run = 0;
  LOGDBG("app: run: done err=%d", err);
  return !err;
}


void appStop(App *app, int err) {
  LOGDBG("app: stop: err=%d", err);
  if (!app->run) {
    LOGERR("app: stop: seems not started");
    return;
  }
  if (app->run == 1) app->run = -1;
  if (err) app->run = -2;
}


void appMove(App *app, int x, int y, int w, int h) {
  LOGDBG("app: move: x=%d, y=%d, w=%d, h=%d", x, y, w, h);
  if (w <= 0 || h <= 0) return;
  if (app->x == x && app->y == y && app->w == w && app->h == h)
    return;
  uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
  uint32_t values[] = { x, y, w, h };
  xcb_configure_window(app->xcb, app->win, mask, values);
  xcb_flush(app->xcb);
}


void appInvalidateRect(App *app, int x, int y, int w, int h) {
  LOGDBG("app: invalidate rect: x=%d, y=%d, w=%d, h=%d", x, y, w, h);
  rectIntersect(&x, &y, &w, &h, 0, 0, app->w, app->h);
  rectMerge(&app->irx, &app->iry, &app->irw, &app->irh, x, y, w, h);
  LOGDBG("app: invalidate rect: summary x=%d, y=%d, w=%d, h=%d", app->irx, app->iry, app->irw, app->irh);
}