/* Minimal fbview module: the subset of the fbview shared-framebuffer API * the netSurf framebuffer frontend links against. Real /dev/fb0 mmap blit * only; no image/pdf/cairo backends (those live in the standalone fbview). */ #include "fb.h" #include #include #include #include #include #include #include #include int fbopen(FB *fb) { struct fb_var_screeninfo vi; struct fb_fix_screeninfo fi; if (!fb) return -1; memset(fb, 0, sizeof(*fb)); fb->fd = -1; fb->fd = open("/dev/fb0", O_RDWR); if (fb->fd < 0) return -1; if (ioctl(fb->fd, FBIOGET_VSCREENINFO, &vi) != 0 || ioctl(fb->fd, FBIOGET_FSCREENINFO, &fi) != 0) { close(fb->fd); fb->fd = -1; return -1; } fb->w = vi.xres; fb->h = vi.yres; fb->depth = vi.bits_per_pixel; fb->stride = fi.line_length; fb->smem = fi.smem_len ? fi.smem_len : (unsigned long)vi.yres_virtual * fi.line_length; fb->ox = 0; fb->oy = 0; fb->vw = fb->w; fb->vh = fb->h; fb->ptr = mmap(NULL, fb->smem, PROT_READ | PROT_WRITE, MAP_SHARED, fb->fd, 0); if (fb->ptr == MAP_FAILED) { fb->ptr = NULL; close(fb->fd); fb->fd = -1; return -1; } return 0; } int fbclose(FB *fb) { if (!fb) return -1; if (fb->ptr != NULL) { munmap(fb->ptr, fb->smem); fb->ptr = NULL; } if (fb->fd >= 0) { close(fb->fd); fb->fd = -1; } return 0; } int fbviewport(FB *fb, int x, int y, int w, int h) { if (!fb) return -1; fb->ox = x; fb->oy = y; fb->vw = w > 0 ? w : fb->w; fb->vh = h > 0 ? h : fb->h; return 0; } int fbviewportfull(FB *fb) { if (!fb) return -1; fb->ox = 0; fb->oy = 0; fb->vw = fb->w; fb->vh = fb->h; return 0; } int fbviewportpane(FB *fb) { const char *pane = getenv("TMUX_PANE"); int left, top, pw, ph, cw, ch; FILE *fp; int cell_w, cell_h; if (!fb) return -1; /* Without tmux the pane is the whole screen. */ if (!pane || !*pane) return 0; fp = popen("tmux display-message -p " "'#{pane_left} #{pane_top} #{pane_width} #{pane_height} " "#{client_width} #{client_height}' 2>/dev/null", "r"); if (!fp) return 0; if (fscanf(fp, "%d %d %d %d %d %d", &left, &top, &pw, &ph, &cw, &ch) == 6 && pw > 0 && ph > 0 && cw > 0 && ch > 0) { cell_w = fb->w / cw; cell_h = fb->h / ch; if (cell_w < 1) cell_w = 1; if (cell_h < 1) cell_h = 1; fb->ox = left * cell_w; fb->oy = top * cell_h; fb->vw = pw * cell_w; fb->vh = ph * cell_h; } pclose(fp); return 0; } int fbblit(FB *fb, const void *rgba, int sw, int sh, int ox, int oy, int w, int h) { const unsigned char *src = (const unsigned char *)rgba; int bpp = 4; int y; (void)sh; if (!fb || !fb->ptr || !src) return -1; if (fb->depth >= 24) bpp = 4; else if (fb->depth >= 15) bpp = 2; if (ox < 0) ox = 0; if (oy < 0) oy = 0; if (ox + w > fb->w) w = fb->w - ox; if (oy + h > fb->h) h = fb->h - oy; if (w <= 0 || h <= 0) return 0; for (y = 0; y < h; y++) { memcpy(fb->ptr + (size_t)(oy + y) * fb->stride + (size_t)ox * bpp, src + (size_t)y * sw * 4, (size_t)w * bpp); } return 0; } int fbclear(FB *fb) { if (!fb || !fb->ptr) return -1; memset(fb->ptr, 0, (size_t)fb->h * fb->stride); return 0; } int fbfill(FB *fb, unsigned char r, unsigned char g, unsigned char b) { unsigned char px[4]; int x, y; if (!fb || !fb->ptr) return -1; px[0] = b; px[1] = g; px[2] = r; px[3] = 0; for (y = 0; y < fb->h; y++) { for (x = 0; x < fb->w; x++) { memcpy(fb->ptr + (size_t)y * fb->stride + (size_t)x * 4, px, 4); } } return 0; } int fbpix(FB *fb, int x, int y, unsigned char r, unsigned char g, unsigned char b) { if (!fb || !fb->ptr) return -1; if (x < 0 || y < 0 || x >= fb->w || y >= fb->h) return -1; fb->ptr[(size_t)y * fb->stride + (size_t)x * 4 + 0] = b; fb->ptr[(size_t)y * fb->stride + (size_t)x * 4 + 1] = g; fb->ptr[(size_t)y * fb->stride + (size_t)x * 4 + 2] = r; return 0; } int fbviewimg(FB *fb, void *pix, int w, int h) { (void)fb; (void)pix; (void)w; (void)h; return -1; } int fbimgload(FB *fb, const char *path) { (void)fb; (void)path; return -1; } int fbimgdraw(FB *fb) { (void)fb; return -1; } const char *fbvt_name(void) { return NULL; } int fbvt_active(void) { return 1; }