Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / lib / DtSvc / DtEncap / spc-xt.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 libraries 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 /*
24  * File:         spc-xt.c $TOG: spc-xt.c /main/6 1998/03/16 14:41:02 mgreess $
25  * Language:     C
26  *
27  * (c) Copyright 1989, Hewlett-Packard Company, all rights reserved.
28  *
29  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
30  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
31  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
32  * (c) Copyright 1993, 1994 Novell, Inc.                                *
33  */
34
35 #define __need_fd_set
36
37 #include <bms/sbport.h> /* NOTE: sbport.h must be the first include. */
38 #include <time.h>
39 #include <SPC/spcP.h>
40 /* #include <bms/SbEvent.h>  */ /* This file now included by spcP.h */
41 #include "DtSvcLock.h"
42
43 /* Externals */
44
45 extern int break_on_termination;
46
47 /* Utility functions */
48
49 /* First, declarations */
50
51 typedef struct {
52   SbInputId                     read_id;
53   SbInputId                     except_id;
54 } SPC_Callback_Struct;
55
56 static SPC_Callback_Struct **SPC_Fd_Mapping = NULL;
57
58 #define SPC_LOOKUP_FD_MAPPING(fd)   SPC_Fd_Mapping[(fd)]
59
60 static Boolean spc_xe_termination_flag;
61
62 /*-----------------------------------------------------------------------+*/
63 static int SPC_AddInput(int                     source,
64                  SPC_Callback_Condition         condition,
65                  SbInputId                      id)
66 /*-----------------------------------------------------------------------+*/
67 {
68   SPC_Callback_Struct *structptr = NULL;
69
70   _DtSvcProcessLock();
71   if (SPC_Fd_Mapping == NULL) {
72     SPC_Fd_Mapping = (SPC_Callback_Struct **) 
73                      XeMalloc (FD_SETSIZE * sizeof (SPC_Callback_Struct *));
74     memset(SPC_Fd_Mapping, 0, FD_SETSIZE * sizeof(SPC_Callback_Struct *));
75   }
76   structptr=SPC_LOOKUP_FD_MAPPING(source);
77
78   if(!structptr) {
79     structptr=(SPC_Callback_Struct *) XeMalloc(sizeof(SPC_Callback_Struct));
80     SPC_LOOKUP_FD_MAPPING(source)=structptr;
81   }
82   _DtSvcProcessUnlock();
83
84   switch (condition) {
85
86   case SPC_Input:
87   case SPC_Terminator:
88   case SPC_Client:
89     structptr->read_id   = id;
90     break;
91
92   case SPC_Exception:
93     structptr->except_id = id;
94     break;
95
96   default:
97     break;
98   }
99
100   return(source);
101 }
102
103
104 /*-----------------------------------------------------------------------+*/
105 static SbInputId SPC_RemoveInput(int                     source,
106                           SPC_Callback_Condition condition)
107 /*-----------------------------------------------------------------------+*/
108 {
109   SPC_Callback_Struct *structptr = NULL;
110   
111   _DtSvcProcessLock();
112   if (SPC_Fd_Mapping == NULL) {
113     SPC_Fd_Mapping = (SPC_Callback_Struct **) 
114                      XeMalloc (FD_SETSIZE * sizeof (SPC_Callback_Struct *));
115     memset(SPC_Fd_Mapping, 0, FD_SETSIZE * sizeof(SPC_Callback_Struct *));
116   }
117   structptr=SPC_LOOKUP_FD_MAPPING(source);
118   _DtSvcProcessUnlock();
119
120   switch(condition) {
121     
122   case SPC_Input:
123   case SPC_Terminator:
124   case SPC_Client:
125      return structptr->read_id;
126
127   case SPC_Exception:
128      return structptr->except_id;
129     
130   }
131
132   return 0;
133 }
134   
135 /*-----------------------------------------------------------------------+*/
136 static int
137 SPC_Select(void )
138 /*-----------------------------------------------------------------------+*/
139 {
140     break_on_termination=TRUE;
141
142     _DtSvcProcessLock();
143     spc_xe_termination_flag= FALSE;
144
145     /* Use a function pointer so we don't have explict dependancy */
146     /* on libXe.a                                                   */
147     /* ---------------------------------------------------------- */
148     if (SbMainLoopUntil_hookfn == NULL)
149       (void) fprintf (stderr, "Error: SbMainLoopUntil = NULL\n");
150     else
151       (*SbMainLoopUntil_hookfn)(&spc_xe_termination_flag);
152     _DtSvcProcessUnlock();
153
154     break_on_termination=FALSE;
155     return(TRUE);
156 }
157
158
159 /*-----------------------------------------------------------------------+*/
160 int SPC_Wait_For_Termination(SPC_Channel_Ptr channel)
161 /*-----------------------------------------------------------------------+*/
162 {
163   int result;
164   
165   call_parent_method(channel, wait_for_termination, (channel), result);
166   
167   if(result==SPC_ERROR) return(SPC_ERROR);
168
169   do {
170
171     if(SPC_Select() == SPC_ERROR)
172       return(SPC_ERROR);
173     
174   } while(IS_ACTIVE(channel));
175   
176   return(TRUE);
177   
178 }
179
180
181 /*-----------------------------------------------------------------------+*/
182 void SPC_XtBreak(void)
183 /*-----------------------------------------------------------------------+*/
184 {
185   _DtSvcProcessLock();
186   if(!spc_xe_termination_flag) {
187      spc_xe_termination_flag = TRUE;
188      if (SbBreakMainLoop_hookfn == NULL)
189        (void) fprintf (stderr, "Error: SbBreakMainLoop = NULL\n");
190      else
191        (*SbBreakMainLoop_hookfn)();      
192   } 
193   _DtSvcProcessUnlock();
194 }
195
196
197   
198 /*-----------------------------------------------------------------------+*/
199 void SPC_XtAddInput(SPC_Channel_Ptr channel,
200                     int *id_addr,
201                     int fd,
202                     spc_handler_func_type handler,
203                     SPC_Callback_Condition condition)
204 /*-----------------------------------------------------------------------+*/
205 {
206   SbInputId id;
207   
208   switch(condition) {
209     
210   case SPC_Input:
211   case SPC_Terminator:
212   case SPC_Client:
213     /* fprintf(stderr, "SPC add input/terminator for %d\n", fd); */
214     if (SbAddInput_hookfn == NULL)
215       (void) fprintf (stderr, "Error: SbAddInput = NULL\n");
216     else
217       id = (*SbAddInput_hookfn)(fd, handler, channel);
218     break;
219     
220   case SPC_Exception:
221     /* fprintf(stderr, "SPC add exception for %d\n", fd); */
222     if (SbAddException_hookfn == NULL)
223       (void) fprintf (stderr, "Error: SbAddException = NULL\n");
224     else
225       id = (*SbAddException_hookfn)(fd, handler, channel);
226     break;
227     
228   }
229   
230   *id_addr=SPC_AddInput(fd, condition, id);
231 }
232
233
234 /*-----------------------------------------------------------------------+*/
235 void SPC_XtRemoveInput(int *id_addr, 
236                        SPC_Callback_Condition condition)
237 /*-----------------------------------------------------------------------+*/
238 {
239   if((*id_addr) != -1) {
240     
241     switch(condition) {
242       
243     case SPC_Input:
244     case SPC_Terminator:
245     case SPC_Client:
246       /* fprintf(stderr, "SPC remove input/terminator\n"); */
247       if (SbRemoveInput_hookfn == NULL)
248         (void) fprintf (stderr, "Error: SbRemoveInput = NULL\n");
249       else
250         (*SbRemoveInput_hookfn)(SPC_RemoveInput(*id_addr, condition));
251       break;
252       
253     case SPC_Exception:
254       /* fprintf(stderr, "SPC remove exception"); */
255       if (SbRemoveException_hookfn == NULL)
256         (void) fprintf (stderr, "Error: SbRemoveException = NULL\n");
257       else
258         (*SbRemoveException_hookfn)(SPC_RemoveInput(*id_addr, condition));
259       break;
260       
261     }
262     
263     *id_addr=(-1);
264   }
265 }