applied clang-format (no functional changes), but exempted custom include order in keyboard.c and main.c

This commit is contained in:
Maarten van Gompel 2021-09-19 14:23:51 +02:00 committed by John Sullivan
parent 4695a78e25
commit bb1eff09be
11 changed files with 185 additions and 212 deletions

View File

@ -88,7 +88,7 @@ PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60 PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right PointerAlignment: Right
ReflowComments: true ReflowComments: true
SortIncludes: true SortIncludes: false
SortUsingDeclarations: true SortUsingDeclarations: true
SpaceAfterCStyleCast: false SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true SpaceAfterTemplateKeyword: true

View File

@ -4,39 +4,32 @@
static const char *default_font = "Monospace 14"; static const char *default_font = "Monospace 14";
struct clr_scheme scheme = { struct clr_scheme scheme = {
/* colors */ /* colors */
.bg = {.bgra = {15, 15, 15, 225}}, .bg = {.bgra = {15, 15, 15, 225}},
.fg = {.bgra = {45, 45, 45, 225}}, .fg = {.bgra = {45, 45, 45, 225}},
.high = {.bgra = {100, 100, 100, 225}}, .high = {.bgra = {100, 100, 100, 225}},
.text = {.color = UINT32_MAX}, .text = {.color = UINT32_MAX},
}; };
struct clr_scheme scheme1 = { struct clr_scheme scheme1 = {
/* colors */ /* colors */
.bg = {.bgra = {15, 15, 15, 225}}, .bg = {.bgra = {15, 15, 15, 225}},
.fg = {.bgra = {32, 32, 32, 225}}, .fg = {.bgra = {32, 32, 32, 225}},
.high = {.bgra = {100, 100, 100, 225}}, .high = {.bgra = {100, 100, 100, 225}},
.text = {.color = UINT32_MAX}, .text = {.color = UINT32_MAX},
}; };
/* layers is an ordered list of layouts, used to cycle through */ /* layers is an ordered list of layouts, used to cycle through */
static enum layout_id layers[] = { static enum layout_id layers[] = {
Full, //First layout is the default layout on startup Full, // First layout is the default layout on startup
Special, Special, Emoji, Simple, SimpleGrid, Cyrillic, Arabic,
Emoji, NumLayouts // signals the last item, may not be omitted
Simple,
SimpleGrid,
Cyrillic,
Arabic,
NumLayouts //signals the last item, may not be omitted
}; };
/* layers is an ordered list of layouts, used to cycle through */ /* layers is an ordered list of layouts, used to cycle through */
static enum layout_id landscape_layers[] = { static enum layout_id landscape_layers[] = {
Landscape, //First layout is the default layout on startup Landscape, // First layout is the default layout on startup
Special, Special, Emoji,
Emoji, NumLayouts // signals the last item, may not be omitted
NumLayouts //signals the last item, may not be omitted
}; };
#endif // config_def_h_INCLUDED #endif // config_def_h_INCLUDED

60
drw.c
View File

