Compare commits

...

24 Commits

Author SHA1 Message Date
Zach DeCook
b0fd6777fc namespace: change to wvkbd
Hyprland recently added a layerrule to allow certain layers to appear on the lockscreen,
but layers only identify themselves by the namespace.

Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2025-04-26 23:43:55 +02:00
Willow Barraco
3142271882
Do not expose drwsurf_flip 2025-04-18 07:51:19 +02:00
Willow Barraco
ace77b2aaa
Tie the damage tracking to the drawing methods
So that keyboard.c just need to draw things, and stop worring about
damaging.
2025-04-18 07:51:19 +02:00
Willow Barraco
47d27e48d5
gitignore man page 2025-04-18 07:41:27 +02:00
Maarten van Gompel
3826cd6050 version bump 2025-04-17 22:02:29 +02:00
Willow Barraco
73caeee513 Deduplicate some drwbuf fields
We have those fields on the drwbuf struct, but we always use the
same ones. Let's deduplicate those fields by moving them on the drwsurf.

Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2025-04-17 19:48:53 +02:00
Willow Barraco
55b88bc14f Optimize the backporting condition
At the moment we flip the two buffers, and we backport the damaged
area on every frame callback. We completely ignore the compositor
sent event buffer->release. This event means that the compositor
finished reading the buffer pixels, and will never read them again. So
we can keep using the same buffer instead of fliping, if we receive the
event before asking for a new frame callback.

The change is not complicated, but we have to distinguish
drwsurf_attach, and drwsurf_flip. We try to flip before trying to
draw anything. And if the buffer has already been released, we don't
flip.

We also have to track the backport_damage separately than the buffer
current damage.

Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2025-04-17 19:48:50 +02:00
Willow Barraco
686b27a72d Implement double buffering
Until this point we was using one single buffer. This cause tearing when
they are scanned while we are drawing.

This refactorize our buffer and damage tracking to use two buffers. One
buffer, the back_buffer, is our dirty area. The other one, the
display_buffer, is left untouched.

Now we only flip buffers on adequate moments, indicated by the
compositor sending the frame callback event. We can draw multiple
time between those events, and we store all damaged areas. We only
schedule frame callbacks when needed, after storing a new damaged area.

Once flipped, we backport the damaged area from the new display_buffer
to the new back_buffer. Which generally means, for wvkbd, one rectangle
for each one of the surf and popup_surf.

Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2025-04-17 19:48:43 +02:00
Maarten van Gompel
646d47d072 fixup: spacing 2025-04-15 23:44:51 +02:00
Viacheslav Gagara
e0ecf38853 add missing cyrillic letter from ukrainian alphabet
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2025-04-15 23:44:28 +02:00
Maarten van Gompel
ca53099c9d version bump after release 2024-11-25 13:40:36 +01:00
Maarten van Gompel
29639c28d5 Makefile: minor update to make clean 2024-11-25 13:18:53 +01:00
Maarten van Gompel
745a0b6f53 documentation: added section on compose button to man page 2024-11-25 13:11:50 +01:00
Maarten van Gompel
c742b58f51 documentation: updated man page
Using scdoc to make editing a bit more comfortable.
2024-11-25 13:01:20 +01:00
Maarten van Gompel
97efb1de74 README: fixed image links 2024-11-25 12:35:59 +01:00
Maarten van Gompel
125664afd5 use OVERLAY so keyboard is visible over fullscreen windows as well
Ref: https://github.com/jjsullivan5196/wvkbd/issues/73
2024-11-25 12:31:19 +01:00
Maarten van Gompel
6041473421 Only draw caps-lock upper-case labels for alphabetical keys
Fixes an issue introduced in 9d130e7fd23678de34396ac80dab51f036abb033
2024-11-25 12:19:11 +01:00
Willow Barraco
9cc8931b46 Fix the initial output geometry guessing
wvkbd adapt its height, and layout depending on if the screen
is in landscape mode or not.

The only realiable way to know which outputs our surface is
rendered on is the wl_surface->enter(wl_output) event. But we receive
this event very late in the flow. After the first frame rendering, and
commit to the surface.

There already is some code to guess the best initial tentative, on the
most simple situations, to try to avoid an immediate re-creation of the
Wayland objects, buffer, and re-rendering of the layout.

But two issues was present:

First, we need a second roundtrip of the events, after the global
handle. This make the compositor to send the wl_outputs events earlier,
giving their geometry before our first show().

Then, the code that loop on them, if we don't already know the
current_output, was wrong. Because we changed the default state to
landscaped a while ago.

We also change how this code behave, we use the very first wl_output we
know about. Every behaviors are somehow wrong at this point, this one is
the simplest.

Now when starting wvkbd with only one screen, we don't need a second
loop anymore.

On situation where multiple screens are present, it will eventually need
a second one.

Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2024-11-05 10:14:12 +01:00
Willow Barraco
7d195c8217 Skip the first resize when landscaped while starting
Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2024-11-02 21:27:31 +01:00
Frank Oltmanns
d5db545dcc Restore cairo also when using rounding
Cairo is properly restored when not rounding, but in the codepath for
rounding it is not. Call cairo_restore() in both cases, otherwise
artefacts appear when using transparent fonts.

