#include #include #include #include #include "drw.h" #include "util.h" /* static function declarations */ static Color to_color(uint32_t color); static uint32_t to_uint32_t(Color color); Canvas* create_drw(struct wl_shm *shm, unsigned width, unsigned height) { struct wl_shm_pool *pool; Canvas *canvas; int stride; int fd; canvas = malloc(sizeof(Canvas)); canvas->width = width; canvas->height = height; stride = width * sizeof(uint32_t); canvas->size = stride * height; fd = allocate_shm_file(canvas->size); if (fd == -1) { return NULL; } canvas->data = mmap( NULL, canvas->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); if (canvas->data == MAP_FAILED) { close(fd); return NULL; } pool = wl_shm_create_pool( shm, fd, canvas->size ); canvas->buffer = wl_shm_pool_create_buffer( pool, 0, width, height, stride, WL_SHM_FORMAT_XRGB8888 ); wl_shm_pool_destroy(pool); close(fd); return canvas; } void free_drw(Canvas *canvas) { munmap(canvas->data, canvas->size); free(canvas); } void draw_point(Canvas *canvas, unsigned x, unsigned y, uint32_t color) { if (!(x < canvas->width && y < canvas->height)) return; canvas->data[x + canvas->width * y] = color; } void draw_rect(Canvas *canvas, unsigned x, unsigned y, unsigned width, unsigned height, uint32_t color) { int py; int px; for (py = y; py < y + height; ++py) { for (px = x; px < x + width; ++px) { draw_point(canvas, px, py, color); } } }