rust-cgui 0.1.15

A low-level cross-platform GUI library
Documentation
#ifndef CGUI_HPP
#define CGUI_HPP
/*
 * C++ wrapper class for the CGui library.
 * Library not required at compile time, only at run time.
 * 
 * To build a single binary for deployment I suggest using a utility like
 * xd (http://www.fourmilab.ch/xd/) to quote:
 *   Finally, xd can read a binary file and emit a C language data declaration which contains the data from the file.
 *   This is handy when you wish to embed binary data within C programs.
 * Then extract to a temporary file and pass the library temp path to the CGui constructor.
 */

#include <iostream>
#include <dlfcn.h>
#include <cstdint>

// Function pointer types
typedef void* (*ptr_frm_void)(void);
typedef void  (*void_frm_voidp)(void*);
typedef char* (*charp_frm_voidp)(void*);
typedef void  (*v_frm_voidp_u32_x2_u8_x3)(void*, uint32_t, uint32_t, uint8_t, uint8_t, uint8_t);

// typedef void  (*add_callback_fun_ptr)(void* /*ptr to cwin*/, void (*)(void* /*callb ptr to cwinrep*/, char* /*null-term event name string*/));
// typedef void  (*call_callbacks_fun_ptr)(void* /*ptr to cwin*/, char* /*event name string*/);
// typedef void  (*ptr_frm_void)(void*);
// typedef void  (*run_main_loop_fun_ptr)(void*);
// typedef void  (*set_rep_exit_flag)(void*, bool);

// typedef void  (*write_px_fun)(void*, std::uint32_t, std::uint32_t, std::uint8_t, std::uint8_t, std::uint8_t);
// typedef void  (*redraw_dirty_fun)(void*);


class CGui {
public:
  // Screw OO rules, I want everything to be accessible
  static void* libcgui;
  
  ptr_frm_void alloc_win;
  void_frm_voidp init_win;
  charp_frm_voidp event_tick_win;
  v_frm_voidp_u32_x2_u8_x3 write_px_win;
  void_frm_voidp redraw_dirty_win;
  
  void* cgui_ptr;
  
  CGui(std::string lib_location="./target/release/libcgui.so") {
    // Get library pointer
    if (CGui::libcgui == nullptr) {
      CGui::libcgui = dlopen(lib_location.c_str(), RTLD_NOW);
      if (CGui::libcgui == nullptr) {
        std::cerr << "CGui: Fatal error, cannot open " << lib_location << std::endl;
      }
    }
    // Get some pointers to methods we'll be calling
    this->alloc_win =      (ptr_frm_void)             dlsym(libcgui, "alloc_win");
    this->init_win =       (void_frm_voidp)           dlsym(libcgui, "init");
    this->event_tick_win = (charp_frm_voidp)          dlsym(libcgui, "event_tick");
    this->write_px_win =   (v_frm_voidp_u32_x2_u8_x3) dlsym(libcgui, "write_px");
    this->redraw_dirty_win = (void_frm_voidp)           dlsym(libcgui, "redraw_dirty");
    
    // Constuct and get pointer to _this_ window
    this->cgui_ptr = (*this->alloc_win)();
    if (this->cgui_ptr == nullptr) {
      std::cerr << "CGui: Failed to allocate cgui_ptr" << std::endl;
    }
  }
  
  void init() {
    (*this->init_win)(this->cgui_ptr);
  }
  
  std::string event_tick() {
    return (*this->event_tick_win)(this->cgui_ptr);
  }
  
  void write_px(uint32_t x, uint32_t y, uint8_t red, uint8_t green, uint8_t blue) {
    (*this->write_px_win)(this->cgui_ptr, x, y, red, green, blue);
  }
  
  void redraw_dirty() {
    (*this->redraw_dirty_win)(this->cgui_ptr);
  }
  
private:
  
};

// "out-of-line" initialization - another detail that can go die
void* CGui::libcgui = nullptr;


#endif