@ -1,6 +1,6 @@
#include <wayland-client.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
#include <wayland-client.h>
#include "drw.h" #include "drw.h"
#include "shm_open.h" #include "shm_open.h"
@ -23,9 +23,8 @@ drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, uint32_t s) {
static void surface_frame_callback(void *data, struct wl_callback *cb, static void surface_frame_callback(void *data, struct wl_callback *cb,
uint32_t time); uint32_t time);
static struct wl_callback_listener frame_listener = { static struct wl_callback_listener frame_listener = {.done =
.done = surface_frame_callback surface_frame_callback};
};
void void
drwsurf_flip(struct drwsurf *ds) { drwsurf_flip(struct drwsurf *ds) {
@ -46,20 +45,14 @@ surface_frame_callback(void *data, struct wl_callback *cb, uint32_t time) {
} }
void void
drw_draw_text(struct drwsurf *d, Color color, drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
uint32_t x, uint32_t y, uint32_t w, uint32_t h, const char *label) {
uint32_t w, uint32_t h,
const char *label) {
cairo_save(d->cairo); cairo_save(d->cairo);
cairo_set_source_rgba ( cairo_set_source_rgba(
d->cairo, d->cairo, color.bgra[2] / (double)255, color.bgra[1] / (double)255,
color.bgra[2] / (double)255, color.bgra[0] / (double)255, color.bgra[3] / (double)255);
color.bgra[1] / (double)255,
color.bgra[0] / (double)255,
color.bgra[3] / (double)255
);
cairo_move_to(d->cairo, x + (double)w / 2.0, y + (double)h / 2.0); cairo_move_to(d->cairo, x + (double)w / 2.0, y + (double)h / 2.0);
pango_layout_set_text(d->layout, label, -1); pango_layout_set_text(d->layout, label, -1);
@ -67,7 +60,8 @@ drw_draw_text(struct drwsurf *d, Color color,
int width, height; int width, height;
pango_layout_get_size(d->layout, &width, &height); pango_layout_get_size(d->layout, &width, &height);
cairo_rel_move_to(d->cairo, - ((double)width / PANGO_SCALE) / 2, - ((double)height / PANGO_SCALE) / 2); cairo_rel_move_to(d->cairo, -((double)width / PANGO_SCALE) / 2,
-((double)height / PANGO_SCALE) / 2);
pango_cairo_show_layout(d->cairo, d->layout); pango_cairo_show_layout(d->cairo, d->layout);
cairo_restore(d->cairo); cairo_restore(d->cairo);
@ -76,19 +70,15 @@ drw_draw_text(struct drwsurf *d, Color color,
void void
drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h) { uint32_t w, uint32_t h) {
cairo_save(d->cairo); cairo_save(d->cairo);
cairo_set_operator(d->cairo, CAIRO_OPERATOR_SOURCE); cairo_set_operator(d->cairo, CAIRO_OPERATOR_SOURCE);
cairo_rectangle(d->cairo, x, y, w, h); cairo_rectangle(d->cairo, x, y, w, h);
cairo_set_source_rgba( cairo_set_source_rgba(
d->cairo, d->cairo, color.bgra[2] / (double)255, color.bgra[1] / (double)255,
color.bgra[2] / (double)255, color.bgra[0] / (double)255, color.bgra[3] / (double)255);
color.bgra[1] / (double)255,
color.bgra[0] / (double)255,
color.bgra[3] / (double)255
);
cairo_fill(d->cairo); cairo_fill(d->cairo);
cairo_restore(d->cairo); cairo_restore(d->cairo);
@ -97,8 +87,7 @@ drw_fill_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) {
{
int stride = drwsurf->width * 4; int stride = drwsurf->width * 4;
drwsurf->size = stride * drwsurf->height; drwsurf->size = stride * drwsurf->height;
@ -107,31 +96,32 @@ setup_buffer(struct drwsurf *drwsurf)
return 1; return 1;
} }
drwsurf->pool_data = mmap(NULL, drwsurf->size, drwsurf->pool_data =
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); mmap(NULL, drwsurf->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (drwsurf->pool_data == MAP_FAILED) { if (drwsurf->pool_data == MAP_FAILED) {
close(fd); close(fd);
return 1; return 1;
} }
struct wl_shm_pool *pool = wl_shm_create_pool(drwsurf->ctx->shm, fd, drwsurf->size); struct wl_shm_pool *pool =
drwsurf->buf = wl_shm_pool_create_buffer(pool, 0, wl_shm_create_pool(drwsurf->ctx->shm, fd, drwsurf->size);
drwsurf->width, drwsurf->height, stride, WL_SHM_FORMAT_ARGB8888); drwsurf->buf = wl_shm_pool_create_buffer(
pool, 0, drwsurf->width, drwsurf->height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool); wl_shm_pool_destroy(pool);
close(fd); close(fd);
cairo_surface_t *s = cairo_image_surface_create_for_data(drwsurf->pool_data, cairo_surface_t *s = cairo_image_surface_create_for_data(
CAIRO_FORMAT_ARGB32, drwsurf->pool_data, CAIRO_FORMAT_ARGB32, drwsurf->width, drwsurf->height,
drwsurf->width, drwsurf->height, stride); stride);
drwsurf->cairo = cairo_create(s); drwsurf->cairo = cairo_create(s);
cairo_scale(drwsurf->cairo, drwsurf->scale, drwsurf->scale); cairo_scale(drwsurf->cairo, drwsurf->scale, drwsurf->scale);
drwsurf->layout = pango_cairo_create_layout(drwsurf->cairo); drwsurf->layout = pango_cairo_create_layout(drwsurf->cairo);
pango_layout_set_font_description(drwsurf->layout, drwsurf->ctx->font_description); pango_layout_set_font_description(drwsurf->layout,
drwsurf->ctx->font_description);
cairo_save(drwsurf->cairo); cairo_save(drwsurf->cairo);
wl_surface_set_buffer_scale(drwsurf->surf, drwsurf->scale); wl_surface_set_buffer_scale(drwsurf->surf, drwsurf->scale);
return 0; return 0;
} }

15
drw.h
View File

@ -31,17 +31,12 @@ typedef union {
uint32_t color; uint32_t color;
} Color; } Color;
void void drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, uint32_t w, uint32_t h);
uint32_t w, uint32_t h);
void void drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
drw_draw_text(struct drwsurf *d, Color color, uint32_t w, uint32_t h, const char *label);
uint32_t x, uint32_t y,
uint32_t w, uint32_t h,
const char *label);
uint32_t uint32_t setup_buffer(struct drwsurf *drwsurf);
setup_buffer(struct drwsurf *drwsurf);
#endif #endif

View File

