<commit_before>#include "window.h"

#include "keyboard.h"
#include "list.h"

#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>

#include <wayland-client.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GL/gl.h>
#include "xdg-shell-client-protocol.h"

struct imv_window {
  struct wl_display    *wl_display;
  struct wl_registry   *wl_registry;
  struct wl_compositor *wl_compositor;
  struct wl_surface    *wl_surface;
  struct xdg_wm_base   *wl_xdg;
  struct xdg_surface   *wl_xdg_surface;
  struct xdg_toplevel  *wl_xdg_toplevel;
  struct wl_seat       *wl_seat;
  struct wl_keyboard   *wl_keyboard;
  struct wl_pointer    *wl_pointer;
  EGLDisplay           egl_display;
  EGLContext           egl_context;
  EGLSurface           egl_surface;
  struct wl_egl_window *egl_window;

  bool xdg_configured;

  struct imv_keyboard *keyboard;
  struct list *wl_outputs;

  int display_fd;
  int pipe_fds[2];

  timer_t timer_id;
  int repeat_scancode; /* scancode of key to repeat */
  int repeat_delay; /* time before repeat in ms */
  int repeat_interval; /* time between repeats in ms */

  int width;
  int height;
  bool fullscreen;
  int scale;

  struct {
    struct {
      double last;
      double current;
    } x;
    struct {
      double last;
      double current;
    } y;
    struct {
      bool last;
      bool current;
    } mouse1;
    struct {
      double dx;
      double dy;
    } scroll;
  } pointer;
};

struct output_data {
  struct wl_output *wl_output;
  int scale;
  int pending_scale;
  bool contains_window;
};

static void set_nonblocking(int fd)
{
  int flags = fcntl(fd, F_GETFL);
  assert(flags != -1);
  flags |= O_NONBLOCK;
  int rc = fcntl(fd, F_SETFL, flags);
  assert(rc != -1);
}

static void handle_ping_xdg_wm_base(void *data, struct xdg_wm_base *xdg,
    uint32_t serial)
{
  (void)data;
  xdg_wm_base_pong(xdg, serial);
}

static const struct xdg_wm_base_listener shell_listener_xdg = {
  .ping = handle_ping_xdg_wm_base
};

static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
    uint32_t format, int32_t fd, uint32_t size)
{
  (void)keyboard;
  (void)format;
  struct imv_window *window = data;
  char *src = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  imv_keyboard_set_keymap(window->keyboard, src);
  munmap(src, size);
  close(fd);
}

static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
    uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
{
  (void)data;
  (void)keyboard;
  (void)serial;
  (void)surface;
  (void)keys;
}

static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
    uint32_t serial, struct wl_surface *surface)
{
  (void)data;
  (void)keyboard;
  (void)serial;
  (void)surface;
}<commit_msg>Use private mapping for memfd with keymap.<commit_after>#include "window.h"

#include "keyboard.h"
#include "list.h"

#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>

#include <wayland-client.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GL/gl.h>
#include "xdg-shell-client-protocol.h"

struct imv_window {
  struct wl_display    *wl_display;
  struct wl_registry   *wl_registry;
  struct wl_compositor *wl_compositor;
  struct wl_surface    *wl_surface;
  struct xdg_wm_base   *wl_xdg;
  struct xdg_surface   *wl_xdg_surface;
  struct xdg_toplevel  *wl_xdg_toplevel;
  struct wl_seat       *wl_seat;
  struct wl_keyboard   *wl_keyboard;
  struct wl_pointer    *wl_pointer;
  EGLDisplay           egl_display;
  EGLContext           egl_context;
  EGLSurface           egl_surface;
  struct wl_egl_window *egl_window;

  bool xdg_configured;

  struct imv_keyboard *keyboard;
  struct list *wl_outputs;

  int display_fd;
  int pipe_fds[2];

  timer_t timer_id;
  int repeat_scancode; /* scancode of key to repeat */
  int repeat_delay; /* time before repeat in ms */
  int repeat_interval; /* time between repeats in ms */

  int width;
  int height;
  bool fullscreen;
  int scale;

  struct {
    struct {
      double last;
      double current;
    } x;
    struct {
      double last;
      double current;
    } y;
    struct {
      bool last;
      bool current;
    } mouse1;
    struct {
      double dx;
      double dy;
    } scroll;
  } pointer;
};

struct output_data {
  struct wl_output *wl_output;
  int scale;
  int pending_scale;
  bool contains_window;
};

static void set_nonblocking(int fd)
{
  int flags = fcntl(fd, F_GETFL);
  assert(flags != -1);
  flags |= O_NONBLOCK;
  int rc = fcntl(fd, F_SETFL, flags);
  assert(rc != -1);
}

static void handle_ping_xdg_wm_base(void *data, struct xdg_wm_base *xdg,
    uint32_t serial)
{
  (void)data;
  xdg_wm_base_pong(xdg, serial);
}

static const struct xdg_wm_base_listener shell_listener_xdg = {
  .ping = handle_ping_xdg_wm_base
};

static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
    uint32_t format, int32_t fd, uint32_t size)
{
  (void)keyboard;
  (void)format;
  struct imv_window *window = data;
  char *src = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);

  if (src != MAP_FAILED) {
    imv_keyboard_set_keymap(window->keyboard, src);
    munmap(src, size);
  }
  close(fd);
}

static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
    uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
{
  (void)data;
  (void)keyboard;
  (void)serial;
  (void)surface;
  (void)keys;
}

static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
    uint32_t serial, struct wl_surface *surface)
{
  (void)data;
  (void)keyboard;
  (void)serial;
  (void)surface;
}
{'fraction_non_alphanumeric': 0.10708087469628601, 'fraction_numerical': 0.005900728913571677, 'mean_word_length': 3.606714628297362, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '58930', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2216, 'n_words': 527}