Simplest clang-format

This commit is contained in:
Willow Barraco 2023-09-08 22:56:50 +02:00
parent 2747980a4e
commit fbbf563d70
No known key found for this signature in database
GPG Key ID: EABA44759877E02A
6 changed files with 1476 additions and 1626 deletions

View File

@ -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

33
drw.c
View File

@ -5,8 +5,7 @@
#include "drw.h"
#include "shm_open.h"
void
drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, double s) {
void drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, double s) {
if (ds->buf) {
munmap(ds->pool_data, ds->size);
wl_buffer_destroy(ds->buf);
@ -20,14 +19,12 @@ drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, double s) {
setup_buffer(ds);
}
void
drwsurf_flip(struct drwsurf *ds) {
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,
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);
@ -50,8 +47,7 @@ drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
cairo_restore(d->cairo);
}
void
drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t w,
void drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t w,
uint32_t h) {
cairo_save(d->cairo);
@ -62,8 +58,7 @@ drw_do_clear(struct drwsurf *d, uint32_t x, uint32_t y, uint32_t w,
cairo_restore(d->cairo);
}
void
drw_do_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
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);
@ -82,20 +77,17 @@ drw_do_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
cairo_restore(d->cairo);
}
void
drw_fill_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
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,
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) {
uint32_t setup_buffer(struct drwsurf *drwsurf) {
int stride = drwsurf->width * 4;
drwsurf->size = stride * drwsurf->height;
@ -113,14 +105,15 @@ setup_buffer(struct drwsurf *drwsurf) {
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);
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);
drwsurf->pool_data, CAIRO_FORMAT_ARGB32, drwsurf->width,
drwsurf->height, stride);
drwsurf->cairo = cairo_create(s);
cairo_scale(drwsurf->cairo, drwsurf->scale, drwsurf->scale);

View File

