<commit_before>
/* See LICENSE file for license details. */
#include "config.h"

#define _XOPEN_SOURCE 500
#if HAVE_SHADOW_H
#include <shadow.h>
#endif

#include <ctype.h>
#include <errno.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <xcb/xcb.h>
#include <xcb/xcb_keysyms.h>
#include <xcb/xcb_image.h>

#if HAVE_BSD_AUTH
#include <login_cap.h>
#include <bsd_auth.h>
#endif

static uint8_t pixmap_data[1] = { 0 };

struct screen_t {
    xcb_connection_t* c;
    xcb_screen_t* xcb;
    xcb_window_t win;
    xcb_gcontext_t fg;
    xcb_gcontext_t bg;
    xcb_font_t font;
    uint32_t text_height;
    uint32_t text_width;
    struct screen_t* next;
};

static void
die(const char *errstr, ...) {
	va_list ap;

	va_start(ap, errstr);
	vfprintf(stderr, errstr, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
}

#ifndef HAVE_BSD_AUTH
static const char *
getpw(void) { /* only run as root */
	const char *rval;
	struct passwd *pw;

	pw = getpwuid(getuid());
	if(!pw)
		die("slock: cannot retrieve password entry (make sure to suid or sgid slock)");
	endpwent();
	rval =  pw->pw_passwd;

#if HAVE_SHADOW_H
	if (strlen(rval) >= 1) { /* kludge, assumes pw placeholder entry has len >= 1 */
		struct spwd *sp;
		sp = getspnam(getenv("USER"));
		if(!sp)
			die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
		endspent();
		rval = sp->sp_pwdp;
	}
#endif

	/* drop privileges */
	if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
		die("slock: cannot drop privileges");
	return rval;
}
#endif

static void grab_key(struct screen_t* scr)
{
    xcb_grab_keyboard_cookie_t cookie;
    xcb_grab_keyboard_reply_t* reply;

    cookie = xcb_grab_keyboard(scr->c, 1, scr->win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC);
    reply  = xcb_grab_keyboard_reply(scr->c, cookie, NULL);
    free(reply);
}

static void ungrab_key(struct screen_t* scr)
{
    xcb_ungrab_keyboard(scr->c, XCB_CURRENT_TIME);
    xcb_ungrab_pointer (scr->c, XCB_CURRENT_TIME);
}

static xcb_font_t load_font(xcb_connection_t* c)
{
    static xcb_font_t font = 0;
    if(font > 0)
        return font;
    font = xcb_generate_id(c);
    xcb_open_font(c, font, strlen(FONT), FONT);
    return font;
}

static void load_gcs(struct screen_t* scr)
{
    xcb_connection_t* c = scr->c;
    xcb_gcontext_t gc;
    uint32_t mask;
    uint32_t values[3];
    char* word = DISPLAYED_MSG;
    xcb_char2b_t xcbword[strlen(DISPLAYED_MSG)];
    xcb_query_text_extents_reply_t* reply;
    xcb_query_text_extents_cookie_t cookie;
    uint32_t i;

    /* FG */
    gc = xcb_generate_id(c);
    mask = XCB_GC_FOREGROUND
        | XCB_GC_BACKGROUND
        | XCB_GC_FONT;
    values[0] = scr->xcb->white_pixel;
    values[1] = scr->xcb->black_pixel;
    values[2] = load_font(c);
    xcb_create_gc(c, gc, scr->xcb->root, mask, values);
    scr->fg = gc;

    /* BG */
    gc = xcb_generate_id(c);
    xcb_create_gc(c, gc, scr->xcb->root, mask, values);
    scr->bg = gc;

    /* Font */
    scr->font = values[2];
    for(i = 0; i < strlen(word); ++i) {
        xcbword[i].byte1 = word[i];
        xcbword[i].byte2 = 0;
    }
    cookie = xcb_query_text_extents(c, scr->font, strlen(word), xcbword);
    reply  = xcb_query_text_extents_reply(c, cookie, NULL);
    scr->text_width  = reply->overall_width;
    scr->text_height = reply->overall_ascent + reply->overall_descent;
    free(reply);
}

static void close_gcs(struct screen_t* scr)
{
    /* Nothing to do. */
}

static void open_window(struct screen_t* scr)
{
    xcb_window_t win;
    xcb_pixmap_t pixmap;
    xcb_cursor_t cursor;
    xcb_void_cookie_t cookie_win;
    xcb_void_cookie_t cookie_map;
    xcb_void_cookie_t cookie_cursor;
    xcb_generic_error_t* error;
    uint32_t mask;
    uint32_t values[4];

    /* Create an empty cursor. */
    pixmap = xcb_create_pixmap_from_bitmap_data(scr->c, scr->win, pixmap_data,
            1, 1, 1,
            scr->xcb->black_pixel,
            scr->xcb->white_pixel,
            NULL);
    cursor = xcb_generate_id(scr->c);
    cookie_cursor = xcb_create_cursor_checked(scr->c, cursor, pixmap, pixmap,
            scr->xcb->black_pixel, scr->xcb->black_pixel, scr->xcb->black_pixel,
            scr->xcb->black_pixel, scr->xcb->black_pixel, scr->xcb->black_pixel,
            0, 0);

    /* Create the window. */
    win = xcb_generate_id(scr->c);
    mask = XCB_CW_BACK_PIXEL
        | XCB_CW_OVERRIDE_REDIRECT
        | XCB_CW_EVENT_MASK
        | XCB_CW_CURSOR;
    values[0] = scr->xcb->black_pixel;
    values[1] = 1;
    values[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
    values[3] = cursor;
    cookie_win = xcb_create_window_checked(
            scr->c,
            XCB_COPY_FROM_PARENT,
            win,
            scr->xcb->root,
            0, 0,
            scr->xcb->width_in_pixels,
            scr->xcb->height_in_pixels,
            1,
            XCB_WINDOW_CLASS_INPUT_OUTPUT,
            scr->xcb->root_visual,
            mask, values
            );

    /* Free the pixmap and the cursor. */
    xcb_free_pixmap(scr->c, pixmap);
    xcb_free_cursor(scr->c, cursor);

    scr->win = win;
    cookie_map = xcb_map_window_checked(scr->c, win);

    /* Check errors. */
    error = xcb_request_check(scr->c, cookie_cursor);
    if(error)
        die("Couldn't create cursor : %d\n", error->error_code);<commit_msg>Removed application of the cursor to the window.<commit_after>
/* See LICENSE file for license details. */
#include "config.h"

#define _XOPEN_SOURCE 500
#if HAVE_SHADOW_H
#include <shadow.h>
#endif

#include <ctype.h>
#include <errno.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <xcb/xcb.h>
#include <xcb/xcb_keysyms.h>
#include <xcb/xcb_image.h>

#if HAVE_BSD_AUTH
#include <login_cap.h>
#include <bsd_auth.h>
#endif

static uint8_t pixmap_data[1] = { 0 };

struct screen_t {
    xcb_connection_t* c;
    xcb_screen_t* xcb;
    xcb_window_t win;
    xcb_gcontext_t fg;
    xcb_gcontext_t bg;
    xcb_font_t font;
    uint32_t text_height;
    uint32_t text_width;
    struct screen_t* next;
};

static void
die(const char *errstr, ...) {
	va_list ap;

	va_start(ap, errstr);
	vfprintf(stderr, errstr, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
}

#ifndef HAVE_BSD_AUTH
static const char *
getpw(void) { /* only run as root */
	const char *rval;
	struct passwd *pw;

	pw = getpwuid(getuid());
	if(!pw)
		die("slock: cannot retrieve password entry (make sure to suid or sgid slock)");
	endpwent();
	rval =  pw->pw_passwd;

#if HAVE_SHADOW_H
	if (strlen(rval) >= 1) { /* kludge, assumes pw placeholder entry has len >= 1 */
		struct spwd *sp;
		sp = getspnam(getenv("USER"));
		if(!sp)
			die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
		endspent();
		rval = sp->sp_pwdp;
	}
#endif

	/* drop privileges */
	if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
		die("slock: cannot drop privileges");
	return rval;
}
#endif

static void grab_key(struct screen_t* scr)
{
    xcb_grab_keyboard_cookie_t cookie;
    xcb_grab_keyboard_reply_t* reply;

    cookie = xcb_grab_keyboard(scr->c, 1, scr->win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC);
    reply  = xcb_grab_keyboard_reply(scr->c, cookie, NULL);
    free(reply);
}

static void ungrab_key(struct screen_t* scr)
{
    xcb_ungrab_keyboard(scr->c, XCB_CURRENT_TIME);
    xcb_ungrab_pointer (scr->c, XCB_CURRENT_TIME);
}

static xcb_font_t load_font(xcb_connection_t* c)
{
    static xcb_font_t font = 0;
    if(font > 0)
        return font;
    font = xcb_generate_id(c);
    xcb_open_font(c, font, strlen(FONT), FONT);
    return font;
}

static void load_gcs(struct screen_t* scr)
{
    xcb_connection_t* c = scr->c;
    xcb_gcontext_t gc;
    uint32_t mask;
    uint32_t values[3];
    char* word = DISPLAYED_MSG;
    xcb_char2b_t xcbword[strlen(DISPLAYED_MSG)];
    xcb_query_text_extents_reply_t* reply;
    xcb_query_text_extents_cookie_t cookie;
    uint32_t i;

    /* FG */
    gc = xcb_generate_id(c);
    mask = XCB_GC_FOREGROUND
        | XCB_GC_BACKGROUND
        | XCB_GC_FONT;
    values[0] = scr->xcb->white_pixel;
    values[1] = scr->xcb->black_pixel;
    values[2] = load_font(c);
    xcb_create_gc(c, gc, scr->xcb->root, mask, values);
    scr->fg = gc;

    /* BG */
    gc = xcb_generate_id(c);
    xcb_create_gc(c, gc, scr->xcb->root, mask, values);
    scr->bg = gc;

    /* Font */
    scr->font = values[2];
    for(i = 0; i < strlen(word); ++i) {
        xcbword[i].byte1 = word[i];
        xcbword[i].byte2 = 0;
    }
    cookie = xcb_query_text_extents(c, scr->font, strlen(word), xcbword);
    reply  = xcb_query_text_extents_reply(c, cookie, NULL);
    scr->text_width  = reply->overall_width;
    scr->text_height = reply->overall_ascent + reply->overall_descent;
    free(reply);
}

static void close_gcs(struct screen_t* scr)
{
    /* Nothing to do. */
}

static void open_window(struct screen_t* scr)
{
    xcb_window_t win;
    xcb_pixmap_t pixmap;
    xcb_cursor_t cursor;
    xcb_void_cookie_t cookie_win;
    xcb_void_cookie_t cookie_map;
    xcb_void_cookie_t cookie_cursor;
    xcb_generic_error_t* error;
    uint32_t mask;
    uint32_t values[4];

    /* Create an empty cursor. */
    pixmap = xcb_create_pixmap_from_bitmap_data(scr->c, scr->win, pixmap_data,
            1, 1, 1,
            scr->xcb->black_pixel,
            scr->xcb->white_pixel,
            NULL);
    cursor = xcb_generate_id(scr->c);
    cookie_cursor = xcb_create_cursor_checked(scr->c, cursor, pixmap, pixmap,
            scr->xcb->black_pixel, scr->xcb->black_pixel, scr->xcb->black_pixel,
            scr->xcb->black_pixel, scr->xcb->black_pixel, scr->xcb->black_pixel,
            0, 0);

    /* Create the window. */
    win = xcb_generate_id(scr->c);
    mask = XCB_CW_BACK_PIXEL
        | XCB_CW_OVERRIDE_REDIRECT
        | XCB_CW_EVENT_MASK;
    values[0] = scr->xcb->black_pixel;
    values[1] = 1;
    values[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
    values[3] = cursor;
    cookie_win = xcb_create_window_checked(
            scr->c,
            XCB_COPY_FROM_PARENT,
            win,
            scr->xcb->root,
            0, 0,
            scr->xcb->width_in_pixels,
            scr->xcb->height_in_pixels,
            1,
            XCB_WINDOW_CLASS_INPUT_OUTPUT,
            scr->xcb->root_visual,
            mask, values
            );

    /* Free the pixmap and the cursor. */
    xcb_free_pixmap(scr->c, pixmap);
    xcb_free_cursor(scr->c, cursor);

    scr->win = win;
    cookie_map = xcb_map_window_checked(scr->c, win);

    /* Check errors. */
    error = xcb_request_check(scr->c, cookie_cursor);
    if(error)
        die("Couldn't create cursor : %d\n", error->error_code);
{'fraction_non_alphanumeric': 0.13028008393394763, 'fraction_numerical': 0.00930572028099626, 'mean_word_length': 3.085724934774506, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 173, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6660155', 'n_tokens_mistral': 4515, 'n_tokens_neox': 4353, 'n_words': 863}