Signed-off-by: Frank Oltmanns <frank@oltmanns.dev>
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
2024-10-26 20:49:11 +02:00
Jami Kettunen
7d677d23b7 Makefile: accomodate cross-build pkg-config 2024-10-02 23:26:24 +02:00
Maarten van Gompel
fe3257450a version bump 2024-09-01 18:23:07 +02:00
Paul Rimmer
1852b3ab06 Don't forget the obscure AltGr modifier key 2024-09-01 18:21:53 +02:00
Paul Rimmer
9d130e7fd2 Make all modifiers except capslock one shot and redraw keyboard when capslock pressed 2024-09-01 18:21:53 +02:00
11 changed files with 371 additions and 162 deletions

1
.gitignore vendored
View File

@ -6,5 +6,6 @@ include/config.h
.gdb_history .gdb_history
*.log *.log
wvkbd wvkbd
*.1
config.h config.h
wvkbd-mobintl wvkbd-mobintl

View File

@ -10,9 +10,10 @@ PKGS = wayland-client xkbcommon pangocairo
WVKBD_SOURCES += $(wildcard $(SRC)/*.c) WVKBD_SOURCES += $(wildcard $(SRC)/*.c)
WVKBD_HEADERS += $(wildcard $(SRC)/*.h) WVKBD_HEADERS += $(wildcard $(SRC)/*.h)
PKG_CONFIG ?= pkg-config
CFLAGS += -std=gnu99 -Wall -g -DWITH_WAYLAND_SHM -DLAYOUT=\"layout.${LAYOUT}.h\" -DKEYMAP=\"keymap.${LAYOUT}.h\" CFLAGS += -std=gnu99 -Wall -g -DWITH_WAYLAND_SHM -DLAYOUT=\"layout.${LAYOUT}.h\" -DKEYMAP=\"keymap.${LAYOUT}.h\"
CFLAGS += $(shell pkg-config --cflags $(PKGS)) CFLAGS += $(shell $(PKG_CONFIG) --cflags $(PKGS))
LDFLAGS += $(shell pkg-config --libs $(PKGS)) -lm -lutil -lrt LDFLAGS += $(shell $(PKG_CONFIG) --libs $(PKGS)) -lm -lutil -lrt
WAYLAND_HEADERS = $(wildcard proto/*.xml) WAYLAND_HEADERS = $(wildcard proto/*.xml)
@ -20,9 +21,12 @@ HDRS = $(WAYLAND_HEADERS:.xml=-client-protocol.h)
WAYLAND_SRC = $(HDRS:.h=.c) WAYLAND_SRC = $(HDRS:.h=.c)
SOURCES = $(WVKBD_SOURCES) $(WAYLAND_SRC) SOURCES = $(WVKBD_SOURCES) $(WAYLAND_SRC)
SCDOC=scdoc
DOCS = wvkbd.1
OBJECTS = $(SOURCES:.c=.o) OBJECTS = $(SOURCES:.c=.o)
all: ${BIN} all: ${BIN} ${DOCS}
config.h: config.h:
cp config.def.h config.h cp config.def.h config.h
@ -39,11 +43,14 @@ wvkbd-${LAYOUT}: config.h $(OBJECTS) layout.${LAYOUT}.h
$(CC) -o wvkbd-${LAYOUT} $(OBJECTS) $(LDFLAGS) $(CC) -o wvkbd-${LAYOUT} $(OBJECTS) $(LDFLAGS)
clean: clean:
rm -f $(OBJECTS) $(HDRS) $(WAYLAND_SRC) ${BIN} rm -f $(OBJECTS) $(HDRS) $(WAYLAND_SRC) ${BIN} ${DOCS}
format: format:
clang-format -i $(WVKBD_SOURCES) $(WVKBD_HEADERS) clang-format -i $(WVKBD_SOURCES) $(WVKBD_HEADERS)
%: %.scd
$(SCDOC) < $< > $@
install: all install: all
mkdir -p ${DESTDIR}${PREFIX}/bin mkdir -p ${DESTDIR}${PREFIX}/bin
cp -f ${NAME}-${LAYOUT} ${DESTDIR}${PREFIX}/bin cp -f ${NAME}-${LAYOUT} ${DESTDIR}${PREFIX}/bin

View File

@ -1,6 +1,6 @@
# wvkbd - On-screen keyboard for wlroots that sucks less # wvkbd - On-screen keyboard for wlroots that sucks less
<img src="https://raw.githubusercontent.com/proycon/wvkbd/master/contrib/wvkbd-mobintl.jpg" width=300 /> <img src="https://raw.githubusercontent.com/proycon/wvkbd/master/contrib/wvkbd-mobintl-cyrillic.jpg" width=300 /> <img src="https://raw.githubusercontent.com/jjsullivan5196/wvkbd/master/contrib/wvkbd-mobintl.jpg" width=300 /> <img src="https://raw.githubusercontent.com/jjsullivan5196/wvkbd/master/contrib/wvkbd-mobintl-cyrillic.jpg" width=300 />
This project aims to deliver a minimal but practically usable implementation of a wlroots on-screen This project aims to deliver a minimal but practically usable implementation of a wlroots on-screen
keyboard in legible C. This will **only** be a keyboard, not a feedback buzzer, keyboard in legible C. This will **only** be a keyboard, not a feedback buzzer,
@ -25,7 +25,7 @@ new features.
- Automatic portrait/landscape detection and subsequent layout switching - Automatic portrait/landscape detection and subsequent layout switching
<img src="https://raw.githubusercontent.com/proycon/wvkbd/master/contrib/wvkbd-mobintl-landscape.jpg" width=640 /> <img src="https://raw.githubusercontent.com/jjsullivan5196/wvkbd/master/contrib/wvkbd-mobintl-landscape.jpg" width=640 />
There are some areas that still need work: There are some areas that still need work:
@ -42,6 +42,8 @@ You'll need the following developer packages
- wayland-client - wayland-client
- xkbcommon - xkbcommon
You also need [scdoc](https://git.sr.ht/~sircmpwn/scdoc/) to generate the documentation.
Make any customizations you would like in `config.def.h` and run `make` Make any customizations you would like in `config.def.h` and run `make`
The default set of layouts is called `mobintl` *(mobile international)*, which groups various layouts aimed at mobile devices The default set of layouts is called `mobintl` *(mobile international)*, which groups various layouts aimed at mobile devices

View File

@ -1,4 +1,4 @@
VERSION = 0.15 VERSION = 0.17
CFLAGS = -DVERSION=\"$(VERSION)\" -D_XOPEN_SOURCE=700 CFLAGS = -DVERSION=\"$(VERSION)\" -D_XOPEN_SOURCE=700
PREFIX = /usr/local PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man MANPREFIX = ${PREFIX}/share/man

175
drw.c
View File

@ -6,34 +6,132 @@
#include "shm_open.h" #include "shm_open.h"
#include "math.h" #include "math.h"
void drwbuf_handle_release(void *data, struct wl_buffer *wl_buffer) {
struct drwsurf *ds = data;
ds->released = true;
};
const struct wl_buffer_listener buffer_listener = {
.release = drwbuf_handle_release
};
void drwsurf_handle_frame_cb(void* data, struct wl_callback* callback,
uint32_t time)
{
struct drwsurf *ds = data;
wl_callback_destroy(ds->frame_cb);
ds->frame_cb = NULL;
cairo_rectangle_int_t r = {0};
for (int i = 0; i < cairo_region_num_rectangles(ds->damage); i++) {
cairo_region_get_rectangle(ds->damage, i, &r);
wl_surface_damage(ds->surf, r.x, r.y, r.width, r.height);
};
cairo_region_subtract(ds->damage, ds->damage);
drwsurf_attach(ds);
}
const struct wl_callback_listener frame_listener = {
.done = drwsurf_handle_frame_cb
};
void drwsurf_register_frame_cb(struct drwsurf *ds)
{
if (ds->frame_cb)
return;
if (!ds->attached)
return;
ds->frame_cb = wl_surface_frame(ds->surf);
wl_callback_add_listener(ds->frame_cb, &frame_listener, ds);
wl_surface_commit(ds->surf);
}
void drwsurf_damage(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t w, uint32_t h)
{
cairo_rectangle_int_t rect = { x, y, w, h };
cairo_region_union_rectangle(ds->damage, &rect);
cairo_region_union_rectangle(ds->backport_damage, &rect);
drwsurf_register_frame_cb(ds);
}
void void
drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, double s) drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, double s)
{ {
if (ds->buf) {
munmap(ds->pool_data, ds->size);
wl_buffer_destroy(ds->buf);
ds->buf = NULL;
}
ds->scale = s; ds->scale = s;
ds->width = ceil(w * s); ds->width = ceil(w * s);
ds->height = ceil(h * s); ds->height = ceil(h * s);
setup_buffer(ds); if (ds->damage)
cairo_region_destroy(ds->damage);
ds->damage = cairo_region_create();
if (ds->backport_damage)
cairo_region_destroy(ds->backport_damage);
ds->backport_damage = cairo_region_create();
ds->released = true;
setup_buffer(ds, ds->back_buffer);
setup_buffer(ds, ds->display_buffer);
}
void
drwsurf_backport(struct drwsurf *ds)
{
cairo_save(ds->back_buffer->cairo);
cairo_scale(ds->back_buffer->cairo, 1/ds->scale, 1/ds->scale);
cairo_set_operator(ds->back_buffer->cairo, CAIRO_OPERATOR_SOURCE);
cairo_rectangle_int_t r = {0};
for (int i = 0; i < cairo_region_num_rectangles(ds->backport_damage); i++) {
cairo_region_get_rectangle(ds->backport_damage, i, &r);
cairo_set_source_surface(ds->back_buffer->cairo, ds->display_buffer->cairo_surf, 0, 0);
cairo_rectangle(
ds->back_buffer->cairo,
r.x * ds->scale,
r.y * ds->scale,
r.width * ds->scale,
r.height * ds->scale
);
cairo_fill(ds->back_buffer->cairo);
};
cairo_restore(ds->back_buffer->cairo);
cairo_region_subtract(ds->backport_damage, ds->backport_damage);
}
void
drwsurf_attach(struct drwsurf *ds)
{
wl_surface_attach(ds->surf, ds->back_buffer->buf, 0, 0);
wl_surface_commit(ds->surf);
ds->released = false;
ds->attached = true;
} }
void void
drwsurf_flip(struct drwsurf *ds) drwsurf_flip(struct drwsurf *ds)
{ {
wl_surface_attach(ds->surf, ds->buf, 0, 0); if (ds->released)
wl_surface_commit(ds->surf); return;
ds->released = true;
struct drwbuf *tmp = ds->back_buffer;
ds->back_buffer = ds->display_buffer;
ds->display_buffer = tmp;
drwsurf_backport(ds);
} }
void void
drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y, drw_draw_text(struct drwsurf *ds, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, uint32_t b, const char *label, uint32_t w, uint32_t h, uint32_t b, const char *label,
PangoFontDescription *font_description) PangoFontDescription *font_description)
{ {
drwsurf_flip(ds);
struct drwbuf *d = ds->back_buffer;
drwsurf_damage(ds, x, y, w, h);
cairo_save(d->cairo); cairo_save(d->cairo);
@ -58,8 +156,12 @@ drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
} }
void void
drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t w, uint32_t h) drw_do_clear(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t w, uint32_t h)
{ {
drwsurf_flip(ds);
struct drwbuf *d = ds->back_buffer;
drwsurf_damage(ds, x, y, w, h);
cairo_save(d->cairo); cairo_save(d->cairo);
cairo_set_operator(d->cairo, CAIRO_OPERATOR_CLEAR); cairo_set_operator(d->cairo, CAIRO_OPERATOR_CLEAR);
@ -70,9 +172,13 @@ drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t w, uint32_t h)
} }
void void
drw_do_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, drw_do_rectangle(struct drwsurf *ds, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, bool over, int rounding) uint32_t w, uint32_t h, bool over, int rounding)
{ {
drwsurf_flip(ds);
struct drwbuf *d = ds->back_buffer;
drwsurf_damage(ds, x, y, w, h);
cairo_save(d->cairo); cairo_save(d->cairo);
if (over) { if (over) {
@ -99,6 +205,8 @@ drw_do_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
cairo_set_source_rgba(d->cairo, 0, 0, 0, 0.9); cairo_set_source_rgba(d->cairo, 0, 0, 0, 0.9);
cairo_set_line_width(d->cairo, 1.0); cairo_set_line_width(d->cairo, 1.0);
cairo_stroke(d->cairo); cairo_stroke(d->cairo);
cairo_restore(d->cairo);
} }
else { else {
cairo_rectangle(d->cairo, x, y, w, h); cairo_rectangle(d->cairo, x, y, w, h);
@ -126,41 +234,52 @@ drw_over_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
} }
uint32_t uint32_t
setup_buffer(struct drwsurf *drwsurf) setup_buffer(struct drwsurf *drwsurf, struct drwbuf *drwbuf)
{ {
int prev_size = drwbuf->size;
int stride = drwsurf->width * 4; int stride = drwsurf->width * 4;
drwsurf->size = stride * drwsurf->height; drwbuf->size = stride * drwsurf->height;
int fd = allocate_shm_file(drwsurf->size); int fd = allocate_shm_file(drwbuf->size);
if (fd == -1) { if (fd == -1) {
return 1; return 1;
} }
drwsurf->pool_data = if (drwbuf->pool_data)
mmap(NULL, drwsurf->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); munmap(drwbuf->pool_data, prev_size);
if (drwsurf->pool_data == MAP_FAILED) { drwbuf->pool_data =
mmap(NULL, drwbuf->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (drwbuf->pool_data == MAP_FAILED) {
close(fd); close(fd);
return 1; return 1;
} }
if (drwbuf->buf)
wl_buffer_destroy(drwbuf->buf);
struct wl_shm_pool *pool = struct wl_shm_pool *pool =
wl_shm_create_pool(drwsurf->ctx->shm, fd, drwsurf->size); wl_shm_create_pool(drwsurf->ctx->shm, fd, drwbuf->size);
drwsurf->buf = drwbuf->buf =
wl_shm_pool_create_buffer(pool, 0, drwsurf->width, drwsurf->height, wl_shm_pool_create_buffer(pool, 0, drwsurf->width, drwsurf->height,
stride, WL_SHM_FORMAT_ARGB8888); stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool); wl_shm_pool_destroy(pool);
close(fd); close(fd);
wl_buffer_add_listener(drwbuf->buf, &buffer_listener, drwsurf);
cairo_surface_t *s = cairo_image_surface_create_for_data(
drwsurf->pool_data, CAIRO_FORMAT_ARGB32, drwsurf->width, if (drwbuf->cairo_surf)
cairo_surface_destroy(drwbuf->cairo_surf);
drwbuf->cairo_surf = cairo_image_surface_create_for_data(
drwbuf->pool_data, CAIRO_FORMAT_ARGB32, drwsurf->width,
drwsurf->height, stride); drwsurf->height, stride);
drwsurf->cairo = cairo_create(s); if (drwbuf->cairo)
cairo_scale(drwsurf->cairo, drwsurf->scale, drwsurf->scale); cairo_destroy(drwbuf->cairo);
cairo_set_antialias(drwsurf->cairo, CAIRO_ANTIALIAS_NONE); drwbuf->cairo = cairo_create(drwbuf->cairo_surf);
drwsurf->layout = pango_cairo_create_layout(drwsurf->cairo); cairo_scale(drwbuf->cairo, drwsurf->scale, drwsurf->scale);
pango_layout_set_auto_dir(drwsurf->layout, false); cairo_set_antialias(drwbuf->cairo, CAIRO_ANTIALIAS_NONE);
cairo_save(drwsurf->cairo); drwbuf->layout = pango_cairo_create_layout(drwbuf->cairo);
pango_layout_set_auto_dir(drwbuf->layout, false);
cairo_save(drwbuf->cairo);
return 0; return 0;
} }

35
drw.h
View File

@ -7,42 +7,53 @@
struct drw { struct drw {
struct wl_shm *shm; struct wl_shm *shm;
}; };
struct drwbuf {
uint32_t size;
struct wl_buffer *buf;
cairo_surface_t *cairo_surf;
cairo_t *cairo;
PangoLayout *layout;
unsigned char *pool_data;
};
struct drwsurf { struct drwsurf {
uint32_t width, height, size; uint32_t width, height;
double scale; double scale;
struct drw *ctx; struct drw *ctx;
struct wl_surface *surf; struct wl_surface *surf;
struct wl_buffer *buf;
struct wl_shm *shm; struct wl_shm *shm;
unsigned char *pool_data; struct wl_callback *frame_cb;
cairo_t *cairo; cairo_region_t *damage, *backport_damage;
PangoLayout *layout; bool attached;
bool released;
struct drwbuf *back_buffer;
struct drwbuf *display_buffer;
}; };
struct kbd; struct kbd;
void drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, double s); void drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, double s);
void drwsurf_flip(struct drwsurf *ds); void drwsurf_attach(struct drwsurf *ds);
typedef union { typedef union {
uint8_t bgra[4]; uint8_t bgra[4];
uint32_t color; uint32_t color;
} Color; } Color;
void drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, void drw_do_clear(struct drwsurf *ds, uint32_t x, uint32_t y,
uint32_t w, uint32_t h); uint32_t w, uint32_t h);
void drw_do_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, void drw_do_rectangle(struct drwsurf *ds, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, bool fill, int rounding); uint32_t w, uint32_t h, bool fill, int rounding);
void drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, void drw_fill_rectangle(struct drwsurf *ds, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, int rounding); uint32_t w, uint32_t h, int rounding);
void drw_over_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, void drw_over_rectangle(struct drwsurf *ds, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, int rounding); uint32_t w, uint32_t h, int rounding);
void drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y, void drw_draw_text(struct drwsurf *ds, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, uint32_t b, const char *label, uint32_t w, uint32_t h, uint32_t b, const char *label,
PangoFontDescription *font_description); PangoFontDescription *font_description);
uint32_t setup_buffer(struct drwsurf *drwsurf); uint32_t setup_buffer(struct drwsurf *ds, struct drwbuf *db);
#endif #endif

View File

@ -3,6 +3,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <ctype.h>
#include "keyboard.h" #include "keyboard.h"
#include "drw.h" #include "drw.h"
#include "os-compatibility.h" #include "os-compatibility.h"
@ -276,13 +277,23 @@ kbd_get_layer_index(struct kbd *kb, struct layout *l)
void void
kbd_unpress_key(struct kbd *kb, uint32_t time) kbd_unpress_key(struct kbd *kb, uint32_t time)
{ {
bool unlatch_shift = false; bool unlatch_shift, unlatch_ctrl, unlatch_alt, unlatch_super, unlatch_altgr;
unlatch_shift = unlatch_ctrl = unlatch_alt = unlatch_super = unlatch_altgr = false;
if (kb->last_press) { if (kb->last_press) {
unlatch_shift = (kb->mods & Shift) == Shift; unlatch_shift = (kb->mods & Shift) == Shift;
unlatch_ctrl = (kb->mods & Ctrl) == Ctrl;
unlatch_alt = (kb->mods & Alt) == Alt;
unlatch_super = (kb->mods & Super) == Super;
unlatch_altgr = (kb->mods & AltGr) == AltGr;
if (unlatch_shift) { if (unlatch_shift) kb->mods ^= Shift;
kb->mods ^= Shift; if (unlatch_ctrl) kb->mods ^= Ctrl;
if (unlatch_alt) kb->mods ^= Alt;
if (unlatch_super) kb->mods ^= Super;
if (unlatch_altgr) kb->mods ^= AltGr;
if (unlatch_shift||unlatch_ctrl||unlatch_alt||unlatch_super||unlatch_altgr) {
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
} }
@ -304,7 +315,7 @@ kbd_unpress_key(struct kbd *kb, uint32_t time)
if (kb->compose >= 2) { if (kb->compose >= 2) {
kb->compose = 0; kb->compose = 0;
kbd_switch_layout(kb, kb->last_abc_layout, kb->last_abc_index); kbd_switch_layout(kb, kb->last_abc_layout, kb->last_abc_index);
} else if (unlatch_shift) { } else if (unlatch_shift||unlatch_ctrl||unlatch_alt||unlatch_super||unlatch_altgr) {
kbd_draw_layout(kb); kbd_draw_layout(kb);
} else { } else {
kbd_draw_key(kb, kb->last_press, Unpress); kbd_draw_key(kb, kb->last_press, Unpress);
@ -326,10 +337,7 @@ kbd_release_key(struct kbd *kb, uint32_t time)
kb->last_swipe = NULL; kb->last_swipe = NULL;
} }
drwsurf_flip(kb->surf);
kbd_clear_last_popup(kb); kbd_clear_last_popup(kb);
drwsurf_flip(kb->popup_surf);
} }
void void
@ -355,10 +363,7 @@ kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y)
kbd_unpress_key(kb, time); kbd_unpress_key(kb, time);
} }
drwsurf_flip(kb->surf);
kbd_clear_last_popup(kb); kbd_clear_last_popup(kb);
drwsurf_flip(kb->popup_surf);
} }
void void
@ -418,7 +423,7 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time)
break; break;
case Mod: case Mod:
kb->mods ^= k->code; kb->mods ^= k->code;
if (k->code == Shift) { if ((k->code == Shift) || (k->code == CapsLock)) {
kbd_draw_layout(kb); kbd_draw_layout(kb);
} else { } else {
if (kb->mods & k->code) { if (kb->mods & k->code) {
@ -490,9 +495,6 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time)
default: default:
break; break;
} }
drwsurf_flip(kb->surf);
drwsurf_flip(kb->popup_surf);
} }
void void
@ -527,7 +529,8 @@ kbd_print_key_stdout(struct kbd *kb, struct key *k)
} }
if (!handled) { if (!handled) {
if ((kb->mods & Shift) || (kb->mods & CapsLock)) if ((kb->mods & Shift) ||
((kb->mods & CapsLock) & (strlen(k->label) == 1 && isalpha(k->label[0]))))
printf("%s", k->shift_label); printf("%s", k->shift_label);
else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super)) else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super))
printf("%s", k->label); printf("%s", k->label);
@ -541,9 +544,6 @@ kbd_clear_last_popup(struct kbd *kb)
if (kb->last_popup_w && kb->last_popup_h) { if (kb->last_popup_w && kb->last_popup_h) {
drw_do_clear(kb->popup_surf, kb->last_popup_x, kb->last_popup_y, drw_do_clear(kb->popup_surf, kb->last_popup_x, kb->last_popup_y,
kb->last_popup_w, kb->last_popup_h); kb->last_popup_w, kb->last_popup_h);
wl_surface_damage(kb->popup_surf->surf, kb->last_popup_x,
kb->last_popup_y, kb->last_popup_w, kb->last_popup_h);
kb->last_popup_w = kb->last_popup_h = 0; kb->last_popup_w = kb->last_popup_h = 0;
} }
} }
@ -551,7 +551,8 @@ kbd_clear_last_popup(struct kbd *kb)
void void
kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type) kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type)
{ {
const char *label = (kb->mods & Shift) ? k->shift_label : k->label; const char *label = ((kb->mods & Shift)||((kb->mods & CapsLock) &&
strlen(k->label) == 1 && isalpha(k->label[0]))) ? k->shift_label : k->label;
if (kb->debug) if (kb->debug)
fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h, fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h,
label); label);
@ -575,7 +576,6 @@ kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type)
drw_draw_text(kb->surf, scheme->text, k->x, k->y, k->w, k->h, drw_draw_text(kb->surf, scheme->text, k->x, k->y, k->w, k->h,
KBD_KEY_BORDER, label, scheme->font_description); KBD_KEY_BORDER, label, scheme->font_description);
wl_surface_damage(kb->surf->surf, k->x, k->y, k->w, k->h);
if (type == Press || type == Unpress) { if (type == Press || type == Unpress) {
kbd_clear_last_popup(kb); kbd_clear_last_popup(kb);
@ -592,8 +592,6 @@ kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type)
drw_draw_text(kb->popup_surf, scheme->text, k->x, kb->last_popup_y, drw_draw_text(kb->popup_surf, scheme->text, k->x, kb->last_popup_y,
k->w, k->h, KBD_KEY_BORDER, label, k->w, k->h, KBD_KEY_BORDER, label,
scheme->font_description); scheme->font_description);
wl_surface_damage(kb->popup_surf->surf, k->x, kb->last_popup_y, k->w,
k->h);
} }
} }
@ -620,7 +618,6 @@ kbd_draw_layout(struct kbd *kb)
} }
next_key++; next_key++;
} }
wl_surface_damage(d->surf, 0, 0, kb->w, kb->h);
} }
void void

View File

@ -1626,7 +1626,8 @@ static struct key keys_compose_cyr_tse[] = {
static struct key keys_compose_cyr_g[] = { static struct key keys_compose_cyr_g[] = {
{"ѓ", "Ѓ", 1.0, Copy, 0x0453, 0, 0x0403}, {"ѓ", "Ѓ", 1.0, Copy, 0x0453, 0, 0x0403},
{"", "", 9.0, Pad}, {"ґ", "Ґ", 1.0, Copy, 0x0491, 0, 0x0490},
{"", "", 8.0, Pad},
{"", "", 0.0, EndRow}, {"", "", 0.0, EndRow},
{"", "", 0.0, EndRow}, {"", "", 0.0, EndRow},
{"", "", 1.5, Mod, Shift, .scheme = 1}, {"", "", 1.5, Mod, Shift, .scheme = 1},

48
main.c
View File

@ -28,7 +28,7 @@
#define countof(x) (sizeof(x) / sizeof(*x)) #define countof(x) (sizeof(x) / sizeof(*x))
/* client state */ /* client state */
static const char *namespace = "wlroots"; static const char *namespace = "wvkbd";
static struct wl_display *display; static struct wl_display *display;
static struct wl_compositor *compositor; static struct wl_compositor *compositor;
static struct wl_seat *seat; static struct wl_seat *seat;
@ -62,10 +62,11 @@ static int wl_outputs_size;
/* drawing */ /* drawing */
static struct drw draw_ctx; static struct drw draw_ctx;
static struct drwbuf draw_surf_back_buffer, draw_surf_display_buffer, popup_draw_surf_back_buffer, popup_draw_surf_display_buffer;
static struct drwsurf draw_surf, popup_draw_surf; static struct drwsurf draw_surf, popup_draw_surf;
/* layer surface parameters */ /* layer surface parameters */
static uint32_t layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP; static uint32_t layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
static uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | static uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
@ -132,6 +133,7 @@ static void layer_surface_configure(void *data,
static void layer_surface_closed(void *data, static void layer_surface_closed(void *data,
struct zwlr_layer_surface_v1 *surface); struct zwlr_layer_surface_v1 *surface);
static void flip_landscape(); static void flip_landscape();
static void show();
/* event handlers */ /* event handlers */
static const struct wl_pointer_listener pointer_listener = { static const struct wl_pointer_listener pointer_listener = {
@ -331,7 +333,6 @@ wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
} }
kbd_next_layer(&keyboard, NULL, (value >= 0)); kbd_next_layer(&keyboard, NULL, (value >= 0));
drwsurf_flip(keyboard.surf);
} }
void void
@ -516,7 +517,7 @@ xdg_popup_surface_configure(void *data, struct xdg_surface *xdg_surface,
{ {
xdg_surface_ack_configure(xdg_surface, serial); xdg_surface_ack_configure(xdg_surface, serial);
popup_xdg_surface_configured = true; popup_xdg_surface_configured = true;
drwsurf_flip(&popup_draw_surf); drwsurf_attach(&popup_draw_surf);
} }
static const struct xdg_surface_listener xdg_popup_surface_listener = { static const struct xdg_surface_listener xdg_popup_surface_listener = {
@ -560,12 +561,7 @@ flip_landscape()
if (current_output) { if (current_output) {
keyboard.landscape = current_output->w > current_output->h; keyboard.landscape = current_output->w > current_output->h;
} else if (wl_outputs_size) { } else if (wl_outputs_size) {
for (int i = 0; i < wl_outputs_size; i += 1) { keyboard.landscape = wl_outputs[0].w > wl_outputs[0].h;
if (wl_outputs[i].w > wl_outputs[i].h) {
keyboard.landscape = true;
break;
}
}
} }
enum layout_id layer; enum layout_id layer;
@ -583,10 +579,25 @@ flip_landscape()
keyboard.last_abc_layout = keyboard.layout; keyboard.last_abc_layout = keyboard.layout;
keyboard.last_abc_index = 0; keyboard.last_abc_index = 0;
if (layer_surface) { if (layer_surface && previous_landscape != keyboard.landscape) {
zwlr_layer_surface_v1_set_size(layer_surface, 0, height); if (popup_xdg_popup) {
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); xdg_popup_destroy(popup_xdg_popup);
wl_surface_commit(draw_surf.surf); popup_xdg_popup = NULL;
}
if (popup_xdg_surface) {
xdg_surface_destroy(popup_xdg_surface);
popup_xdg_surface = NULL;
}
if (popup_draw_surf.surf) {
wl_surface_destroy(popup_draw_surf.surf);
popup_draw_surf.surf = NULL;
}
zwlr_layer_surface_v1_destroy(layer_surface);
layer_surface = NULL;
wl_surface_destroy(draw_surf.surf);
show();
} }
} }
@ -653,7 +664,7 @@ layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
zwlr_layer_surface_v1_ack_configure(surface, serial); zwlr_layer_surface_v1_ack_configure(surface, serial);
kbd_resize(&keyboard, layouts, NumLayouts); kbd_resize(&keyboard, layouts, NumLayouts);
drwsurf_flip(&draw_surf); drwsurf_attach(&draw_surf);
} else { } else {
zwlr_layer_surface_v1_ack_configure(surface, serial); zwlr_layer_surface_v1_ack_configure(surface, serial);
} }
@ -1007,7 +1018,11 @@ main(int argc, char **argv)
} }
draw_surf.ctx = &draw_ctx; draw_surf.ctx = &draw_ctx;
draw_surf.back_buffer = &draw_surf_back_buffer;
draw_surf.display_buffer = &draw_surf_display_buffer;
popup_draw_surf.ctx = &draw_ctx; popup_draw_surf.ctx = &draw_ctx;
popup_draw_surf.back_buffer = &popup_draw_surf_back_buffer;
popup_draw_surf.display_buffer = &popup_draw_surf_display_buffer;
keyboard.surf = &draw_surf; keyboard.surf = &draw_surf;
keyboard.popup_surf = &popup_draw_surf; keyboard.popup_surf = &popup_draw_surf;
@ -1031,6 +1046,9 @@ main(int argc, char **argv)
die("virtual_keyboard_manager not available\n"); die("virtual_keyboard_manager not available\n");
} }
// A second round-trip to receive wl_outputs events
wl_display_roundtrip(display);
empty_region = wl_compositor_create_region(compositor); empty_region = wl_compositor_create_region(compositor);
popup_xdg_positioner = xdg_wm_base_create_positioner(wm_base); popup_xdg_positioner = xdg_wm_base_create_positioner(wm_base);

