Back to index

im-sdk  12.3.91
commonIM.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 "commonIM.h"
00045 #include "guiIM.h"
00046 #include "iiimpDL.h"
00047 #include "popupIM.h"
00048 #include "XimpIm.h"
00049 #include "ximp40.h"
00050 #include "trace_message.h"
00051 
00052 /*
00053    It is our private extension to prepare for CommonIM_Error_Handler
00054    as an error handler for the BadWindow error caused by XSendEvent().
00055 
00056    The BadWindow error may occur when the connection with the input
00057    method server is lost when XSendEvent() is called and failed to
00058    send a request to the input method server.
00059    We assume that this condition is not fatal and an application
00060    can survive after the error occurs. Therefore, _IIIMP_Error_Handler
00061    returns without calling exit system call.
00062 
00063    In addtion, when an application program sets its own error handler,
00064    we don't call _IIIMP_Error_Handler, but calls application's
00065    error handler. In this case, appl_error_handler is used to store the
00066    pointer of the application's error handler function.
00067 */
00068 static XErrorHandler appl_error_handler = (XErrorHandler)NULL;
00069 static int
00070 CommonIM_Error_Handler(
00071     Display *dpy,
00072     XErrorEvent *event
00073 )
00074 {
00075     extern int _XDefaultError();
00076 
00077     if (appl_error_handler &&
00078        appl_error_handler != _XDefaultError) {
00079        return appl_error_handler(dpy, event);
00080     } else {
00081        char buffer[BUFSIZE];
00082        char number[32];
00083 
00084        if (event->request_code < 128) {
00085            if (event->error_code == BadDrawable || event->error_code == BadWindow) {
00086               snprintf(number, sizeof(number), "%d", event->request_code);
00087               XGetErrorDatabaseText(dpy, "XRequest", number,
00088                                   "", buffer, BUFSIZE);
00089            }
00090            /* 4378199: Linux Htt: can not operate after WMDoc client is
00091               closed on VineLinux2.0
00092               Need to check client_window is valid or not before creating
00093               status, preedit and lookup
00094 
00095               XFactoryCheckIMWindow() uses XGetGeometry() for checking.
00096               XGetGeometry() may generate BadDrawable or BadDrawable
00097            */
00098            if (event->error_code == BadDrawable) {
00099               if (!strcmp(buffer, "X_GetGeometry"))
00100                   return 0;
00101            } else if (event->error_code == BadWindow) {
00102               return 0;
00103 #if 0
00104               if (!strcmp(buffer, "X_SendEvent"))
00105                   return 0;
00106               if (!strcmp(buffer, "X_GetGeometry"))
00107                   return 0;
00108               if (!strcmp(buffer, "X_GetWindowAttributes"))
00109                   return 0;
00110               if (!strcmp(buffer, "X_ChangeWindowAttributes"))
00111                   return 0;
00112               if (!strcmp(buffer, "X_ClearArea"))
00113                   return 0;
00114               if (!strcmp(buffer, "X_DestroyWindow"))
00115                   return 0;
00116               if (!strcmp(buffer, "X_GetProperty"))
00117                   return 0;
00118 #endif
00119            }
00120        }
00121     }
00122 
00123     return _XDefaultError(dpy, event);
00124 }
00125 
00126 Status
00127 CommonOpenIM(
00128     XIM xim,
00129     XLCd lcd,
00130     Display *dpy,
00131     XrmDatabase rdb,
00132     char *res_name,
00133     char *res_class
00134 )
00135 {
00136     XimCommon im = (XimCommon)xim;
00137 
00138     TRACE_MESSAGE_INIT(NULL);
00139 
00140     /* Open mbs->wcs and wcs->mbs converters */
00141     if (!(im->wtom_conv = _XlcOpenConverter(lcd, XlcNWideChar,
00142                                        lcd, XlcNMultiByte))) {
00143        goto Set_Error;
00144     }
00145     if (!(im->mtow_conv = _XlcOpenConverter(lcd, XlcNMultiByte,
00146                                        lcd, XlcNWideChar))) {
00147        goto Set_Error;
00148     }
00149     im->core.lcd       = lcd;
00150     im->core.ic_chain  = (XIC)NULL;
00151     im->core.display   = dpy;
00152     im->core.rdb       = rdb;
00153     im->core.res_name  = NULL;
00154     im->core.res_class = NULL;
00155     im->core.im_name   = NULL;
00156 
00157     if ((res_name != NULL) && (*res_name != '\0')){
00158        im->core.res_name  = (char *)Xmalloc(strlen(res_name)+1);
00159        strcpy(im->core.res_name,res_name);
00160     }
00161     if ((res_class != NULL) && (*res_class != '\0')){
00162        im->core.res_class = (char *)Xmalloc(strlen(res_class)+1);
00163        strcpy(im->core.res_class,res_class);
00164     }
00165 
00166     /* set XIMP-dependent data */
00167     im->ximp_impart = (XIMXimp)Ximp_OpenIM(lcd);
00168 
00169     /* set dynamic loadble object data */
00170     im->dl_impart = OpenDynamicObject(lcd);
00171 
00172 #if 1
00173     /* set error handler only once */
00174     if (appl_error_handler == (XErrorHandler)NULL)
00175        appl_error_handler = XSetErrorHandler(CommonIM_Error_Handler);
00176 #endif
00177 
00178     return True;
00179 
00180 Set_Error:
00181     if (im)
00182        CommonCloseIM((XIM)im);
00183     return False;
00184 }
00185 
00186 Status
00187 CommonCloseIM(
00188     XIM xim
00189 )
00190 {
00191     XimCommon im = (XimCommon)xim;
00192 
00193 #if 0
00194     XIC ic;
00195     for (ic = xim->core.ic_chain; ic; ic = ic->core.next)
00196        XDestroyIC(ic);
00197 #endif
00198 
00199     if (im->mtow_conv)
00200        _XlcCloseConverter(im->mtow_conv);
00201 
00202     if (im->wtom_conv)
00203        _XlcCloseConverter(im->wtom_conv);
00204 
00205     if (im->ximp_impart) {
00206        Ximp_CloseIM(im->ximp_impart);
00207     }
00208 
00209     if (im->popup_impart) ClosePopupIM(im);
00210 
00211     if (im->dl_impart) {
00212        CloseDynamicObject(im->dl_impart);
00213     }
00214     if (im->core.res_name) Xfree(im->core.res_name);
00215     if (im->core.res_class) Xfree(im->core.res_class);
00216 
00217     return True;
00218 }
00219 
00220 Status
00221 CommonCreateIC(
00222     XIC xic,
00223     XIMArg *arg
00224 )
00225 {
00226     XicCommon ic = (XicCommon)xic;
00227 
00228     /* ximp icpart */
00229     if (!ic->ximp_icpart) {
00230        ic->ximp_icpart = (XICXimp)Xmalloc(sizeof(XICXimpRec));
00231        if (!ic->ximp_icpart) goto Error;;
00232        memset(ic->ximp_icpart, 0, sizeof(XICXimpRec));
00233     }
00234     if (!ic->gui_icpart) {
00235        if (!(ic->gui_icpart = CreateGUI(ic, arg))) goto Error;
00236     }
00237     return True;
00238 Error:
00239     CommonDestroyIC((XIC)ic);
00240     return False;
00241 }
00242 
00243 Status
00244 CommonDestroyIC(
00245     XIC xic
00246 )
00247 {
00248     XicCommon ic = (XicCommon)xic;
00249     if (ic->ximp_icpart) Xfree(ic->ximp_icpart);
00250     ic->ximp_icpart = 0;
00251     if (ic->gui_icpart) DestroyGUI(ic); /* destroy gui */
00252     ic->gui_icpart = 0;
00253     return True;
00254 }
00255 
00256 /* Local Variables: */
00257 /* c-file-style: "iiim-project" */
00258 /* End: */