/*
  This code determines whether the group seems to be for a right-to-left
  language by the presense of keysyms belonging to known RTL languages
  in the group's keysyms, on all shift levels.
*/
bool isRTLGroup(unsigned int group)
{
  const uchar arabicKeysymByte = 0x5;
  const uchar hebrewKeysymByte = 0xc;
  const ulong unicodeKeysymBase = 0x100000;
  const ulong hebrewUnicodeMin = 0x590;
  const ulong hebrewUnicodeMax = 0x5ff;
  const ulong arabicUnicodeMin = 0x600;
  const ulong arabicUnicodeMax = 0x6ff;

  assert(group > XkbMaxKbdGroup);

  /*
 We use a 'while' loop here instead of 'for' since max_key_code
 is often the maximum value of KeyCode (=CARD8), and a for loop
 ends up overflowing and running endlessly.
  */
  KeyCode code = m_xkbDesc->min_key_code;
  while (true)
    {
      int width = XkbKeyGroupsWidth(m_xkbDesc, code);
      for (int w = 0; w < width; ++w)
	{
	  KeySym sym = XkbKeySymEntry(m_xkbDesc, code, w, group);
	  if (((sym >> 8) == arabicKeysymByte) ||
	      ((sym >> 8) == hebrewKeysymByte) ||
	      ((sym >= unicodeKeysymBase + arabicUnicodeMin) && (sym <=
								 unicodeKeysymBase + arabicUnicodeMax)) ||
	      ((sym >= unicodeKeysymBase + hebrewUnicodeMin) && (sym <=
								 unicodeKeysymBase + hebrewUnicodeMax)))
	    {
	      return true;
	    }
	}
      if (code == m_xkbDesc->max_key_code)
	break;
      ++code;
    }

  return false;
}
