<commit_before>#include "os-compatibility.h"
#include <string.h>
#include <assert.h>
#include <wayland-server.h>
#include <chck/string/string.h>
#include "internal.h"
#include "keymap.h"

const char *WLC_MOD_NAMES[WLC_MOD_LAST] = {
   XKB_MOD_NAME_SHIFT,
   XKB_MOD_NAME_CAPS,
   XKB_MOD_NAME_CTRL,
   XKB_MOD_NAME_ALT,
   "Mod2",
   "Mod3",
   XKB_MOD_NAME_LOGO,
   "Mod5",
};

const char *WLC_LED_NAMES[WLC_LED_LAST] = {
   XKB_LED_NAME_NUM,
   XKB_LED_NAME_CAPS,
   XKB_LED_NAME_SCROLL
};

WLC_PURE uint32_t
wlc_keymap_get_mod_mask(struct wlc_keymap *keymap, uint32_t in)
{
   assert(keymap);

   uint32_t mods = 0;
   for (uint32_t i = 0; i < WLC_MOD_LAST; ++i) {
      if (keymap->mods[i] != XKB_MOD_INVALID && (in & (1 << keymap->mods[i])))
         mods |= (1 << i);
   }

   return mods;
}

uint32_t
wlc_keymap_get_led_mask(struct wlc_keymap *keymap, struct xkb_state *xkb)
{
   assert(keymap && xkb);

   uint32_t leds = 0;
   for (uint32_t i = 0; i < WLC_LED_LAST; ++i) {
      if (xkb_state_led_index_is_active(xkb, keymap->leds[i]))
         leds |= (1 << i);
   }

   return leds;
}

void
wlc_keymap_release(struct wlc_keymap *keymap)
{
   if (!keymap)
      return;

   if (keymap->keymap)
      xkb_map_unref(keymap->keymap);

   if (keymap->area)
      munmap(keymap->area, keymap->size);

   if (keymap->fd >= 0)
      close(keymap->fd);

   *keymap = (struct wlc_keymap){0};
}

bool
wlc_keymap(struct wlc_keymap *keymap, const struct xkb_rule_names *names, enum xkb_keymap_compile_flags flags)
{
   assert(keymap);
   memset(keymap, 0, sizeof(struct wlc_keymap));

   char *keymap_str = NULL;

   struct xkb_context *context;
   if (!(context = xkb_context_new(XKB_CONTEXT_NO_FLAGS)))<commit_msg>keymap: Invalidate fd on release<commit_after>#include "os-compatibility.h"
#include <string.h>
#include <assert.h>
#include <wayland-server.h>
#include <chck/string/string.h>
#include "internal.h"
#include "keymap.h"

const char *WLC_MOD_NAMES[WLC_MOD_LAST] = {
   XKB_MOD_NAME_SHIFT,
   XKB_MOD_NAME_CAPS,
   XKB_MOD_NAME_CTRL,
   XKB_MOD_NAME_ALT,
   "Mod2",
   "Mod3",
   XKB_MOD_NAME_LOGO,
   "Mod5",
};

const char *WLC_LED_NAMES[WLC_LED_LAST] = {
   XKB_LED_NAME_NUM,
   XKB_LED_NAME_CAPS,
   XKB_LED_NAME_SCROLL
};

WLC_PURE uint32_t
wlc_keymap_get_mod_mask(struct wlc_keymap *keymap, uint32_t in)
{
   assert(keymap);

   uint32_t mods = 0;
   for (uint32_t i = 0; i < WLC_MOD_LAST; ++i) {
      if (keymap->mods[i] != XKB_MOD_INVALID && (in & (1 << keymap->mods[i])))
         mods |= (1 << i);
   }

   return mods;
}

uint32_t
wlc_keymap_get_led_mask(struct wlc_keymap *keymap, struct xkb_state *xkb)
{
   assert(keymap && xkb);

   uint32_t leds = 0;
   for (uint32_t i = 0; i < WLC_LED_LAST; ++i) {
      if (xkb_state_led_index_is_active(xkb, keymap->leds[i]))
         leds |= (1 << i);
   }

   return leds;
}

void
wlc_keymap_release(struct wlc_keymap *keymap)
{
   if (!keymap)
      return;

   if (keymap->keymap)
      xkb_map_unref(keymap->keymap);

   if (keymap->area)
      munmap(keymap->area, keymap->size);

   if (keymap->fd >= 0)
      close(keymap->fd);

   *keymap = (struct wlc_keymap){0};
   keymap->fd = -1;
}

bool
wlc_keymap(struct wlc_keymap *keymap, const struct xkb_rule_names *names, enum xkb_keymap_compile_flags flags)
{
   assert(keymap);
   memset(keymap, 0, sizeof(struct wlc_keymap));

   char *keymap_str = NULL;

   struct xkb_context *context;
   if (!(context = xkb_context_new(XKB_CONTEXT_NO_FLAGS)))
{'fraction_non_alphanumeric': 0.1367176233054514, 'fraction_numerical': 0.015863859244303433, 'mean_word_length': 3.8368200836820083, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 34, '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': '6860323', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1583, 'n_words': 271}