summaryrefslogtreecommitdiffstats
path: root/xpa/gtkloop.c
blob: df5137e7ec7d5f9efbfd82476bc634ce3f05f05b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 *	Copyright (c) 2006 Smithsonian Astrophysical Observatory
 */

#include <xpap.h>

#ifdef HAVE_GTK

#include <gtk/gtk.h>
#include <gtk/gtkmain.h>

/*----------------------------------------------------------------------------
 *
 * 			Private Routines and Data
 *
 *----------------------------------------------------------------------------
 */

/* record struct for maintining gtk info in gtk select loop  */
typedef struct xpagtkrec{
  int fd;
  void *client_data;
  int id;
} *XPAGtk, XPAGtkRec;

/*----------------------------------------------------------------------------
 *
 * Routine:	XPAGtkHandler
 *
 * Purpose:	handle one request for an xpaset or xpaget
 *
 * Return:	none
 *
 *----------------------------------------------------------------------------
 */
static gboolean XPAGtkHandler(GIOChannel *gio, GIOCondition condition, 
   gpointer data)
{
  XPAGtk xptr = (XPAGtk)data;
  if( (xptr == NULL) || (xptr->client_data == NULL) )
    return TRUE;
  XPAHandler((XPA)xptr->client_data, xptr->fd);
   return TRUE; 
}

/*----------------------------------------------------------------------------
 *
 * Routine:	XPAGtkEnableOneInput
 *
 * Purpose:	Enable 1 XPA entry from the Xt event loop
 *
 * Results:	none
 *
 *----------------------------------------------------------------------------
 */
static void XPAGtkEnableOneInput (void *client_data)
{
   XPAGtk xptr = (XPAGtk)client_data;
   
   if( xptr && !xptr->id ){
        GIOChannel* ioc = g_io_channel_unix_new(xptr->fd);
	xptr->id =  g_io_add_watch(ioc, (G_IO_IN | G_IO_HUP | G_IO_NVAL),
		       XPAGtkHandler, xptr);
   }
}

/*----------------------------------------------------------------------------
 *
 * Routine:	XPAGtkDisableOneInput
 *
 * Purpose:	Disable 1 XPA entry from the Xt event loop
 *
 * Results:	none
 *
 *----------------------------------------------------------------------------
 */
static void XPAGtkDisableOneInput (void *client_data)
{
   XPAGtk xptr = (XPAGtk)client_data;

   if(xptr && xptr->id){
     g_source_remove(xptr->id); 
     xptr->id = 0;
   }
}

/*----------------------------------------------------------------------------
 *
 * Routine:	XPAGtkAddOneInput
 *
 * Purpose:	Add 1 XPA entry to the gtk event loop
 *
 * Results:	none
 *
 *----------------------------------------------------------------------------
 */
static void* XPAGtkAddOneInput (void *client_data, int fd)
{
  XPAGtk xptr;
  if( fd < 0 )
    return(NULL);
  xptr = (XPAGtk)calloc(1, sizeof(XPAGtkRec));
  xptr->fd = fd;
  xptr->client_data = client_data;
  XPAGtkEnableOneInput(xptr);
  return(xptr);
}

/*----------------------------------------------------------------------------
 *
 * Routine:	XPAGtkDelOneInput
 *
 * Purpose:	Delete 1 XPA entry from the gtk event loop (called by XPAFree)
 *
 * Results:	none
 *
 *----------------------------------------------------------------------------
 */

static void XPAGtkDelOneInput (void *client_data)
{
  XPAGtk xptr = (XPAGtk)client_data;
  if( xptr == NULL)
    return;
  XPAGtkDisableOneInput(xptr);
  free(xptr);
}

/*----------------------------------------------------------------------------
 *
 * 			Public Routines and Data
 *
 *----------------------------------------------------------------------------
 */

/*----------------------------------------------------------------------------
 *
 * Routine:	XPAGtkAddInput
 *
 * Purpose:	Add XPA entries to the Xt event loop
 *
 * Results:	number of xpa entried added
 *
 *----------------------------------------------------------------------------
 */
int XPAGtkAddInput (XPA xpa)
{
  XPA cur;
  int got=0;

  /* if a specific xpa was specified, just add it */
  if( xpa != NULL ){
    /* remove old one */
    if( xpa->seldel && xpa->selptr ){
      (xpa->seldel)(xpa->selptr);
    }
    /* add new one */
    xpa->seldel = XPAGtkDelOneInput;
    xpa->seladd = XPAGtkAddOneInput;
    xpa->selon =  XPAGtkEnableOneInput;
    xpa->seloff = XPAGtkDisableOneInput;
    xpa->selptr = XPAGtkAddOneInput((void *)xpa, xpa->fd);
    got = 1;
  }
  /* otherwise set up all xpa's */
  else{
    for(cur=(XPA)XPAListHead(); cur!=NULL; cur=cur->next){
      /* remove old one */
      if( cur->seldel && cur->selptr ){
	(cur->seldel)(cur->selptr);
      }
      /* add new one */
      cur->seldel = XPAGtkDelOneInput;
      cur->seladd = XPAGtkAddOneInput;
      cur->selon =  XPAGtkEnableOneInput;
      cur->seloff = XPAGtkDisableOneInput;
      cur->selptr = XPAGtkAddOneInput((void *)cur, cur->fd);
      got++;
    }
  }
  return(got);
}

int xpa_gtk = 1;

#else

int xpa_gtk = 0;

#endif