summaryrefslogtreecommitdiffstats
path: root/tcliis/xim.C
blob: bdbd5572ef15450b115393ef9869f876eed8bed5 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (C) 1999-2013
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <string.h>
#include <sys/time.h>

#include <iostream>
#include <sstream>
using namespace std;

#include <tcl.h>

#ifdef __WIN32__
#include <Winsock2.h>
#endif

#include "iistcl.h"

extern "C" {
#include "iis.h"
#include "xim.h"

void iisIO(ClientData data, int mask)
{
  int fd = (long)data;

  if (IISDebug)
    cerr << "iisIO() " << fd << ' ' << mask << endl;

  if ((fd < MAXCHANNEL) && iis->func[fd]) {
    (*iis->func[fd])(iis->chan[fd], &fd, NULL);
  }
  else
    cerr << "Error: IIS iisIO problems" << endl;
}

} // extern 'C'

int xim_addInput(XimDataPtr xim, int fd, 
		 void (*func)(IoChan*, int*, void*), IoChanPtr chan)
{
  if (IISDebug)
    cerr << "xim_addInput() " << fd << ' ' << func << ' ' << chan << endl;

  iis->func[fd] = func;
  iis->chan[fd] = chan;
#ifndef __WIN32__
  Tcl_CreateFileHandler(fd, TCL_READABLE, (void (*)(ClientData,int))iisIO,
			(ClientData)long(fd));
#else
  Tcl_CreateEventSource(setupProc, checkProc, (ClientData)long(fd));
#endif

  return fd;
}

#ifdef __WIN32__
void setupProc(void* fd, int flags)
{
  Tcl_Time blockTime = {0,1000};
  Tcl_SetMaxBlockTime(&blockTime);
  return;
}

void checkProc(void* fdd, int flags)
{
  int fd = (int)fdd;
  fd_set readfds;
  struct timeval tv = {0,0};

  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);
  int got = select(fd+1, &readfds, NULL, NULL, &tv);

  if (got<0)
    Tcl_DeleteEventSource(setupProc, checkProc, fdd);
  else if (got>0) 
    iisIO(fdd,0);
}
#endif

void xim_removeInput(XimDataPtr xim, int fd)
{
  if (IISDebug)
    cerr << "xim_removeInput() " << fd << endl;

  if (fd < MAXCHANNEL) {
    iis->func[fd] = NULL;
    iis->chan[fd] = NULL;
#ifndef __WIN32__
    Tcl_DeleteFileHandler(fd);
#endif
  }
  else
    cerr << "Error: IIS xim_removeInput-- bad fd" << endl;
}

void xim_initialize(XimDataPtr xim, int config, int nframes, int hardreset)
{
  // from ximtool xim_initialize

  get_fbconfig(xim);

  xim->fb_configno = config;
  xim->df_p = &xim->frames[0];

  FbConfigPtr cf = &xim->fb_config[config-1];
  xim->width = cf->width;
  xim->height = cf->height;

  ostringstream str;
  str << "IISInitializeCmd " << xim->width << ' ' << xim->height << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "IISInitializeCmd " << xim->width << ' ' << xim->height << endl;
}

void xim_initFrame(XimDataPtr xim, int frame, int nframes, FbConfig* config,
		   char* memModel)
{
  // from ximtool xim_initFrame
 
  FrameBufPtr fb = &xim->frames[frame-1];
  fb->frameno = frame;
  fb->raster = frame;
  fb->zoomras = 0;
  fb->zoommap = 0;
  fb->dispmap = 0;
  fb->colormap = DEF_COLORMAP;
  fb->offset = 0.5;
  fb->scale = 1.0;
  fb->xscale = fb->yscale = 1.0;
  fb->xmag = fb->ymag = 1.0;
  fb->xcen = fb->ycen = 0.0;
  fb->xoff = fb->yoff = 0.0;
  fb->xflip = fb->yflip = 0;
  fb->label[0] = '\0';
  fb->wcsbuf[0] = '\0';
  fb->nmaps = 0;
 
   // my stuff
  ostringstream str;
  str << "IISInitFrameCmd " << frame << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "xim_initFrame() " << str.str().c_str() << endl;
}

