From e015694229ae825f0b7cac840ccfaeb7120626c7 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Thu, 23 May 2024 10:05:22 +0200 Subject: [PATCH] clang-format: Use InsertBraces --- .clang-format | 11 ++-- dpi.c | 3 +- i3lock.c | 129 +++++++++++++++++++++++++++++---------------- randr.c | 6 ++- unlock_indicator.c | 12 +++-- xcb.c | 3 +- 6 files changed, 106 insertions(+), 58 deletions(-) diff --git a/.clang-format b/.clang-format index 6e49d835..2b8e0b3d 100644 --- a/.clang-format +++ b/.clang-format @@ -1,11 +1,12 @@ -BasedOnStyle: google +AllowShortBlocksOnASingleLine: false +AllowShortFunctionsOnASingleLine: None AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false -AllowShortFunctionsOnASingleLine: None -AllowShortBlocksOnASingleLine: false AlwaysBreakBeforeMultilineStrings: false +BasedOnStyle: google +ColumnLimit: 0 IndentWidth: 4 +InsertBraces: true PointerBindsToType: false -ColumnLimit: 0 -SpaceBeforeParens: ControlStatements SortIncludes: false +SpaceBeforeParens: ControlStatements diff --git a/dpi.c b/dpi.c index 3cb08eec..78a8e1df 100644 --- a/dpi.c +++ b/dpi.c @@ -103,7 +103,8 @@ int logical_px(const int logical) { * systems to 96 dpi in order to get the behavior they expect/are used to, * but since we can easily detect this case in code, let’s do it for them. */ - if ((dpi / 96.0) < 1.25) + if ((dpi / 96.0) < 1.25) { return logical; + } return ceil((dpi / 96.0) * logical); } diff --git a/i3lock.c b/i3lock.c index fc87ad53..9cae3767 100644 --- a/i3lock.c +++ b/i3lock.c @@ -187,12 +187,13 @@ static void clear_password_memory(void) { /* A volatile pointer to the password buffer to prevent the compiler from * optimizing this out. */ volatile char *vpassword = password; - for (size_t c = 0; c < sizeof(password); c++) + for (size_t c = 0; c < sizeof(password); c++) { /* We store a non-random pattern which consists of the (irrelevant) * index plus (!) the value of the beep variable. This prevents the * compiler from optimizing the calls away, since the value of 'beep' * is not known at compile-time. */ vpassword[c] = c + (int)beep; + } #endif } @@ -283,8 +284,9 @@ static void input_done(void) { #ifdef __OpenBSD__ struct passwd *pw; - if (!(pw = getpwuid(getuid()))) + if (!(pw = getpwuid(getuid()))) { errx(1, "unknown uid %u.", getuid()); + } if (auth_userokay(pw->pw_name, NULL, NULL, password) != 0) { DEBUG("successfully authenticated\n"); @@ -310,14 +312,16 @@ static void input_done(void) { } #endif - if (debug_mode) + if (debug_mode) { fprintf(stderr, "Authentication failure\n"); + } auth_state = STATE_AUTH_WRONG; failed_attempts += 1; clear_input(); - if (unlock_indicator) + if (unlock_indicator) { redraw_screen(); + } /* Clear this state after 2 seconds (unless the user enters another * password during that time). */ @@ -341,11 +345,13 @@ static void redraw_timeout(EV_P_ ev_timer *w, int revents) { } static bool skip_without_validation(void) { - if (input_position != 0) + if (input_position != 0) { return false; + } - if (skip_repeated_empty_password || ignore_empty_password) + if (skip_repeated_empty_password || ignore_empty_password) { return true; + } return false; } @@ -398,8 +404,9 @@ static void handle_key_press(xcb_key_press_event_t *event) { case XKB_KEY_Return: case XKB_KEY_KP_Enter: case XKB_KEY_XF86ScreenSaver: - if ((ksym == XKB_KEY_j || ksym == XKB_KEY_m) && !ctrl) + if ((ksym == XKB_KEY_j || ksym == XKB_KEY_m) && !ctrl) { break; + } if (auth_state == STATE_AUTH_WRONG) { retry_verification = true; @@ -431,8 +438,9 @@ static void handle_key_press(xcb_key_press_event_t *event) { DEBUG("C-u pressed\n"); clear_input(); /* Also hide the unlock indicator */ - if (unlock_indicator) + if (unlock_indicator) { clear_indicator(); + } return; } break; @@ -447,8 +455,9 @@ static void handle_key_press(xcb_key_press_event_t *event) { case XKB_KEY_h: case XKB_KEY_BackSpace: - if (ksym == XKB_KEY_h && !ctrl) + if (ksym == XKB_KEY_h && !ctrl) { break; + } if (input_position == 0) { START_TIMER(clear_indicator_timeout, 1.0, clear_indicator_cb); @@ -470,8 +479,9 @@ static void handle_key_press(xcb_key_press_event_t *event) { return; } - if ((input_position + 8) >= (int)sizeof(password)) + if ((input_position + 8) >= (int)sizeof(password)) { return; + } #if 0 /* FIXME: handle all of these? */ @@ -484,8 +494,9 @@ static void handle_key_press(xcb_key_press_event_t *event) { printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym)); #endif - if (n < 2) + if (n < 2) { return; + } /* store it in the password array as UTF-8 */ memcpy(password + input_position, buffer, n - 1); @@ -546,8 +557,9 @@ static void process_xkb_event(xcb_generic_event_t *gevent) { DEBUG("process_xkb_event for device %d\n", event->any.deviceID); - if (event->any.deviceID != xkb_x11_get_core_keyboard_device_id(conn)) + if (event->any.deviceID != xkb_x11_get_core_keyboard_device_id(conn)) { return; + } /* * XkbNewKkdNotify and XkbMapNotify together capture all sorts of keymap @@ -556,8 +568,9 @@ static void process_xkb_event(xcb_generic_event_t *gevent) { */ switch (event->any.xkbType) { case XCB_XKB_NEW_KEYBOARD_NOTIFY: - if (event->new_keyboard_notify.changed & XCB_XKB_NKN_DETAIL_KEYCODES) + if (event->new_keyboard_notify.changed & XCB_XKB_NKN_DETAIL_KEYCODES) { (void)load_keymap(); + } break; case XCB_XKB_MAP_NOTIFY: @@ -586,8 +599,9 @@ static void handle_screen_resize(void) { xcb_get_geometry_cookie_t geomc; xcb_get_geometry_reply_t *geom; geomc = xcb_get_geometry(conn, screen->root); - if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) + if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) { return; + } if (last_resolution[0] == geom->width && last_resolution[1] == geom->height) { @@ -615,8 +629,9 @@ static ssize_t read_raw_image_native(uint32_t *dest, FILE *src, size_t width, si for (size_t y = 0; y < height; y++) { size_t n = fread(&dest[y * pixstride], 1, width * 4, src); count += n; - if (n < (size_t)(width * 4)) + if (n < (size_t)(width * 4)) { break; + } } return count; @@ -632,15 +647,17 @@ struct raw_pixel_format { static ssize_t read_raw_image_fmt(uint32_t *dest, FILE *src, size_t width, size_t height, int pixstride, struct raw_pixel_format fmt) { unsigned char *buf = malloc(width * fmt.bpp); - if (buf == NULL) + if (buf == NULL) { return -1; + } ssize_t count = 0; for (size_t y = 0; y < height; y++) { size_t n = fread(buf, 1, width * fmt.bpp, src); count += n; - if (n < (size_t)(width * fmt.bpp)) + if (n < (size_t)(width * fmt.bpp)) { break; + } for (size_t x = 0; x < width; ++x) { int idx = x * fmt.bpp; @@ -711,18 +728,19 @@ static cairo_surface_t *read_raw_image(const char *image_path, const char *image } else { const struct raw_pixel_format *fmt = NULL; - if (strcmp(pixfmt, "rgb") == 0) + if (strcmp(pixfmt, "rgb") == 0) { fmt = &raw_fmt_rgb; - else if (strcmp(pixfmt, "rgbx") == 0) + } else if (strcmp(pixfmt, "rgbx") == 0) { fmt = &raw_fmt_rgbx; - else if (strcmp(pixfmt, "xrgb") == 0) + } else if (strcmp(pixfmt, "xrgb") == 0) { fmt = &raw_fmt_xrgb; - else if (strcmp(pixfmt, "bgr") == 0) + } else if (strcmp(pixfmt, "bgr") == 0) { fmt = &raw_fmt_bgr; - else if (strcmp(pixfmt, "bgrx") == 0) + } else if (strcmp(pixfmt, "bgrx") == 0) { fmt = &raw_fmt_bgrx; - else if (strcmp(pixfmt, "xbgr") == 0) + } else if (strcmp(pixfmt, "xbgr") == 0) { fmt = &raw_fmt_xbgr; + } if (fmt == NULL) { fprintf(stderr, "Unknown raw pixel format: %s\n", pixfmt); @@ -793,8 +811,9 @@ static bool verify_png_image(const char *image_path) { */ static int conv_callback(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) { - if (num_msg == 0) + if (num_msg == 0) { return 1; + } /* PAM expects an array of responses, one for each message */ if ((*resp = calloc(num_msg, sizeof(struct pam_response))) == NULL) { @@ -804,8 +823,9 @@ static int conv_callback(int num_msg, const struct pam_message **msg, for (int c = 0; c < num_msg; c++) { if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF && - msg[c]->msg_style != PAM_PROMPT_ECHO_ON) + msg[c]->msg_style != PAM_PROMPT_ECHO_ON) { continue; + } /* return code is currently not used but should be set to zero */ resp[c]->resp_retcode = 0; @@ -860,15 +880,17 @@ static void maybe_close_sleep_lock_fd(void) { static void xcb_check_cb(EV_P_ ev_check *w, int revents) { xcb_generic_event_t *event; - if (xcb_connection_has_error(conn)) + if (xcb_connection_has_error(conn)) { errx(EXIT_FAILURE, "X11 connection broke, did your server terminate?"); + } while ((event = xcb_poll_for_event(conn)) != NULL) { if (event->response_type == 0) { xcb_generic_error_t *error = (xcb_generic_error_t *)event; - if (debug_mode) + if (debug_mode) { fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n", error->sequence, error->error_code); + } free(event); continue; } @@ -893,8 +915,9 @@ static void xcb_check_cb(EV_P_ ev_check *w, int revents) { dont_fork = true; /* In the parent process, we exit */ - if (fork() != 0) + if (fork() != 0) { exit(0); + } ev_loop_fork(EV_DEFAULT); } @@ -931,8 +954,9 @@ static void raise_loop(xcb_window_t window) { xcb_generic_event_t *event; int screens; - if (xcb_connection_has_error((conn = xcb_connect(NULL, &screens))) > 0) + if (xcb_connection_has_error((conn = xcb_connect(NULL, &screens))) > 0) { errx(EXIT_FAILURE, "Cannot open display"); + } /* We need to know about the window being obscured or getting destroyed. */ xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, @@ -959,13 +983,15 @@ static void raise_loop(xcb_window_t window) { break; case XCB_UNMAP_NOTIFY: DEBUG("UnmapNotify for 0x%08x\n", (((xcb_unmap_notify_event_t *)event)->window)); - if (((xcb_unmap_notify_event_t *)event)->window == window) + if (((xcb_unmap_notify_event_t *)event)->window == window) { exit(EXIT_SUCCESS); + } break; case XCB_DESTROY_NOTIFY: DEBUG("DestroyNotify for 0x%08x\n", (((xcb_destroy_notify_event_t *)event)->window)); - if (((xcb_destroy_notify_event_t *)event)->window == window) + if (((xcb_destroy_notify_event_t *)event)->window == window) { exit(EXIT_SUCCESS); + } break; default: DEBUG("Unhandled event type %d\n", type); @@ -1029,11 +1055,13 @@ int main(int argc, char *argv[]) { char *arg = optarg; /* Skip # if present */ - if (arg[0] == '#') + if (arg[0] == '#') { arg++; + } - if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1) + if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1) { errx(EXIT_FAILURE, "color is invalid, it must be given in 3-byte hexadecimal format: rrggbb"); + } break; } @@ -1059,10 +1087,11 @@ int main(int argc, char *argv[]) { ignore_empty_password = true; break; case 0: - if (strcmp(longopts[longoptind].name, "debug") == 0) + if (strcmp(longopts[longoptind].name, "debug") == 0) { debug_mode = true; - else if (strcmp(longopts[longoptind].name, "raw") == 0) + } else if (strcmp(longopts[longoptind].name, "raw") == 0) { image_raw_format = strdup(optarg); + } break; case 'f': show_failed_attempts = true; @@ -1095,11 +1124,13 @@ int main(int argc, char *argv[]) { #ifndef __OpenBSD__ /* Initialize PAM */ - if ((ret = pam_start("i3lock", username, &conv, &pam_handle)) != PAM_SUCCESS) + if ((ret = pam_start("i3lock", username, &conv, &pam_handle)) != PAM_SUCCESS) { errx(EXIT_FAILURE, "PAM: %s", pam_strerror(pam_handle, ret)); + } - if ((ret = pam_set_item(pam_handle, PAM_TTY, getenv("DISPLAY"))) != PAM_SUCCESS) + if ((ret = pam_set_item(pam_handle, PAM_TTY, getenv("DISPLAY"))) != PAM_SUCCESS) { errx(EXIT_FAILURE, "PAM: %s", pam_strerror(pam_handle, ret)); + } #endif /* Using mlock() as non-super-user seems only possible in Linux. @@ -1111,15 +1142,17 @@ int main(int argc, char *argv[]) { /* Lock the area where we store the password in memory, we don’t want it to * be swapped to disk. Since Linux 2.6.9, this does not require any * privileges, just enough bytes in the RLIMIT_MEMLOCK limit. */ - if (mlock(password, sizeof(password)) != 0) + if (mlock(password, sizeof(password)) != 0) { err(EXIT_FAILURE, "Could not lock page in memory, check RLIMIT_MEMLOCK"); + } #endif /* Double checking that connection is good and operatable with xcb */ int screennr; if ((conn = xcb_connect(NULL, &screennr)) == NULL || - xcb_connection_has_error(conn)) + xcb_connection_has_error(conn)) { errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?"); + } if (xkb_x11_setup_xkb_extension(conn, XKB_X11_MIN_MAJOR_XKB_VERSION, @@ -1128,8 +1161,9 @@ int main(int argc, char *argv[]) { NULL, NULL, &xkb_base_event, - &xkb_base_error) != 1) + &xkb_base_error) != 1) { errx(EXIT_FAILURE, "Could not setup XKB extension."); + } static const xcb_xkb_map_part_t required_map_parts = (XCB_XKB_MAP_PART_KEY_TYPES | @@ -1156,17 +1190,21 @@ int main(int argc, char *argv[]) { 0); /* When we cannot initially load the keymap, we better exit */ - if (!load_keymap()) + if (!load_keymap()) { errx(EXIT_FAILURE, "Could not load keymap"); + } const char *locale = getenv("LC_ALL"); - if (!locale || !*locale) + if (!locale || !*locale) { locale = getenv("LC_CTYPE"); - if (!locale || !*locale) + } + if (!locale || !*locale) { locale = getenv("LANG"); + } if (!locale || !*locale) { - if (debug_mode) + if (debug_mode) { fprintf(stderr, "Can't detect your locale, fallback to C\n"); + } locale = "C"; } @@ -1255,8 +1293,9 @@ int main(int argc, char *argv[]) { /* Initialize the libev event loop. */ main_loop = EV_DEFAULT; - if (main_loop == NULL) + if (main_loop == NULL) { errx(EXIT_FAILURE, "Could not initialize libev. Bad LIBEV_FLAGS?"); + } /* Explicitly call the screen redraw in case "locking…" message was displayed */ auth_state = STATE_AUTH_IDLE; diff --git a/randr.c b/randr.c index 4b1ea97d..ef84c49c 100644 --- a/randr.c +++ b/randr.c @@ -58,8 +58,9 @@ void randr_init(int *event_base, xcb_window_t root) { free(randr_version); - if (event_base != NULL) + if (event_base != NULL) { *event_base = extreply->first_event; + } xcb_randr_select_input(conn, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE | @@ -81,8 +82,9 @@ void _xinerama_init(void) { cookie = xcb_xinerama_is_active(conn); reply = xcb_xinerama_is_active_reply(conn, cookie, NULL); - if (!reply) + if (!reply) { return; + } if (!reply->state) { free(reply); diff --git a/unlock_indicator.c b/unlock_indicator.c index a50b52f3..27521616 100644 --- a/unlock_indicator.c +++ b/unlock_indicator.c @@ -145,12 +145,14 @@ static void check_modifier_keys(void) { num_mods = xkb_keymap_num_mods(xkb_keymap); for (idx = 0; idx < num_mods; idx++) { - if (!xkb_state_mod_index_is_active(xkb_state, idx, XKB_STATE_MODS_EFFECTIVE)) + if (!xkb_state_mod_index_is_active(xkb_state, idx, XKB_STATE_MODS_EFFECTIVE)) { continue; + } mod_name = xkb_keymap_mod_get_name(xkb_keymap, idx); - if (mod_name == NULL) + if (mod_name == NULL) { continue; + } /* Replace certain xkb names with nicer, human-readable ones. */ if (strcmp(mod_name, XKB_MOD_NAME_CAPS) == 0) { @@ -177,8 +179,9 @@ void draw_image(xcb_pixmap_t bg_pixmap, uint32_t *resolution) { DEBUG("scaling_factor is %.f, physical diameter is %d px\n", scaling_factor, button_diameter_physical); - if (!vistype) + if (!vistype) { vistype = get_root_visual_type(screen); + } /* Initialize cairo: Create one in-memory surface to render the unlock * indicator on, create one XCB surface to actually draw (one or more, @@ -453,7 +456,8 @@ void redraw_screen(void) { void clear_indicator(void) { if (input_position == 0) { unlock_state = STATE_STARTED; - } else + } else { unlock_state = STATE_KEY_PRESSED; + } redraw_screen(); } diff --git a/xcb.c b/xcb.c index 2867a473..f0a784f0 100644 --- a/xcb.c +++ b/xcb.c @@ -95,8 +95,9 @@ xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) { for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data); visual_iter.rem; xcb_visualtype_next(&visual_iter)) { - if (screen->root_visual != visual_iter.data->visual_id) + if (screen->root_visual != visual_iter.data->visual_id) { continue; + } visual_type = visual_iter.data; return visual_type;