76
wvkbd.1
View File

@ -1,76 +0,0 @@
.TH "wvkbd" "1" "2022-03-12"
.P
.SH NAME
.P
wvkbd - on-screen keyboard for wlroots
.P
.SH SYNOPSIS
.P
wvkbd-mobintl [OPTION]...
.P
.SH DESCRIPTION
.P
wvkbd is an on-screen keyboard for wlroots that sucks less.
.P
.SH OPTIONS
.P
\fB-D\fR
.RS 4
enable debug
.P
.RE
\fB-o\fR
.RS 4
print pressed keys to standard output
.P
.RE
\fB-O\fR
.RS 4
print intersected keys to standard output
.P
.RE
\fB-l\fR
.RS 4
comma separated list of layers
.P
.RE
\fB--landscape-layers\fR
.RS 4
comma separated list of layers used in landscape mode
.P
.RE
\fB-H\fR \fIPX\fR
.RS 4
height in pixels
.P
.RE
\fB-L\fR \fIPX\fR
.RS 4
landscape height in pixels
.P
.RE
\fB--fn\fR \fIFONT\fR
.RS 4
set font (e.g: DejaVu Sans 20)
.P
.RE
\fB--hidden\fR
.RS 4
start hidden (send SIGUSR2 to show, SIGUSR1 to hide, SIGRTMIN to toggle)
.P
.RE
\fB-v\fR, \fB--version\fR
.RS 4
print version
.P
.RE
\fB-h\fR, \fB--help\fR
.RS 4
show help
.P
.RE
.SH AUTHORS
.P
Created by John Sullivan <jsullivan@csumb.edu>, maintained by the Sxmo project in
collaboration with other open source contributors. For more information about wvkbd development, see
<https://git.sr.ht/~proycon/wvkbd> or <https://github.com/jjsullivan5196/wvkbd>.

