Back to index

im-sdk  12.3.91
xaux_atommng.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2003 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 #pragma ident "$Id$"
00044 
00045 #include      <stdlib.h>
00046 #include      <limits.h>
00047 #include      <X11/Xlib.h>
00048 #include      "xaux_common.h"
00049 #include      "xaux_atommng.h"
00050 
00051 #include      <stdio.h>
00052 
00053 xaux_atommng_data_t *
00054 xaux_atommng_alloc_data(
00055        const char           *classname,
00056        Bool                 is_xs,
00057        Display                     *display
00058 )
00059 {
00060        xaux_atommng_data_t  *data;
00061        int                  i;
00062        char                 buf[PATH_MAX];
00063 
00064        data = (xaux_atommng_data_t *)malloc(sizeof (xaux_atommng_data_t));
00065 
00066        if (data == NULL) {
00067               return (NULL);
00068        }
00069 
00070        data->classname = classname;
00071        data->display = display;
00072 
00073        snprintf(buf, PATH_MAX, "%s%s", data->classname,
00074               XAUX_ATOMMNG_SX_SUFFIX);
00075        data->atom_sx = XInternAtom(data->display, buf, False);
00076        data->atom_sx_inuse = False;
00077 
00078        snprintf(buf, PATH_MAX, "%s%s", data->classname,
00079               XAUX_ATOMMNG_XS_SUFFIX);
00080        data->atom_xs = XInternAtom(data->display, buf, False);
00081        data->atom_xs_inuse = False;
00082 
00083        data->idx = -1;
00084        data->is_xs = is_xs;
00085 
00086        return (data);
00087 }
00088 
00089 Bool
00090 xaux_atommng_check_atom(
00091        xaux_atommng_data_t  *data,
00092        Bool                 is_xs,
00093        Atom                 atom
00094 )
00095 {
00096        if (((is_xs == True) && (atom == data->atom_xs))
00097                      || ((is_xs == False) && (atom == data->atom_sx))) {
00098               return (True);
00099        }
00100 
00101        return (False);
00102 }
00103 
00104 Atom
00105 xaux_atommng_get_atom(
00106        xaux_atommng_data_t  *data,
00107        Window               win
00108 )
00109 {
00110        if (data->is_xs == True) {
00111               if (data->atom_xs_inuse == True) {
00112 #if defined(DEBUG_XAUX)
00113 fprintf(stderr, "atommng: atom_xs=%d is busy\n", data->atom_xs);
00114 #endif /* defined(DEBUG_XAUX) */
00115                      return ((Atom)None);
00116               } else {
00117 #if defined(DEBUG_XAUX)
00118 fprintf(stderr, "atommng: atom_xs=%d is available\n", data->atom_xs);
00119 #endif /* defined(DEBUG_XAUX) */
00120                      data->atom_xs_inuse = True;
00121                      return (data->atom_xs);
00122               }
00123        } else {
00124               if (data->atom_sx_inuse == True) {
00125 #if defined(DEBUG_XAUX)
00126 fprintf(stderr, "atommng: atom_sx=%d is busy\n", data->atom_sx);
00127 #endif /* defined(DEBUG_XAUX) */
00128                      return ((Atom)None);
00129               } else {
00130 #if defined(DEBUG_XAUX)
00131 fprintf(stderr, "atommng: atom_sx=%d is available\n", data->atom_sx);
00132 #endif /* defined(DEBUG_XAUX) */
00133                      data->atom_sx_inuse = True;
00134                      return (data->atom_sx);
00135               }
00136        }
00137 }
00138 
00139 void
00140 xaux_atommng_free_data(
00141        xaux_atommng_data_t  *data
00142 )
00143 {
00144        if (data == NULL) {
00145               return;
00146        }
00147 
00148        free(data);
00149 
00150        return;
00151 }
00152 
00153 Bool
00154 xaux_atommng_release_atom(
00155        xaux_atommng_data_t  *data)
00156 {
00157        if (data->is_xs == True) {
00158               data->atom_xs_inuse = False;
00159        } else {
00160               data->atom_sx_inuse = False;
00161        }
00162        return (True);
00163 }
00164 
00165 Bool
00166 xaux_atommng_process_delete(
00167        xaux_atommng_data_t  *data,
00168        XPropertyEvent              *pev)
00169 {
00170               if ((data->is_xs == True) && (pev->atom == data->atom_xs)) {
00171                      data->atom_xs_inuse = False;
00172 #if defined(DEBUG_XAUX)
00173 fprintf(stderr, "atommng: atom_xs=%d is released\n", data->atom_xs);
00174 #endif /* defined(DEBUG_XAUX) */
00175                      return (True);
00176               } else if ((data->is_xs == False) &&
00177                             (pev->atom == data->atom_sx)) {
00178 #if defined(DEBUG_XAUX)
00179 fprintf(stderr, "atommng: atom_sx=%d is released\n", data->atom_sx);
00180 #endif /* defined(DEBUG_XAUX) */
00181                      data->atom_sx_inuse = False;
00182                      return (True);
00183               } else {
00184 #if defined(DEBUG_XAUX)
00185 fprintf(stderr, "atommng: unknown PropertyDelete to atom=%d\n", pev->atom);
00186 #endif /* defined(DEBUG_XAUX) */
00187                      return (False);
00188               }
00189 }
00190 
00191 Bool
00192 xaux_atommng_process_selclr(
00193        xaux_atommng_data_t  *data,
00194        XSelectionClearEvent *scev)
00195 {
00196               if ((data->is_xs == True)
00197                             && (scev->selection == data->atom_xs)) {
00198                      data->atom_xs_inuse = False;
00199 #if defined(DEBUG_XAUX)
00200 fprintf(stderr, "atommng: atom_xs=%d is released\n", data->atom_xs);
00201 #endif /* defined(DEBUG_XAUX) */
00202                      return (True);
00203               } else if ((data->is_xs == False) &&
00204                             (scev->selection == data->atom_sx)) {
00205 #if defined(DEBUG_XAUX)
00206 fprintf(stderr, "atommng: atom_sx=%d is released\n", data->atom_sx);
00207 #endif /* defined(DEBUG_XAUX) */
00208                      data->atom_sx_inuse = False;
00209                      return (True);
00210               } else {
00211 #if defined(DEBUG_XAUX)
00212 fprintf(stderr, "atommng: unknown SelectionClear to atom=%d\n", scev->selection);
00213 #endif /* defined(DEBUG_XAUX) */
00214                      return (False);
00215               }
00216 }
00217 
00218 Atom
00219 xaux_atommng_know_atom(
00220        xaux_atommng_data_t  *data,
00221        Bool                 is_xs)
00222 {
00223        if (is_xs == True) {
00224               return (data->atom_xs);
00225        } else {
00226               return (data->atom_sx);
00227        }
00228 }
00229 
00230 xaux_propq_t *
00231 xaux_propq_alloc(size_t     maxtotalsz)
00232 {
00233        xaux_propq_t  *p;
00234 
00235        if ((p = (xaux_propq_t *)malloc(sizeof (xaux_propq_t)))
00236                      == NULL) {
00237               return (NULL);
00238        }
00239        p->count = 0;
00240        p->totalsz = 0U;
00241        p->maxtotalsz = maxtotalsz;
00242        p->list = NULL;
00243 
00244        return (p);
00245 }
00246 
00247 int
00248 xaux_propq_add(
00249        xaux_propq_t         *data,
00250        unsigned char        *buf,
00251        int                  len)
00252 {
00253        xaux_propq_prop_t    *add;
00254        xaux_propq_prop_t    *last;
00255 
00256        if ((data->maxtotalsz > 0) &&
00257                      ((data->totalsz + len) > data->maxtotalsz)) {
00258 #if defined(DEBUG_XAUX)
00259 fprintf(stderr, "propq: add() failed because of size limit\n");
00260 #endif /* defined(DEBUG_XAUX) */
00261               return (-2);
00262        }
00263 
00264        if ((add = (xaux_propq_prop_t *)malloc(sizeof (xaux_propq_prop_t)))
00265                      == NULL) {
00266               return (-1);
00267        }
00268 
00269        add->buf = buf;
00270        add->len = len;
00271 
00272        if (data->list == NULL) {
00273               add->prev = NULL;
00274               add->next = NULL;
00275               data->list = add;
00276        } else {
00277               for (last = data->list; ; last = last->next) {
00278                      if (last->next == NULL) {
00279                             break;
00280                      }
00281               }
00282               add->prev = last;
00283               add->next = NULL;
00284               last->next = add;
00285        }
00286 
00287        data->count++;
00288        data->totalsz += len;
00289 
00290 #if defined(DEBUG_XAUX)
00291 fprintf(stderr, "propq: add(%d:0x%x)\n", data->count, data->totalsz);
00292 #endif /* defined(DEBUG_XAUX) */
00293        return (data->count);
00294 }
00295 
00296 int
00297 xaux_propq_check(
00298        xaux_propq_t  *data)
00299 {
00300 #if defined(DEBUG_XAUX_DETAIL)
00301 fprintf(stderr, "propq: check(%d)\n", data->count);
00302 #endif /* defined(DEBUG_XAUX_DETAIL) */
00303        return (data->count);
00304 }
00305 
00306 int
00307 xaux_propq_get(
00308        xaux_propq_t  *data,
00309        unsigned char        **pbuf,
00310        int                  *plen)
00311 {
00312        xaux_propq_prop_t    *prev;
00313 
00314        if (data->list == NULL) {
00315               return (-1);
00316        }
00317 
00318        (*pbuf) = data->list->buf;
00319        (*plen) = data->list->len;
00320 
00321        prev = data->list;
00322        data->list = data->list->next;
00323        if (data->list != NULL) {
00324               data->list->prev = NULL;
00325        }
00326        free(prev);
00327 
00328        data->count--;
00329        data->totalsz -= *plen;
00330 
00331 #if defined(DEBUG_XAUX)
00332 fprintf(stderr, "propq: get(%d:0x%x)\n", data->count, data->totalsz);
00333 #endif /* defined(DEBUG_XAUX) */
00334        return (data->count);
00335 }
00336 
00337 void
00338 xaux_propq_clean(
00339        xaux_propq_t  *data)
00340 {
00341        xaux_propq_prop_t    *cur = data->list;
00342        xaux_propq_prop_t    *prev;
00343 
00344        while (cur != NULL) {
00345               prev = cur;
00346               cur = cur->next;
00347               free(prev->buf);
00348               free(prev);
00349        }
00350 
00351        data->count = 0;
00352        data->totalsz = 0U;
00353        data->list = NULL;
00354 
00355        return;
00356 }
00357 
00358 void
00359 xaux_propq_free(
00360        xaux_propq_t  *data)
00361 {
00362        xaux_propq_prop_t    *cur = data->list;
00363        xaux_propq_prop_t    *prev;
00364 
00365        xaux_propq_clean(data);
00366 
00367        free(data);
00368 
00369        return;
00370 }