<commit_before>#include <errno.h>
#include <string.h>
#include <locale.h>
#include <sys/stat.h>
#include <sys/param.h>

#include <libxml/xpath.h>

#include "config.h"

#include "xklavier_private.h"

#include "xklavier_private_xkb.h"

#ifdef XKB_HEADERS_PRESENT
#include <X11/extensions/XKBfile.h>
#include <X11/extensions/XKM.h>
#endif

// For "bad" X servers we hold our own copy
#define XML_CFG_FALLBACK_PATH ( DATA_DIR "/xfree86.xml" )

#define MULTIPLE_LAYOUTS_CHECK_PATH ( XKB_BASE "/symbols/pc/en_US" )

#define XK_XKB_KEYS
#include <X11/keysymdef.h>

#ifdef XKB_HEADERS_PRESENT
XkbRF_VarDefsRec _xklVarDefs;
static XkbRF_RulesPtr _xklRules;
static XkbComponentNamesRec componentNames;
#endif

static char *locale;

static char* _XklGetRulesSetName( void )
{
#ifdef XKB_HEADERS_PRESENT
  static char rulesSetName[_XKB_RF_NAMES_PROP_MAXLEN] = "";
  if ( !rulesSetName[0] )
  {
    char* rf = NULL;
    if( !XklGetNamesProp( _xklAtoms[XKB_RF_NAMES_PROP_ATOM], &rf, NULL ) || ( rf == NULL ) )
    {
      strncpy( rulesSetName, XKB_DEFAULT_RULESET, sizeof rulesSetName );
      XklDebug( 100, "Using default rules set: [%s]\n", rulesSetName );
      return rulesSetName;
    }
    strncpy( rulesSetName, rf, sizeof rulesSetName );
    free( rf );
  }
  XklDebug( 100, "Rules set: [%s]\n", rulesSetName );
  return rulesSetName;
#else
  return NULL;
#endif
}

#ifdef XKB_HEADERS_PRESENT
static XkbRF_RulesPtr _XklLoadRulesSet( void )
{
  char fileName[MAXPATHLEN] = "";
  char* rf = _XklGetRulesSetName();

  _xklRules = NULL;
  if( rf == NULL )
  {
    _xklLastErrorMsg = "Could not find the XKB rules set";
    return NULL;
  }

  locale = setlocale( LC_ALL, NULL );
  if( locale != NULL )
    locale = strdup( locale );

  snprintf( fileName, sizeof fileName, XKB_BASE "/rules/%s", rf );
  _xklRules = XkbRF_Load( fileName, locale, True, True );

  if( _xklRules == NULL )
  {
    _xklLastErrorMsg = "Could not load rules";
    return NULL;
  }
  return _xklRules;
}
#endif

static void _XklFreeRulesSet( void )
{
#ifdef XKB_HEADERS_PRESENT
  if ( _xklRules )
    XkbRF_Free( _xklRules, True );
#endif
}

Bool XklConfigLoadRegistry( void )
{
  struct stat statBuf;
  char fileName[MAXPATHLEN] = "";
  char* rf = _XklGetRulesSetName();

  if ( rf == NULL )
    return False;

  snprintf( fileName, sizeof fileName, XKB_BASE "/rules/%s.xml", rf );

  if( stat( fileName, &statBuf ) != 0 )
  {
    strncpy( fileName, XML_CFG_FALLBACK_PATH, sizeof fileName );
    fileName[ MAXPATHLEN - 1 ] = '\0';
  }

  return XklConfigLoadRegistryFromFile( fileName );
}

static Bool _XklConfigPrepareBeforeKbd( const XklConfigRecPtr data )
{
#ifdef XKB_HEADERS_PRESENT
  XkbRF_RulesPtr rulesPtr = _XklLoadRulesSet();

  memset( &_xklVarDefs, 0, sizeof( _xklVarDefs ) );

  if( !rulesPtr )
    return False;

  _xklVarDefs.model = ( char * ) data->model;

  if( data->layouts != NULL )
    _xklVarDefs.layout = _XklConfigRecMergeLayouts( data );

  if( data->variants != NULL )
    _xklVarDefs.variant = _XklConfigRecMergeVariants( data );

  if( data->options != NULL )
    _xklVarDefs.options = _XklConfigRecMergeOptions( data );

  if( !XkbRF_GetComponents( rulesPtr, &_xklVarDefs, &componentNames ) )
  {
    _xklLastErrorMsg = "Could not translate rules into components";
    return False;
  }
#endif
  return True;
}