129
wvkbd.1.scd Normal file
View File

@ -0,0 +1,129 @@
wvkbd(1)
# NAME
wvkbd - on-screen virtual keyboard for wayland compositors using wlroots
# SYNOPSIS
wvkbd-mobintl [OPTIONS]...
*NOTE*: Your binary may have a different suffix depending on which layout you compiled.
# DESCRIPTION
This project aims to deliver a minimal but practically usable implementation of
a wlroots on-screen keyboard in legible C. This will _only_ be a keyboard, not
a feedback buzzer, led blinker, or anything that requires more than what's
needed to input text quickly. The end product should be a static codebase that
can be patched to add new features.
## OPTIONS
*-D*
enable debug mode.
*-o*
print pressed keys to standard output.
*-O*
print intersected keys to standard output.
*-l* _layers_
comma separated list of layers in vertical/portrait mode.
*--landscape-layers* _layers_
comma separated list of layers used in horizontal/landscape mode.
*--list-layers*
prints a list of all available layers.
*-H* _pixels_
Height of the keyboard in pixels, for vertical/portrait mode.
*-L* _pixels_
Height of the keyboard in pixels, for horizontal/landscape mode
*--fn* _font_
set font and size (e.g. DejaVu Sans 20)
*--hidden*
Start hidden (send SIGUSR2 to show).
*--alpha* _int_
Set alpha value (i.e. transparency) for all colors [0-255]
*--bg* _rrggbb|aa_
Set color of background
*--fg* _rrggbb|aa_
Set color of keys
*--fg-sp* _rrggbb|aa_
Set color of special keys
*--press* _rrggbb|aa_
Set color of pressed keys
*--press-sp* _rrggbb|aa_
Set color of pressed special keys
*--swipe* _rrggbb|aa_
Set color of swiped keys
*--swipe-sp* _rrggbb|aa_
Set color of swiped special keys
*--text* _rrggbb|aa_
Set color text on keys
*--text-sp* _rrggbb|aa_
Set color text on special keys
*--version*
Print version information
*-h*, *--help*
Print usage help
# SIGNALS
You can send signals to wvkbd to hide/show it (e.g. using _kill_(1) with _-s_):
*SIGUSR1*
Hide the keyboard.
*SIGUSR2*
Show the keyboard
*SIGRTMIN*
Toggle visibility
# COMPOSE BUTTON
The default mobile international layout features a Compose button (*Cmp*)
which, when combined with another key, opens up a layout that offers variants
for that key. This is similar to functionality that other keyboards implemented
using a *long press* (wvkbd has no such notion, holding a key will repeat
it like on a physical keyboard).
For example, press Cmp + a to access variants with diacritics like á,à,â,ä, etc..
Most layouts also feature the following that are less obvious:
- Press Cmp and . to access more punctuation
- Press Cmp and - or , to access 'mathematical' symbols (+,-,=,etc)
- Press Cmp and ' or 0 or 9 to access more brackets and quotes
- Press Cmp and q to access emojis
Last, but not least, pressing Cmp + space or Cmp + ⌨ or Cmp + Abc opens up an
index that allows you to immediately jump to any layout by name, even layouts
not explicitly added to your layers on startup.
# AUTHORS
Created by John Sullivan <jsullivan@csumb.edu>, maintained by the Sxmo project
<https://sxmo.org> in collaboration with other open source contributors. For
more information about wvkbd development, see <https://git.sr.ht/~proycon/wvkbd>
or <https://github.com/jjsullivan5196/wvkbd>.