@ -1,11 +1,11 @@
#include "keyboard.h"
#include "drw.h"
#include "os-compatibility.h"
#include "proto/virtual-keyboard-unstable-v1-client-protocol.h"
#include <linux/input-event-codes.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/mman.h>
#include "keyboard.h"
#include "drw.h"
#include "os-compatibility.h"
#define MAX_LAYERS 25
@ -19,8 +19,7 @@
#endif
#include KEYMAP
void
kbd_switch_layout(struct kbd *kb, struct layout *l, size_t layer_index) {
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;
@ -41,8 +40,7 @@ kbd_switch_layout(struct kbd *kb, struct layout *l, size_t layer_index) {
kbd_draw_layout(kb);
}
void
kbd_next_layer(struct kbd *kb, struct key *k, bool invert) {
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)) {
@ -50,8 +48,8 @@ kbd_next_layer(struct kbd *kb, struct key *k, bool invert) {
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
// with modifiers shift/capslock or invert set: switch to the
// previous layout in the layer sequence
if (layer_index > 0) {
layer_index--;
} else {
@ -70,7 +68,8 @@ kbd_next_layer(struct kbd *kb, struct key *k, bool invert) {
if (!invert)
kb->mods ^= Shift;
} else {
// normal behaviour: switch to the next layout in the layer sequence
// normal behaviour: switch to the next layout in the layer
// sequence
layer_index++;
}
size_t layercount = 0;
@ -101,8 +100,7 @@ kbd_next_layer(struct kbd *kb, struct key *k, bool invert) {
kbd_switch_layout(kb, &kb->layouts[layer], layer_index);
}
uint8_t
kbd_get_rows(struct layout *l) {
uint8_t kbd_get_rows(struct layout *l) {
uint8_t rows = 0;
struct key *k = l->keys;
while (k->type != Last) {
@ -114,8 +112,7 @@ kbd_get_rows(struct layout *l) {
return rows + 1;
}
enum layout_id *
kbd_init_layers(char *layer_names_list) {
enum layout_id *kbd_init_layers(char *layer_names_list) {
enum layout_id *layers;
uint8_t numlayers = 0;
bool found;
@ -153,8 +150,7 @@ kbd_init_layers(char *layer_names_list) {
return layers;
}
void
kbd_init(struct kbd *kb, struct layout *layouts, char *layer_names_list,
void kbd_init(struct kbd *kb, struct layout *layouts, char *layer_names_list,
char *landscape_layer_names_list) {
int i;
@ -195,8 +191,7 @@ kbd_init(struct kbd *kb, struct layout *layouts, char *layer_names_list,
create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0);
}
void
kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) {
void kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) {
uint32_t x = 0, y = 0;
uint8_t rows = kbd_get_rows(l);
@ -227,8 +222,7 @@ kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) {
}
}
double
kbd_get_row_length(struct key *k) {
double kbd_get_row_length(struct key *k) {
double l = 0.0;
while ((k->type != Last) && (k->type != EndRow)) {
l += k->width;
@ -237,15 +231,15 @@ kbd_get_row_length(struct key *k) {
return l;
}
struct key *
kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
struct key *kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
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)) {
(x >= k->x) && (y >= k->y) && (x < k->x + k->w) &&
(y < k->y + k->h)) {
return k;
}
k++;
@ -253,8 +247,7 @@ kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y) {
return NULL;
}
size_t
kbd_get_layer_index(struct kbd *kb, struct layout *l) {
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;
@ -263,8 +256,7 @@ kbd_get_layer_index(struct kbd *kb, struct layout *l) {
return 0;
}
void
kbd_unpress_key(struct kbd *kb, uint32_t time) {
void kbd_unpress_key(struct kbd *kb, uint32_t time) {
bool unlatch_shift = false;
if (kb->last_press) {
@ -284,7 +276,8 @@ kbd_unpress_key(struct kbd *kb, uint32_t time) {
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,
zwp_virtual_keyboard_v1_key(kb->vkbd, time,
kb->last_press->code,
WL_KEYBOARD_KEY_STATE_RELEASED);
}
}
@ -302,8 +295,7 @@ kbd_unpress_key(struct kbd *kb, uint32_t time) {
}
}
void
kbd_release_key(struct kbd *kb, uint32_t time) {
void kbd_release_key(struct kbd *kb, uint32_t time) {
kbd_unpress_key(kb, time);
if (kb->print_intersect && kb->last_swipe) {
printf("\n");
@ -319,8 +311,7 @@ kbd_release_key(struct kbd *kb, uint32_t time) {
drwsurf_flip(kb->popup_surf);
}
void
kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) {
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) {
@ -331,8 +322,8 @@ kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) {
}
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)) {
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);
@ -347,8 +338,7 @@ kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y) {
drwsurf_flip(kb->popup_surf);
}
void
kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
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))) {
@ -361,7 +351,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
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));
kbd_switch_layout(kb, k->layout,
kbd_get_layer_index(kb, k->layout));
return;
} else {
return;
@ -372,10 +363,11 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
case Code:
if (k->code_mod) {
if (k->reset_mod) {
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, k->code_mod, 0, 0, 0);
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);
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);
@ -415,7 +407,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
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
// 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]];
@ -424,7 +417,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
}
break;
case Compose:
// switch to the associated layout determined by the *next* keypress
// switch to the associated layout determined by the *next*
// keypress
if (kb->compose == 0) {
kb->compose = 1;
} else {
@ -436,8 +430,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
kbd_draw_key(kb, k, Unpress);
}
break;
case NextLayer: //(also handles previous layer when shift modifier is on, or
//"first layer" with other modifiers)
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
@ -445,7 +439,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
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
// 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]];
@ -460,7 +455,8 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
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);
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);
@ -475,8 +471,7 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
drwsurf_flip(kb->popup_surf);
}
void
kbd_print_key_stdout(struct kbd *kb, struct key *k) {
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)
@ -514,33 +509,34 @@ kbd_print_key_stdout(struct kbd *kb, struct key *k) {
fflush(stdout);
}
void
kbd_clear_last_popup(struct kbd *kb) {
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);
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;
}
}
void
kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type) {
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);
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);
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);
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,
@ -548,8 +544,8 @@ kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type) {
break;
}
drw_draw_text(kb->surf, scheme->text, k->x, k->y, k->w, k->h, KBD_KEY_BORDER,
label);
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) {
@ -560,18 +556,18 @@ kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type) {
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);
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_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) {
void kbd_draw_layout(struct kbd *kb) {
struct drwsurf *d = kb->surf;
struct key *next_key = kb->layout->keys;
if (kb->debug)
@ -595,8 +591,7 @@ kbd_draw_layout(struct kbd *kb) {
wl_surface_damage(d->surf, 0, 0, kb->w, kb->h);
}
void
kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t 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);
@ -605,32 +600,30 @@ kbd_resize(struct kbd *kb, struct layout *layouts, uint8_t layoutcount) {
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);
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);
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,
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_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,
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++) {
@ -651,8 +644,8 @@ create_and_upload_keymap(struct kbd *kb, const char *name, uint32_t comp_unichr,
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);
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");
}

203
main.c
View File

