neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

commit 2a4a013ba2f9959e2ade2c154147dfa9ed55c059
parent ac63183b41a8034c0099f20e6164a343167f24bc
Author: shrub <maybeshrub@gmail.com>
Date:   Sun,  9 Aug 2026 23:01:22 +0000

make drm optional, add fb video on linux fbdev

Diffstat:
MREADME.md | 11+++++++----
Mlaunch/devmajor-linux.c | 8++++++++
Mlaunch/devmajor.h | 4++++
Mlaunch/launch.c | 22+++++++++++++++++++++-
Mlaunch/meson.build | 2+-
Alibswc/backend.h | 17+++++++++++++++++
Mlibswc/compositor.c | 41++++++++++++++++++++++++++---------------
Mlibswc/decor.c | 3++-
Mlibswc/drm.c | 5+++++
Mlibswc/drm.h | 3+++
Alibswc/fb.c | 142+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibswc/fb.h | 32++++++++++++++++++++++++++++++++
Alibswc/fb_fbdev.c | 185+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibswc/internal.h | 3+++
Mlibswc/meson.build | 6+++---
Mlibswc/mode.c | 12++++++++++++
Mlibswc/mode.h | 9+++++++++
Mlibswc/output.c | 39++++++++++++++++++++++++++++++++++++++-
Mlibswc/output.h | 8++++++++
Mlibswc/pointer.c | 29+++++++++++++++++++++++------
Mlibswc/primary_plane.c | 44++++++++++++++++++++++++++++++++++++++++++--
Mlibswc/primary_plane.h | 13++++++++++++-
Mlibswc/screen.c | 34+++++++++++++++++++++++++++++++---
Mlibswc/screen.h | 7+++++++
Mlibswc/swc.c | 26++++++++++++++++++++++++--
Mmeson.build | 25+++++++++++++++++++++++--
Mmeson.options | 2++
27 files changed, 690 insertions(+), 42 deletions(-)

