Back to index

im-sdk  12.3.91
iiimpUtil.c
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 
00043 #include <stdio.h>
00044 #include "iiimpReq.h"
00045 #include "iiimpIM.h"
00046 #include "iiimpUtil.h"
00047 #include "KeyMap.h"
00048 
00049 /* Utility functions */
00050 static char *
00051 IMToString(
00052     CARD16 *chars,
00053     int char_length
00054 )
00055 {
00056     char *ptr;
00057     int i;
00058     int total_size;
00059     unsigned char *data;
00060 
00061     if (char_length & 0x01) {
00062        total_size = (2 + (2 * char_length));
00063     } else {
00064        total_size = (2 + (2 * char_length) + 2); /* padding */
00065     }
00066 
00067     data = (unsigned char *)Xmalloc(total_size);
00068     if (!data) {
00069        return NULL;
00070     }
00071 
00072     ptr = (char *)data;
00073     req_put16(ptr, (2 * char_length));
00074     for (i = 0; i < char_length; i++) {
00075        req_put16(ptr, *(chars + i));
00076     }
00077     if (0x00 == (char_length & 0x01)) {
00078        req_put16(ptr, 0);
00079     }
00080 
00081     return (char*)data;
00082 }
00083 
00084 /********************************************************************************
00085                             Trigger keys
00086  ********************************************************************************/
00087 
00088 static unsigned int
00089 str_to_mod(
00090     char *str
00091 )
00092 {
00093     if(!strcmp("Ctrl", str))
00094         return ControlMask;
00095     else if(!strcmp("Shift", str))
00096         return ShiftMask;
00097     else if(!strcmp("Meta", str))
00098         return Mod4Mask;
00099     else if(!strcmp("Alt", str))
00100         return Mod1Mask;
00101     else
00102         return 0;
00103 }
00104 
00105 static void
00106 add_conversion_keys_from_resource(
00107     XrmDatabase rdb,
00108     IIIMCF_keyevent **ppkev,
00109     int* pnum
00110 )
00111 {
00112     char *buf = NULL;
00113     char *dummy = NULL;
00114     char *p;
00115     Bool is_modmask;
00116     unsigned int mod, mod_mask, tmp_mask;
00117     char *ret_type;
00118     XrmValue rmvalue;
00119     char *names = NULL;
00120     int num = *pnum;
00121     IIIMCF_keyevent *pkev = *ppkev;
00122     IIIMCF_keyevent kev;
00123     int code;
00124 
00125     if (!XrmGetResource(rdb, "conversionOnKeys", "ConversionOnKeys",
00126                      &ret_type, &rmvalue))
00127        return;
00128 
00129     names = (char *) malloc(rmvalue.size + 1);
00130     if (!names) goto err;
00131     strncpy(names, rmvalue.addr, rmvalue.size);
00132     names[rmvalue.size] = '\0';
00133 
00134     dummy = buf = (char *) malloc(strlen(names) + 1);
00135     if (!dummy) goto err;
00136     strcpy(buf, names);
00137 
00138     is_modmask = False;
00139     mod = 0;
00140     while (*buf) {
00141         switch(*buf) {
00142           case '~':
00143           if (is_modmask)
00144               goto err;
00145           is_modmask = True;
00146           buf += 1;
00147           break;
00148           case '<':
00149           p = strchr(buf+1, '>');
00150           if (!p)
00151               goto err;
00152           *p = 0;
00153           if ((tmp_mask = str_to_mod(buf + 1)) == 0)
00154               goto err;
00155           if (is_modmask) {
00156               mod_mask |= tmp_mask;
00157               is_modmask = False;
00158           } else {
00159               mod |= tmp_mask;
00160           }
00161           buf = p + 1;
00162           if (!*buf)
00163               goto err;
00164           break;
00165           default:
00166           p = strchr(buf, ' ');
00167           if (p) *p = 0;
00168 
00169           code = XStringToKeysym(buf);
00170           if (XKeysym_To_IIIMCF_keyevent(code, mod, &kev)) {
00171               pkev = (IIIMCF_keyevent*) realloc(pkev, sizeof(IIIMCF_keyevent) * (num + 1));
00172               if (!pkev) goto err;
00173               pkev[num] = kev;
00174               num++;
00175               *ppkev = pkev;
00176               *pnum = num;
00177           }
00178 
00179           mod = 0;
00180           if (p) {
00181               buf = p + 1;
00182               break;
00183           } else {
00184               goto done;
00185           }
00186         }
00187     }
00188 
00189 done:
00190 err:
00191     if (names) free(names);
00192     if (dummy) free(dummy);
00193     return;
00194 }
00195 
00196 static void
00197 register_conversion_keys_from_resource(
00198     XimCommon im
00199 )
00200 {
00201     XrmDatabase rdb = im->core.rdb;
00202 
00203     if (!rdb) return;
00204     add_conversion_keys_from_resource(rdb, &XIM_IIIMP(im, pkev_on),
00205                                   &XIM_IIIMP(im, on_keys_num));
00206     add_conversion_keys_from_resource(rdb, &XIM_IIIMP(im, pkev_off),
00207                                   &XIM_IIIMP(im, off_keys_num));
00208 
00209     return;
00210 }
00211 
00212 void
00213 RegisterTriggerKeys(
00214     XimCommon im
00215 )
00216 {
00217     int on_num, off_num;
00218     const IIIMCF_keyevent *pkev_on, *pkev_off;
00219 
00220     if (!XIM_IS_IIIMP(im)) return;
00221 
00222     if (XIM_IIIMP(im, pkev_on)) {
00223        Xfree(XIM_IIIMP(im, pkev_on));
00224        XIM_IIIMP(im, on_keys_num) = 0;
00225     }
00226     if (XIM_IIIMP(im, pkev_off)) {
00227        Xfree(XIM_IIIMP(im, pkev_off));
00228        XIM_IIIMP(im, off_keys_num) = 0;
00229     }
00230 
00231     if (iiimcf_get_trigger_keys(XIM_IIIMP(im, handle),
00232                             &on_num, &pkev_on,
00233                             &off_num, &pkev_off)
00234        == IIIMF_STATUS_SUCCESS) {
00235        IIIMCF_keyevent *pkev;
00236 
00237        pkev = (IIIMCF_keyevent*) Xmalloc(sizeof(*pkev) * on_num);
00238        if (pkev) {
00239            memcpy(pkev, pkev_on, sizeof(*pkev) * on_num);
00240            XIM_IIIMP(im, on_keys_num) = on_num;
00241            XIM_IIIMP(im, pkev_on) = pkev;
00242        }
00243        pkev = (IIIMCF_keyevent*) Xmalloc(sizeof(*pkev) * off_num);
00244        if (pkev) {
00245            memcpy(pkev, pkev_off, sizeof(*pkev) * off_num);
00246            XIM_IIIMP(im, off_keys_num) = off_num;
00247            XIM_IIIMP(im, pkev_off) = pkev;
00248        }
00249     }
00250 
00251     register_conversion_keys_from_resource(im);
00252 
00253     return;
00254 }
00255 
00256 static Bool
00257 isMatchKey(
00258     XEvent *ev,
00259     const IIIMCF_keyevent *pkev,
00260     int num
00261 )
00262 {
00263     int i;
00264     IIIMCF_keyevent kev;
00265 
00266     if (ev->type != KeyPress) return False;
00267     if (ev->xkey.keycode == XIM_COMPOSE_KEYCODE) return False;
00268     if (!XKeyEvent_To_IIIMCF_keyevent((XKeyEvent*)ev, &kev)) return False;
00269 
00270     for (i = 0; i < num; i++, pkev++) {
00271        if ((kev.keycode == pkev->keycode)
00272            /* && (kev.keychar == pkev->keychar )*/
00273            && (kev.modifier == pkev->modifier)) {
00274            return True;
00275        }
00276     }
00277 
00278     return False;
00279 }
00280 
00281 Bool
00282 isConversionOnKey(
00283     XimCommon im,
00284     XEvent *ev
00285 )
00286 {
00287     if (!XIM_IS_IIIMP(im)) return False;
00288     return isMatchKey(ev, XIM_IIIMP(im, pkev_on), XIM_IIIMP(im, on_keys_num));
00289 }
00290 
00291 Bool
00292 isConversionOffKey(
00293     XimCommon im,
00294     XEvent *ev
00295 )
00296 {
00297     if (!XIM_IS_IIIMP(im)) return False;
00298     return isMatchKey(ev, XIM_IIIMP(im, pkev_off), XIM_IIIMP(im, off_keys_num));
00299 }
00300 
00301 /* Local Variables: */
00302 /* c-file-style: "iiim-project" */
00303 /* End: */