@ -22,9 +22,10 @@ void
kbd_switch_layout(struct kbd *kb, struct layout *l) { kbd_switch_layout(struct kbd *kb, struct layout *l) {
kb->prevlayout = kb->layout; kb->prevlayout = kb->layout;
kb->layout = l; kb->layout = l;
if (kb->debug) fprintf(stderr, "Switching to layout %s)\n", kb->layout->name); if (kb->debug)
fprintf(stderr, "Switching to layout %s)\n", kb->layout->name);
if ((!kb->prevlayout) || if ((!kb->prevlayout) ||
(strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) { (strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) {
fprintf(stderr, "Switching to keymap %s\n", kb->layout->keymap_name); fprintf(stderr, "Switching to keymap %s\n", kb->layout->keymap_name);
create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0); create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0);
} }
@ -44,8 +45,8 @@ kbd_get_rows(struct layout *l) {
return rows + 1; return rows + 1;
} }
void
void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list) { kbd_init(struct kbd *kb, struct layout *layouts, char *layer_names_list) {
char *s; char *s;
int i; int i;
bool found; bool found;
@ -54,8 +55,9 @@ void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list)
kb->layouts = layouts; kb->layouts = layouts;
for (i = 0; i < NumLayouts - 1; i++); for (i = 0; i < NumLayouts - 1; i++)
fprintf(stderr, "Found %d layouts\n",i); ;
fprintf(stderr, "Found %d layouts\n", i);
kb->layer_index = 0; kb->layer_index = 0;
@ -81,9 +83,9 @@ void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list)
fprintf(stderr, "No such layer: %s\n", s); fprintf(stderr, "No such layer: %s\n", s);
exit(3); exit(3);
} }
s = strtok(NULL,","); s = strtok(NULL, ",");
} }
kb->layers[numlayers] = NumLayouts; //mark the end of the sequence kb->layers[numlayers] = NumLayouts; // mark the end of the sequence
if (numlayers == 0) { if (numlayers == 0) {
fprintf(stderr, "No layers defined\n"); fprintf(stderr, "No layers defined\n");
exit(3); exit(3);
@ -95,7 +97,7 @@ void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list)
while (lid != NumLayouts) { while (lid != NumLayouts) {
lid = kb->layers[++i]; lid = kb->layers[++i];
} }
fprintf(stderr, "Found %d layers\n",i); fprintf(stderr, "Found %d layers\n", i);
enum layout_id layer; enum layout_id layer;
if (kb->landscape) { if (kb->landscape) {
@ -150,7 +152,8 @@ struct key *
kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) { kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
struct layout *l = kb->layout; struct layout *l = kb->layout;
struct key *k = l->keys; struct key *k = l->keys;
if (kb->debug) fprintf(stderr, "get key: +%d+%d\n", x, y); if (kb->debug)
fprintf(stderr, "get key: +%d+%d\n", x, y);
while (k->type != Last) { while (k->type != Last) {
if ((k->type != EndRow) && (k->type != Pad) && (k->type != Pad) && if ((k->type != EndRow) && (k->type != Pad) && (k->type != Pad) &&
(x >= k->x) && (y >= k->y) && (x < k->x + k->w) && (y < k->y + k->h)) { (x >= k->x) && (y >= k->y) && (x < k->x + k->w) && (y < k->y + k->h)) {
@ -191,7 +194,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
if ((kb->compose == 1) && (k->type != Compose) && (k->type != Mod) && if ((kb->compose == 1) && (k->type != Compose) && (k->type != Mod) &&
(k->layout)) { (k->layout)) {
kb->compose++; kb->compose++;
if (kb->debug) fprintf(stderr, "showing compose %d\n", kb->compose); if (kb->debug)
fprintf(stderr, "showing compose %d\n", kb->compose);
kbd_switch_layout(kb, k->layout); kbd_switch_layout(kb, k->layout);
return; return;
} }
@ -215,7 +219,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
if (kb->print) if (kb->print)
kbd_print_key_stdout(kb, k); kbd_print_key_stdout(kb, k);
if (kb->compose) { if (kb->compose) {
if (kb->debug) fprintf(stderr, "pressing composed key\n"); if (kb->debug)
fprintf(stderr, "pressing composed key\n");
kb->compose++; kb->compose++;
} }
break; break;
@ -228,11 +233,11 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
break; break;
case Layout: case Layout:
//switch to the layout determined by the key // switch to the layout determined by the key
kbd_switch_layout(kb, k->layout); kbd_switch_layout(kb, k->layout);
break; break;
case Compose: case Compose:
//switch to the associated layout determined by the *next* keypress // switch to the associated layout determined by the *next* keypress
if (kb->compose == 0) { if (kb->compose == 0) {
kb->compose = 1; kb->compose = 1;
} else { } else {
@ -241,7 +246,7 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
kbd_draw_key(kb, k, (bool)kb->compose); kbd_draw_key(kb, k, (bool)kb->compose);
break; break;
case NextLayer: case NextLayer:
//switch to the next layout in the layer sequence // switch to the next layout in the layer sequence
kb->layer_index++; kb->layer_index++;
enum layout_id layer; enum layout_id layer;
if (kb->landscape) { if (kb->landscape) {
@ -260,15 +265,16 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
kbd_switch_layout(kb, &kb->layouts[layer]); kbd_switch_layout(kb, &kb->layouts[layer]);
break; break;
case BackLayer: case BackLayer:
//switch to the previously active layout // switch to the previously active layout
if (kb->prevlayout) if (kb->prevlayout)
kbd_switch_layout(kb, kb->prevlayout); kbd_switch_layout(kb, kb->prevlayout);
break; break;
case Copy: case Copy:
//copy code as unicode chr by setting a temporary keymap // copy code as unicode chr by setting a temporary keymap
kb->last_press = k; kb->last_press = k;
kbd_draw_key(kb, k, true); kbd_draw_key(kb, k, true);
if (kb->debug) fprintf(stderr, "pressing copy key\n"); if (kb->debug)
fprintf(stderr, "pressing copy key\n");
create_and_upload_keymap(kb, kb->layout->keymap_name, k->code, k->code_mod); create_and_upload_keymap(kb, kb->layout->keymap_name, k->code, k->code_mod);
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key
@ -281,7 +287,6 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
} }
} }
void void
kbd_print_key_stdout(struct kbd *kb, struct key *k) { kbd_print_key_stdout(struct kbd *kb, struct key *k) {
/* printed keys may slightly differ from the actual output /* printed keys may slightly differ from the actual output
@ -292,21 +297,21 @@ kbd_print_key_stdout(struct kbd *kb, struct key *k) {
bool handled = true; bool handled = true;
if (k->type == Code) { if (k->type == Code) {
switch (k->code) { switch (k->code) {
case KEY_SPACE: case KEY_SPACE:
printf(" "); printf(" ");
break; break;
case KEY_ENTER: case KEY_ENTER:
printf("\n"); printf("\n");
break; break;
case KEY_BACKSPACE: case KEY_BACKSPACE:
printf("\b"); printf("\b");
break; break;
case KEY_TAB: case KEY_TAB:
printf("\t"); printf("\t");
break; break;
default: default:
handled = false; handled = false;
break; break;
} }
} else if (k->type != Copy) { } else if (k->type != Copy) {
return; return;
@ -325,8 +330,9 @@ void
kbd_draw_key(struct kbd *kb, struct key *k, bool pressed) { kbd_draw_key(struct kbd *kb, struct key *k, bool pressed) {
struct drwsurf *d = kb->surf; struct drwsurf *d = kb->surf;
const char *label = (kb->mods & Shift) ? k->shift_label : k->label; const char *label = (kb->mods & Shift) ? k->shift_label : k->label;
if (kb->debug) fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h, if (kb->debug)
label); fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h,
label);
struct clr_scheme *scheme = (k->scheme == 0) ? &(kb->scheme) : &(kb->scheme1); struct clr_scheme *scheme = (k->scheme == 0) ? &(kb->scheme) : &(kb->scheme1);
Color *fill = pressed ? &scheme->high : &scheme->fg; Color *fill = pressed ? &scheme->high : &scheme->fg;
draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, *fill); draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, *fill);
@ -338,7 +344,8 @@ kbd_draw_layout(struct kbd *kb) {
struct drwsurf *d = kb->surf; struct drwsurf *d = kb->surf;
struct key *next_key = kb->layout->keys; struct key *next_key = kb->layout->keys;
bool pressed = false; bool pressed = false;
if (kb->debug) fprintf(stderr, "Draw layout"); if (kb->debug)
fprintf(stderr, "Draw layout");
drw_fill_rectangle(d, kb->scheme.bg, 0, 0, kb->w, kb->h); drw_fill_rectangle(d, kb->scheme.bg, 0, 0, kb->w, kb->h);
@ -357,7 +364,8 @@ void
kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t layoutcount) { kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t layoutcount) {
struct drwsurf *d = kb->surf; struct drwsurf *d = kb->surf;
fprintf(stderr, "Resize %dx%d %d, %d layouts\n", kb->w, kb->h, kb->s, layoutcount); fprintf(stderr, "Resize %dx%d %d, %d layouts\n", kb->w, kb->h, kb->s,
layoutcount);
drwsurf_resize(d, kb->w, kb->h, kb->s); drwsurf_resize(d, kb->w, kb->h, kb->s);
for (int i = 0; i < layoutcount; i++) { for (int i = 0; i < layoutcount; i++) {
@ -374,7 +382,7 @@ draw_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width,
} }
void void
create_and_upload_keymap(struct kbd * kb, const char *name, uint32_t comp_unichr, create_and_upload_keymap(struct kbd *kb, const char *name, uint32_t comp_unichr,
uint32_t comp_shift_unichr) { uint32_t comp_shift_unichr) {
int keymap_index = -1; int keymap_index = -1;
for (int i = 0; i < NUMKEYMAPS; i++) { for (int i = 0; i < NUMKEYMAPS; i++) {
@ -383,10 +391,10 @@ create_and_upload_keymap(struct kbd * kb, const char *name, uint32_t comp_unichr
} }
} }
if (keymap_index == -1) { if (keymap_index == -1) {
fprintf(stderr,"No such keymap defined: %s\n", name); fprintf(stderr, "No such keymap defined: %s\n", name);
exit(9); exit(9);
} }
const char * keymap_template = keymaps[keymap_index]; const char *keymap_template = keymaps[keymap_index];
const size_t keymap_size = strlen(keymap_template) + 64; const size_t keymap_size = strlen(keymap_template) + 64;
char *keymap_str = malloc(keymap_size); char *keymap_str = malloc(keymap_size);
sprintf(keymap_str, keymap_template, comp_unichr, comp_shift_unichr); sprintf(keymap_str, keymap_template, comp_unichr, comp_shift_unichr);
@ -403,7 +411,7 @@ create_and_upload_keymap(struct kbd * kb, const char *name, uint32_t comp_unichr
die("kb.vkbd = NULL\n"); die("kb.vkbd = NULL\n");
} }
strcpy(ptr, keymap_str); strcpy(ptr, keymap_str);
zwp_virtual_keyboard_v1_keymap( zwp_virtual_keyboard_v1_keymap(kb->vkbd, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
kb->vkbd, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keymap_fd, keymap_size); keymap_fd, keymap_size);
free((void *)keymap_str); free((void *)keymap_str);
} }

View File

@ -13,17 +13,19 @@ struct layout;
struct kbd; struct kbd;
enum key_type { enum key_type {
Pad = 0, //Padding, not a pressable key Pad = 0, // Padding, not a pressable key
Code, //A normal key emitting a keycode Code, // A normal key emitting a keycode
Mod, //A modifier key Mod, // A modifier key
Copy, //Copy key, copies the unicode value specified in code (creates and activates temporary keymap) Copy, // Copy key, copies the unicode value specified in code (creates and
// used for keys that are not part of the keymap // activates temporary keymap)
Layout, //Layout switch to a specific layout // used for keys that are not part of the keymap
BackLayer, //Layout switch to the layout that was previously active Layout, // Layout switch to a specific layout
NextLayer, //Layout switch to the next layout in the layers sequence BackLayer, // Layout switch to the layout that was previously active
Compose, //Compose modifier key, switches to a specific associated layout upon next keypress NextLayer, // Layout switch to the next layout in the layers sequence
EndRow, //Incidates the end of a key row Compose, // Compose modifier key, switches to a specific associated layout
Last, //Indicated the end of a layout // upon next keypress
EndRow, // Incidates the end of a key row
Last, // Indicated the end of a layout
}; };
/* Modifiers passed to the virtual_keyboard protocol. They are based on /* Modifiers passed to the virtual_keyboard protocol. They are based on
@ -53,12 +55,12 @@ struct key {
const enum key_type type; const enum key_type type;
const uint32_t const uint32_t
code; /* code: key scancode or modifier name (see code; /* code: key scancode or modifier name (see
* `/usr/include/linux/input-event-codes.h` for scancode names, and * `/usr/include/linux/input-event-codes.h` for scancode names, and
* `keyboard.h` for modifiers) * `keyboard.h` for modifiers)
* XKB keycodes are +8 */ * XKB keycodes are +8 */
struct layout *layout; // pointer back to the parent layout that holds this struct layout *layout; // pointer back to the parent layout that holds this
// key // key
const uint32_t code_mod; /* modifier to force when this key is pressed */ const uint32_t code_mod; /* modifier to force when this key is pressed */
uint8_t scheme; // index of the scheme to use uint8_t scheme; // index of the scheme to use
bool reset_mod; /* reset modifiers when clicked */ bool reset_mod; /* reset modifiers when clicked */
@ -99,11 +101,10 @@ struct kbd {
struct zwp_virtual_keyboard_v1 *vkbd; struct zwp_virtual_keyboard_v1 *vkbd;
}; };
void draw_inset(struct drwsurf *ds, uint32_t x, uint32_t y, void draw_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width,
uint32_t width, uint32_t height, uint32_t border, uint32_t height, uint32_t border, Color color);
Color color);
void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list); void kbd_init(struct kbd *kb, struct layout *layouts, char *layer_names_list);
void kbd_init_layout(struct layout *l, uint32_t width, uint32_t height); void kbd_init_layout(struct layout *l, uint32_t width, uint32_t height);
struct key *kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y); struct key *kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y);
void kbd_unpress_key(struct kbd *kb, uint32_t time); void kbd_unpress_key(struct kbd *kb, uint32_t time);
@ -116,8 +117,8 @@ uint8_t kbd_get_rows(struct layout *l);
double kbd_get_row_length(struct key *k); double kbd_get_row_length(struct key *k);
void kbd_switch_layout(struct kbd *kb, struct layout *l); void kbd_switch_layout(struct kbd *kb, struct layout *l);
void create_and_upload_keymap(struct kbd *kb, const char *name, uint32_t comp_unichr, void create_and_upload_keymap(struct kbd *kb, const char *name,
uint32_t comp_shift_unichr); uint32_t comp_unichr, uint32_t comp_shift_unichr);
#ifndef LAYOUT #ifndef LAYOUT
#error "make sure to define LAYOUT" #error "make sure to define LAYOUT"

View File

@ -1,11 +1,11 @@
#define NUMKEYMAPS 3 #define NUMKEYMAPS 3
static const char * keymap_names[] = {"latin","cyrillic","arabic"}; static const char *keymap_names[] = {"latin", "cyrillic", "arabic"};
static const char * keymaps[NUMKEYMAPS] = { static const char *keymaps[NUMKEYMAPS] = {
//LATIN // LATIN
"xkb_keymap {\ "xkb_keymap {\
xkb_keycodes \"(unnamed)\" {\ xkb_keycodes \"(unnamed)\" {\
minimum = 8;\ minimum = 8;\
maximum = 255;\ maximum = 255;\
@ -1468,8 +1468,8 @@ xkb_symbols \"(unnamed)\" {\
\ \
};\ };\
", ",
//CYRILLIC // CYRILLIC
"xkb_keymap {\ "xkb_keymap {\
xkb_keycodes \"(unnamed)\" {\ xkb_keycodes \"(unnamed)\" {\
minimum = 8;\ minimum = 8;\
maximum = 255;\ maximum = 255;\
@ -2931,8 +2931,8 @@ xkb_symbols \"(unnamed)\" {\
\ \
};\ };\
", ",
//ARABIC // ARABIC
"xkb_keymap {\ "xkb_keymap {\
xkb_keycodes \"(unnamed)\" {\ xkb_keycodes \"(unnamed)\" {\
minimum = 8;\ minimum = 8;\
maximum = 255;\ maximum = 255;\
@ -4389,6 +4389,4 @@ xkb_symbols \"(unnamed)\" {\
modifier_map Mod4 { <LWIN>, <RWIN>, <SUPR>, <HYPR> };\ modifier_map Mod4 { <LWIN>, <RWIN>, <SUPR>, <HYPR> };\
modifier_map Mod5 { <LVL3>, <MDSW> };\ modifier_map Mod5 { <LVL3>, <MDSW> };\
};\ };\
};" };"};
};

View File

@ -63,21 +63,19 @@ enum layout_id {
NumLayouts, NumLayouts,
}; };
static struct key keys_full[], keys_special[], keys_simple[], keys_simplegrid[], keys_cyrillic[], static struct key keys_full[], keys_special[], keys_simple[], keys_simplegrid[],
keys_arabic[], keys_cyrillic[], keys_arabic[], keys_emoji[], keys_landscape[],
keys_emoji[],
keys_landscape[],
keys_compose_a[], keys_compose_e[], keys_compose_y[], keys_compose_u[], keys_compose_a[], keys_compose_e[], keys_compose_y[], keys_compose_u[],
keys_compose_i[], keys_compose_o[], keys_compose_w[], keys_compose_i[], keys_compose_o[], keys_compose_w[], keys_compose_r[],
keys_compose_r[], keys_compose_t[], keys_compose_p[], keys_compose_s[], keys_compose_t[], keys_compose_p[], keys_compose_s[], keys_compose_d[],
keys_compose_d[], keys_compose_f[], keys_compose_g[], keys_compose_h[], keys_compose_f[], keys_compose_g[], keys_compose_h[], keys_compose_j[],
keys_compose_j[], keys_compose_k[], keys_compose_l[], keys_compose_z[], keys_compose_k[], keys_compose_l[], keys_compose_z[], keys_compose_x[],
keys_compose_x[], keys_compose_c[], keys_compose_v[], keys_compose_b[], keys_compose_c[], keys_compose_v[], keys_compose_b[], keys_compose_n[],
keys_compose_n[], keys_compose_m[], keys_compose_math[], keys_compose_m[], keys_compose_math[], keys_compose_punctuation[],
keys_compose_punctuation[], keys_compose_bracket[], keys_compose_cyr_i[], keys_compose_bracket[], keys_compose_cyr_i[], keys_compose_cyr_j[],
keys_compose_cyr_j[], keys_compose_cyr_e[], keys_compose_cyr_u[], keys_compose_cyr_e[], keys_compose_cyr_u[], keys_compose_cyr_l[],
keys_compose_cyr_l[], keys_compose_cyr_n[], keys_compose_cyr_tse[], keys_compose_cyr_n[], keys_compose_cyr_tse[], keys_compose_cyr_che[],
keys_compose_cyr_che[], keys_compose_cyr_g[], keys_compose_cyr_k[]; keys_compose_cyr_g[], keys_compose_cyr_k[];
static struct layout layouts[NumLayouts] = { static struct layout layouts[NumLayouts] = {
[Full] = {keys_full, "latin", "full"}, // second parameter is the keymap name [Full] = {keys_full, "latin", "full"}, // second parameter is the keymap name
@ -129,8 +127,6 @@ static struct layout layouts[NumLayouts] = {
[ComposeCyrK] = {keys_compose_cyr_k, "cyrillic"}, [ComposeCyrK] = {keys_compose_cyr_k, "cyrillic"},
}; };
/* key layouts /* key layouts
* *
* define keys like: * define keys like:
@ -711,14 +707,14 @@ static struct key keys_emoji[] = {
{"😛", "😜", 1.0, Copy, 0x1f61b, 0, 0x1f61c}, {"😛", "😜", 1.0, Copy, 0x1f61b, 0, 0x1f61c},
{"😮", "😝", 1.0, Copy, 0x1f62e, 0, 0x1f61d}, {"😮", "😝", 1.0, Copy, 0x1f62e, 0, 0x1f61d},
{"😟", "😞", 1.0, Copy, 0x1f61f, 0, 0x1f61e}, {"😟", "😞", 1.0, Copy, 0x1f61f, 0, 0x1f61e},
{"😟", "🥺", 1.0, Copy, 0x1f620, 0, 0x1f97a }, {"😟", "🥺", 1.0, Copy, 0x1f620, 0, 0x1f97a},
{"😢", "👿", 1.0, Copy, 0x1f622, 0, 0x1f47f}, {"😢", "👿", 1.0, Copy, 0x1f622, 0, 0x1f47f},
{"😭", "😯", 1.0, Copy, 0x1f62d, 0, 0x1f62f}, {"😭", "😯", 1.0, Copy, 0x1f62d, 0, 0x1f62f},
{"😳", "😕", 1.0, Copy, 0x1f633, 0, 0x1f615}, {"😳", "😕", 1.0, Copy, 0x1f633, 0, 0x1f615},
{"😴", "😵", 1.0, Copy, 0x1f634, 0, 0x1f635}, {"😴", "😵", 1.0, Copy, 0x1f634, 0, 0x1f635},
{"", "", 1.0, Code, KEY_BACKSPACE, .scheme=1}, {"", "", 1.0, Code, KEY_BACKSPACE, .scheme = 1},
{"", "", 0.0, EndRow}, {"", "", 0.0, EndRow},
{"Abc", "Abc", 1.0, BackLayer, .scheme=1}, {"Abc", "Abc", 1.0, BackLayer, .scheme = 1},
{"👆", "👊", 1.0, Copy, 0x1f446, 0, 0x1f44a}, {"👆", "👊", 1.0, Copy, 0x1f446, 0, 0x1f44a},
{"👇", "👌", 1.0, Copy, 0x1f447, 0, 0x1f44c}, {"👇", "👌", 1.0, Copy, 0x1f447, 0, 0x1f44c},
{"👈", "👏", 1.0, Copy, 0x1f448, 0, 0x1f44f}, {"👈", "👏", 1.0, Copy, 0x1f448, 0, 0x1f44f},
@ -726,7 +722,7 @@ static struct key keys_emoji[] = {
{"👋", "🙌", 1.0, Copy, 0x1f603, 0, 0x1f44c}, {"👋", "🙌", 1.0, Copy, 0x1f603, 0, 0x1f44c},
{"👍", "", 1.0, Copy, 0x1f604, 0, 0x2705}, {"👍", "", 1.0, Copy, 0x1f604, 0, 0x2705},
{"👎", "💪", 1.0, Copy, 0x1f605, 0, 0x1f606}, {"👎", "💪", 1.0, Copy, 0x1f605, 0, 0x1f606},
{"Enter", "Enter", 2.0, Code, KEY_ENTER, .scheme=1}, {"Enter", "Enter", 2.0, Code, KEY_ENTER, .scheme = 1},
{"", "", 0.0, Last}, {"", "", 0.0, Last},
}; };
@ -1460,4 +1456,3 @@ static struct key keys_compose_bracket[] = {
{"Enter", "Enter", 2.0, Code, KEY_ENTER, .scheme = 1}, {"Enter", "Enter", 2.0, Code, KEY_ENTER, .scheme = 1},
{"", "", 0.0, Last}, {"", "", 0.0, Last},
}; };

70
main.c
View File

@ -5,9 +5,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h> #include <wayland-client.h>
#include <wchar.h> #include <wchar.h>
#include <unistd.h>
#include "keyboard.h" #include "keyboard.h"
#include "config.h" #include "config.h"
@ -127,8 +127,7 @@ static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
/* configuration, allows nested code to access above variables */ /* configuration, allows nested code to access above variables */
char * char *
estrdup(const char *s) estrdup(const char *s) {
{
char *p; char *p;
if (!(p = strdup(s))) { if (!(p = strdup(s))) {
@ -236,8 +235,9 @@ void
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) {} seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) {}
static void static void
display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make, const char *model, int transform) display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y,
{ int physical_width, int physical_height, int subpixel,
const char *make, const char *model, int transform) {
if (transform % 2 == 0 && keyboard.landscape) { if (transform % 2 == 0 && keyboard.landscape) {
keyboard.landscape = false; keyboard.landscape = false;
height = normal_height; height = normal_height;
@ -264,28 +264,22 @@ display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, i
} }
static void static void
display_handle_done(void *data, struct wl_output *wl_output) display_handle_done(void *data, struct wl_output *wl_output) {}
{
}
static void static void
display_handle_scale(void *data, struct wl_output *wl_output, int32_t scale) display_handle_scale(void *data, struct wl_output *wl_output, int32_t scale) {
{
keyboard.s = scale; keyboard.s = scale;
} }
static void static void
display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int width, int height, int refresh) display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
{ int width, int height, int refresh) {}
}
static const struct wl_output_listener output_listener = { static const struct wl_output_listener output_listener = {
.geometry = display_handle_geometry, .geometry = display_handle_geometry,
.mode = display_handle_mode, .mode = display_handle_mode,
.done = display_handle_done, .done = display_handle_done,
.scale = display_handle_scale .scale = display_handle_scale};
};
void void
handle_global(void *data, struct wl_registry *registry, uint32_t name, handle_global(void *data, struct wl_registry *registry, uint32_t name,
@ -334,9 +328,11 @@ layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface) {
} }
void void
usage(char *argv0) usage(char *argv0) {
{ fprintf(stderr,
fprintf(stderr, "usage: %s [-hov] [-H height] [-L landscape height] [-fn font] [-l layers]\n", argv0); "usage: %s [-hov] [-H height] [-L landscape height] [-fn font] [-l "
"layers]\n",
argv0);
fprintf(stderr, "Options:\n"); fprintf(stderr, "Options:\n");
fprintf(stderr, " -D - Enable debug\n"); fprintf(stderr, " -D - Enable debug\n");
fprintf(stderr, " -o - Print press keys to standard output\n"); fprintf(stderr, " -o - Print press keys to standard output\n");
@ -347,8 +343,7 @@ usage(char *argv0)
} }
void void
freeze(int sigint) freeze(int sigint) {
{
signal(SIGUSR1, freeze); signal(SIGUSR1, freeze);
if (!layer_surface) { if (!layer_surface) {
return; return;
@ -364,8 +359,7 @@ freeze(int sigint)
} }
void void
unfreeze(int sigint) unfreeze(int sigint) {
{
signal(SIGUSR2, unfreeze); signal(SIGUSR2, unfreeze);
if (layer_surface) { if (layer_surface) {
return; return;
@ -373,7 +367,8 @@ unfreeze(int sigint)
wl_display_sync(display); wl_display_sync(display);
draw_surf.surf = wl_compositor_create_surface(compositor);; draw_surf.surf = wl_compositor_create_surface(compositor);
;
layer_surface = zwlr_layer_shell_v1_get_layer_surface( layer_surface = zwlr_layer_shell_v1_get_layer_surface(
layer_shell, draw_surf.surf, wl_output, layer, namespace); layer_shell, draw_surf.surf, wl_output, layer, namespace);
@ -381,7 +376,8 @@ unfreeze(int sigint)
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor); zwlr_layer_surface_v1_set_anchor(layer_surface, anchor);
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height);
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false); zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false);
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, NULL); zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener,
NULL);
wl_surface_commit(draw_surf.surf); wl_surface_commit(draw_surf.surf);
wl_display_roundtrip(display); wl_display_roundtrip(display);
@ -405,14 +401,13 @@ main(int argc, char **argv) {
landscape_height = atoi(tmp); landscape_height = atoi(tmp);
/* keyboard settings */ /* keyboard settings */
keyboard.layers = (enum layout_id *) &layers; keyboard.layers = (enum layout_id *)&layers;
keyboard.landscape_layers = (enum layout_id *) &landscape_layers; keyboard.landscape_layers = (enum layout_id *)&landscape_layers;
keyboard.scheme = scheme; keyboard.scheme = scheme;
keyboard.layer_index = 0; keyboard.layer_index = 0;
keyboard.scheme1 = scheme1; keyboard.scheme1 = scheme1;
keyboard.scheme1 = scheme1; keyboard.scheme1 = scheme1;
int i; int i;
for (i = 1; argv[i]; i++) { for (i = 1; argv[i]; i++) {
if ((!strcmp(argv[i], "-v")) || (!strcmp(argv[i], "--version"))) { if ((!strcmp(argv[i], "-v")) || (!strcmp(argv[i], "--version"))) {
@ -489,11 +484,13 @@ main(int argc, char **argv) {
die("failed to init virtual keyboard_manager\n"); die("failed to init virtual keyboard_manager\n");
} }
kbd_init(&keyboard, (struct layout *) &layouts, layer_names_list); kbd_init(&keyboard, (struct layout *)&layouts, layer_names_list);
draw_surf.surf = wl_compositor_create_surface(compositor);; draw_surf.surf = wl_compositor_create_surface(compositor);
;
draw_ctx.font_description = pango_font_description_from_string(fc_font_pattern); draw_ctx.font_description =
pango_font_description_from_string(fc_font_pattern);
layer_surface = zwlr_layer_shell_v1_get_layer_surface( layer_surface = zwlr_layer_shell_v1_get_layer_surface(
layer_shell, draw_surf.surf, wl_output, layer, namespace); layer_shell, draw_surf.surf, wl_output, layer, namespace);
@ -502,7 +499,8 @@ main(int argc, char **argv) {
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor); zwlr_layer_surface_v1_set_anchor(layer_surface, anchor);
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height);
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false); zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false);
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, NULL); zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener,
NULL);
wl_surface_commit(draw_surf.surf); wl_surface_commit(draw_surf.surf);
wl_display_roundtrip(display); wl_display_roundtrip(display);
@ -521,7 +519,7 @@ main(int argc, char **argv) {
} }
if (fc_font_pattern != default_font) { if (fc_font_pattern != default_font) {
free((void*) fc_font_pattern); free((void *)fc_font_pattern);
} }
return 0; return 0;

View File

@ -6,21 +6,19 @@
#include <unistd.h> #include <unistd.h>
static void static void
randname(char *buf) randname(char *buf) {
{
struct timespec ts; struct timespec ts;
long r; long r;
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
r = ts.tv_nsec; r = ts.tv_nsec;
for (int i = 0; i < 6; ++i) { for (int i = 0; i < 6; ++i) {
buf[i] = 'A'+(r&15)+(r&16)*2; buf[i] = 'A' + (r & 15) + (r & 16) * 2;
r >>= 5; r >>= 5;
} }
} }
static int static int
create_shm_file(void) create_shm_file(void) {
{
int retries = 100; int retries = 100;
int fd; int fd;
do { do {
@ -37,8 +35,7 @@ create_shm_file(void)
} }
int int
allocate_shm_file(size_t size) allocate_shm_file(size_t size) {
{
int fd = create_shm_file(); int fd = create_shm_file();
int ret; int ret;
if (fd < 0) if (fd < 0)
@ -52,4 +49,3 @@ allocate_shm_file(size_t size)
} }
return fd; return fd;
} }

View File

@ -6,4 +6,3 @@ int create_shm_file(void);
int allocate_shm_file(size_t size); int allocate_shm_file(size_t size);
#endif // shm_open_h_INCLUDED #endif // shm_open_h_INCLUDED