diff --git a/README.md b/README.md @@ -1,7 +1,7 @@ neuswc ------ -neuswc is a fork of [swc](https://github.com/michaelforney/swc/) created by [wayland.fyi](https://wayland.fyi). it provides an easy C library interface to make a wayland compositor. it is much smaller than alternatives like wlroots, and easier to use. it is also portable to linux, freebsd, netbsd, and openbsd. if you want tohelp with that (or anything else) submit a patch to the [mailing list](https://lists.sr.ht/~shrub900/neuswc) +neuswc is a fork of [swc](https://github.com/michaelforney/swc/) created by [wayland.fyi](https://wayland.fyi). it provides an easy C library interface to make a wayland compositor. it is much smaller than alternatives like wlroots, and easier to use. it is also portable to linux, freebsd, netbsd, and openbsd. if you want to help with that (or anything else) submit a patch to the [mailing list](https://lists.sr.ht/~shrub900/neuswc) you can view a list of some compositors made with neuswc at the [wayland.fyi website.](https://wayland.fyi) @@ -20,6 +20,7 @@ neu features - screenshots - layer shell support - window decorations +- framebuffer support neuswc is in active development, we plan to add many more features, and increase compatibility with new wayland protocols. @@ -32,7 +33,8 @@ you will need: - pkg-config - wayland-scanner, wayland-server, wayland-client - wayland-server, wayland-client -- libdrm, pixman, xkbcommon +- libdrm (if building with DRM support) +- pixman, xkbcommon - [neuwld](https://git.sr.ht/~shrub900/neuwld) - libinput on Linux and wscons on BSD - xcb, xcb-composite, xcb-ewmh and xcb-icccm if you want Xwayland support. @@ -52,5 +54,6 @@ an extremely large thank you to [michael forney](https://mforney.org) for creati repositories ------------ -[srcdump](https://srcdump.net/shrub/neuswc) -[sourcehut](https://git.sr.ht/~shrub900/neuswc) + +- [srcdump](https://srcdump.net/shrub/neuswc) +- [sourcehut](https://git.sr.ht/~shrub900/neuswc) diff --git a/launch/devmajor-linux.c b/launch/devmajor-linux.c @@ -50,3 +50,11 @@ device_is_drm(dev_t rdev) { return major(rdev) == DRM_MAJOR; } + +#ifdef ENABLE_FBDEV +bool +device_is_fbdev(dev_t rdev) +{ + return major(rdev) == FB_MAJOR; +} +#endif diff --git a/launch/devmajor.h b/launch/devmajor.h @@ -33,4 +33,8 @@ bool device_is_tty(dev_t); bool device_is_drm(dev_t); +#ifdef ENABLE_FBDEV +bool device_is_fbdev(dev_t); +#endif + #endif diff --git a/launch/launch.c b/launch/launch.c @@ -34,6 +34,7 @@ #include <poll.h> #include <signal.h> #include <spawn.h> +#include <stdarg.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -69,7 +70,9 @@ #include <dev/wscons/wsdisplay_usl_io.h> #endif +#ifdef ENABLE_DRM #include <xf86drm.h> +#endif #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0]) @@ -81,7 +84,9 @@ deactivate(void); static bool nflag; static int sigfd[2], sock[2]; static int input_fds[128], num_input_fds; +#ifdef ENABLE_DRM static int drm_fds[16], num_drm_fds; +#endif static int tty_fd; static bool active; @@ -123,6 +128,7 @@ die(const char *format, ...) static void start_devices(void) { +#ifdef ENABLE_DRM int i; for (i = 0; i < num_drm_fds; ++i) { @@ -130,6 +136,7 @@ start_devices(void) die("failed to set DRM master"); } } +#endif } static void @@ -137,11 +144,13 @@ stop_devices(bool fatal) { int i; +#ifdef ENABLE_DRM for (i = 0; i < num_drm_fds; ++i) { if (drmDropMaster(drm_fds[i]) < 0 && fatal) { die("drmDropMaster:"); } } +#endif for (i = 0; i < num_input_fds; ++i) { #ifdef EVIOCREVOKE if (ioctl(input_fds[i], EVIOCREVOKE, 0) < 0 && errno != ENODEV && @@ -278,13 +287,24 @@ handle_socket_data(int socket) #endif input_fds[num_input_fds++] = fd; } else if (device_is_drm(st.st_rdev)) { +#ifdef ENABLE_DRM if (num_drm_fds == ARRAY_LENGTH(drm_fds)) { fprintf(stderr, "too many DRM devices opened\n"); goto fail; } drm_fds[num_drm_fds++] = fd; +#else + fprintf(stderr, "DRM device requested by non-DRM backend\n"); + goto fail; +#endif +#ifdef ENABLE_FBDEV + } else if (device_is_fbdev(st.st_rdev)) { + if (!active) { + goto fail; + } +#endif } else { - fprintf(stderr, "requested fd is not a DRM or input device\n"); + fprintf(stderr, "requested fd is not a video or input device\n"); goto fail; } break; diff --git a/launch/meson.build b/launch/meson.build @@ -1,7 +1,7 @@ launch_protocol = files('protocol.c') executable('swc-launch', ['launch.c', launch_protocol, 'devmajor-@0@.c'.format(os)], - dependencies: requires_private[0], # libdrm + dependencies: video_backend == 'drm' ? drm : [], install: true, install_mode: 'rwsr-xr-x', ) diff --git a/libswc/backend.h b/libswc/backend.h @@ -0,0 +1,17 @@ +#ifndef SWC_BACKEND_H +#define SWC_BACKEND_H + +#include <stdint.h> + +struct wld_context; +struct wld_renderer; + +/* rendering state shared by the drm and fb backends */ +struct swc_backend { + struct wld_context *context; + struct wld_renderer *renderer; + uint32_t cursor_width; + uint32_t cursor_height; +}; + +#endif diff --git a/libswc/compositor.c b/libswc/compositor.c @@ -28,9 +28,12 @@ */ #include "compositor.h" +#include "backend.h" #include "data_device_manager.h" #include "decor.h" +#ifdef ENABLE_DRM #include "drm.h" +#endif #include "event.h" #include "internal.h" #include "launch.h" @@ -52,7 +55,9 @@ #include <limits.h> #include <stdio.h> #include <stdlib.h> +#ifdef ENABLE_DRM #include <wld/drm.h> +#endif #include <wld/wld.h> #include <xkbcommon/xkbcommon-keysyms.h> @@ -214,8 +219,14 @@ target_new(struct screen *screen) } target->surface = - wld_create_surface(swc.drm->context, geom->width, geom->height, - WLD_FORMAT_XRGB8888, WLD_DRM_FLAG_SCANOUT); + wld_create_surface(swc.backend->context, geom->width, geom->height, + WLD_FORMAT_XRGB8888, +#ifdef ENABLE_DRM + WLD_DRM_FLAG_SCANOUT +#else + WLD_FLAG_MAP +#endif + ); if (!target->surface) { goto error1; @@ -290,7 +301,7 @@ repaint_view(struct target *target, struct compositor_view *view, pixman_region32_translate(&buffer_damage, -geom->x + view->buffer_offset_x, -geom->y + view->buffer_offset_y); - wld_copy_region(swc.drm->renderer, view->buffer, buf_x - target_geom->x, + wld_copy_region(swc.backend->renderer, view->buffer, buf_x - target_geom->x, buf_y - target_geom->y, &buffer_damage); } @@ -321,12 +332,12 @@ repaint_view(struct target *target, struct compositor_view *view, if (view->border.outwidth > 0 && pixman_region32_not_empty(&out_border)) { pixman_region32_translate(&out_border, -target_geom->x, -target_geom->y); - wld_fill_region(swc.drm->renderer, view->border.outcolor, &out_border); + wld_fill_region(swc.backend->renderer, view->border.outcolor, &out_border); } if (view->border.inwidth > 0 && pixman_region32_not_empty(&in_border)) { pixman_region32_translate(&in_border, -target_geom->x, -target_geom->y); - wld_fill_region(swc.drm->renderer, view->border.incolor, &in_border); + wld_fill_region(swc.backend->renderer, view->border.incolor, &in_border); } pixman_region32_fini(&border_damage); @@ -336,7 +347,7 @@ repaint_view(struct target *target, struct compositor_view *view, if ((view->decor.top || view->decor.right || view->decor.bottom || view->decor.left)) { - decor_repaint(swc.drm->renderer, target_geom, view, damage); + decor_repaint(swc.backend->renderer, target_geom, view, damage); } } @@ -400,12 +411,12 @@ renderer_repaint(struct target *target, pixman_region32_t *damage, target->view->geometry.x, target->view->geometry.y, target->view->geometry.width, target->view->geometry.height); - wld_set_target_surface(swc.drm->renderer, target->surface); + wld_set_target_surface(swc.backend->renderer, target->surface); if (pixman_region32_not_empty(base_damage)) { pixman_region32_translate(base_damage, -target->view->geometry.x, -target->view->geometry.y); - wld_fill_region(swc.drm->renderer, DEFAULT_BG, + wld_fill_region(swc.backend->renderer, DEFAULT_BG, base_damage); } @@ -416,9 +427,9 @@ renderer_repaint(struct target *target, pixman_region32_t *damage, } } - draw_overlays(swc.drm->renderer, target_geom); + draw_overlays(swc.backend->renderer, target_geom); - wld_flush(swc.drm->renderer); + wld_flush(swc.backend->renderer); } static int @@ -427,7 +438,7 @@ renderer_attach(struct compositor_view *view, struct wld_buffer *client_buffer) struct wld_buffer *buffer; bool was_proxy = view->buffer != view->base.buffer; bool needs_proxy = - client_buffer && !(wld_capabilities(swc.drm->renderer, client_buffer) & + client_buffer && !(wld_capabilities(swc.backend->renderer, client_buffer) & WLD_CAPABILITY_READ); bool resized = view->buffer && client_buffer && (view->buffer->width != client_buffer->width || @@ -440,7 +451,7 @@ renderer_attach(struct compositor_view *view, struct wld_buffer *client_buffer) if (!was_proxy || resized) { DEBUG("Creating a proxy buffer\n"); buffer = wld_create_buffer( - swc.drm->context, client_buffer->width, + swc.backend->context, client_buffer->width, client_buffer->height, client_buffer->format, WLD_FLAG_MAP); if (!buffer) { @@ -1656,9 +1667,9 @@ update_screen(struct screen *screen) pixman_region32_t full; pixman_region32_init_rect(&full, 0, 0, geom->width, geom->height); - wld_set_target_surface(swc.drm->renderer, target->surface); - wld_copy_region(swc.drm->renderer, zoomed, 0, 0, &full); - wld_flush(swc.drm->renderer); + wld_set_target_surface(swc.backend->renderer, target->surface); + wld_copy_region(swc.backend->renderer, zoomed, 0, 0, &full); + wld_flush(swc.backend->renderer); pixman_region32_fini(&full); wld_buffer_unreference(zoomed); diff --git a/libswc/decor.c b/libswc/decor.c @@ -1,4 +1,5 @@ #include "decor.h" +#include "backend.h" #include "compositor.h" #include "drm.h" @@ -210,7 +211,7 @@ copy_decor_part(struct decor_part_buffer *dst, const struct swc_decor_part *src) dst->height = src->height; dst->stride = src->stride; /* DRM renderers only allow read support for their native buffers not pixman/shmbuffers */ - dst->buffer = wld_create_buffer(swc.drm->context, src->width, src->height, + dst->buffer = wld_create_buffer(swc.backend->context, src->width, src->height, WLD_FORMAT_ARGB8888, WLD_FLAG_MAP); if (!dst->buffer) { free(dst->data); diff --git a/libswc/drm.c b/libswc/drm.c @@ -290,6 +290,9 @@ drm_initialize(void) val = 64; } swc.drm->cursor_h = val; + swc.backend = &swc.drm->backend; + swc.backend->cursor_width = swc.drm->cursor_w; + swc.backend->cursor_height = swc.drm->cursor_h; drm.path = drmGetRenderDeviceNameFromFd(swc.drm->fd); if (!drm.path) { @@ -306,6 +309,8 @@ drm_initialize(void) ERROR("Could not create WLD DRM renderer\n"); goto error2; } + swc.backend->context = swc.drm->context; + swc.backend->renderer = swc.drm->renderer; drm.event_source = wl_event_loop_add_fd( swc.event_loop, swc.drm->fd, WL_EVENT_READABLE, &handle_data, NULL); diff --git a/libswc/drm.h b/libswc/drm.h @@ -1,6 +1,8 @@ #ifndef SWC_DRM_H #define SWC_DRM_H +#include "backend.h" + #include <stdbool.h> #include <stdint.h> @@ -12,6 +14,7 @@ struct drm_handler { }; struct swc_drm { + struct swc_backend backend; int fd; uint32_t cursor_w, cursor_h; struct wld_context *context; diff --git a/libswc/fb.c b/libswc/fb.c @@ -0,0 +1,142 @@ +#include "fb.h" +#include "internal.h" +#include "output.h" +#include "pointer.h" +#include "screen.h" +#include "seat.h" +#include "util.h" + +#include <stdlib.h> +#include <string.h> +#include <wayland-server.h> +#include <wld/pixman.h> +#include <wld/wld.h> + +struct swc_fb swc_fb; + +/* for the cursor */ +static uint32_t +blend(uint32_t background, uint32_t foreground) +{ + uint32_t alpha = foreground >> 24; + uint32_t inverse = 255 - alpha; + uint32_t red = ((foreground >> 16) & 0xff) + + (((background >> 16) & 0xff) * inverse + 127) / 255; + uint32_t green = ((foreground >> 8) & 0xff) + + (((background >> 8) & 0xff) * inverse + 127) / 255; + uint32_t blue = (foreground & 0xff) + + ((background & 0xff) * inverse + 127) / 255; + return (MIN(red, 255) << 16) | (MIN(green, 255) << 8) | MIN(blue, 255); +} + +bool +fb_initialize(void) +{ + if (!fbdev_initialize(&swc_fb)) { + return false; + } + swc_fb.pitch = (size_t)swc_fb.width * sizeof(*swc_fb.pixels); + swc_fb.pixels = calloc(swc_fb.height, swc_fb.pitch); + if (!swc_fb.pixels) { + goto error0; + } + swc_fb.backend.context = wld_pixman_create_context(); + if (!swc_fb.backend.context) { + goto error1; + } + swc_fb.backend.renderer = wld_create_renderer(swc_fb.backend.context); + if (!swc_fb.backend.renderer) { + goto error2; + } + swc_fb.backend.cursor_width = 64; + swc_fb.backend.cursor_height = 64; + swc.backend = &swc_fb.backend; + return true; + +error2: + wld_destroy_context(swc_fb.backend.context); +error1: + free(swc_fb.pixels); +error0: + fbdev_finalize(&swc_fb); + return false; +} + +void +fb_finalize(void) +{ + wld_destroy_renderer(swc_fb.backend.renderer); + wld_destroy_context(swc_fb.backend.context); + free(swc_fb.pixels); + fbdev_finalize(&swc_fb); +} + +bool +fb_create_screens(struct wl_list *screens) +{ + struct output *output; + struct screen *screen; + + output = output_new_fb(swc_fb.width, swc_fb.height, "fbdev-0"); + if (!output) { + return false; + } + screen = screen_new(output); + if (!screen) { + output_destroy(output); + return false; + } + screen->id = 0; + output->screen = screen; + wl_list_insert(screens, &screen->link); + return true; +} + +bool +fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y) +{ + struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; + struct wld_buffer *cursor = NULL; + int32_t cursor_x = 0, cursor_y = 0; + uint32_t width, height, x, y; + bool cursor_mapped = false; + + if (!buffer || !wld_map(buffer)) { + return false; + } + if (pointer && pointer->cursor.view.buffer) { + cursor = pointer->cursor.buffer; + cursor_x = pointer->cursor.view.geometry.x - origin_x; + cursor_y = pointer->cursor.view.geometry.y - origin_y; + cursor_mapped = wld_map(cursor); + } + + width = buffer->width < swc_fb.width ? buffer->width : swc_fb.width; + height = buffer->height < swc_fb.height ? buffer->height : swc_fb.height; + if (width != swc_fb.width || height != swc_fb.height) { + memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); + } + for (y = 0; y < height; ++y) { + uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + + (size_t)y * buffer->pitch); + uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + + (size_t)y * swc_fb.pitch); + for (x = 0; x < width; ++x) { + uint32_t pixel = source[x]; + int32_t cx = (int32_t)x - cursor_x; + int32_t cy = (int32_t)y - cursor_y; + if (cursor_mapped && cx >= 0 && cy >= 0 && + (uint32_t)cx < cursor->width && (uint32_t)cy < cursor->height) { + uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + + (size_t)cy * cursor->pitch + (size_t)cx * 4); + pixel = blend(pixel, foreground); + } + destination[x] = pixel; + } + } + if (cursor_mapped) { + wld_unmap(cursor); + } + wld_unmap(buffer); + return fbdev_present(&swc_fb); +} diff --git a/libswc/fb.h b/libswc/fb.h @@ -0,0 +1,32 @@ +#ifndef SWC_FB_H +#define SWC_FB_H + +#include "backend.h" + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +struct wl_list; +struct wld_buffer; + +struct swc_fb { + struct swc_backend backend; + uint32_t width; + uint32_t height; + uint32_t *pixels; + size_t pitch; +}; + +extern struct swc_fb swc_fb; + +bool fb_initialize(void); +void fb_finalize(void); +bool fb_create_screens(struct wl_list *screens); +bool fb_present(struct wld_buffer *buffer, int32_t x, int32_t y); + +bool fbdev_initialize(struct swc_fb *fb); +void fbdev_finalize(struct swc_fb *fb); +bool fbdev_present(struct swc_fb *fb); + +#endif diff --git a/libswc/fb_fbdev.c b/libswc/fb_fbdev.c @@ -0,0 +1,185 @@ +#include "fb.h" +#include "launch.h" +#include "util.h" + +#include <fcntl.h> +#include <linux/fb.h> +#include <stdint.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <unistd.h> + +struct fb_channel { + uint8_t offset; + uint8_t length; +}; + +static struct { + int fd; + void *memory; + size_t memory_length; + size_t memory_offset; + uint32_t pitch; + uint8_t bits_per_pixel; + struct fb_channel red; + struct fb_channel green; + struct fb_channel blue; +} fbdev; + +static struct fb_channel +channel_from_fbdev(struct fb_bitfield field) +{ + return (struct fb_channel) { + .offset = field.offset, + .length = field.length, + }; +} + +bool +fbdev_initialize(struct swc_fb *fb) +{ + struct fb_fix_screeninfo fixed; + struct fb_var_screeninfo variable; + size_t visible_offset; + + memset(&fbdev, 0, sizeof(fbdev)); + fbdev.fd = launch_open_device(FBDEV_DEVICE, O_RDWR | O_CLOEXEC); + if (fbdev.fd < 0) { + ERROR("Could not open framebuffer device %s\n", FBDEV_DEVICE); + return false; + } + if (ioctl(fbdev.fd, FBIOGET_FSCREENINFO, &fixed) < 0 || + ioctl(fbdev.fd, FBIOGET_VSCREENINFO, &variable) < 0) { + ERROR("Could not query framebuffer device %s\n", FBDEV_DEVICE); + goto error; + } + if (variable.bits_per_pixel != 16 && variable.bits_per_pixel != 24 && + variable.bits_per_pixel != 32) { + ERROR("Unsupported fbdev depth: %u bits per pixel\n", + variable.bits_per_pixel); + goto error; + } + if (fixed.type != FB_TYPE_PACKED_PIXELS || + (fixed.visual != FB_VISUAL_TRUECOLOR && + fixed.visual != FB_VISUAL_DIRECTCOLOR)) { + ERROR("Unsupported fbdev framebuffer type or visual\n"); + goto error; + } + + if (!variable.xres || !variable.yres || variable.xres > UINT16_MAX || + variable.yres > UINT16_MAX || + variable.red.msb_right || variable.green.msb_right || + variable.blue.msb_right || + variable.red.length > 16 || variable.green.length > 16 || + variable.blue.length > 16 || + variable.red.offset + variable.red.length > variable.bits_per_pixel || + variable.green.offset + variable.green.length > variable.bits_per_pixel || + variable.blue.offset + variable.blue.length > variable.bits_per_pixel) { + ERROR("Unsupported fbdev geometry or channel layout\n"); + goto error; + } + fbdev.memory_length = fixed.smem_len; + fbdev.memory = mmap(NULL, fbdev.memory_length, PROT_READ | PROT_WRITE, + MAP_SHARED, fbdev.fd, 0); + if (fbdev.memory == MAP_FAILED) { + fbdev.memory = NULL; + ERROR("Could not map framebuffer device %s\n", FBDEV_DEVICE); + goto error; + } + + fb->width = variable.xres; + fb->height = variable.yres; + fbdev.pitch = fixed.line_length; + fbdev.bits_per_pixel = variable.bits_per_pixel; + fbdev.red = channel_from_fbdev(variable.red); + fbdev.green = channel_from_fbdev(variable.green); + fbdev.blue = channel_from_fbdev(variable.blue); + visible_offset = (size_t)variable.yoffset * fbdev.pitch + + (size_t)variable.xoffset * variable.bits_per_pixel / 8; + if (fixed.line_length < (size_t)variable.xres * variable.bits_per_pixel / 8 || + visible_offset >= fbdev.memory_length || + (size_t)(variable.yres - 1) * fixed.line_length > + fbdev.memory_length - visible_offset || + (size_t)variable.xres * variable.bits_per_pixel / 8 > + fbdev.memory_length - visible_offset - + (size_t)(variable.yres - 1) * fixed.line_length) { + ERROR("fbdev visible buffer lies outside its mapping\n"); + goto error; + } + fbdev.memory_offset = visible_offset; + return true; + +error: + if (fbdev.memory) { + munmap(fbdev.memory, fbdev.memory_length); + } + close(fbdev.fd); + fbdev.fd = -1; + return false; +} + +void +fbdev_finalize(struct swc_fb *fb) +{ + (void)fb; + munmap(fbdev.memory, fbdev.memory_length); + close(fbdev.fd); +} + +static uint32_t +channel(uint8_t value, struct fb_channel field) +{ + uint32_t maximum; + + if (!field.length) { + return 0; + } + maximum = field.length >= 32 ? UINT32_MAX : (1u << field.length) - 1; + return (((uint32_t)value * maximum + 127) / 255) << field.offset; +} + +static uint32_t +native_pixel(uint32_t pixel) +{ + return channel(pixel >> 16, fbdev.red) | + channel(pixel >> 8, fbdev.green) | + channel(pixel, fbdev.blue); +} + +static void +store_pixel(uint8_t *destination, uint32_t pixel) +{ + uint32_t native = native_pixel(pixel); + + if (fbdev.bits_per_pixel == 24) { +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + destination[0] = native >> 16; + destination[1] = native >> 8; + destination[2] = native; +#else + destination[0] = native; + destination[1] = native >> 8; + destination[2] = native >> 16; +#endif + } else { + memcpy(destination, &native, fbdev.bits_per_pixel / 8); + } +} + +bool +fbdev_present(struct swc_fb *fb) +{ + uint32_t bytes = fbdev.bits_per_pixel / 8; + uint32_t x, y; + + for (y = 0; y < fb->height; ++y) { + uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + y * fb->pitch); + uint8_t *destination = (uint8_t *)fbdev.memory + fbdev.memory_offset + + (size_t)y * fbdev.pitch; + for (x = 0; x < fb->width; ++x) { + store_pixel(destination + (size_t)x * bytes, source[x]); + } + } + return true; +} diff --git a/libswc/internal.h b/libswc/internal.h @@ -44,7 +44,10 @@ struct swc { struct wl_list screens; struct swc_compositor *const compositor; struct swc_shm *shm; +#ifdef ENABLE_DRM struct swc_drm *const drm; +#endif + struct swc_backend *backend; struct wl_global *data_device_manager; struct wl_global *kde_decoration_manager; struct wl_global *layer_shell; diff --git a/libswc/meson.build b/libswc/meson.build @@ -7,8 +7,6 @@ libswc_src = files( 'data_device.c', 'data_device_manager.c', 'decor.c', - 'dmabuf.c', - 'drm.c', 'input.c', 'kde_decoration.c', 'keyboard.c', @@ -18,7 +16,6 @@ libswc_src = files( 'output.c', 'panel.c', 'panel_manager.c', - 'plane.c', 'pointer.c', 'primary_plane.c', 'region.c', @@ -44,6 +41,9 @@ libswc_src = files( input_backend == 'wscons' ? 'seat-ws.c' : [], xwayland.found() ? ['xserver.c', 'xwm.c'] : [], + + video_backend == 'drm' ? ['dmabuf.c', 'drm.c', 'plane.c'] : [], + video_backend == 'fb' ? ['fb.c', 'fb_fbdev.c'] : [], ) libswc_src += [cursor, launch_protocol] diff --git a/libswc/mode.c b/libswc/mode.c @@ -23,6 +23,7 @@ #include "mode.h" +#ifdef ENABLE_DRM bool mode_initialize(struct mode *mode, drmModeModeInfo *mode_info) { @@ -33,6 +34,17 @@ mode_initialize(struct mode *mode, drmModeModeInfo *mode_info) mode->info = *mode_info; return true; } +#endif + +void +mode_initialize_simple(struct mode *mode, uint16_t width, uint16_t height, + uint32_t refresh) +{ + mode->width = width; + mode->height = height; + mode->refresh = refresh; + mode->preferred = true; +} bool mode_equal(const struct mode *mode1, const struct mode *mode2) diff --git a/libswc/mode.h b/libswc/mode.h @@ -27,7 +27,9 @@ #include <stdbool.h> #include <stddef.h> #include <stdint.h> +#ifdef ENABLE_DRM #include <xf86drmMode.h> +#endif struct mode { uint16_t width, height; @@ -35,11 +37,18 @@ struct mode { bool preferred; +#ifdef ENABLE_DRM drmModeModeInfo info; +#endif }; +#ifdef ENABLE_DRM bool mode_initialize(struct mode *mode, drmModeModeInfo *mode_info); +#endif +void +mode_initialize_simple(struct mode *mode, uint16_t width, uint16_t height, + uint32_t refresh); bool mode_equal(const struct mode *mode1, const struct mode *mode2); diff --git a/libswc/output.c b/libswc/output.c @@ -1,15 +1,19 @@ #include "output.h" +#ifdef ENABLE_DRM #include "drm.h" +#endif #include "internal.h" #include "mode.h" #include "screen.h" #include "util.h" +#ifdef ENABLE_DRM #include <drm.h> +#include <xf86drm.h> +#endif #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <xf86drm.h> static const struct wl_output_interface output_impl = { .release = destroy_resource, @@ -65,6 +69,7 @@ bind_output(struct wl_client *client, void *data, uint32_t version, uint32_t id) } } +#ifdef ENABLE_DRM struct output * output_new(drmModeConnectorPtr connector) { @@ -132,6 +137,38 @@ error1: error0: return NULL; } +#endif + +struct output * +output_new_fb(uint32_t width, uint32_t height, const char *name) +{ + struct output *output; + struct mode *mode; + + output = calloc(1, sizeof(*output)); + if (!output) { + return NULL; + } + output->global = wl_global_create(swc.display, &wl_output_interface, 4, + output, &bind_output); + if (!output->global) { + free(output); + return NULL; + } + wl_list_init(&output->resources); + wl_array_init(&output->modes); + pixman_region32_init(&output->current_damage); + pixman_region32_init(&output->previous_damage); + mode = wl_array_add(&output->modes, sizeof(*mode)); + if (!mode) { + output_destroy(output); + return NULL; + } + mode_initialize_simple(mode, width, height, 60000); + output->preferred_mode = mode; + snprintf(output->name, sizeof(output->name), "%s", name); + return output; +} void output_destroy(struct output *output) diff --git a/libswc/output.h b/libswc/output.h @@ -4,7 +4,9 @@ #include <pixman.h> #include <stdint.h> #include <wayland-util.h> +#ifdef ENABLE_DRM #include <xf86drmMode.h> +#endif struct wl_display; @@ -21,16 +23,22 @@ struct output { pixman_region32_t current_damage, previous_damage; +#ifdef ENABLE_DRM /* The DRM connector corresponding to this output */ uint32_t connector; +#endif struct wl_global *global; struct wl_list resources; struct wl_list link; }; +#ifdef ENABLE_DRM struct output * output_new(drmModeConnector *connector); +#endif +struct output * +output_new_fb(uint32_t width, uint32_t height, const char *name); void output_destroy(struct output *output); diff --git a/libswc/pointer.c b/libswc/pointer.c @@ -22,11 +22,14 @@ */ #include "pointer.h" +#include "backend.h" #include "compositor.h" #include "cursor/cursor_data.h" #include "event.h" #include "internal.h" +#ifdef ENABLE_DRM #include "plane.h" +#endif #include "screen.h" #include "seat.h" #include "shm.h" @@ -175,7 +178,9 @@ attach(struct view *view, struct wld_buffer *buffer) { struct pointer *pointer = wl_container_of(view, pointer, cursor.view); struct surface *surface = pointer->cursor.surface; +#ifdef ENABLE_DRM struct screen *screen; +#endif if (surface && !pixman_region32_not_empty(&surface->state.damage)) { return 0; @@ -203,12 +208,15 @@ attach(struct view *view, struct wld_buffer *buffer) view_update_screens(view); } - wl_list_for_each(screen, &swc.screens, link) - { +#ifdef ENABLE_DRM + wl_list_for_each(screen, &swc.screens, link) { view_attach(&screen->planes.cursor->view, buffer ? pointer->cursor.buffer : NULL); view_update(&screen->planes.cursor->view); } +#else + compositor_damage_all(); +#endif return 0; } @@ -216,18 +224,23 @@ attach(struct view *view, struct wld_buffer *buffer) static bool move(struct view *view, int32_t x, int32_t y) { +#ifdef ENABLE_DRM struct screen *screen; +#endif if (view_set_position(view, x, y)) { view_update_screens(view); } - wl_list_for_each(screen, &swc.screens, link) - { +#ifdef ENABLE_DRM + wl_list_for_each(screen, &swc.screens, link) { view_move(&screen->planes.cursor->view, view->geometry.x, view->geometry.y); view_update(&screen->planes.cursor->view); } +#else + compositor_damage_all(); +#endif return true; } @@ -509,7 +522,8 @@ pointer_initialize(struct pointer *pointer) pointer->cursor.surface = NULL; pointer->cursor.destroy_listener.notify = &handle_cursor_surface_destroy; pointer->cursor.buffer = wld_create_buffer( - swc.drm->context, swc.drm->cursor_w, swc.drm->cursor_h, + swc.backend->context, swc.backend->cursor_width, + swc.backend->cursor_height, WLD_FORMAT_ARGB8888, WLD_FLAG_MAP | WLD_FLAG_CURSOR); pointer->cursor.internal_buffer = NULL; @@ -519,8 +533,11 @@ pointer_initialize(struct pointer *pointer) pointer_set_cursor(pointer, cursor_left_ptr); +#ifdef ENABLE_DRM wl_list_for_each(screen, &swc.screens, link) - view_attach(&screen->planes.cursor->view, pointer->cursor.buffer); + if (screen->planes.cursor) + view_attach(&screen->planes.cursor->view, pointer->cursor.buffer); +#endif input_focus_initialize(&pointer->focus, &pointer->focus_handler); pixman_region32_init(&pointer->region); diff --git a/libswc/primary_plane.c b/libswc/primary_plane.c @@ -22,17 +22,24 @@ */ #include "primary_plane.h" -#include "drm.h" #include "event.h" +#ifdef ENABLE_DRM +#include "drm.h" +#else +#include "compositor.h" +#include "fb.h" +#endif #include "internal.h" #include "launch.h" #include "util.h" +#ifdef ENABLE_DRM #include <errno.h> #include <wld/drm.h> -#include <wld/wld.h> #include <xf86drm.h> #include <xf86drmMode.h> +#endif +#include <wld/wld.h> static bool update(struct view *view) @@ -52,6 +59,7 @@ static int attach(struct view *view, struct wld_buffer *buffer) { struct primary_plane *plane = wl_container_of(view, plane, view); +#ifdef ENABLE_DRM uint32_t fb; int ret; @@ -80,6 +88,13 @@ attach(struct view *view, struct wld_buffer *buffer) } return 0; +#else + if (!fb_present(buffer, view->geometry.x, view->geometry.y)) { + return -1; + } + wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); + return 0; +#endif } static bool @@ -95,12 +110,14 @@ static const struct view_impl view_impl = { .move = move, }; +#ifdef ENABLE_DRM static void handle_page_flip(struct drm_handler *handler, uint32_t time) { struct primary_plane *plane = wl_container_of(handler, plane, drm_handler); view_frame(&plane->view, time); } +#endif static void handle_swc_event(struct wl_listener *listener, void *data) @@ -111,16 +128,25 @@ handle_swc_event(struct wl_listener *listener, void *data) switch (event->type) { case SWC_EVENT_ACTIVATED: +#ifdef ENABLE_DRM plane->need_modeset = true; +#else + compositor_damage_all(); +#endif break; } } bool +#ifdef ENABLE_DRM primary_plane_initialize(struct primary_plane *plane, uint32_t crtc, struct mode *mode, uint32_t *connectors, uint32_t num_connectors) +#else +primary_plane_initialize(struct primary_plane *plane, struct mode *mode) +#endif { +#ifdef ENABLE_DRM uint32_t *plane_connectors; if (!(plane->original_crtc_state = drmModeGetCrtc(swc.drm->fd, crtc))) { @@ -148,22 +174,36 @@ primary_plane_initialize(struct primary_plane *plane, uint32_t crtc, plane->drm_handler.page_flip = &handle_page_flip; plane->swc_listener.notify = &handle_swc_event; plane->mode = *mode; +#else + view_initialize(&plane->view, &view_impl); + plane->view.geometry.width = mode->width; + plane->view.geometry.height = mode->height; + plane->mode = *mode; +#endif + plane->swc_listener.notify = &handle_swc_event; wl_signal_add(&swc.event_signal, &plane->swc_listener); return true; +#ifdef ENABLE_DRM error1: drmModeFreeCrtc(plane->original_crtc_state); error0: return false; +#endif } void primary_plane_finalize(struct primary_plane *plane) { + wl_list_remove(&plane->swc_listener.link); +#ifdef ENABLE_DRM wl_array_release(&plane->connectors); drmModeCrtcPtr crtc = plane->original_crtc_state; drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, NULL, 0, &crtc->mode); drmModeFreeCrtc(crtc); +#else + (void)plane; +#endif } diff --git a/libswc/primary_plane.h b/libswc/primary_plane.h @@ -24,29 +24,40 @@ #ifndef SWC_PRIMARY_PLANE_H #define SWC_PRIMARY_PLANE_H -#include "drm.h" #include "mode.h" #include "view.h" +#ifdef ENABLE_DRM +#include "drm.h" +#endif #include <stdbool.h> #include <stdint.h> #include <wayland-server.h> struct primary_plane { +#ifdef ENABLE_DRM uint32_t crtc; drmModeCrtcPtr original_crtc_state; +#endif struct mode mode; struct view view; +#ifdef ENABLE_DRM struct wl_array connectors; bool need_modeset; struct drm_handler drm_handler; +#endif struct wl_listener swc_listener; }; +#ifdef ENABLE_DRM bool primary_plane_initialize(struct primary_plane *plane, uint32_t crtc, struct mode *mode, uint32_t *connectors, uint32_t num_connectors); +#else +bool +primary_plane_initialize(struct primary_plane *plane, struct mode *mode); +#endif void primary_plane_finalize(struct primary_plane *plane); diff --git a/libswc/screen.c b/libswc/screen.c @@ -22,12 +22,18 @@ */ #include "screen.h" +#ifdef ENABLE_DRM #include "drm.h" +#else +#include "fb.h" +#endif #include "event.h" #include "internal.h" #include "mode.h" #include "output.h" +#ifdef ENABLE_DRM #include "plane.h" +#endif #include "pointer.h" #include "util.h" @@ -65,7 +71,13 @@ screens_initialize(void) { wl_list_init(&swc.screens); - if (!drm_create_screens(&swc.screens)) { + if (! +#ifdef ENABLE_DRM + drm_create_screens(&swc.screens) +#else + fb_create_screens(&swc.screens) +#endif + ) { return false; } @@ -103,7 +115,11 @@ bind_screen(struct wl_client *client, void *data, uint32_t version, uint32_t id) } struct screen * +#ifdef ENABLE_DRM screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane) +#else +screen_new(struct output *output) +#endif { struct screen *screen; int32_t x = 0; @@ -124,8 +140,8 @@ screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane) goto error1; } +#ifdef ENABLE_DRM screen->crtc = crtc; - if (!primary_plane_initialize(&screen->planes.primary, crtc, output->preferred_mode, @@ -137,6 +153,14 @@ screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane) cursor_plane->screen = screen; screen->planes.cursor = cursor_plane; +#else + if (!primary_plane_initialize(&screen->planes.primary, + output->preferred_mode)) { + ERROR("Failed to initialize primary plane\n"); + goto error2; + } + screen->planes.cursor = NULL; +#endif screen->handler = &null_handler; wl_signal_init(&screen->destroy_signal); @@ -176,7 +200,11 @@ screen_destroy(struct screen *screen) wl_list_for_each_safe(output, next, &screen->outputs, link) output_destroy(output); primary_plane_finalize(&screen->planes.primary); - plane_destroy(screen->planes.cursor); +#ifdef ENABLE_DRM + if (screen->planes.cursor) { + plane_destroy(screen->planes.cursor); + } +#endif free(screen); } diff --git a/libswc/screen.h b/libswc/screen.h @@ -51,7 +51,9 @@ struct screen { struct wl_signal destroy_signal; uint8_t id; +#ifdef ENABLE_DRM uint32_t crtc; +#endif struct { struct primary_plane primary; @@ -71,8 +73,13 @@ screens_initialize(void); void screens_finalize(void); +#ifdef ENABLE_DRM struct screen * screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane); +#else +struct screen * +screen_new(struct output *output); +#endif void screen_destroy(struct screen *screen); diff --git a/libswc/swc.c b/libswc/swc.c @@ -25,7 +25,11 @@ #include "bindings.h" #include "compositor.h" #include "data_device_manager.h" +#ifdef ENABLE_DRM #include "drm.h" +#else +#include "fb.h" +#endif #include "event.h" #include "internal.h" #include "kde_decoration.h" @@ -53,7 +57,9 @@ extern struct swc_launch swc_launch; extern const struct swc_bindings swc_bindings; extern struct swc_compositor swc_compositor; +#ifdef ENABLE_DRM extern struct swc_drm swc_drm; +#endif #ifdef ENABLE_XWAYLAND extern struct swc_xserver swc_xserver; #endif @@ -63,7 +69,9 @@ extern struct pointer_handler screens_pointer_handler; struct swc swc = { .bindings = &swc_bindings, .compositor = &swc_compositor, +#ifdef ENABLE_DRM .drm = &swc_drm, +#endif #ifdef ENABLE_XWAYLAND .xserver = &swc_xserver, #endif @@ -160,8 +168,14 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, goto error0; } - if (!drm_initialize()) { - ERROR("Could not initialize DRM\n"); + if (! +#ifdef ENABLE_DRM + drm_initialize() +#else + fb_initialize() +#endif + ) { + ERROR("Could not initialize video backend\n"); goto error1; } @@ -302,7 +316,11 @@ error4: error3: shm_destroy(swc.shm); error2: +#ifdef ENABLE_DRM drm_finalize(); +#else + fb_finalize(); +#endif error1: launch_finalize(); error0: @@ -329,6 +347,10 @@ swc_finalize(void) screens_finalize(); bindings_finalize(); shm_destroy(swc.shm); +#ifdef ENABLE_DRM drm_finalize(); +#else + fb_finalize(); +#endif launch_finalize(); } diff --git a/meson.build b/meson.build @@ -31,14 +31,34 @@ requires = [ dependency('wayland-server', version: '>=1.6.0'), ] requires_private = [ - dependency('libdrm'), dependency('pixman-1'), dependency('wayland-protocols'), dependency('wld'), dependency('xkbcommon'), ] -xwayland = dependency('xwayland', required: get_option('xwayland')) +video_backend = get_option('video') +drm = disabler() +if video_backend == 'drm' + drm = dependency('libdrm') + requires_private += drm + add_project_arguments('-DENABLE_DRM=1', language: 'c') +elif video_backend == 'fb' + if os != 'linux' + error('The fb video backend is currently supported only on Linux') + endif + add_project_arguments([ + '-DENABLE_FBDEV=1', + '-DFBDEV_DEVICE="@0@"'.format(get_option('fbdev-device')), + ], language: 'c') +endif + +if video_backend != 'drm' and get_option('xwayland').enabled() + error('Xwayland requires the DRM video backend') +endif +xwayland = video_backend == 'drm' \ + ? dependency('xwayland', required: get_option('xwayland')) \ + : disabler() xwayland_deps = [ 'xcb', 'xcb-composite', @@ -93,4 +113,5 @@ endif summary({ 'Xwayland support': xwayland.found(), 'Input backend': input_backend, + 'Video backend': video_backend, }, bool_yn: true) diff --git a/meson.options b/meson.options @@ -1,5 +1,7 @@ option('xwayland', type: 'feature', value: 'auto', description: 'Automatic Xwayland handling') option('input', type: 'combo', value: 'auto', choices: ['auto', 'libinput', 'wscons'], description: 'Input backend to use') +option('video', type: 'combo', value: 'drm', choices: ['drm', 'fb'], description: 'Video backend to use') +option('fbdev-device', type: 'string', value: '/dev/fb0', description: 'Linux framebuffer device') option('udev', type: 'feature', value: 'auto', description: 'libinput libudev support') option('extra', type: 'boolean', value: true, description: 'Build swc utility applications') option('example', type: 'boolean', value: false, description: 'Build swc example compositor')