static void _XklConfigCleanAfterKbd(  )
{
#ifdef XKB_HEADERS_PRESENT<commit_msg>dumping low-level components before calling XkbGetKeyboardByName<commit_after>#include <errno.h>
#include <string.h>
#include <locale.h>
#include <sys/stat.h>
#include <sys/param.h>

#include <libxml/xpath.h>

#include "config.h"

#include "xklavier_private.h"

#include "xklavier_private_xkb.h"

#ifdef XKB_HEADERS_PRESENT
#include <X11/extensions/XKBfile.h>
#include <X11/extensions/XKM.h>
#endif

// For "bad" X servers we hold our own copy
#define XML_CFG_FALLBACK_PATH ( DATA_DIR "/xfree86.xml" )

#define MULTIPLE_LAYOUTS_CHECK_PATH ( XKB_BASE "/symbols/pc/en_US" )

#define XK_XKB_KEYS
#include <X11/keysymdef.h>

#ifdef XKB_HEADERS_PRESENT
XkbRF_VarDefsRec _xklVarDefs;
static XkbRF_RulesPtr _xklRules;
static XkbComponentNamesRec componentNames;
#endif

static char *locale;

static char* _XklGetRulesSetName( void )
{
#ifdef XKB_HEADERS_PRESENT
  static char rulesSetName[_XKB_RF_NAMES_PROP_MAXLEN] = "";
  if ( !rulesSetName[0] )
  {
    char* rf = NULL;
    if( !XklGetNamesProp( _xklAtoms[XKB_RF_NAMES_PROP_ATOM], &rf, NULL ) || ( rf == NULL ) )
    {
      strncpy( rulesSetName, XKB_DEFAULT_RULESET, sizeof rulesSetName );
      XklDebug( 100, "Using default rules set: [%s]\n", rulesSetName );
      return rulesSetName;
    }
    strncpy( rulesSetName, rf, sizeof rulesSetName );
    free( rf );
  }
  XklDebug( 100, "Rules set: [%s]\n", rulesSetName );
  return rulesSetName;
#else
  return NULL;
#endif
}

#ifdef XKB_HEADERS_PRESENT
static XkbRF_RulesPtr _XklLoadRulesSet( void )
{
  char fileName[MAXPATHLEN] = "";
  char* rf = _XklGetRulesSetName();

  _xklRules = NULL;
  if( rf == NULL )
  {
    _xklLastErrorMsg = "Could not find the XKB rules set";
    return NULL;
  }

  locale = setlocale( LC_ALL, NULL );
  if( locale != NULL )
    locale = strdup( locale );

  snprintf( fileName, sizeof fileName, XKB_BASE "/rules/%s", rf );
  _xklRules = XkbRF_Load( fileName, locale, True, True );

  if( _xklRules == NULL )
  {
    _xklLastErrorMsg = "Could not load rules";
    return NULL;
  }
  return _xklRules;
}
#endif

static void _XklFreeRulesSet( void )
{
#ifdef XKB_HEADERS_PRESENT
  if ( _xklRules )
    XkbRF_Free( _xklRules, True );
#endif
}

Bool XklConfigLoadRegistry( void )
{
  struct stat statBuf;
  char fileName[MAXPATHLEN] = "";
  char* rf = _XklGetRulesSetName();

  if ( rf == NULL )
    return False;

  snprintf( fileName, sizeof fileName, XKB_BASE "/rules/%s.xml", rf );

  if( stat( fileName, &statBuf ) != 0 )
  {
    strncpy( fileName, XML_CFG_FALLBACK_PATH, sizeof fileName );
    fileName[ MAXPATHLEN - 1 ] = '\0';
  }

  return XklConfigLoadRegistryFromFile( fileName );
}

static Bool _XklConfigPrepareBeforeKbd( const XklConfigRecPtr data )
{
#ifdef XKB_HEADERS_PRESENT
  XkbRF_RulesPtr rulesPtr = _XklLoadRulesSet();

  memset( &_xklVarDefs, 0, sizeof( _xklVarDefs ) );

  if( !rulesPtr )
    return False;

  _xklVarDefs.model = ( char * ) data->model;

  if( data->layouts != NULL )
    _xklVarDefs.layout = _XklConfigRecMergeLayouts( data );

  if( data->variants != NULL )
    _xklVarDefs.variant = _XklConfigRecMergeVariants( data );

  if( data->options != NULL )
    _xklVarDefs.options = _XklConfigRecMergeOptions( data );

  if( !XkbRF_GetComponents( rulesPtr, &_xklVarDefs, &componentNames ) )
  {
    _xklLastErrorMsg = "Could not translate rules into components";
    return False;
  }

  if ( _xklDebugLevel >= 200 )
  {
    XklDebug( 200, "keymap: %s\n", componentNames.keymap );
    XklDebug( 200, "keycodes: %s\n", componentNames.keycodes );
    XklDebug( 200, "compat: %s\n", componentNames.compat );
    XklDebug( 200, "types: %s\n", componentNames.types );
    XklDebug( 200, "symbols: %s\n", componentNames.symbols );
    XklDebug( 200, "geometry: %s\n", componentNames.geometry );
  }
#endif
  return True;
}

static void _XklConfigCleanAfterKbd(  )
{
#ifdef XKB_HEADERS_PRESENT
{'fraction_non_alphanumeric': 0.11997790970592297, 'fraction_numerical': 0.008145795940908463, 'mean_word_length': 4.101408450704225, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1059313', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2772, 'n_words': 621}