@ -1,22 +1,22 @@
#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 <linux/input-event-codes.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/signalfd.h>
#include <poll.h>
#include <unistd.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
#include <wchar.h>
#include "keyboard.h"
#include "config.h"
#include "keyboard.h"
/* lazy die macro */
#define die(...) \
@ -165,8 +165,7 @@ static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
/* configuration, allows nested code to access above variables */
char *
estrdup(const char *s) {
char *estrdup(const char *s) {
char *p;
if (!(p = strdup(s))) {
@ -177,8 +176,7 @@ estrdup(const char *s) {
return p;
}
void
wl_touch_down(void *data, struct wl_touch *wl_touch, uint32_t serial,
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;
@ -194,18 +192,17 @@ wl_touch_down(void *data, struct wl_touch *wl_touch, uint32_t serial,
kbd_press_key(&keyboard, next_key, time);
} else if (keyboard.compose) {
keyboard.compose = 0;
kbd_switch_layout(&keyboard, keyboard.prevlayout, keyboard.last_abc_index);
kbd_switch_layout(&keyboard, keyboard.prevlayout,
keyboard.last_abc_index);
}
}
void
wl_touch_up(void *data, struct wl_touch *wl_touch, uint32_t serial,
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,
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;
@ -215,33 +212,26 @@ wl_touch_motion(void *data, struct wl_touch *wl_touch, uint32_t time,
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,
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,
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) {
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,
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);
@ -251,9 +241,9 @@ wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time,
}
}
void
wl_pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state) {
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;
@ -275,15 +265,13 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
}
}
void
wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
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,
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) {
@ -309,11 +297,9 @@ seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
}
}
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,
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) {
@ -325,10 +311,11 @@ wl_surface_enter(void *data, struct wl_surface *wl_surface,
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) {
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
@ -346,11 +333,10 @@ display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y,
};
}
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) {
static void display_handle_scale(void *data, struct wl_output *wl_output,
int32_t scale) {
struct Output *output = data;
output->scale = scale;
@ -359,9 +345,9 @@ display_handle_scale(void *data, struct wl_output *wl_output, int32_t scale) {
};
}
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,
@ -369,8 +355,8 @@ static const struct wl_output_listener output_listener = {
.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) {
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);
}
@ -378,17 +364,18 @@ static const struct xdg_wm_base_listener xdg_wm_base_listener = {
.ping = xdg_wm_base_ping,
};
void
handle_global(void *data, struct wl_registry *registry, uint32_t name,
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);
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->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);
@ -403,21 +390,22 @@ handle_global(void *data, struct wl_registry *registry, uint32_t name,
} 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_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);
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);
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) {
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);
@ -430,8 +418,8 @@ handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
}
}
static void
xdg_popup_surface_configure(void *data, struct xdg_surface *xdg_surface,
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);
@ -441,24 +429,22 @@ static const struct xdg_surface_listener xdg_popup_surface_listener = {
.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) {
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);
}
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,
};
static void
wp_fractional_scale_prefered_scale(
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;
@ -469,8 +455,7 @@ static const struct wp_fractional_scale_v1_listener
.preferred_scale = wp_fractional_scale_prefered_scale,
};
void
resize() {
void resize() {
keyboard.landscape = current_output->w > current_output->h;
enum layout_id layer;
@ -499,8 +484,7 @@ resize() {
}
}
void
layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
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) {
@ -510,7 +494,8 @@ layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
if (wfs_mgr && viewporter) {
if (!wfs_draw_surf) {
wfs_draw_surf = wp_fractional_scale_manager_v1_get_fractional_scale(
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);
@ -519,7 +504,8 @@ layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
draw_surf_viewport =
wp_viewporter_get_viewport(viewporter, draw_surf.surf);
}
wp_viewport_set_destination(draw_surf_viewport, keyboard.w, keyboard.h);
wp_viewport_set_destination(draw_surf_viewport, keyboard.w,
keyboard.h);
} else {
wl_surface_set_buffer_scale(draw_surf.surf, keyboard.scale);
}
@ -544,8 +530,8 @@ layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *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);
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);
@ -564,15 +550,13 @@ layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
zwlr_layer_surface_v1_ack_configure(surface, serial);
}
void
layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface) {
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) {
void usage(char *argv0) {
fprintf(stderr,
"usage: %s [-hov] [-H height] [-L landscape height] [-fn font] [-l "
"layers]\n",
@ -605,13 +589,11 @@ usage(char *argv0) {
" --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");
fprintf(stderr, " --landscape-layers - Comma separated list of "
"landscape layers\n");
}
void
list_layers() {
void list_layers() {
int i;
for (i = 0; i < NumLayouts - 1; i++) {
if (layouts[i].name) {
@ -620,8 +602,7 @@ list_layers() {
}
}
void
hide() {
void hide() {
if (!layer_surface) {
return;
}
@ -632,8 +613,7 @@ hide() {
hidden = true;
}
void
show() {
void show() {
if (layer_surface) {
return;
}
@ -656,21 +636,16 @@ show() {
hidden = false;
}
void
toggle_visibility() {
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) {
void set_kbd_colors(uint8_t *bgra, char *hex) {
// bg, fg, text, high, swipe
int length = strlen(hex);
if (length == 6 || length == 8) {
@ -688,8 +663,7 @@ set_kbd_colors(uint8_t *bgra, char *hex) {
}
}
int
main(int argc, char **argv) {
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;
@ -748,7 +722,8 @@ main(int argc, char **argv) {
exit(1);
}
set_kbd_colors(keyboard.scheme.bg.bgra, argv[++i]);
} else if ((!strcmp(argv[i], "-alpha")) || (!strcmp(argv[i], "--alpha"))) {
} else if ((!strcmp(argv[i], "-alpha")) ||
(!strcmp(argv[i], "--alpha"))) {
if (i >= argc - 1) {
usage(argv[0]);
exit(1);
@ -761,13 +736,15 @@ main(int argc, char **argv) {
exit(1);
}
set_kbd_colors(keyboard.scheme.fg.bgra, argv[++i]);
} else if ((!strcmp(argv[i], "-fg-sp")) || (!strcmp(argv[i], "--fg-sp"))) {
} 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"))) {
} else if ((!strcmp(argv[i], "-press")) ||
(!strcmp(argv[i], "--press"))) {
if (i >= argc - 1) {
usage(argv[0]);
exit(1);
@ -780,7 +757,8 @@ main(int argc, char **argv) {
exit(1);
}
set_kbd_colors(keyboard.scheme1.high.bgra, argv[++i]);
} else if ((!strcmp(argv[i], "-swipe")) || (!strcmp(argv[i], "--swipe"))) {
} else if ((!strcmp(argv[i], "-swipe")) ||
(!strcmp(argv[i], "--swipe"))) {
if (i >= argc - 1) {
usage(argv[0]);
exit(1);
@ -793,7 +771,8 @@ main(int argc, char **argv) {
exit(1);
}
set_kbd_colors(keyboard.scheme1.swipe.bgra, argv[++i]);
} else if ((!strcmp(argv[i], "-text")) || (!strcmp(argv[i], "--text"))) {
} else if ((!strcmp(argv[i], "-text")) ||
(!strcmp(argv[i], "--text"))) {
if (i >= argc - 1) {
usage(argv[0]);
exit(1);

View File

@ -34,8 +34,7 @@
#include "os-compatibility.h"
int
os_fd_set_cloexec(int fd) {
int os_fd_set_cloexec(int fd) {
long flags;
if (fd == -1)
@ -51,8 +50,7 @@ os_fd_set_cloexec(int fd) {
return 0;
}
static int
set_cloexec_or_close(int fd) {
static int set_cloexec_or_close(int fd) {
if (os_fd_set_cloexec(fd) != 0) {
close(fd);
return -1;
@ -60,8 +58,7 @@ set_cloexec_or_close(int fd) {
return fd;
}
int
os_socketpair_cloexec(int domain, int type, int protocol, int *sv) {
int os_socketpair_cloexec(int domain, int type, int protocol, int *sv) {
int ret;
#ifdef SOCK_CLOEXEC
@ -85,8 +82,7 @@ os_socketpair_cloexec(int domain, int type, int protocol, int *sv) {
return -1;
}
int
os_epoll_create_cloexec(void) {
int os_epoll_create_cloexec(void) {
int fd;
#ifdef EPOLL_CLOEXEC
@ -101,8 +97,7 @@ os_epoll_create_cloexec(void) {
return set_cloexec_or_close(fd);
}
static int
create_tmpfile_cloexec(char *tmpname) {
static int create_tmpfile_cloexec(char *tmpname) {
int fd;
#ifdef HAVE_MKOSTEMP
@ -141,8 +136,7 @@ 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) {
int os_create_anonymous_file(off_t size) {
static const char template[] = "/weston-shared-XXXXXX";
const char *path;
char *name;
@ -192,8 +186,7 @@ os_create_anonymous_file(off_t size) {
}
#ifndef MISSING_STRCHRNUL
char *
strchrnul(const char *s, int c) {
char *strchrnul(const char *s, int c) {
while (*s && *s != c)
s++;
return (char *)s;

View File

@ -5,8 +5,7 @@
#include <time.h>
#include <unistd.h>
static void
randname(char *buf) {
static void randname(char *buf) {
struct timespec ts;
long r;
clock_gettime(CLOCK_REALTIME, &ts);
@ -17,8 +16,7 @@ randname(char *buf) {
}
}
static int
create_shm_file(void) {
static int create_shm_file(void) {
int retries = 100;
int fd;
do {
@ -34,8 +32,7 @@ create_shm_file(void) {
return -1;
}
int
allocate_shm_file(size_t size) {
int allocate_shm_file(size_t size) {
int fd = create_shm_file();
int ret;
if (fd < 0)