Back to index

im-sdk  12.3.91
IMKeyUtils.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include "IMLog.hh"
00043 #include "IMKeyUtils.hh"
00044 
00045 /*******************************************************************************
00046                                  IMKeySpec
00047 *******************************************************************************/
00048 
00049 IMKeySpec::IMKeySpec(int x_keycode,
00050                    int x_keychar,
00051                    int x_modifier,
00052                    int x_timestamp)
00053 {
00054        keycode = x_keycode;
00055        keychar = x_keychar;
00056        modifier = x_modifier;
00057        timestamp = x_timestamp;
00058 }
00059 
00060 /*******************************************************************************
00061                  IMKeyParser (helper object to parse key symbols
00062 *******************************************************************************/
00063 IMKeyParser::IMKeyParser(const char *strings)
00064 {
00065        char *ptop, *pend;
00066        IMKeySpec *key;
00067        std::string s;
00068 
00069        ptop = (char *) strings;
00070        while (ptop) {
00071               if ((pend = strchr(ptop, ',')) != NULL) {
00072                      s.assign(ptop, pend - ptop);
00073                      ptop = pend + 1;
00074               } else {
00075                      s.assign(ptop);
00076                      ptop = NULL;
00077               }
00078                 parse_key(s);
00079        }
00080 }
00081 
00082 void
00083 IMKeyParser::parse_key(std::string& s)
00084 {
00085        std::string token, mtoken;
00086        int j, pos, size, mod = 0;
00087        static struct {
00088               const char *symbol;
00089               int modifier;
00090        } modifiers[] = {
00091               {"shift", 1}, {"sh", 1}, {"s", 1},
00092               {"control", 2}, {"ctrl", 2}, {"c", 2},
00093               {"alt", 4},
00094               {NULL, 0},
00095        };
00096 #include "keysyms.h"
00097 
00098        size = s.size();
00099        for (j = 0; j < size; j++) {
00100               if (s[j] == '<') {
00101                      pos = s.find('>', j + 1);
00102                      if (pos > j) {
00103                             mtoken = s.substr(j + 1, pos - j - 1);
00104                             for (int i = 0; modifiers[i].symbol != NULL; i++) {
00105                                    if (!strcasecmp(modifiers[i].symbol, mtoken.c_str())) {
00106                                           mod |= modifiers[i].modifier;
00107                                           j = pos;
00108                                    }
00109                             }
00110                             if (pos > j) {
00111                                    LOG_DEBUG("unknown modifier symbol: %s\n", mtoken.c_str());
00112                             }
00113                      }
00114               } else {
00115                      if (token.size() > 0) {
00116                             LOG_DEBUG("duplicated key definition. ignoring a previous key(%s)\n", token.c_str());
00117                      }
00118                      pos = s.find('<', j + 1);
00119                      if (pos > j) {
00120                             token = s.substr(j, pos - j);
00121                             j = pos - 1;
00122                      } else {
00123                             token = s.substr(j);
00124                             break;
00125                      }
00126               }
00127        }
00128        if (token.size() > 0) {
00129               for (j = 0; keysymtable[j].keyname != NULL; j++) {
00130                      if (!strcasecmp (keysymtable[j].keyname, token.c_str())) {
00131                             /* FIXME: xkb's keycode and IIIMF's keycode seems different. */
00132                                 keys.push_back(IMKeySpec(keysymtable[j].keysym,
00133                                                keysymtable[j].keysym,
00134                                                mod,
00135                                                0));
00136                                 return;
00137                      }
00138               }
00139               LOG_DEBUG("cannot parse the unknown key(%s)\n", token.c_str());
00140        }
00141 
00142         return;
00143 }
00144 
00145 std::string
00146 IMKeyParser::generate_key(IMKeySpec &spec)
00147 {
00148        int keycode = spec.get_keycode();
00149        int modifier = spec.get_modifier();
00150        std::string key;
00151        static struct {
00152               const char *symbol;
00153               int modifier;
00154        } modifiers[] = {
00155               {"Shift", 1}, {"Control", 2}, {"Alt", 4}, {NULL, 0}
00156        };
00157 #include "keysyms.h"
00158 
00159        for (int i = 0; modifiers[i].symbol != NULL; i++) {
00160               if (modifier & modifiers[i].modifier) {
00161                      key += std::string("<") + std::string(modifiers[i].symbol) + std::string(">");
00162               }
00163        }
00164        for (int j = 0; keysymtable[j].keyname != NULL; j++) {
00165               if (keysymtable[j].keysym == keycode) {
00166                      key += std::string(keysymtable[j].keyname);
00167                      return key;
00168               }
00169        }
00170        key = "";
00171 
00172        return key;
00173 }