xkeysymtounicodetablegen.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 06 Sep 2017 10:04:18 +0200
branchjv
changeset 8180 25149dfd68e0
parent 8038 c7cab63f9ec9
child 8420 76e39223f5ab
permissions -rwxr-xr-x
Build files: removed a bunch of make rules for long-dead unsupported systems ...in order to unify and simplify the build. If a need to support this ancient systems arose, these hacks may ni longer be needed (due to new versions of tools) or the hacks would have to be written again (better) or retrieved from SCM (worse). Time will show.

#!/usr/bin/env ruby
# 
#  Copyright (c) 2017-now Jan Vrany
#  
#  Permission is hereby granted, free of charge, to any person obtaining
#  a copy of this software and associated documentation files (the
#  "Software"), to deal in the Software without restriction, including
#  without limitation the rights to use, copy, modify, merge, publish,
#  distribute, sublicense, and/or sell copies of the Software, and to
#  permit persons to whom the Software is furnished to do so, subject to
#  the following conditions:
#  
#  The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#  
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#  MeERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

#
# A quick and dirty generator of character lookup table used
# by XKeysymToUnicode(). 
#
# Usage: 
#
#   ruby xkeysymtounicodetablegen.rb > xkeysymtounicodetable.h
#

KEYSYMDEF_H = '/usr/include/X11/keysymdef.h'
KEYSYMDEF_MATCHER = /^#define (XK_[A-Za-z_]+)+\ +0x([0-9A-Fa-f]+)  \/\* U\+([0-9A-Fa-f]+) (.+) \*\/$/
KEYSYM_DIRECT_LOW_NAME = "XK_space"

keysyms = []
keysym_direct_low = nil
keysym_direct_high = nil

class KeySym
  attr_accessor :x_code, :x_name, :u_code, :u_name

  def initialize(xcode, xname, ucode, uname) 
    @x_code = xcode
    @x_name = xname
    @u_code = ucode
    @u_name = uname
  end

  def to_s
    return "#define #{x_name} 0x#{x_code.to_s(16)} /* U+#{u_code.to_s(16)} #{u_name} */"
  end
end

File.open(KEYSYMDEF_H).each do | line |
  m = line.match(KEYSYMDEF_MATCHER)
  if m then
    x_name, x_code, u_code, u_name = m.captures
    keysym = KeySym.new(x_code.to_i(16), x_name, u_code.to_i(16), u_name)    
    if keysym.x_code != 0 then
      keysyms << keysym
      if keysym.x_name == KEYSYM_DIRECT_LOW_NAME then
        keysym_direct_low = keysym
        keysym_direct_high = keysym
      end
    end
  end
end

keysyms.sort! { | a, b | a.x_code <=> b.x_code }

begin
  i = keysyms.index(keysym_direct_low) + 1
  while (true)
    keysym = keysyms[i]
    i = i + 1

    if (keysym.x_code == keysym.u_code && keysym.x_code >= keysym_direct_high.x_code) then
      keysym_direct_high = keysym      
    else
      break
    end
  end
end

print <<-end
/* 
 * Automatically generated - do not edit! 
 */

/* 
 * Copyright (c) 2017-now Jan Vrany
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MeERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/* 
 * This header contain table definitions for XKeysymToUnicode() function. 
 *
 * Automatically generated by `xkeysymtounicodetablegen.rb` from `keysymdef.h`.
 */

#ifndef _XKEYSYMTOUNICODETABLE_H_
#define _XKEYSYMTOUNICODETABLE_H_

#include <stdint.h>
#include <X11/X.h>
#include <X11/keysym.h>

#define XKEYSYMTOUNICODE_DIRECT_LO 0x#{keysym_direct_low.x_code.to_s(16)} /* #{keysym_direct_low.x_name}, #{keysym_direct_low.u_name} */
#define XKEYSYMTOUNICODE_DIRECT_HI 0x#{keysym_direct_high.x_code.to_s(16)} /* #{keysym_direct_high.x_name}, #{keysym_direct_high.u_name} */

typedef struct _xkeysymtounicode_table_entry {
  KeySym keysym;
  uint16_t codepoint;
} xkeysymtounicode_table_entry_t;

static const xkeysymtounicode_table_entry_t 
xkeysymtounicode_table[] = {
end

keysyms_size = 0
keysyms.each do | keysym | 
  if keysym.x_code < keysym_direct_low.x_code || keysym.x_code > keysym_direct_high.x_code
    keysyms_size = keysyms_size + 1 
    print "    { 0x#{keysym.x_code.to_s(16)}, 0x#{keysym.u_code.to_s(16)} }, /* #{keysym.x_name}, #{keysym.u_name} */\n"
  end
end
print "    { 0, 0 } /* -- terminator -- */\n"
print "};\n"

print <<-end

static const int 
xkeysymtounicode_table_size = #{keysyms_size};

#endif // _XKEYSYMTOUNICODETABLE_H_
end