void xim_setDisplayFrame(XimDataPtr xim, int frame)
{
  // from imtool xim_setDisplayFrame

  FbConfig* config = &xim->fb_config[xim->fb_configno-1];
  xim->df_p = &xim->frames[frame-1];
  xim->width = config->width;
  xim->height = config->height;

  // and my stuff
  ostringstream str;
  str << "IISSetDisplayFrameCmd " << frame << ' '
      << config->width << ' ' << config->height << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug) {
    cerr << "xim_setDisplayFrame() " << str.str().c_str() << endl;
  }
}

void xim_setReferenceFrame(IoChanPtr chan, int frame)
{
  // from imtool xim_setDisplayFrame

  // Ignore request if channel not active.
  if (!chan->type)
    return;

  XimDataPtr xim = (XimDataPtr)chan->xim;
  int frameno = max(1, min(MAX_FRAMES, frame));
  FrameBufPtr fb = &xim->frames[frameno-1];

  // Ignore request if not a valid frame.
  //
  // All frames are valid, ds9 frames now work with iis.
  //
  // if (fb->frameno > 0) {
  chan->reference_frame = frameno;
  chan->rf_p = fb;
  //  }

  ostringstream str;
  str << "IISSetRefFrameCmd " << frame << ends;
  const char *wcs = iis->evalstr((char*)str.str().c_str());

  if (IISDebug) {
    if (*wcs)
      cerr << "xim_setReferenceFrame() " << str.str().c_str() 
	   << " " << wcs << endl;
    else
      cerr << "xim_setReferenceFrame() " << str.str().c_str() << endl;
  }

  if (*wcs)
    strcpy(fb->wcsbuf, wcs);
}

void xim_eraseFrame(XimDataPtr xim, int frame)
{
  ostringstream str;
  str << "IISEraseFrameCmd " << frame << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "xim_eraseFrame() " << str.str().c_str() << endl;
}

void xim_message(XimDataPtr xim, char* message, char* imtitle)
{
  ostringstream str;
  str << "IISMessageCmd {" << message << ' ' << imtitle << '}' << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "xim_message() " << str.str().c_str() << endl;
}

void xim_wcs(int frame, float a, float b, float c, float d, 
	     float tx, float ty, float z1, float z2, int zt)
{
  ostringstream str;
  str << "IISWCSCmd " << frame << ' '
      << a << ' ' << b << ' ' << c << ' ' << d << ' ' 
      << tx << ' ' << ty << ' ' 
      << z1 << ' ' << z2 << ' ' 
      << zt << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "xim_wcs() " << frame << ' '
      << a << ' ' << b << ' ' << c << ' ' << d << ' ' 
      << tx << ' ' << ty << ' ' 
      << z1 << ' ' << z2 << ' ' 
      << zt << endl;
}

void GtWritePixels(void* gt, int frame, void* pixels, int bits,
		   int xx, int yy, int dx, int dy)
{
  ostringstream str;
  str << "IISWritePixelsCmd " << frame << ' ' << pixels << ' ' 
      << xx << ' ' << yy << ' ' << dx << ' ' << dy << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "GtWritePixels() " << str.str().c_str() << endl;
}

void GtReadPixels(void* gt, int frame, void* pixels, int nbits, 
		  int xx, int yy, int dx, int dy)
{
  ostringstream str;
  str << "IISReadPixelsCmd " << frame << ' ' << pixels << ' ' 
      << xx << ' ' << yy << ' ' << dx << ' ' << dy << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "GtReadPixels() " << str.str().c_str() << endl;
}

void xim_cursorMode(XimDataPtr xim, int state)
{
  ostringstream str;
  str << "IISCursorModeCmd " << state << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "xim_cursorMode() " << state << endl;
}

void xim_getCursorPos(XimDataPtr xim, float* xx, float* yy, 
		      int* raster, int* frame)
{
  {
    ostringstream str;
    str << "IISGetCursorPosCmd " << ends;
    iis->eval((char*)str.str().c_str());
  }
  if (IISDebug)
    cerr << "xim_getCursorPos()" << endl;
  {
    string x(iis->result());
    istringstream str(x);
    str >> *xx >> *yy  >> *frame;
    *raster = *frame;
  }
}

void xim_setCursorPos(XimDataPtr xim, int xx, int yy)
{
  ostringstream str;
  str << "IISSetCursorPosCmd " << xx << ' ' << yy << ends;
  iis->eval((char*)str.str().c_str());

  if (IISDebug)
    cerr << "xim_setCursorPos()" << endl;
}