From fbbf563d708cab49e62993d2e94d21381f870ac8 Mon Sep 17 00:00:00 2001 From: Willow Barraco Date: Fri, 8 Sep 2023 22:56:50 +0200 Subject: [PATCH] Simplest clang-format --- .clang-format | 109 +--- drw.c | 181 +++--- keyboard.c | 1143 ++++++++++++++++++----------------- main.c | 1405 ++++++++++++++++++++++---------------------- os-compatibility.c | 189 +++--- shm_open.c | 75 ++- 6 files changed, 1476 insertions(+), 1626 deletions(-) diff --git a/.clang-format b/.clang-format index d275da8..80d3293 100644 --- a/.clang-format +++ b/.clang-format @@ -1,107 +1,2 @@ ---- -Language: Cpp -# BasedOnStyle: LLVM -AccessModifierOffset: -2 -AlignAfterOpenBracket: Align -AlignConsecutiveAssignments: false -AlignConsecutiveDeclarations: false -AlignEscapedNewlines: Right -AlignOperands: true -AlignTrailingComments: true -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortBlocksOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: All -AllowShortIfStatementsOnASingleLine: false -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: All -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: false -BinPackArguments: true -BinPackParameters: true -BraceWrapping: - AfterClass: false - AfterControlStatement: false - AfterEnum: false - AfterFunction: false - AfterNamespace: false - AfterObjCDeclaration: false - AfterStruct: false - AfterUnion: false - BeforeCatch: false - BeforeElse: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true -BreakBeforeBinaryOperators: None -BreakBeforeBraces: Attach -BreakBeforeInheritanceComma: false -BreakBeforeTernaryOperators: true -BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeColon -BreakAfterJavaFieldAnnotations: false -BreakStringLiterals: true -ColumnLimit: 80 -CommentPragmas: '^ IWYU pragma:' -CompactNamespaces: false -ConstructorInitializerAllOnOneLineOrOnePerLine: false -ConstructorInitializerIndentWidth: 2 -ContinuationIndentWidth: 2 -Cpp11BracedListStyle: true -DerivePointerAlignment: false -DisableFormat: false -ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: true -ForEachMacros: - - foreach - - Q_FOREACH - - BOOST_FOREACH -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - - Regex: '^(<|"(gtest|gmock|isl|json)/)' - Priority: 3 - - Regex: '.*' - Priority: 1 -IncludeIsMainRegex: '(Test)?$' -IndentCaseLabels: false -IndentWidth: 2 -IndentWrappedFunctionNames: false -JavaScriptQuotes: Leave -JavaScriptWrapImports: true -KeepEmptyLinesAtTheStartOfBlocks: true -MacroBlockBegin: '' -MacroBlockEnd: '' -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -ObjCBlockIndentWidth: 2 -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PenaltyBreakAssignment: 2 -PenaltyBreakBeforeFirstCallParameter: 19 -PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 -PenaltyBreakString: 1000 -PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 60 -PointerAlignment: Right -ReflowComments: true -SortIncludes: false -SortUsingDeclarations: true -SpaceAfterCStyleCast: false -SpaceAfterTemplateKeyword: true -SpaceBeforeAssignmentOperators: true -SpaceBeforeParens: ControlStatements -SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 1 -SpacesInAngles: false -SpacesInContainerLiterals: true -SpacesInCStyleCastParentheses: false -SpacesInParentheses: false -SpacesInSquareBrackets: false -Standard: Cpp11 -TabWidth: 2 -UseTab: ForIndentation -... +BasedOnStyle: LLVM +IndentWidth: 4 diff --git a/drw.c b/drw.c index 9c5edec..713e85e 100644 --- a/drw.c +++ b/drw.c @@ -5,131 +5,124 @@ #include "drw.h" #include "shm_open.h" -void -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; - } +void 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->width = w * s; - ds->height = h * s; + ds->scale = s; + ds->width = w * s; + ds->height = h * s; - setup_buffer(ds); + setup_buffer(ds); } -void -drwsurf_flip(struct drwsurf *ds) { - wl_surface_attach(ds->surf, ds->buf, 0, 0); - wl_surface_commit(ds->surf); +void drwsurf_flip(struct drwsurf *ds) { + wl_surface_attach(ds->surf, ds->buf, 0, 0); + wl_surface_commit(ds->surf); } -void -drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y, - uint32_t w, uint32_t h, uint32_t b, const char *label) { +void drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y, + uint32_t w, uint32_t h, uint32_t b, const char *label) { - cairo_save(d->cairo); + cairo_save(d->cairo); - cairo_set_source_rgba( - d->cairo, color.bgra[2] / (double)255, color.bgra[1] / (double)255, - color.bgra[0] / (double)255, color.bgra[3] / (double)255); - cairo_move_to(d->cairo, x + w / 2, y + h / 2); + cairo_set_source_rgba( + d->cairo, color.bgra[2] / (double)255, color.bgra[1] / (double)255, + color.bgra[0] / (double)255, color.bgra[3] / (double)255); + cairo_move_to(d->cairo, x + w / 2, y + h / 2); - pango_layout_set_text(d->layout, label, -1); - pango_layout_set_width(d->layout, (w - (b * 2)) * PANGO_SCALE); - pango_layout_set_height(d->layout, (h - (b * 2)) * PANGO_SCALE); + pango_layout_set_text(d->layout, label, -1); + pango_layout_set_width(d->layout, (w - (b * 2)) * PANGO_SCALE); + pango_layout_set_height(d->layout, (h - (b * 2)) * PANGO_SCALE); - int width, height; - pango_layout_get_pixel_size(d->layout, &width, &height); + int width, height; + pango_layout_get_pixel_size(d->layout, &width, &height); - cairo_rel_move_to(d->cairo, -width / 2, -height / 2); + cairo_rel_move_to(d->cairo, -width / 2, -height / 2); - pango_cairo_show_layout(d->cairo, d->layout); - cairo_restore(d->cairo); + pango_cairo_show_layout(d->cairo, d->layout); + cairo_restore(d->cairo); } -void -drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t w, - uint32_t h) { - cairo_save(d->cairo); +void drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t w, + uint32_t h) { + cairo_save(d->cairo); - cairo_set_operator(d->cairo, CAIRO_OPERATOR_CLEAR); - cairo_rectangle(d->cairo, x, y, w, h); - cairo_fill(d->cairo); + cairo_set_operator(d->cairo, CAIRO_OPERATOR_CLEAR); + cairo_rectangle(d->cairo, x, y, w, h); + cairo_fill(d->cairo); - cairo_restore(d->cairo); + cairo_restore(d->cairo); } -void -drw_do_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, - uint32_t w, uint32_t h, bool over) { - cairo_save(d->cairo); +void drw_do_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, + uint32_t w, uint32_t h, bool over) { + cairo_save(d->cairo); - if (over) { - cairo_set_operator(d->cairo, CAIRO_OPERATOR_OVER); - } else { - cairo_set_operator(d->cairo, CAIRO_OPERATOR_SOURCE); - } + if (over) { + cairo_set_operator(d->cairo, CAIRO_OPERATOR_OVER); + } else { + cairo_set_operator(d->cairo, CAIRO_OPERATOR_SOURCE); + } - cairo_rectangle(d->cairo, x, y, w, h); - cairo_set_source_rgba( - d->cairo, color.bgra[2] / (double)255, color.bgra[1] / (double)255, - color.bgra[0] / (double)255, color.bgra[3] / (double)255); - cairo_fill(d->cairo); + cairo_rectangle(d->cairo, x, y, w, h); + cairo_set_source_rgba( + d->cairo, color.bgra[2] / (double)255, color.bgra[1] / (double)255, + color.bgra[0] / (double)255, color.bgra[3] / (double)255); + cairo_fill(d->cairo); - cairo_restore(d->cairo); + cairo_restore(d->cairo); } -void -drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, - uint32_t w, uint32_t h) { - drw_do_rectangle(d, color, x, y, w, h, false); +void drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, + uint32_t w, uint32_t h) { + drw_do_rectangle(d, color, x, y, w, h, false); } -void -drw_over_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, - uint32_t w, uint32_t h) { - drw_do_rectangle(d, color, x, y, w, h, true); +void drw_over_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y, + uint32_t w, uint32_t h) { + drw_do_rectangle(d, color, x, y, w, h, true); } -uint32_t -setup_buffer(struct drwsurf *drwsurf) { - int stride = drwsurf->width * 4; - drwsurf->size = stride * drwsurf->height; +uint32_t setup_buffer(struct drwsurf *drwsurf) { + int stride = drwsurf->width * 4; + drwsurf->size = stride * drwsurf->height; - int fd = allocate_shm_file(drwsurf->size); - if (fd == -1) { - return 1; - } + int fd = allocate_shm_file(drwsurf->size); + if (fd == -1) { + return 1; + } - drwsurf->pool_data = - mmap(NULL, drwsurf->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (drwsurf->pool_data == MAP_FAILED) { - close(fd); - return 1; - } + drwsurf->pool_data = + mmap(NULL, drwsurf->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (drwsurf->pool_data == MAP_FAILED) { + close(fd); + return 1; + } - struct wl_shm_pool *pool = - wl_shm_create_pool(drwsurf->ctx->shm, fd, drwsurf->size); - drwsurf->buf = wl_shm_pool_create_buffer( - pool, 0, drwsurf->width, drwsurf->height, stride, WL_SHM_FORMAT_ARGB8888); - wl_shm_pool_destroy(pool); - close(fd); + struct wl_shm_pool *pool = + wl_shm_create_pool(drwsurf->ctx->shm, fd, drwsurf->size); + drwsurf->buf = + wl_shm_pool_create_buffer(pool, 0, drwsurf->width, drwsurf->height, + stride, WL_SHM_FORMAT_ARGB8888); + wl_shm_pool_destroy(pool); + close(fd); - cairo_surface_t *s = cairo_image_surface_create_for_data( - drwsurf->pool_data, CAIRO_FORMAT_ARGB32, drwsurf->width, drwsurf->height, - stride); + cairo_surface_t *s = cairo_image_surface_create_for_data( + drwsurf->pool_data, CAIRO_FORMAT_ARGB32, drwsurf->width, + drwsurf->height, stride); - drwsurf->cairo = cairo_create(s); - cairo_scale(drwsurf->cairo, drwsurf->scale, drwsurf->scale); - cairo_set_antialias(drwsurf->cairo, CAIRO_ANTIALIAS_NONE); - drwsurf->layout = pango_cairo_create_layout(drwsurf->cairo); - pango_layout_set_font_description(drwsurf->layout, - drwsurf->ctx->font_description); - pango_layout_set_auto_dir(drwsurf->layout, false); - cairo_save(drwsurf->cairo); + drwsurf->cairo = cairo_create(s); + cairo_scale(drwsurf->cairo, drwsurf->scale, drwsurf->scale); + cairo_set_antialias(drwsurf->cairo, CAIRO_ANTIALIAS_NONE); + drwsurf->layout = pango_cairo_create_layout(drwsurf->cairo); + pango_layout_set_font_description(drwsurf->layout, + drwsurf->ctx->font_description); + pango_layout_set_auto_dir(drwsurf->layout, false); + cairo_save(drwsurf->cairo); - return 0; + return 0; } diff --git a/keyboard.c b/keyboard.c index fa12375..0685976 100644 --- a/keyboard.c +++ b/keyboard.c @@ -1,666 +1,659 @@ +#include "keyboard.h" +#include "drw.h" +#include "os-compatibility.h" #include "proto/virtual-keyboard-unstable-v1-client-protocol.h" #include #include #include #include -#include "keyboard.h" -#include "drw.h" -#include "os-compatibility.h" #define MAX_LAYERS 25 /* lazy die macro */ #define die(...) \ - fprintf(stderr, __VA_ARGS__); \ - exit(1) + fprintf(stderr, __VA_ARGS__); \ + exit(1) #ifndef KEYMAP #error "make sure to define KEYMAP" #endif #include KEYMAP -void -kbd_switch_layout(struct kbd *kb, struct layout *l, size_t layer_index) { - kb->prevlayout = kb->layout; - if ((kb->layer_index != kb->last_abc_index) && (kb->layout->abc)) { - kb->last_abc_layout = kb->layout; - kb->last_abc_index = kb->layer_index; - } - kb->layer_index = layer_index; - kb->layout = l; - if (kb->debug) - fprintf(stderr, "Switching to layout %s, layer_index %ld\n", - kb->layout->name, layer_index); - if (!l->keymap_name) - fprintf(stderr, "Layout has no keymap!"); // sanity check - if ((!kb->prevlayout) || - (strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) { - fprintf(stderr, "Switching to keymap %s\n", kb->layout->keymap_name); - create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0); - } - kbd_draw_layout(kb); +void kbd_switch_layout(struct kbd *kb, struct layout *l, size_t layer_index) { + kb->prevlayout = kb->layout; + if ((kb->layer_index != kb->last_abc_index) && (kb->layout->abc)) { + kb->last_abc_layout = kb->layout; + kb->last_abc_index = kb->layer_index; + } + kb->layer_index = layer_index; + kb->layout = l; + if (kb->debug) + fprintf(stderr, "Switching to layout %s, layer_index %ld\n", + kb->layout->name, layer_index); + if (!l->keymap_name) + fprintf(stderr, "Layout has no keymap!"); // sanity check + if ((!kb->prevlayout) || + (strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) { + fprintf(stderr, "Switching to keymap %s\n", kb->layout->keymap_name); + create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0); + } + kbd_draw_layout(kb); } -void -kbd_next_layer(struct kbd *kb, struct key *k, bool invert) { - size_t layer_index = kb->layer_index; - if ((kb->mods & Ctrl) || (kb->mods & Alt) || (kb->mods & AltGr) || - ((bool)kb->compose)) { - // with modifiers ctrl/alt/altgr: switch to the first layer - layer_index = 0; - kb->mods = 0; - } else if ((kb->mods & Shift) || (kb->mods & CapsLock) || (invert)) { - // with modifiers shift/capslock or invert set: switch to the previous - // layout in the layer sequence - if (layer_index > 0) { - layer_index--; - } else { - size_t layercount = 0; - for (size_t i = 0; layercount == 0; i++) { - if (kb->landscape) { - if (kb->landscape_layers[i] == NumLayouts) - layercount = i; - } else { - if (kb->layers[i] == NumLayouts) - layercount = i; - } - } - layer_index = layercount - 1; - } - if (!invert) - kb->mods ^= Shift; - } else { - // normal behaviour: switch to the next layout in the layer sequence - layer_index++; - } - size_t layercount = 0; - for (size_t i = 0; layercount == 0; i++) { - if (kb->landscape) { - if (kb->landscape_layers[i] == NumLayouts) - layercount = i; - } else { - if (kb->layers[i] == NumLayouts) - layercount = i; - } - } - if (layer_index >= layercount) { - if (kb->debug) - fprintf(stderr, "wrapping layer_index back to start\n"); - layer_index = 0; - } - enum layout_id layer; - if (kb->landscape) { - layer = kb->landscape_layers[layer_index]; - } else { - layer = kb->layers[layer_index]; - } - if (((bool)kb->compose) && (k)) { - kb->compose = 0; - kbd_draw_key(kb, k, Unpress); - } - kbd_switch_layout(kb, &kb->layouts[layer], layer_index); +void kbd_next_layer(struct kbd *kb, struct key *k, bool invert) { + size_t layer_index = kb->layer_index; + if ((kb->mods & Ctrl) || (kb->mods & Alt) || (kb->mods & AltGr) || + ((bool)kb->compose)) { + // with modifiers ctrl/alt/altgr: switch to the first layer + layer_index = 0; + kb->mods = 0; + } else if ((kb->mods & Shift) || (kb->mods & CapsLock) || (invert)) { + // with modifiers shift/capslock or invert set: switch to the + // previous layout in the layer sequence + if (layer_index > 0) { + layer_index--; + } else { + size_t layercount = 0; + for (size_t i = 0; layercount == 0; i++) { + if (kb->landscape) { + if (kb->landscape_layers[i] == NumLayouts) + layercount = i; + } else { + if (kb->layers[i] == NumLayouts) + layercount = i; + } + } + layer_index = layercount - 1; + } + if (!invert) + kb->mods ^= Shift; + } else { + // normal behaviour: switch to the next layout in the layer + // sequence + layer_index++; + } + size_t layercount = 0; + for (size_t i = 0; layercount == 0; i++) { + if (kb->landscape) { + if (kb->landscape_layers[i] == NumLayouts) + layercount = i; + } else { + if (kb->layers[i] == NumLayouts) + layercount = i; + } + } + if (layer_index >= layercount) { + if (kb->debug) + fprintf(stderr, "wrapping layer_index back to start\n"); + layer_index = 0; + } + enum layout_id layer; + if (kb->landscape) { + layer = kb->landscape_layers[layer_index]; + } else { + layer = kb->layers[layer_index]; + } + if (((bool)kb->compose) && (k)) { + kb->compose = 0; + kbd_draw_key(kb, k, Unpress); + } + kbd_switch_layout(kb, &kb->layouts[layer], layer_index); } -uint8_t -kbd_get_rows(struct layout *l) { - uint8_t rows = 0; - struct key *k = l->keys; - while (k->type != Last) { - if (k->type == EndRow) { - rows++; - } - k++; - } - return rows + 1; +uint8_t kbd_get_rows(struct layout *l) { + uint8_t rows = 0; + struct key *k = l->keys; + while (k->type != Last) { + if (k->type == EndRow) { + rows++; + } + k++; + } + return rows + 1; } -enum layout_id * -kbd_init_layers(char *layer_names_list) { - enum layout_id *layers; - uint8_t numlayers = 0; - bool found; - char *s; - int i; +enum layout_id *kbd_init_layers(char *layer_names_list) { + enum layout_id *layers; + uint8_t numlayers = 0; + bool found; + char *s; + int i; - layers = malloc(MAX_LAYERS * sizeof(enum layout_id)); - s = strtok(layer_names_list, ","); - while (s != NULL) { - if (numlayers + 1 == MAX_LAYERS) { - fprintf(stderr, "too many layers specified"); - exit(3); - } - found = false; - for (i = 0; i < NumLayouts - 1; i++) { - if (layouts[i].name && strcmp(layouts[i].name, s) == 0) { - fprintf(stderr, "layer #%d = %s\n", numlayers + 1, s); - layers[numlayers++] = i; - found = true; - break; - } - } - if (!found) { - fprintf(stderr, "No such layer: %s\n", s); - exit(3); - } - s = strtok(NULL, ","); - } - layers[numlayers] = NumLayouts; // mark the end of the sequence - if (numlayers == 0) { - fprintf(stderr, "No layers defined\n"); - exit(3); - } + layers = malloc(MAX_LAYERS * sizeof(enum layout_id)); + s = strtok(layer_names_list, ","); + while (s != NULL) { + if (numlayers + 1 == MAX_LAYERS) { + fprintf(stderr, "too many layers specified"); + exit(3); + } + found = false; + for (i = 0; i < NumLayouts - 1; i++) { + if (layouts[i].name && strcmp(layouts[i].name, s) == 0) { + fprintf(stderr, "layer #%d = %s\n", numlayers + 1, s); + layers[numlayers++] = i; + found = true; + break; + } + } + if (!found) { + fprintf(stderr, "No such layer: %s\n", s); + exit(3); + } + s = strtok(NULL, ","); + } + layers[numlayers] = NumLayouts; // mark the end of the sequence + if (numlayers == 0) { + fprintf(stderr, "No layers defined\n"); + exit(3); + } - return layers; + return layers; } -void -kbd_init(struct kbd *kb, struct layout *layouts, char *layer_names_list, - char *landscape_layer_names_list) { - int i; +void kbd_init(struct kbd *kb, struct layout *layouts, char *layer_names_list, + char *landscape_layer_names_list) { + int i; - fprintf(stderr, "Initializing keyboard\n"); + fprintf(stderr, "Initializing keyboard\n"); - kb->layouts = layouts; + kb->layouts = layouts; - for (i = 0; i < NumLayouts - 1; i++) - ; - fprintf(stderr, "Found %d layouts\n", i); + for (i = 0; i < NumLayouts - 1; i++) + ; + fprintf(stderr, "Found %d layouts\n", i); - kb->layer_index = 0; - kb->last_abc_index = 0; + kb->layer_index = 0; + kb->last_abc_index = 0; - if (layer_names_list) - kb->layers = kbd_init_layers(layer_names_list); - if (landscape_layer_names_list) - kb->landscape_layers = kbd_init_layers(landscape_layer_names_list); + if (layer_names_list) + kb->layers = kbd_init_layers(layer_names_list); + if (landscape_layer_names_list) + kb->landscape_layers = kbd_init_layers(landscape_layer_names_list); - i = 0; - enum layout_id lid = kb->layers[0]; - while (lid != NumLayouts) { - lid = kb->layers[++i]; - } - fprintf(stderr, "Found %d layers\n", i); + i = 0; + enum layout_id lid = kb->layers[0]; + while (lid != NumLayouts) { + lid = kb->layers[++i]; + } + fprintf(stderr, "Found %d layers\n", i); - enum layout_id layer; - if (kb->landscape) { - layer = kb->landscape_layers[kb->layer_index]; - } else { - layer = kb->layers[kb->layer_index]; - } + enum layout_id layer; + if (kb->landscape) { + layer = kb->landscape_layers[kb->layer_index]; + } else { + layer = kb->layers[kb->layer_index]; + } - kb->layout = &kb->layouts[layer]; - kb->last_abc_layout = &kb->layouts[layer]; + kb->layout = &kb->layouts[layer]; + kb->last_abc_layout = &kb->layouts[layer]; - /* upload keymap */ - create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0); + /* upload keymap */ + create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0); } -void -kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) { - uint32_t x = 0, y = 0; - uint8_t rows = kbd_get_rows(l); +void kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) { + uint32_t x = 0, y = 0; + uint8_t rows = kbd_get_rows(l); - l->keyheight = height / rows; + l->keyheight = height / rows; - struct key *k = l->keys; - double rowlength = kbd_get_row_length(k); - double rowwidth = 0.0; - while (k->type != Last) { - if (k->type == EndRow) { - y += l->keyheight; - x = 0; - rowwidth = 0.0; - rowlength = kbd_get_row_length(k + 1); - } else if (k->width > 0) { - k->x = x; - k->y = y; - k->w = ((double)width / rowlength) * k->width; - x += k->w; - rowwidth += k->width; - if (x < (rowwidth / rowlength) * (double)width) { - k->w++; - x++; - } - } - k->h = l->keyheight; - k++; - } + struct key *k = l->keys; + double rowlength = kbd_get_row_length(k); + double rowwidth = 0.0; + while (k->type != Last) { + if (k->type == EndRow) { + y += l->keyheight; + x = 0; + rowwidth = 0.0; + rowlength = kbd_get_row_length(k + 1); + } else if (k->width > 0) { + k->x = x; + k->y = y; + k->w = ((double)width / rowlength) * k->width; + x += k->w; + rowwidth += k->width; + if (x < (rowwidth / rowlength) * (double)width) { + k->w++; + x++; + } + } + k->h = l->keyheight; + k++; + } } -double -kbd_get_row_length(struct key *k) { - double l = 0.0; - while ((k->type != Last) && (k->type != EndRow)) { - l += k->width; - k++; - } - return l; +double kbd_get_row_length(struct key *k) { + double l = 0.0; + while ((k->type != Last) && (k->type != EndRow)) { + l += k->width; + k++; + } + return l; } -struct key * -kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) { - struct layout *l = kb->layout; - struct key *k = l->keys; - if (kb->debug) - fprintf(stderr, "get key: +%d+%d\n", x, y); - while (k->type != Last) { - 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)) { - return k; - } - k++; - } - return NULL; +struct key *kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) { + struct layout *l = kb->layout; + struct key *k = l->keys; + if (kb->debug) + fprintf(stderr, "get key: +%d+%d\n", x, y); + while (k->type != Last) { + 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)) { + return k; + } + k++; + } + return NULL; } -size_t -kbd_get_layer_index(struct kbd *kb, struct layout *l) { - for (size_t i = 0; i < NumLayouts - 1; i++) { - if (l == &kb->layouts[i]) { - return i; - } - } - return 0; +size_t kbd_get_layer_index(struct kbd *kb, struct layout *l) { + for (size_t i = 0; i < NumLayouts - 1; i++) { + if (l == &kb->layouts[i]) { + return i; + } + } + return 0; } -void -kbd_unpress_key(struct kbd *kb, uint32_t time) { - bool unlatch_shift = false; +void kbd_unpress_key(struct kbd *kb, uint32_t time) { + bool unlatch_shift = false; - if (kb->last_press) { - unlatch_shift = (kb->mods & Shift) == Shift; + if (kb->last_press) { + unlatch_shift = (kb->mods & Shift) == Shift; - if (unlatch_shift) { - kb->mods ^= Shift; - zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); - } + if (unlatch_shift) { + kb->mods ^= Shift; + zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); + } - if (kb->last_press->type == Copy) { - zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key - WL_KEYBOARD_KEY_STATE_RELEASED); - } else { - if ((kb->last_press->code == KEY_SPACE) && (unlatch_shift)) { - // shift + space is tab - zwp_virtual_keyboard_v1_key(kb->vkbd, time, KEY_TAB, - WL_KEYBOARD_KEY_STATE_RELEASED); - } else { - zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code, - WL_KEYBOARD_KEY_STATE_RELEASED); - } - } + if (kb->last_press->type == Copy) { + zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key + WL_KEYBOARD_KEY_STATE_RELEASED); + } else { + if ((kb->last_press->code == KEY_SPACE) && (unlatch_shift)) { + // shift + space is tab + zwp_virtual_keyboard_v1_key(kb->vkbd, time, KEY_TAB, + WL_KEYBOARD_KEY_STATE_RELEASED); + } else { + zwp_virtual_keyboard_v1_key(kb->vkbd, time, + kb->last_press->code, + WL_KEYBOARD_KEY_STATE_RELEASED); + } + } - if (kb->compose >= 2) { - kb->compose = 0; - kbd_switch_layout(kb, kb->last_abc_layout, kb->last_abc_index); - } else if (unlatch_shift) { - kbd_draw_layout(kb); - } else { - kbd_draw_key(kb, kb->last_press, Unpress); - } + if (kb->compose >= 2) { + kb->compose = 0; + kbd_switch_layout(kb, kb->last_abc_layout, kb->last_abc_index); + } else if (unlatch_shift) { + kbd_draw_layout(kb); + } else { + kbd_draw_key(kb, kb->last_press, Unpress); + } - kb->last_press = NULL; - } + kb->last_press = NULL; + } } -void -kbd_release_key(struct kbd *kb, uint32_t time) { - kbd_unpress_key(kb, time); - if (kb->print_intersect && kb->last_swipe) { - printf("\n"); - // Important so autocompleted words get typed in time - fflush(stdout); - kbd_draw_layout(kb); - kb->last_swipe = NULL; - } +void kbd_release_key(struct kbd *kb, uint32_t time) { + kbd_unpress_key(kb, time); + if (kb->print_intersect && kb->last_swipe) { + printf("\n"); + // Important so autocompleted words get typed in time + fflush(stdout); + kbd_draw_layout(kb); + kb->last_swipe = NULL; + } - drwsurf_flip(kb->surf); + drwsurf_flip(kb->surf); - kbd_clear_last_popup(kb); - drwsurf_flip(kb->popup_surf); + kbd_clear_last_popup(kb); + drwsurf_flip(kb->popup_surf); } -void -kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) { - // Output intersecting keys - // (for external 'swiping'-based accelerators). - if (kb->print_intersect) { - if (kb->last_press) { - kbd_unpress_key(kb, time); - // Redraw last press as a swipe. - kbd_draw_key(kb, kb->last_swipe, Swipe); - } - struct key *intersect_key; - intersect_key = kbd_get_key(kb, x, y); - if (intersect_key && - (!kb->last_swipe || intersect_key->label != kb->last_swipe->label)) { - kbd_print_key_stdout(kb, intersect_key); - kb->last_swipe = intersect_key; - kbd_draw_key(kb, kb->last_swipe, Swipe); - } - } else { - kbd_unpress_key(kb, time); - } +void kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) { + // Output intersecting keys + // (for external 'swiping'-based accelerators). + if (kb->print_intersect) { + if (kb->last_press) { + kbd_unpress_key(kb, time); + // Redraw last press as a swipe. + kbd_draw_key(kb, kb->last_swipe, Swipe); + } + struct key *intersect_key; + intersect_key = kbd_get_key(kb, x, y); + if (intersect_key && (!kb->last_swipe || + intersect_key->label != kb->last_swipe->label)) { + kbd_print_key_stdout(kb, intersect_key); + kb->last_swipe = intersect_key; + kbd_draw_key(kb, kb->last_swipe, Swipe); + } + } else { + kbd_unpress_key(kb, time); + } - drwsurf_flip(kb->surf); + drwsurf_flip(kb->surf); - kbd_clear_last_popup(kb); - drwsurf_flip(kb->popup_surf); + kbd_clear_last_popup(kb); + drwsurf_flip(kb->popup_surf); } -void -kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) { - if ((kb->compose == 1) && (k->type != Compose) && (k->type != Mod)) { - if ((k->type == NextLayer) || (k->type == BackLayer) || - ((k->type == Code) && (k->code == KEY_SPACE))) { - kb->compose = 0; - if (kb->debug) - fprintf(stderr, "showing layout index\n"); - kbd_switch_layout(kb, &kb->layouts[Index], 0); - return; - } else if (k->layout) { - kb->compose++; - if (kb->debug) - fprintf(stderr, "showing compose %d\n", kb->compose); - kbd_switch_layout(kb, k->layout, kbd_get_layer_index(kb, k->layout)); - return; - } else { - return; - } - } +void kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) { + if ((kb->compose == 1) && (k->type != Compose) && (k->type != Mod)) { + if ((k->type == NextLayer) || (k->type == BackLayer) || + ((k->type == Code) && (k->code == KEY_SPACE))) { + kb->compose = 0; + if (kb->debug) + fprintf(stderr, "showing layout index\n"); + kbd_switch_layout(kb, &kb->layouts[Index], 0); + return; + } else if (k->layout) { + kb->compose++; + if (kb->debug) + fprintf(stderr, "showing compose %d\n", kb->compose); + kbd_switch_layout(kb, k->layout, + kbd_get_layer_index(kb, k->layout)); + return; + } else { + return; + } + } - switch (k->type) { - case Code: - if (k->code_mod) { - if (k->reset_mod) { - zwp_virtual_keyboard_v1_modifiers(kb->vkbd, k->code_mod, 0, 0, 0); - } else { - zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods ^ k->code_mod, 0, - 0, 0); - } - } else { - zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); - } - kb->last_swipe = kb->last_press = k; - kbd_draw_key(kb, k, Press); - if ((k->code == KEY_SPACE) && (kb->mods & Shift)) { - // shift space is tab - zwp_virtual_keyboard_v1_modifiers(kb->vkbd, 0, 0, 0, 0); - zwp_virtual_keyboard_v1_key(kb->vkbd, time, KEY_TAB, - WL_KEYBOARD_KEY_STATE_PRESSED); - } else { - zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code, - WL_KEYBOARD_KEY_STATE_PRESSED); - } - if (kb->print || kb->print_intersect) - kbd_print_key_stdout(kb, k); - if (kb->compose) { - if (kb->debug) - fprintf(stderr, "pressing composed key\n"); - kb->compose++; - } - break; - case Mod: - kb->mods ^= k->code; - if (k->code == Shift) { - kbd_draw_layout(kb); - } else { - if (kb->mods & k->code) { - kbd_draw_key(kb, k, Press); - } else { - kbd_draw_key(kb, k, Unpress); - } - } - zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); - break; - case Layout: - // switch to the layout determined by the key - kbd_switch_layout(kb, k->layout, kbd_get_layer_index(kb, k->layout)); - // reset previous layout to default/first so we don't get any weird cycles - kb->last_abc_index = 0; - if (kb->landscape) { - kb->last_abc_layout = &kb->layouts[kb->landscape_layers[0]]; - } else { - kb->last_abc_layout = &kb->layouts[kb->layers[0]]; - } - break; - case Compose: - // switch to the associated layout determined by the *next* keypress - if (kb->compose == 0) { - kb->compose = 1; - } else { - kb->compose = 0; - } - if ((bool)kb->compose) { - kbd_draw_key(kb, k, Press); - } else { - kbd_draw_key(kb, k, Unpress); - } - break; - case NextLayer: //(also handles previous layer when shift modifier is on, or - //"first layer" with other modifiers) - kbd_next_layer(kb, k, false); - break; - case BackLayer: // triggered when "Abc" keys are pressed - // switch to the last active alphabetical layout - if (kb->last_abc_layout) { - kb->compose = 0; - kbd_switch_layout(kb, kb->last_abc_layout, kb->last_abc_index); - // reset previous layout to default/first so we don't get any weird cycles - kb->last_abc_index = 0; - if (kb->landscape) { - kb->last_abc_layout = &kb->layouts[kb->landscape_layers[0]]; - } else { - kb->last_abc_layout = &kb->layouts[kb->layers[0]]; - } - } - break; - case Copy: - // copy code as unicode chr by setting a temporary keymap - kb->last_swipe = kb->last_press = k; - kbd_draw_key(kb, k, Press); - if (kb->debug) - fprintf(stderr, "pressing copy key\n"); - 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_key(kb->vkbd, time, 127, // COMP key - WL_KEYBOARD_KEY_STATE_PRESSED); - if (kb->print || kb->print_intersect) - kbd_print_key_stdout(kb, k); - break; - default: - break; - } + switch (k->type) { + case Code: + if (k->code_mod) { + if (k->reset_mod) { + zwp_virtual_keyboard_v1_modifiers(kb->vkbd, k->code_mod, 0, 0, + 0); + } else { + zwp_virtual_keyboard_v1_modifiers( + kb->vkbd, kb->mods ^ k->code_mod, 0, 0, 0); + } + } else { + zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); + } + kb->last_swipe = kb->last_press = k; + kbd_draw_key(kb, k, Press); + if ((k->code == KEY_SPACE) && (kb->mods & Shift)) { + // shift space is tab + zwp_virtual_keyboard_v1_modifiers(kb->vkbd, 0, 0, 0, 0); + zwp_virtual_keyboard_v1_key(kb->vkbd, time, KEY_TAB, + WL_KEYBOARD_KEY_STATE_PRESSED); + } else { + zwp_virtual_keyboard_v1_key(kb->vkbd, time, kb->last_press->code, + WL_KEYBOARD_KEY_STATE_PRESSED); + } + if (kb->print || kb->print_intersect) + kbd_print_key_stdout(kb, k); + if (kb->compose) { + if (kb->debug) + fprintf(stderr, "pressing composed key\n"); + kb->compose++; + } + break; + case Mod: + kb->mods ^= k->code; + if (k->code == Shift) { + kbd_draw_layout(kb); + } else { + if (kb->mods & k->code) { + kbd_draw_key(kb, k, Press); + } else { + kbd_draw_key(kb, k, Unpress); + } + } + zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0); + break; + case Layout: + // switch to the layout determined by the key + kbd_switch_layout(kb, k->layout, kbd_get_layer_index(kb, k->layout)); + // reset previous layout to default/first so we don't get any weird + // cycles + kb->last_abc_index = 0; + if (kb->landscape) { + kb->last_abc_layout = &kb->layouts[kb->landscape_layers[0]]; + } else { + kb->last_abc_layout = &kb->layouts[kb->layers[0]]; + } + break; + case Compose: + // switch to the associated layout determined by the *next* + // keypress + if (kb->compose == 0) { + kb->compose = 1; + } else { + kb->compose = 0; + } + if ((bool)kb->compose) { + kbd_draw_key(kb, k, Press); + } else { + kbd_draw_key(kb, k, Unpress); + } + break; + case NextLayer: //(also handles previous layer when shift modifier is + // on, or "first layer" with other modifiers) + kbd_next_layer(kb, k, false); + break; + case BackLayer: // triggered when "Abc" keys are pressed + // switch to the last active alphabetical layout + if (kb->last_abc_layout) { + kb->compose = 0; + kbd_switch_layout(kb, kb->last_abc_layout, kb->last_abc_index); + // reset previous layout to default/first so we don't get + // any weird cycles + kb->last_abc_index = 0; + if (kb->landscape) { + kb->last_abc_layout = &kb->layouts[kb->landscape_layers[0]]; + } else { + kb->last_abc_layout = &kb->layouts[kb->layers[0]]; + } + } + break; + case Copy: + // copy code as unicode chr by setting a temporary keymap + kb->last_swipe = kb->last_press = k; + kbd_draw_key(kb, k, Press); + if (kb->debug) + fprintf(stderr, "pressing copy key\n"); + 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_key(kb->vkbd, time, 127, // COMP key + WL_KEYBOARD_KEY_STATE_PRESSED); + if (kb->print || kb->print_intersect) + kbd_print_key_stdout(kb, k); + break; + default: + break; + } - drwsurf_flip(kb->surf); - drwsurf_flip(kb->popup_surf); + drwsurf_flip(kb->surf); + drwsurf_flip(kb->popup_surf); } -void -kbd_print_key_stdout(struct kbd *kb, struct key *k) { - /* printed keys may slightly differ from the actual output - * we generally print what is on the key LABEL and only support the normal - * and shift layers. Other modifiers produce no output (Ctrl,Alt) - * */ +void kbd_print_key_stdout(struct kbd *kb, struct key *k) { + /* printed keys may slightly differ from the actual output + * we generally print what is on the key LABEL and only support the normal + * and shift layers. Other modifiers produce no output (Ctrl,Alt) + * */ - bool handled = true; - if (k->type == Code) { - switch (k->code) { - case KEY_SPACE: - printf(" "); - break; - case KEY_ENTER: - printf("\n"); - break; - case KEY_BACKSPACE: - printf("\b"); - break; - case KEY_TAB: - printf("\t"); - break; - default: - handled = false; - break; - } - } else if (k->type != Copy) { - return; - } + bool handled = true; + if (k->type == Code) { + switch (k->code) { + case KEY_SPACE: + printf(" "); + break; + case KEY_ENTER: + printf("\n"); + break; + case KEY_BACKSPACE: + printf("\b"); + break; + case KEY_TAB: + printf("\t"); + break; + default: + handled = false; + break; + } + } else if (k->type != Copy) { + return; + } - if (!handled) { - if ((kb->mods & Shift) || (kb->mods & CapsLock)) - printf("%s", k->shift_label); - else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super)) - printf("%s", k->label); - } - fflush(stdout); + if (!handled) { + if ((kb->mods & Shift) || (kb->mods & CapsLock)) + printf("%s", k->shift_label); + else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super)) + printf("%s", k->label); + } + fflush(stdout); } -void -kbd_clear_last_popup(struct kbd *kb) { - if (kb->last_popup_w && kb->last_popup_h) { - drw_do_clear(kb->popup_surf, kb->last_popup_x, kb->last_popup_y, - 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); +void kbd_clear_last_popup(struct kbd *kb) { + if (kb->last_popup_w && kb->last_popup_h) { + drw_do_clear(kb->popup_surf, kb->last_popup_x, kb->last_popup_y, + 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; + } } -void -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; - if (kb->debug) - 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); +void 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; + if (kb->debug) + 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); - switch (type) { - case None: - case Unpress: - draw_inset(kb->surf, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, scheme->fg); - break; - case Press: - draw_inset(kb->surf, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, scheme->high); - break; - case Swipe: - draw_over_inset(kb->surf, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, - scheme->swipe); - break; - } + switch (type) { + case None: + case Unpress: + draw_inset(kb->surf, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, + scheme->fg); + break; + case Press: + draw_inset(kb->surf, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, + scheme->high); + break; + case Swipe: + draw_over_inset(kb->surf, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, + scheme->swipe); + break; + } - drw_draw_text(kb->surf, scheme->text, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, - label); - wl_surface_damage(kb->surf->surf, 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); + wl_surface_damage(kb->surf->surf, k->x, k->y, k->w, k->h); - if (type == Press || type == Unpress) { - kbd_clear_last_popup(kb); + if (type == Press || type == Unpress) { + kbd_clear_last_popup(kb); - kb->last_popup_x = k->x; - kb->last_popup_y = kb->h + k->y - k->h; - kb->last_popup_w = k->w; - kb->last_popup_h = k->h; + kb->last_popup_x = k->x; + kb->last_popup_y = kb->h + k->y - k->h; + kb->last_popup_w = k->w; + kb->last_popup_h = k->h; - drw_fill_rectangle(kb->popup_surf, kb->scheme.bg, k->x, kb->last_popup_y, - k->w, k->h); - draw_inset(kb->popup_surf, k->x, kb->last_popup_y, k->w, k->h, - KBD_KEY_BORDER, scheme->high); - drw_draw_text(kb->popup_surf, scheme->text, k->x, kb->last_popup_y, k->w, - k->h, KBD_KEY_BORDER, label); - wl_surface_damage(kb->popup_surf->surf, k->x, kb->last_popup_y, k->w, k->h); - } + drw_fill_rectangle(kb->popup_surf, kb->scheme.bg, k->x, + kb->last_popup_y, k->w, k->h); + draw_inset(kb->popup_surf, k->x, kb->last_popup_y, k->w, k->h, + KBD_KEY_BORDER, scheme->high); + drw_draw_text(kb->popup_surf, scheme->text, k->x, kb->last_popup_y, + k->w, k->h, KBD_KEY_BORDER, label); + wl_surface_damage(kb->popup_surf->surf, k->x, kb->last_popup_y, k->w, + k->h); + } } -void -kbd_draw_layout(struct kbd *kb) { - struct drwsurf *d = kb->surf; - struct key *next_key = kb->layout->keys; - if (kb->debug) - fprintf(stderr, "Draw layout\n"); +void kbd_draw_layout(struct kbd *kb) { + struct drwsurf *d = kb->surf; + struct key *next_key = kb->layout->keys; + if (kb->debug) + fprintf(stderr, "Draw layout\n"); - 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); - while (next_key->type != Last) { - if ((next_key->type == Pad) || (next_key->type == EndRow)) { - next_key++; - continue; - } - if ((next_key->type == Mod && kb->mods & next_key->code) || - (next_key->type == Compose && kb->compose)) { - kbd_draw_key(kb, next_key, Press); - } else { - kbd_draw_key(kb, next_key, None); - } - next_key++; - } - wl_surface_damage(d->surf, 0, 0, kb->w, kb->h); + while (next_key->type != Last) { + if ((next_key->type == Pad) || (next_key->type == EndRow)) { + next_key++; + continue; + } + if ((next_key->type == Mod && kb->mods & next_key->code) || + (next_key->type == Compose && kb->compose)) { + kbd_draw_key(kb, next_key, Press); + } else { + kbd_draw_key(kb, next_key, None); + } + next_key++; + } + wl_surface_damage(d->surf, 0, 0, kb->w, kb->h); } -void -kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t layoutcount) { - fprintf(stderr, "Resize %dx%d %f, %d layouts\n", kb->w, kb->h, kb->scale, - layoutcount); +void kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t layoutcount) { + fprintf(stderr, "Resize %dx%d %f, %d layouts\n", kb->w, kb->h, kb->scale, + layoutcount); - drwsurf_resize(kb->surf, kb->w, kb->h, kb->scale); - drwsurf_resize(kb->popup_surf, kb->w, kb->h * 2, kb->scale); - for (int i = 0; i < layoutcount; i++) { - if (kb->debug) { - if (layouts[i].name) - fprintf(stderr, "Initialising layout %s, keymap %s\n", layouts[i].name, - layouts[i].keymap_name); - else - fprintf(stderr, "Initialising unnamed layout %d, keymap %s\n", i, - layouts[i].keymap_name); - } - kbd_init_layout(&layouts[i], kb->w, kb->h); - } - kbd_draw_layout(kb); + drwsurf_resize(kb->surf, kb->w, kb->h, kb->scale); + drwsurf_resize(kb->popup_surf, kb->w, kb->h * 2, kb->scale); + for (int i = 0; i < layoutcount; i++) { + if (kb->debug) { + if (layouts[i].name) + fprintf(stderr, "Initialising layout %s, keymap %s\n", + layouts[i].name, layouts[i].keymap_name); + else + fprintf(stderr, "Initialising unnamed layout %d, keymap %s\n", + i, layouts[i].keymap_name); + } + kbd_init_layout(&layouts[i], kb->w, kb->h); + } + kbd_draw_layout(kb); } -void -draw_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width, - uint32_t height, uint32_t border, Color color) { - drw_fill_rectangle(ds, color, x + border, y + border, width - (border * 2), - height - (border * 2)); -} -void -draw_over_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width, +void draw_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t border, Color color) { - drw_over_rectangle(ds, color, x + border, y + border, width - (border * 2), - height - (border * 2)); + drw_fill_rectangle(ds, color, x + border, y + border, width - (border * 2), + height - (border * 2)); +} +void draw_over_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width, + uint32_t height, uint32_t border, Color color) { + drw_over_rectangle(ds, color, x + border, y + border, width - (border * 2), + height - (border * 2)); } -void -create_and_upload_keymap(struct kbd *kb, const char *name, uint32_t comp_unichr, - uint32_t comp_shift_unichr) { - int keymap_index = -1; - for (int i = 0; i < NUMKEYMAPS; i++) { - if (!strcmp(keymap_names[i], name)) { - keymap_index = i; - } - } - if (keymap_index == -1) { - fprintf(stderr, "No such keymap defined: %s\n", name); - exit(9); - } - const char *keymap_template = keymaps[keymap_index]; - size_t keymap_size = strlen(keymap_template) + 64; - char *keymap_str = malloc(keymap_size); - sprintf(keymap_str, keymap_template, comp_unichr, comp_shift_unichr); - keymap_size = strlen(keymap_str); - int keymap_fd = os_create_anonymous_file(keymap_size); - if (keymap_fd < 0) { - die("could not create keymap fd\n"); - } - void *ptr = - mmap(NULL, keymap_size, PROT_READ | PROT_WRITE, MAP_SHARED, keymap_fd, 0); - if (ptr == (void *)-1) { - die("could not map keymap data\n"); - } - if (kb->vkbd == NULL) { - die("kb.vkbd = NULL\n"); - } - strcpy(ptr, keymap_str); - zwp_virtual_keyboard_v1_keymap(kb->vkbd, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, - keymap_fd, keymap_size); - free((void *)keymap_str); +void create_and_upload_keymap(struct kbd *kb, const char *name, + uint32_t comp_unichr, + uint32_t comp_shift_unichr) { + int keymap_index = -1; + for (int i = 0; i < NUMKEYMAPS; i++) { + if (!strcmp(keymap_names[i], name)) { + keymap_index = i; + } + } + if (keymap_index == -1) { + fprintf(stderr, "No such keymap defined: %s\n", name); + exit(9); + } + const char *keymap_template = keymaps[keymap_index]; + size_t keymap_size = strlen(keymap_template) + 64; + char *keymap_str = malloc(keymap_size); + sprintf(keymap_str, keymap_template, comp_unichr, comp_shift_unichr); + keymap_size = strlen(keymap_str); + int keymap_fd = os_create_anonymous_file(keymap_size); + if (keymap_fd < 0) { + die("could not create keymap fd\n"); + } + void *ptr = mmap(NULL, keymap_size, PROT_READ | PROT_WRITE, MAP_SHARED, + keymap_fd, 0); + if (ptr == (void *)-1) { + die("could not map keymap data\n"); + } + if (kb->vkbd == NULL) { + die("kb.vkbd = NULL\n"); + } + strcpy(ptr, keymap_str); + zwp_virtual_keyboard_v1_keymap(kb->vkbd, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, + keymap_fd, keymap_size); + free((void *)keymap_str); } diff --git a/main.c b/main.c index a4afd9f..b2a8492 100644 --- a/main.c +++ b/main.c @@ -1,27 +1,27 @@ +#include "proto/fractional-scale-v1-client-protocol.h" +#include "proto/viewporter-client-protocol.h" #include "proto/virtual-keyboard-unstable-v1-client-protocol.h" #include "proto/wlr-layer-shell-unstable-v1-client-protocol.h" #include "proto/xdg-shell-client-protocol.h" -#include "proto/fractional-scale-v1-client-protocol.h" -#include "proto/viewporter-client-protocol.h" #include +#include #include #include #include #include #include -#include #include #include #include #include -#include "keyboard.h" #include "config.h" +#include "keyboard.h" /* lazy die macro */ #define die(...) \ - fprintf(stderr, __VA_ARGS__); \ - exit(1) + fprintf(stderr, __VA_ARGS__); \ + exit(1) /* client state */ static const char *namespace = "wlroots"; @@ -44,10 +44,10 @@ static struct wp_viewport *draw_surf_viewport, *popup_draw_surf_viewport; static struct wp_viewporter *viewporter; struct Output { - uint32_t name; - uint32_t w, h; - double scale; - struct wl_output *data; + uint32_t name; + uint32_t w, h; + double scale; + struct wl_output *data; }; static struct Output *current_output; @@ -127,836 +127,815 @@ static void resize(); /* event handlers */ static const struct wl_pointer_listener pointer_listener = { - .enter = wl_pointer_enter, - .leave = wl_pointer_leave, - .motion = wl_pointer_motion, - .button = wl_pointer_button, - .axis = wl_pointer_axis, + .enter = wl_pointer_enter, + .leave = wl_pointer_leave, + .motion = wl_pointer_motion, + .button = wl_pointer_button, + .axis = wl_pointer_axis, }; static const struct wl_touch_listener touch_listener = { - .down = wl_touch_down, - .up = wl_touch_up, - .motion = wl_touch_motion, - .frame = wl_touch_frame, - .cancel = wl_touch_cancel, - .shape = wl_touch_shape, - .orientation = wl_touch_orientation, + .down = wl_touch_down, + .up = wl_touch_up, + .motion = wl_touch_motion, + .frame = wl_touch_frame, + .cancel = wl_touch_cancel, + .shape = wl_touch_shape, + .orientation = wl_touch_orientation, }; static const struct wl_seat_listener seat_listener = { - .capabilities = seat_handle_capabilities, - .name = seat_handle_name, + .capabilities = seat_handle_capabilities, + .name = seat_handle_name, }; static const struct wl_surface_listener surface_listener = { - .enter = wl_surface_enter, + .enter = wl_surface_enter, }; static const struct wl_registry_listener registry_listener = { - .global = handle_global, - .global_remove = handle_global_remove, + .global = handle_global, + .global_remove = handle_global_remove, }; static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { - .configure = layer_surface_configure, - .closed = layer_surface_closed, + .configure = layer_surface_configure, + .closed = layer_surface_closed, }; /* configuration, allows nested code to access above variables */ -char * -estrdup(const char *s) { - char *p; +char *estrdup(const char *s) { + char *p; - if (!(p = strdup(s))) { - fprintf(stderr, "strdup:"); - exit(6); - } + if (!(p = strdup(s))) { + fprintf(stderr, "strdup:"); + exit(6); + } - return p; + return p; } -void -wl_touch_down(void *data, struct wl_touch *wl_touch, uint32_t serial, - uint32_t time, struct wl_surface *surface, int32_t id, - wl_fixed_t x, wl_fixed_t y) { - struct key *next_key; - uint32_t touch_x, touch_y; +void wl_touch_down(void *data, struct wl_touch *wl_touch, uint32_t serial, + uint32_t time, struct wl_surface *surface, int32_t id, + wl_fixed_t x, wl_fixed_t y) { + struct key *next_key; + uint32_t touch_x, touch_y; - touch_x = wl_fixed_to_int(x); - touch_y = wl_fixed_to_int(y); + touch_x = wl_fixed_to_int(x); + touch_y = wl_fixed_to_int(y); - kbd_unpress_key(&keyboard, time); + kbd_unpress_key(&keyboard, time); - next_key = kbd_get_key(&keyboard, touch_x, touch_y); - if (next_key) { - kbd_press_key(&keyboard, next_key, time); - } else if (keyboard.compose) { - keyboard.compose = 0; - kbd_switch_layout(&keyboard, keyboard.prevlayout, keyboard.last_abc_index); - } + next_key = kbd_get_key(&keyboard, touch_x, touch_y); + if (next_key) { + kbd_press_key(&keyboard, next_key, time); + } else if (keyboard.compose) { + keyboard.compose = 0; + kbd_switch_layout(&keyboard, keyboard.prevlayout, + keyboard.last_abc_index); + } } -void -wl_touch_up(void *data, struct wl_touch *wl_touch, uint32_t serial, - uint32_t time, int32_t id) { - kbd_release_key(&keyboard, time); +void wl_touch_up(void *data, struct wl_touch *wl_touch, uint32_t serial, + uint32_t time, int32_t id) { + kbd_release_key(&keyboard, time); } -void -wl_touch_motion(void *data, struct wl_touch *wl_touch, uint32_t time, - int32_t id, wl_fixed_t x, wl_fixed_t y) { - uint32_t touch_x, touch_y; +void wl_touch_motion(void *data, struct wl_touch *wl_touch, uint32_t time, + int32_t id, wl_fixed_t x, wl_fixed_t y) { + uint32_t touch_x, touch_y; - touch_x = wl_fixed_to_int(x); - touch_y = wl_fixed_to_int(y); + touch_x = wl_fixed_to_int(x); + touch_y = wl_fixed_to_int(y); - kbd_motion_key(&keyboard, time, touch_x, touch_y); + kbd_motion_key(&keyboard, time, touch_x, touch_y); } -void -wl_touch_frame(void *data, struct wl_touch *wl_touch) {} +void wl_touch_frame(void *data, struct wl_touch *wl_touch) {} -void -wl_touch_cancel(void *data, struct wl_touch *wl_touch) {} +void wl_touch_cancel(void *data, struct wl_touch *wl_touch) {} -void -wl_touch_shape(void *data, struct wl_touch *wl_touch, int32_t id, - wl_fixed_t major, wl_fixed_t minor) {} +void wl_touch_shape(void *data, struct wl_touch *wl_touch, int32_t id, + wl_fixed_t major, wl_fixed_t minor) {} -void -wl_touch_orientation(void *data, struct wl_touch *wl_touch, int32_t id, - wl_fixed_t orientation) {} +void wl_touch_orientation(void *data, struct wl_touch *wl_touch, int32_t id, + wl_fixed_t orientation) {} -void -wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, - struct wl_surface *surface, wl_fixed_t surface_x, - wl_fixed_t surface_y) {} +void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface, + wl_fixed_t surface_x, wl_fixed_t surface_y) {} -void -wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, - struct wl_surface *surface) { - cur_x = cur_y = -1; +void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface) { + cur_x = cur_y = -1; } -void -wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, - wl_fixed_t surface_x, wl_fixed_t surface_y) { - cur_x = wl_fixed_to_int(surface_x); - cur_y = wl_fixed_to_int(surface_y); +void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, + wl_fixed_t surface_x, wl_fixed_t surface_y) { + cur_x = wl_fixed_to_int(surface_x); + cur_y = wl_fixed_to_int(surface_y); - if (cur_press) { - kbd_motion_key(&keyboard, time, cur_x, cur_y); - } + if (cur_press) { + kbd_motion_key(&keyboard, time, cur_x, cur_y); + } } -void -wl_pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, - uint32_t time, uint32_t button, uint32_t state) { - struct key *next_key; - cur_press = state == WL_POINTER_BUTTON_STATE_PRESSED; +void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, uint32_t time, uint32_t button, + uint32_t state) { + struct key *next_key; + cur_press = state == WL_POINTER_BUTTON_STATE_PRESSED; - if (cur_press) { - kbd_unpress_key(&keyboard, time); - } else { - kbd_release_key(&keyboard, time); - } + if (cur_press) { + kbd_unpress_key(&keyboard, time); + } else { + kbd_release_key(&keyboard, time); + } - if (cur_press && cur_x >= 0 && cur_y >= 0) { - next_key = kbd_get_key(&keyboard, cur_x, cur_y); - if (next_key) { - kbd_press_key(&keyboard, next_key, time); - } else if (keyboard.compose) { - keyboard.compose = 0; - kbd_switch_layout(&keyboard, keyboard.prevlayout, - keyboard.last_abc_index); - } - } + if (cur_press && cur_x >= 0 && cur_y >= 0) { + next_key = kbd_get_key(&keyboard, cur_x, cur_y); + if (next_key) { + kbd_press_key(&keyboard, next_key, time); + } else if (keyboard.compose) { + keyboard.compose = 0; + kbd_switch_layout(&keyboard, keyboard.prevlayout, + keyboard.last_abc_index); + } + } } -void -wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, - uint32_t axis, wl_fixed_t value) { - kbd_next_layer(&keyboard, NULL, (value >= 0)); - drwsurf_flip(keyboard.surf); +void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, + uint32_t axis, wl_fixed_t value) { + kbd_next_layer(&keyboard, NULL, (value >= 0)); + drwsurf_flip(keyboard.surf); } -void -seat_handle_capabilities(void *data, struct wl_seat *wl_seat, - enum wl_seat_capability caps) { - if ((caps & WL_SEAT_CAPABILITY_POINTER)) { - if (pointer == NULL) { - pointer = wl_seat_get_pointer(wl_seat); - wl_pointer_add_listener(pointer, &pointer_listener, NULL); - } - } else { - if (pointer != NULL) { - wl_pointer_destroy(pointer); - pointer = NULL; - } - } - if ((caps & WL_SEAT_CAPABILITY_TOUCH)) { - if (touch == NULL) { - touch = wl_seat_get_touch(wl_seat); - wl_touch_add_listener(touch, &touch_listener, NULL); - } - } else { - if (touch != NULL) { - wl_touch_destroy(touch); - touch = NULL; - } - } +void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, + enum wl_seat_capability caps) { + if ((caps & WL_SEAT_CAPABILITY_POINTER)) { + if (pointer == NULL) { + pointer = wl_seat_get_pointer(wl_seat); + wl_pointer_add_listener(pointer, &pointer_listener, NULL); + } + } else { + if (pointer != NULL) { + wl_pointer_destroy(pointer); + pointer = NULL; + } + } + if ((caps & WL_SEAT_CAPABILITY_TOUCH)) { + if (touch == NULL) { + touch = wl_seat_get_touch(wl_seat); + wl_touch_add_listener(touch, &touch_listener, NULL); + } + } else { + if (touch != NULL) { + wl_touch_destroy(touch); + touch = NULL; + } + } } -void -seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) {} +void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) {} -void -wl_surface_enter(void *data, struct wl_surface *wl_surface, - struct wl_output *wl_output) { - for (int i = 0; i < WL_OUTPUTS_LIMIT; i += 1) { - if (wl_outputs[i].data == wl_output) { - current_output = &wl_outputs[i]; - break; - } - } +void wl_surface_enter(void *data, struct wl_surface *wl_surface, + struct wl_output *wl_output) { + for (int i = 0; i < WL_OUTPUTS_LIMIT; i += 1) { + if (wl_outputs[i].data == wl_output) { + current_output = &wl_outputs[i]; + break; + } + } - resize(); + resize(); } -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) { - struct Output *output = data; +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) { + struct Output *output = data; - // Swap width and height on rotated displays - if (transform % 2 != 0) { - int tmp = physical_width; - physical_width = physical_height; - physical_height = tmp; - } + // Swap width and height on rotated displays + if (transform % 2 != 0) { + int tmp = physical_width; + physical_width = physical_height; + physical_height = tmp; + } - output->w = physical_width; - output->h = physical_height; + output->w = physical_width; + output->h = physical_height; - if (current_output == output) { - resize(); - }; + if (current_output == output) { + resize(); + }; } -static void -display_handle_done(void *data, struct wl_output *wl_output) {} +static void display_handle_done(void *data, struct wl_output *wl_output) {} -static void -display_handle_scale(void *data, struct wl_output *wl_output, int32_t scale) { - struct Output *output = data; - output->scale = scale; +static void display_handle_scale(void *data, struct wl_output *wl_output, + int32_t scale) { + struct Output *output = data; + output->scale = scale; - if (current_output == output) { - resize(); - }; + if (current_output == output) { + resize(); + }; } -static void -display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, - int width, int height, int refresh) {} +static void 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 = { - .geometry = display_handle_geometry, - .mode = display_handle_mode, - .done = display_handle_done, - .scale = display_handle_scale}; + .geometry = display_handle_geometry, + .mode = display_handle_mode, + .done = display_handle_done, + .scale = display_handle_scale}; -static void -xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) { - xdg_wm_base_pong(xdg_wm_base, serial); +static void xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, + uint32_t serial) { + xdg_wm_base_pong(xdg_wm_base, serial); } static const struct xdg_wm_base_listener xdg_wm_base_listener = { - .ping = xdg_wm_base_ping, + .ping = xdg_wm_base_ping, }; -void -handle_global(void *data, struct wl_registry *registry, uint32_t name, - const char *interface, uint32_t version) { - if (strcmp(interface, wl_compositor_interface.name) == 0) { - compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 3); - } else if (strcmp(interface, wl_shm_interface.name) == 0) { - draw_ctx.shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); - } else if (strcmp(interface, "wl_output") == 0) { - if (wl_outputs_size < WL_OUTPUTS_LIMIT) { - struct Output *output = &wl_outputs[wl_outputs_size]; - output->data = wl_registry_bind(registry, name, &wl_output_interface, 2); - output->name = name; - output->scale = 1; - wl_output_add_listener(output->data, &output_listener, output); - wl_outputs_size += 1; - } - } else if (strcmp(interface, wl_seat_interface.name) == 0) { - seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); - wl_seat_add_listener(seat, &seat_listener, NULL); - } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { - layer_shell = - wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1); - } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { - wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); - xdg_wm_base_add_listener(wm_base, &xdg_wm_base_listener, NULL); - } else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == - 0) { - wfs_mgr = wl_registry_bind(registry, name, - &wp_fractional_scale_manager_v1_interface, 1); - } else if (strcmp(interface, wp_viewporter_interface.name) == 0) { - viewporter = wl_registry_bind(registry, name, &wp_viewporter_interface, 1); - } else if (strcmp(interface, - zwp_virtual_keyboard_manager_v1_interface.name) == 0) { - vkbd_mgr = wl_registry_bind(registry, name, - &zwp_virtual_keyboard_manager_v1_interface, 1); - } +void handle_global(void *data, struct wl_registry *registry, uint32_t name, + const char *interface, uint32_t version) { + if (strcmp(interface, wl_compositor_interface.name) == 0) { + compositor = + wl_registry_bind(registry, name, &wl_compositor_interface, 3); + } else if (strcmp(interface, wl_shm_interface.name) == 0) { + draw_ctx.shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, "wl_output") == 0) { + if (wl_outputs_size < WL_OUTPUTS_LIMIT) { + struct Output *output = &wl_outputs[wl_outputs_size]; + output->data = + wl_registry_bind(registry, name, &wl_output_interface, 2); + output->name = name; + output->scale = 1; + wl_output_add_listener(output->data, &output_listener, output); + wl_outputs_size += 1; + } + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); + wl_seat_add_listener(seat, &seat_listener, NULL); + } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { + layer_shell = + wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); + xdg_wm_base_add_listener(wm_base, &xdg_wm_base_listener, NULL); + } else if (strcmp(interface, + wp_fractional_scale_manager_v1_interface.name) == 0) { + wfs_mgr = wl_registry_bind( + registry, name, &wp_fractional_scale_manager_v1_interface, 1); + } else if (strcmp(interface, wp_viewporter_interface.name) == 0) { + viewporter = + wl_registry_bind(registry, name, &wp_viewporter_interface, 1); + } else if (strcmp(interface, + zwp_virtual_keyboard_manager_v1_interface.name) == 0) { + vkbd_mgr = wl_registry_bind( + registry, name, &zwp_virtual_keyboard_manager_v1_interface, 1); + } } -void -handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { - for (int i = 0; i < WL_OUTPUTS_LIMIT; i += 1) { - if (wl_outputs[i].name == name) { - wl_output_destroy(wl_outputs[i].data); - for (; i < WL_OUTPUTS_LIMIT - 1; i += 1) { - wl_outputs[i] = wl_outputs[i + 1]; - } - wl_outputs_size -= 1; - break; - } - } +void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + for (int i = 0; i < WL_OUTPUTS_LIMIT; i += 1) { + if (wl_outputs[i].name == name) { + wl_output_destroy(wl_outputs[i].data); + for (; i < WL_OUTPUTS_LIMIT - 1; i += 1) { + wl_outputs[i] = wl_outputs[i + 1]; + } + wl_outputs_size -= 1; + break; + } + } } -static void -xdg_popup_surface_configure(void *data, struct xdg_surface *xdg_surface, - uint32_t serial) { - xdg_surface_ack_configure(xdg_surface, serial); - drwsurf_flip(&popup_draw_surf); +static void xdg_popup_surface_configure(void *data, + struct xdg_surface *xdg_surface, + uint32_t serial) { + xdg_surface_ack_configure(xdg_surface, serial); + drwsurf_flip(&popup_draw_surf); } static const struct xdg_surface_listener xdg_popup_surface_listener = { - .configure = xdg_popup_surface_configure, + .configure = xdg_popup_surface_configure, }; -static void -xdg_popup_configure(void *data, struct xdg_popup *xdg_popup, int32_t x, - int32_t y, int32_t width, int32_t height) { - kbd_resize(&keyboard, layouts, NumLayouts); +static void xdg_popup_configure(void *data, struct xdg_popup *xdg_popup, + int32_t x, int32_t y, int32_t width, + int32_t height) { + kbd_resize(&keyboard, layouts, NumLayouts); - drwsurf_flip(&draw_surf); + drwsurf_flip(&draw_surf); } -static void -xdg_popup_done(void *data, struct xdg_popup *xdg_popup) {} +static void xdg_popup_done(void *data, struct xdg_popup *xdg_popup) {} static const struct xdg_popup_listener xdg_popup_listener = { - .configure = xdg_popup_configure, - .popup_done = xdg_popup_done, + .configure = xdg_popup_configure, + .popup_done = xdg_popup_done, }; -static void -wp_fractional_scale_prefered_scale( - void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, - uint32_t scale) { - keyboard.pending_scale = (double)scale / 120; +static void wp_fractional_scale_prefered_scale( + void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, + uint32_t scale) { + keyboard.pending_scale = (double)scale / 120; } static const struct wp_fractional_scale_v1_listener - wp_fractional_scale_listener = { - .preferred_scale = wp_fractional_scale_prefered_scale, + wp_fractional_scale_listener = { + .preferred_scale = wp_fractional_scale_prefered_scale, }; -void -resize() { - keyboard.landscape = current_output->w > current_output->h; +void resize() { + keyboard.landscape = current_output->w > current_output->h; - enum layout_id layer; - if (keyboard.landscape) { - layer = keyboard.landscape_layers[0]; - height = landscape_height; - } else { - layer = keyboard.layers[0]; - height = normal_height; - } + enum layout_id layer; + if (keyboard.landscape) { + layer = keyboard.landscape_layers[0]; + height = landscape_height; + } else { + layer = keyboard.layers[0]; + height = normal_height; + } - keyboard.layout = &keyboard.layouts[layer]; - keyboard.layer_index = 0; - keyboard.prevlayout = keyboard.layout; - keyboard.last_abc_layout = keyboard.layout; - keyboard.last_abc_index = 0; + keyboard.layout = &keyboard.layouts[layer]; + keyboard.layer_index = 0; + keyboard.prevlayout = keyboard.layout; + keyboard.last_abc_layout = keyboard.layout; + keyboard.last_abc_index = 0; - if (!wfs_mgr || !viewporter) { - keyboard.pending_scale = current_output->scale; - } + if (!wfs_mgr || !viewporter) { + keyboard.pending_scale = current_output->scale; + } - if (layer_surface) { - zwlr_layer_surface_v1_set_size(layer_surface, 0, height); - zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); - wl_surface_commit(draw_surf.surf); - } + if (layer_surface) { + zwlr_layer_surface_v1_set_size(layer_surface, 0, height); + zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); + wl_surface_commit(draw_surf.surf); + } } -void -layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, - uint32_t serial, uint32_t w, uint32_t h) { - if (keyboard.w != w || keyboard.h != h || - keyboard.scale != keyboard.pending_scale) { - keyboard.w = w; - keyboard.h = h; - keyboard.scale = keyboard.pending_scale; +void layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, + uint32_t serial, uint32_t w, uint32_t h) { + if (keyboard.w != w || keyboard.h != h || + keyboard.scale != keyboard.pending_scale) { + keyboard.w = w; + keyboard.h = h; + keyboard.scale = keyboard.pending_scale; - if (wfs_mgr && viewporter) { - if (!wfs_draw_surf) { - wfs_draw_surf = wp_fractional_scale_manager_v1_get_fractional_scale( - wfs_mgr, draw_surf.surf); - wp_fractional_scale_v1_add_listener( - wfs_draw_surf, &wp_fractional_scale_listener, NULL); - } - if (!draw_surf_viewport) { - draw_surf_viewport = - wp_viewporter_get_viewport(viewporter, draw_surf.surf); - } - wp_viewport_set_destination(draw_surf_viewport, keyboard.w, keyboard.h); - } else { - wl_surface_set_buffer_scale(draw_surf.surf, keyboard.scale); - } + if (wfs_mgr && viewporter) { + if (!wfs_draw_surf) { + wfs_draw_surf = + wp_fractional_scale_manager_v1_get_fractional_scale( + wfs_mgr, draw_surf.surf); + wp_fractional_scale_v1_add_listener( + wfs_draw_surf, &wp_fractional_scale_listener, NULL); + } + if (!draw_surf_viewport) { + draw_surf_viewport = + wp_viewporter_get_viewport(viewporter, draw_surf.surf); + } + wp_viewport_set_destination(draw_surf_viewport, keyboard.w, + keyboard.h); + } else { + wl_surface_set_buffer_scale(draw_surf.surf, keyboard.scale); + } - if (popup_xdg_popup) { - xdg_popup_destroy(popup_xdg_popup); - } - if (popup_xdg_surface) { - xdg_surface_destroy(popup_xdg_surface); - } - if (popup_draw_surf.surf) { - wl_surface_destroy(popup_draw_surf.surf); - } + if (popup_xdg_popup) { + xdg_popup_destroy(popup_xdg_popup); + } + if (popup_xdg_surface) { + xdg_surface_destroy(popup_xdg_surface); + } + if (popup_draw_surf.surf) { + wl_surface_destroy(popup_draw_surf.surf); + } - popup_draw_surf.surf = wl_compositor_create_surface(compositor); + popup_draw_surf.surf = wl_compositor_create_surface(compositor); - xdg_positioner_set_size(popup_xdg_positioner, w, h * 2); - xdg_positioner_set_anchor_rect(popup_xdg_positioner, 0, -h, w, h * 2); + xdg_positioner_set_size(popup_xdg_positioner, w, h * 2); + xdg_positioner_set_anchor_rect(popup_xdg_positioner, 0, -h, w, h * 2); - wl_surface_set_input_region(popup_draw_surf.surf, empty_region); - popup_xdg_surface = - xdg_wm_base_get_xdg_surface(wm_base, popup_draw_surf.surf); - xdg_surface_add_listener(popup_xdg_surface, &xdg_popup_surface_listener, - NULL); - popup_xdg_popup = - xdg_surface_get_popup(popup_xdg_surface, NULL, popup_xdg_positioner); - xdg_popup_add_listener(popup_xdg_popup, &xdg_popup_listener, NULL); - zwlr_layer_surface_v1_get_popup(layer_surface, popup_xdg_popup); + wl_surface_set_input_region(popup_draw_surf.surf, empty_region); + popup_xdg_surface = + xdg_wm_base_get_xdg_surface(wm_base, popup_draw_surf.surf); + xdg_surface_add_listener(popup_xdg_surface, &xdg_popup_surface_listener, + NULL); + popup_xdg_popup = xdg_surface_get_popup(popup_xdg_surface, NULL, + popup_xdg_positioner); + xdg_popup_add_listener(popup_xdg_popup, &xdg_popup_listener, NULL); + zwlr_layer_surface_v1_get_popup(layer_surface, popup_xdg_popup); - if (wfs_mgr && viewporter) { - popup_draw_surf_viewport = - wp_viewporter_get_viewport(viewporter, popup_draw_surf.surf); - wp_viewport_set_destination(popup_draw_surf_viewport, keyboard.w, - keyboard.h * 2); - } else { - wl_surface_set_buffer_scale(popup_draw_surf.surf, keyboard.scale); - } + if (wfs_mgr && viewporter) { + popup_draw_surf_viewport = + wp_viewporter_get_viewport(viewporter, popup_draw_surf.surf); + wp_viewport_set_destination(popup_draw_surf_viewport, keyboard.w, + keyboard.h * 2); + } else { + wl_surface_set_buffer_scale(popup_draw_surf.surf, keyboard.scale); + } - wl_surface_commit(popup_draw_surf.surf); - } + wl_surface_commit(popup_draw_surf.surf); + } - zwlr_layer_surface_v1_ack_configure(surface, serial); + zwlr_layer_surface_v1_ack_configure(surface, serial); } -void -layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface) { - zwlr_layer_surface_v1_destroy(surface); - wl_surface_destroy(draw_surf.surf); - run_display = false; +void layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface) { + zwlr_layer_surface_v1_destroy(surface); + wl_surface_destroy(draw_surf.surf); + run_display = false; } -void -usage(char *argv0) { - fprintf(stderr, - "usage: %s [-hov] [-H height] [-L landscape height] [-fn font] [-l " - "layers]\n", - argv0); - fprintf(stderr, "Options:\n"); - fprintf(stderr, " -D - Enable debug\n"); - fprintf(stderr, " -o - Print pressed keys to standard output\n"); - fprintf(stderr, - " -O - Print intersected keys to standard output\n"); - fprintf(stderr, " -H [int] - Height in pixels\n"); - fprintf(stderr, " -L [int] - Landscape height in pixels\n"); - fprintf(stderr, " --fn [font] - Set font (e.g: DejaVu Sans 20)\n"); - fprintf(stderr, " --hidden - Start hidden (send SIGUSR2 to show)\n"); - fprintf( - stderr, - " --alpha [int] - Set alpha value for all colors [0-255]\n"); - fprintf(stderr, " --bg [rrggbb|aa] - Set color of background\n"); - fprintf(stderr, " --fg [rrggbb|aa] - Set color of keys\n"); - fprintf(stderr, " --fg-sp [rrggbb|aa] - Set color of special keys\n"); - fprintf(stderr, " --press [rrggbb|aa] - Set color of pressed keys\n"); - fprintf(stderr, - " --press-sp [rrggbb|aa] - Set color of pressed special keys\n"); - fprintf(stderr, " --swipe [rrggbb|aa] - Set color of swiped keys\n"); - fprintf(stderr, - " --swipe-sp [rrggbb|aa] - Set color of swiped special keys\n"); - fprintf(stderr, " --text [rrggbb|aa] - Set color of text on keys\n"); - fprintf(stderr, - " --text-sp [rrggbb|aa] - Set color of text on special keys\n"); - fprintf(stderr, - " --list-layers - Print the list of available layers\n"); - fprintf(stderr, - " -l - Comma separated list of layers\n"); - fprintf( - stderr, - " --landscape-layers - Comma separated list of landscape layers\n"); +void usage(char *argv0) { + fprintf(stderr, + "usage: %s [-hov] [-H height] [-L landscape height] [-fn font] [-l " + "layers]\n", + argv0); + fprintf(stderr, "Options:\n"); + fprintf(stderr, " -D - Enable debug\n"); + fprintf(stderr, " -o - Print pressed keys to standard output\n"); + fprintf(stderr, + " -O - Print intersected keys to standard output\n"); + fprintf(stderr, " -H [int] - Height in pixels\n"); + fprintf(stderr, " -L [int] - Landscape height in pixels\n"); + fprintf(stderr, " --fn [font] - Set font (e.g: DejaVu Sans 20)\n"); + fprintf(stderr, " --hidden - Start hidden (send SIGUSR2 to show)\n"); + fprintf( + stderr, + " --alpha [int] - Set alpha value for all colors [0-255]\n"); + fprintf(stderr, " --bg [rrggbb|aa] - Set color of background\n"); + fprintf(stderr, " --fg [rrggbb|aa] - Set color of keys\n"); + fprintf(stderr, " --fg-sp [rrggbb|aa] - Set color of special keys\n"); + fprintf(stderr, " --press [rrggbb|aa] - Set color of pressed keys\n"); + fprintf(stderr, + " --press-sp [rrggbb|aa] - Set color of pressed special keys\n"); + fprintf(stderr, " --swipe [rrggbb|aa] - Set color of swiped keys\n"); + fprintf(stderr, + " --swipe-sp [rrggbb|aa] - Set color of swiped special keys\n"); + fprintf(stderr, " --text [rrggbb|aa] - Set color of text on keys\n"); + fprintf(stderr, + " --text-sp [rrggbb|aa] - Set color of text on special keys\n"); + fprintf(stderr, + " --list-layers - Print the list of available layers\n"); + fprintf(stderr, + " -l - Comma separated list of layers\n"); + fprintf(stderr, " --landscape-layers - Comma separated list of " + "landscape layers\n"); } -void -list_layers() { - int i; - for (i = 0; i < NumLayouts - 1; i++) { - if (layouts[i].name) { - puts(layouts[i].name); - } - } +void list_layers() { + int i; + for (i = 0; i < NumLayouts - 1; i++) { + if (layouts[i].name) { + puts(layouts[i].name); + } + } } -void -hide() { - if (!layer_surface) { - return; - } +void hide() { + if (!layer_surface) { + return; + } - zwlr_layer_surface_v1_destroy(layer_surface); - wl_surface_destroy(draw_surf.surf); - layer_surface = NULL; - hidden = true; + zwlr_layer_surface_v1_destroy(layer_surface); + wl_surface_destroy(draw_surf.surf); + layer_surface = NULL; + hidden = true; } -void -show() { - if (layer_surface) { - return; - } +void show() { + if (layer_surface) { + return; + } - wl_display_sync(display); + wl_display_sync(display); - draw_surf.surf = wl_compositor_create_surface(compositor); - wl_surface_add_listener(draw_surf.surf, &surface_listener, NULL); - layer_surface = zwlr_layer_shell_v1_get_layer_surface( - layer_shell, draw_surf.surf, NULL, layer, namespace); + draw_surf.surf = wl_compositor_create_surface(compositor); + wl_surface_add_listener(draw_surf.surf, &surface_listener, NULL); + layer_surface = zwlr_layer_shell_v1_get_layer_surface( + layer_shell, draw_surf.surf, NULL, layer, namespace); - zwlr_layer_surface_v1_set_size(layer_surface, 0, height); - zwlr_layer_surface_v1_set_anchor(layer_surface, anchor); - zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); - zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false); - zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, - NULL); - wl_surface_commit(draw_surf.surf); + zwlr_layer_surface_v1_set_size(layer_surface, 0, height); + zwlr_layer_surface_v1_set_anchor(layer_surface, anchor); + zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); + zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false); + zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, + NULL); + wl_surface_commit(draw_surf.surf); - hidden = false; + hidden = false; } -void -toggle_visibility() { - if (hidden) - show(); - else - hide(); +void toggle_visibility() { + if (hidden) + show(); + else + hide(); } -void -pipewarn() { - fprintf(stderr, "wvkbd: cannot pipe data out.\n"); +void pipewarn() { fprintf(stderr, "wvkbd: cannot pipe data out.\n"); } + +void set_kbd_colors(uint8_t *bgra, char *hex) { + // bg, fg, text, high, swipe + int length = strlen(hex); + if (length == 6 || length == 8) { + char subhex[2]; + memcpy(subhex, hex, 2); + bgra[2] = (int)strtol(subhex, NULL, 16); + memcpy(subhex, hex + 2, 2); + bgra[1] = (int)strtol(subhex, NULL, 16); + memcpy(subhex, hex + 4, 2); + bgra[0] = (int)strtol(subhex, NULL, 16); + if (length == 8) { + memcpy(subhex, hex + 6, 2); + bgra[3] = (int)strtol(subhex, NULL, 16); + } + } } -void -set_kbd_colors(uint8_t *bgra, char *hex) { - // bg, fg, text, high, swipe - int length = strlen(hex); - if (length == 6 || length == 8) { - char subhex[2]; - memcpy(subhex, hex, 2); - bgra[2] = (int)strtol(subhex, NULL, 16); - memcpy(subhex, hex + 2, 2); - bgra[1] = (int)strtol(subhex, NULL, 16); - memcpy(subhex, hex + 4, 2); - bgra[0] = (int)strtol(subhex, NULL, 16); - if (length == 8) { - memcpy(subhex, hex + 6, 2); - bgra[3] = (int)strtol(subhex, NULL, 16); - } - } -} - -int -main(int argc, char **argv) { - /* parse command line arguments */ - char *layer_names_list = NULL, *landscape_layer_names_list = NULL; - const char *fc_font_pattern = NULL; - height = normal_height = KBD_PIXEL_HEIGHT; - landscape_height = KBD_PIXEL_LANDSCAPE_HEIGHT; - - char *tmp; - if ((tmp = getenv("WVKBD_LAYERS"))) - layer_names_list = estrdup(tmp); - if ((tmp = getenv("WVKBD_LANDSCAPE_LAYERS"))) - landscape_layer_names_list = estrdup(tmp); - if ((tmp = getenv("WVKBD_HEIGHT"))) - normal_height = atoi(tmp); - if ((tmp = getenv("WVKBD_LANDSCAPE_HEIGHT"))) - landscape_height = atoi(tmp); - - /* keyboard settings */ - keyboard.layers = (enum layout_id *)&layers; - keyboard.landscape_layers = (enum layout_id *)&landscape_layers; - keyboard.scheme = scheme; - keyboard.layer_index = 0; - keyboard.scheme1 = scheme1; - keyboard.pending_scale = 1; - - uint8_t alpha = 0; - bool alpha_defined = false; - - int i; - for (i = 1; argv[i]; i++) { - if ((!strcmp(argv[i], "-v")) || (!strcmp(argv[i], "--version"))) { - printf("wvkbd-%s\n", VERSION); - exit(0); - } else if ((!strcmp(argv[i], "-h")) || (!strcmp(argv[i], "--help"))) { - usage(argv[0]); - exit(0); - } else if (!strcmp(argv[i], "-l")) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - if (layer_names_list) - free(layer_names_list); - layer_names_list = estrdup(argv[++i]); - } else if ((!strcmp(argv[i], "-landscape-layers")) || - (!strcmp(argv[i], "--landscape-layers"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - if (landscape_layer_names_list) - free(landscape_layer_names_list); - landscape_layer_names_list = estrdup(argv[++i]); - } else if ((!strcmp(argv[i], "-bg")) || (!strcmp(argv[i], "--bg"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme.bg.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-alpha")) || (!strcmp(argv[i], "--alpha"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - alpha = atoi(argv[++i]); - alpha_defined = true; - } else if ((!strcmp(argv[i], "-fg")) || (!strcmp(argv[i], "--fg"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme.fg.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-fg-sp")) || (!strcmp(argv[i], "--fg-sp"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme1.fg.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-press")) || (!strcmp(argv[i], "--press"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme.high.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-press-sp")) || - (!strcmp(argv[i], "--press-sp"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme1.high.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-swipe")) || (!strcmp(argv[i], "--swipe"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme.swipe.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-swipe-sp")) || - (!strcmp(argv[i], "--swipe-sp"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme1.swipe.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-text")) || (!strcmp(argv[i], "--text"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme.text.bgra, argv[++i]); - } else if ((!strcmp(argv[i], "-text-sp")) || - (!strcmp(argv[i], "--text-sp"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - set_kbd_colors(keyboard.scheme1.text.bgra, argv[++i]); - } else if (!strcmp(argv[i], "-H")) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - height = normal_height = atoi(argv[++i]); - } else if (!strcmp(argv[i], "-L")) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - landscape_height = atoi(argv[++i]); - } else if (!strcmp(argv[i], "-D")) { - keyboard.debug = true; - } else if ((!strcmp(argv[i], "-fn")) || (!strcmp(argv[i], "--fn"))) { - if (i >= argc - 1) { - usage(argv[0]); - exit(1); - } - fc_font_pattern = estrdup(argv[++i]); - } else if (!strcmp(argv[i], "-o")) { - keyboard.print = true; - } else if (!strcmp(argv[i], "-O")) { - keyboard.print_intersect = true; - } else if ((!strcmp(argv[i], "-hidden")) || - (!strcmp(argv[i], "--hidden"))) { - hidden = true; - } else if ((!strcmp(argv[i], "-list-layers")) || - (!strcmp(argv[i], "--list-layers"))) { - list_layers(); - exit(0); - } else { - fprintf(stderr, "Invalid argument: %s\n", argv[i]); - usage(argv[0]); - exit(1); - } - } - - if (alpha_defined) { - keyboard.scheme.bg.bgra[3] = alpha; - keyboard.scheme.fg.bgra[3] = alpha; - keyboard.scheme.high.bgra[3] = alpha; - keyboard.scheme1.bg.bgra[3] = alpha; - keyboard.scheme1.fg.bgra[3] = alpha; - keyboard.scheme1.high.bgra[3] = alpha; - } - - if (!fc_font_pattern) { - fc_font_pattern = default_font; - } - - display = wl_display_connect(NULL); - if (display == NULL) { - die("Failed to create display\n"); - } - - draw_surf.ctx = &draw_ctx; - popup_draw_surf.ctx = &draw_ctx; - keyboard.surf = &draw_surf; - keyboard.popup_surf = &popup_draw_surf; - - struct wl_registry *registry = wl_display_get_registry(display); - wl_registry_add_listener(registry, ®istry_listener, NULL); - wl_display_roundtrip(display); - - if (compositor == NULL) { - die("wl_compositor not available\n"); - } - if (draw_ctx.shm == NULL) { - die("wl_shm not available\n"); - } - if (layer_shell == NULL) { - die("layer_shell not available\n"); - } - if (wm_base == NULL) { - die("wm_base not available\n"); - } - if (vkbd_mgr == NULL) { - die("virtual_keyboard_manager not available\n"); - } - - empty_region = wl_compositor_create_region(compositor); - popup_xdg_positioner = xdg_wm_base_create_positioner(wm_base); - - keyboard.vkbd = - zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(vkbd_mgr, seat); - if (keyboard.vkbd == NULL) { - die("failed to init virtual keyboard_manager\n"); - } - - kbd_init(&keyboard, (struct layout *)&layouts, layer_names_list, - landscape_layer_names_list); - - draw_ctx.font_description = - pango_font_description_from_string(fc_font_pattern); - - if (!hidden) - show(); - - struct pollfd fds[2]; - int WAYLAND_FD = 0; - int SIGNAL_FD = 1; - fds[WAYLAND_FD].events = POLLIN; - fds[SIGNAL_FD].events = POLLIN; - - fds[WAYLAND_FD].fd = wl_display_get_fd(display); - if (fds[WAYLAND_FD].fd == -1) { - die("Failed to get wayland_fd: %d\n", errno); - } - - sigset_t signal_mask; - sigemptyset(&signal_mask); - sigaddset(&signal_mask, SIGUSR1); - sigaddset(&signal_mask, SIGUSR2); - sigaddset(&signal_mask, SIGRTMIN); - sigaddset(&signal_mask, SIGPIPE); - if (sigprocmask(SIG_BLOCK, &signal_mask, NULL) == -1) { - die("Failed to disable handled signals: %d\n", errno); - } - - fds[SIGNAL_FD].fd = signalfd(-1, &signal_mask, 0); - if (fds[SIGNAL_FD].fd == -1) { - die("Failed to get signalfd: %d\n", errno); - } - - while (run_display) { - wl_display_flush(display); - poll(fds, 2, -1); - - if (fds[WAYLAND_FD].revents & POLLIN) - wl_display_dispatch(display); - - if (fds[SIGNAL_FD].revents & POLLIN) { - struct signalfd_siginfo si; - - if (read(fds[SIGNAL_FD].fd, &si, sizeof(si)) != sizeof(si)) - fprintf(stderr, "Signal read error: %d", errno); - else if (si.ssi_signo == SIGUSR1) - hide(); - else if (si.ssi_signo == SIGUSR2) - show(); - else if (si.ssi_signo == SIGRTMIN) - toggle_visibility(); - else if (si.ssi_signo == SIGPIPE) - pipewarn(); - } - } - - if (fc_font_pattern != default_font) { - free((void *)fc_font_pattern); - } - - return 0; +int main(int argc, char **argv) { + /* parse command line arguments */ + char *layer_names_list = NULL, *landscape_layer_names_list = NULL; + const char *fc_font_pattern = NULL; + height = normal_height = KBD_PIXEL_HEIGHT; + landscape_height = KBD_PIXEL_LANDSCAPE_HEIGHT; + + char *tmp; + if ((tmp = getenv("WVKBD_LAYERS"))) + layer_names_list = estrdup(tmp); + if ((tmp = getenv("WVKBD_LANDSCAPE_LAYERS"))) + landscape_layer_names_list = estrdup(tmp); + if ((tmp = getenv("WVKBD_HEIGHT"))) + normal_height = atoi(tmp); + if ((tmp = getenv("WVKBD_LANDSCAPE_HEIGHT"))) + landscape_height = atoi(tmp); + + /* keyboard settings */ + keyboard.layers = (enum layout_id *)&layers; + keyboard.landscape_layers = (enum layout_id *)&landscape_layers; + keyboard.scheme = scheme; + keyboard.layer_index = 0; + keyboard.scheme1 = scheme1; + keyboard.pending_scale = 1; + + uint8_t alpha = 0; + bool alpha_defined = false; + + int i; + for (i = 1; argv[i]; i++) { + if ((!strcmp(argv[i], "-v")) || (!strcmp(argv[i], "--version"))) { + printf("wvkbd-%s\n", VERSION); + exit(0); + } else if ((!strcmp(argv[i], "-h")) || (!strcmp(argv[i], "--help"))) { + usage(argv[0]); + exit(0); + } else if (!strcmp(argv[i], "-l")) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + if (layer_names_list) + free(layer_names_list); + layer_names_list = estrdup(argv[++i]); + } else if ((!strcmp(argv[i], "-landscape-layers")) || + (!strcmp(argv[i], "--landscape-layers"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + if (landscape_layer_names_list) + free(landscape_layer_names_list); + landscape_layer_names_list = estrdup(argv[++i]); + } else if ((!strcmp(argv[i], "-bg")) || (!strcmp(argv[i], "--bg"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme.bg.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-alpha")) || + (!strcmp(argv[i], "--alpha"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + alpha = atoi(argv[++i]); + alpha_defined = true; + } else if ((!strcmp(argv[i], "-fg")) || (!strcmp(argv[i], "--fg"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme.fg.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-fg-sp")) || + (!strcmp(argv[i], "--fg-sp"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme1.fg.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-press")) || + (!strcmp(argv[i], "--press"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme.high.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-press-sp")) || + (!strcmp(argv[i], "--press-sp"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme1.high.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-swipe")) || + (!strcmp(argv[i], "--swipe"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme.swipe.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-swipe-sp")) || + (!strcmp(argv[i], "--swipe-sp"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme1.swipe.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-text")) || + (!strcmp(argv[i], "--text"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme.text.bgra, argv[++i]); + } else if ((!strcmp(argv[i], "-text-sp")) || + (!strcmp(argv[i], "--text-sp"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + set_kbd_colors(keyboard.scheme1.text.bgra, argv[++i]); + } else if (!strcmp(argv[i], "-H")) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + height = normal_height = atoi(argv[++i]); + } else if (!strcmp(argv[i], "-L")) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + landscape_height = atoi(argv[++i]); + } else if (!strcmp(argv[i], "-D")) { + keyboard.debug = true; + } else if ((!strcmp(argv[i], "-fn")) || (!strcmp(argv[i], "--fn"))) { + if (i >= argc - 1) { + usage(argv[0]); + exit(1); + } + fc_font_pattern = estrdup(argv[++i]); + } else if (!strcmp(argv[i], "-o")) { + keyboard.print = true; + } else if (!strcmp(argv[i], "-O")) { + keyboard.print_intersect = true; + } else if ((!strcmp(argv[i], "-hidden")) || + (!strcmp(argv[i], "--hidden"))) { + hidden = true; + } else if ((!strcmp(argv[i], "-list-layers")) || + (!strcmp(argv[i], "--list-layers"))) { + list_layers(); + exit(0); + } else { + fprintf(stderr, "Invalid argument: %s\n", argv[i]); + usage(argv[0]); + exit(1); + } + } + + if (alpha_defined) { + keyboard.scheme.bg.bgra[3] = alpha; + keyboard.scheme.fg.bgra[3] = alpha; + keyboard.scheme.high.bgra[3] = alpha; + keyboard.scheme1.bg.bgra[3] = alpha; + keyboard.scheme1.fg.bgra[3] = alpha; + keyboard.scheme1.high.bgra[3] = alpha; + } + + if (!fc_font_pattern) { + fc_font_pattern = default_font; + } + + display = wl_display_connect(NULL); + if (display == NULL) { + die("Failed to create display\n"); + } + + draw_surf.ctx = &draw_ctx; + popup_draw_surf.ctx = &draw_ctx; + keyboard.surf = &draw_surf; + keyboard.popup_surf = &popup_draw_surf; + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_roundtrip(display); + + if (compositor == NULL) { + die("wl_compositor not available\n"); + } + if (draw_ctx.shm == NULL) { + die("wl_shm not available\n"); + } + if (layer_shell == NULL) { + die("layer_shell not available\n"); + } + if (wm_base == NULL) { + die("wm_base not available\n"); + } + if (vkbd_mgr == NULL) { + die("virtual_keyboard_manager not available\n"); + } + + empty_region = wl_compositor_create_region(compositor); + popup_xdg_positioner = xdg_wm_base_create_positioner(wm_base); + + keyboard.vkbd = + zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(vkbd_mgr, seat); + if (keyboard.vkbd == NULL) { + die("failed to init virtual keyboard_manager\n"); + } + + kbd_init(&keyboard, (struct layout *)&layouts, layer_names_list, + landscape_layer_names_list); + + draw_ctx.font_description = + pango_font_description_from_string(fc_font_pattern); + + if (!hidden) + show(); + + struct pollfd fds[2]; + int WAYLAND_FD = 0; + int SIGNAL_FD = 1; + fds[WAYLAND_FD].events = POLLIN; + fds[SIGNAL_FD].events = POLLIN; + + fds[WAYLAND_FD].fd = wl_display_get_fd(display); + if (fds[WAYLAND_FD].fd == -1) { + die("Failed to get wayland_fd: %d\n", errno); + } + + sigset_t signal_mask; + sigemptyset(&signal_mask); + sigaddset(&signal_mask, SIGUSR1); + sigaddset(&signal_mask, SIGUSR2); + sigaddset(&signal_mask, SIGRTMIN); + sigaddset(&signal_mask, SIGPIPE); + if (sigprocmask(SIG_BLOCK, &signal_mask, NULL) == -1) { + die("Failed to disable handled signals: %d\n", errno); + } + + fds[SIGNAL_FD].fd = signalfd(-1, &signal_mask, 0); + if (fds[SIGNAL_FD].fd == -1) { + die("Failed to get signalfd: %d\n", errno); + } + + while (run_display) { + wl_display_flush(display); + poll(fds, 2, -1); + + if (fds[WAYLAND_FD].revents & POLLIN) + wl_display_dispatch(display); + + if (fds[SIGNAL_FD].revents & POLLIN) { + struct signalfd_siginfo si; + + if (read(fds[SIGNAL_FD].fd, &si, sizeof(si)) != sizeof(si)) + fprintf(stderr, "Signal read error: %d", errno); + else if (si.ssi_signo == SIGUSR1) + hide(); + else if (si.ssi_signo == SIGUSR2) + show(); + else if (si.ssi_signo == SIGRTMIN) + toggle_visibility(); + else if (si.ssi_signo == SIGPIPE) + pipewarn(); + } + } + + if (fc_font_pattern != default_font) { + free((void *)fc_font_pattern); + } + + return 0; } diff --git a/os-compatibility.c b/os-compatibility.c index 93f744b..7d5c3ef 100644 --- a/os-compatibility.c +++ b/os-compatibility.c @@ -34,90 +34,85 @@ #include "os-compatibility.h" -int -os_fd_set_cloexec(int fd) { - long flags; +int os_fd_set_cloexec(int fd) { + long flags; - if (fd == -1) - return -1; + if (fd == -1) + return -1; - flags = fcntl(fd, F_GETFD); - if (flags == -1) - return -1; + flags = fcntl(fd, F_GETFD); + if (flags == -1) + return -1; - if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) - return -1; + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) + return -1; - return 0; + return 0; } -static int -set_cloexec_or_close(int fd) { - if (os_fd_set_cloexec(fd) != 0) { - close(fd); - return -1; - } - return fd; +static int set_cloexec_or_close(int fd) { + if (os_fd_set_cloexec(fd) != 0) { + close(fd); + return -1; + } + return fd; } -int -os_socketpair_cloexec(int domain, int type, int protocol, int *sv) { - int ret; +int os_socketpair_cloexec(int domain, int type, int protocol, int *sv) { + int ret; #ifdef SOCK_CLOEXEC - ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv); - if (ret == 0 || errno != EINVAL) - return ret; + ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv); + if (ret == 0 || errno != EINVAL) + return ret; #endif - ret = socketpair(domain, type, protocol, sv); - if (ret < 0) - return ret; + ret = socketpair(domain, type, protocol, sv); + if (ret < 0) + return ret; - sv[0] = set_cloexec_or_close(sv[0]); - sv[1] = set_cloexec_or_close(sv[1]); + sv[0] = set_cloexec_or_close(sv[0]); + sv[1] = set_cloexec_or_close(sv[1]); - if (sv[0] != -1 && sv[1] != -1) - return 0; + if (sv[0] != -1 && sv[1] != -1) + return 0; - close(sv[0]); - close(sv[1]); - return -1; + close(sv[0]); + close(sv[1]); + return -1; } -int -os_epoll_create_cloexec(void) { - int fd; +int os_epoll_create_cloexec(void) { + int fd; #ifdef EPOLL_CLOEXEC - fd = epoll_create1(EPOLL_CLOEXEC); - if (fd >= 0) - return fd; - if (errno != EINVAL) - return -1; + fd = epoll_create1(EPOLL_CLOEXEC); + if (fd >= 0) + return fd; + if (errno != EINVAL) + return -1; #endif - fd = epoll_create(1); - return set_cloexec_or_close(fd); + fd = epoll_create(1); + return set_cloexec_or_close(fd); } -static int -create_tmpfile_cloexec(char *tmpname) { - int fd; +static int create_tmpfile_cloexec(char *tmpname) { + int fd; #ifdef HAVE_MKOSTEMP - fd = mkostemp(tmpname, O_CLOEXEC); - if (fd >= 0) - unlink(tmpname); + fd = mkostemp(tmpname, O_CLOEXEC); + if (fd >= 0) + unlink(tmpname); #else - fd = mkstemp(tmpname); - if (fd >= 0) { - fd = set_cloexec_or_close(fd); - unlink(tmpname); - } + fd = mkstemp(tmpname); + if (fd >= 0) { + fd = set_cloexec_or_close(fd); + unlink(tmpname); + } #endif - return fd; + return fd; } /* @@ -141,61 +136,59 @@ create_tmpfile_cloexec(char *tmpname) { * If posix_fallocate() is not supported, program may receive * SIGBUS on accessing mmap()'ed file contents instead. */ -int -os_create_anonymous_file(off_t size) { - static const char template[] = "/weston-shared-XXXXXX"; - const char *path; - char *name; - int fd; - int ret; +int os_create_anonymous_file(off_t size) { + static const char template[] = "/weston-shared-XXXXXX"; + const char *path; + char *name; + int fd; + int ret; - path = getenv("XDG_RUNTIME_DIR"); - if (!path) { - errno = ENOENT; - return -1; - } + path = getenv("XDG_RUNTIME_DIR"); + if (!path) { + errno = ENOENT; + return -1; + } - name = malloc(strlen(path) + sizeof(template)); - if (!name) - return -1; + name = malloc(strlen(path) + sizeof(template)); + if (!name) + return -1; - strcpy(name, path); - strcat(name, template); + strcpy(name, path); + strcat(name, template); - fd = create_tmpfile_cloexec(name); + fd = create_tmpfile_cloexec(name); - free(name); + free(name); - if (fd < 0) - return -1; + if (fd < 0) + return -1; #ifdef HAVE_POSIX_FALLOCATE - do { - ret = posix_fallocate(fd, 0, size); - } while (ret == EINTR); - if (ret != 0) { - close(fd); - errno = ret; - return -1; - } + do { + ret = posix_fallocate(fd, 0, size); + } while (ret == EINTR); + if (ret != 0) { + close(fd); + errno = ret; + return -1; + } #else - do { - ret = ftruncate(fd, size); - } while (ret < 0 && errno == EINTR); - if (ret < 0) { - close(fd); - return -1; - } + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + if (ret < 0) { + close(fd); + return -1; + } #endif - return fd; + return fd; } #ifndef MISSING_STRCHRNUL -char * -strchrnul(const char *s, int c) { - while (*s && *s != c) - s++; - return (char *)s; +char *strchrnul(const char *s, int c) { + while (*s && *s != c) + s++; + return (char *)s; } #endif diff --git a/shm_open.c b/shm_open.c index 1dfa81a..6e895c8 100644 --- a/shm_open.c +++ b/shm_open.c @@ -5,47 +5,44 @@ #include #include -static void -randname(char *buf) { - struct timespec ts; - long r; - clock_gettime(CLOCK_REALTIME, &ts); - r = ts.tv_nsec; - for (int i = 0; i < 6; ++i) { - buf[i] = 'A' + (r & 15) + (r & 16) * 2; - r >>= 5; - } +static void randname(char *buf) { + struct timespec ts; + long r; + clock_gettime(CLOCK_REALTIME, &ts); + r = ts.tv_nsec; + for (int i = 0; i < 6; ++i) { + buf[i] = 'A' + (r & 15) + (r & 16) * 2; + r >>= 5; + } } -static int -create_shm_file(void) { - int retries = 100; - int fd; - do { - char name[] = "/wl_shm-XXXXXX"; - randname(name + sizeof(name) - 7); - --retries; - fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); - if (fd >= 0) { - shm_unlink(name); - return fd; - } - } while (retries > 0 && errno == EEXIST); - return -1; +static int create_shm_file(void) { + int retries = 100; + int fd; + do { + char name[] = "/wl_shm-XXXXXX"; + randname(name + sizeof(name) - 7); + --retries; + fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + shm_unlink(name); + return fd; + } + } while (retries > 0 && errno == EEXIST); + return -1; } -int -allocate_shm_file(size_t size) { - int fd = create_shm_file(); - int ret; - if (fd < 0) - return -1; - do { - ret = ftruncate(fd, size); - } while (ret < 0 && errno == EINTR); - if (ret < 0) { - close(fd); - return -1; - } - return fd; +int allocate_shm_file(size_t size) { + int fd = create_shm_file(); + int ret; + if (fd < 0) + return -1; + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + if (ret < 0) { + close(fd); + return -1; + } + return fd; }