Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / csa / xtclient.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: xtclient.c /main/1 1996/04/21 19:24:58 drk $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include <EUSCompat.h>
32 #include <stdlib.h>
33 #include <X11/Intrinsic.h>
34 #include <rpc/rpc.h>
35 #include "debug.h"
36
37 typedef struct _appctlist {
38         XtAppContext    appct;
39         struct _appctlist *next;
40 } AppCtList;
41
42 static AppCtList *registered_appct = NULL;
43
44 /*****************************************************************************
45  * forward declaration of static functions
46  *****************************************************************************/
47 static void xtcallback(XtPointer data, int *fid, XtInputId *id);
48 static boolean_t new_appct(XtAppContext apptct);
49
50 /*****************************************************************************
51  * extern functions used in the library
52  *****************************************************************************/
53
54 /*
55  * register callback for all file descriptors that's set
56  * (since we don't know which one is ours).
57  */
58 extern void
59 _DtCm_register_xtcallback(XtAppContext appct)
60 {
61         XtInputId       id;
62         int     i, j;
63         fd_mask fmask, *bits;
64         fd_set  fdset = svc_fdset;
65
66         DP(("xtclient.c: _DtCm_register_xtcallback()\n"));
67
68         if (new_appct(appct) == B_FALSE)
69                 return;
70
71         /* assuming only 1 bit is set */
72         bits = fdset.fds_bits;
73         for (i = 0; i < FD_SETSIZE; i += NFDBITS) {
74                 fmask = *bits;
75                 for (j = 0; fmask != 0; j++, fmask >>= 1) {
76                         if (fmask & 0x1) {
77
78                                 if ((i + j) >= FD_SETSIZE)
79                                         break;
80
81                                 /* register callback with XtAppAddInput
82                                  * for rpc input
83                                  */
84                                 id = XtAppAddInput(appct, ((i *NFDBITS) + j),
85                                         (XtPointer)XtInputReadMask,
86                                         xtcallback, NULL);
87
88                                 DP(("xtclient.c: id %d for input at fd %d\n",
89                                         id, ((i * NFDBITS) + j)));
90                         }
91                 }
92                 bits++;
93         }
94 }
95
96 /*****************************************************************************
97  * static functions used within the file
98  *****************************************************************************/
99
100 /*
101  * callback for rpc events
102  */
103 static void
104 xtcallback(XtPointer data, int *fid, XtInputId *id)
105 {
106         fd_set rpc_bits;
107
108         DP(("xtcallback called\n"));
109
110         FD_ZERO(&rpc_bits);
111         FD_SET(*fid, &rpc_bits);
112         svc_getreqset(&rpc_bits);
113 }
114
115 /*
116  * need to lock registered_appct
117  */
118 static boolean_t
119 new_appct(XtAppContext appct)
120 {
121         AppCtList       *lptr;
122         boolean_t       newappct = B_TRUE;
123
124         for (lptr = registered_appct; lptr != NULL; lptr = lptr->next) {
125                 if (lptr->appct == appct) {
126                         newappct = B_FALSE;
127                         break;
128                 }
129         }
130
131         if (newappct == B_TRUE) {
132                 lptr = (AppCtList *)calloc(1, sizeof(AppCtList));
133                 lptr->appct = appct;
134                 lptr->next = registered_appct;
135                 registered_appct = lptr;
136         }
137
138         return (newappct);
139 }
140