1 | #include <stdio.h>
|
---|
2 | #include <X11/cursorfont.h>
|
---|
3 | #include <X11/IntrinsicP.h>
|
---|
4 | #include <X11/Shell.h>
|
---|
5 | #include <X11/StringDefs.h>
|
---|
6 | #include <Xm/Xm.h>
|
---|
7 | #include "sopnamsp.h"
|
---|
8 | #include "piapplx.h"
|
---|
9 | #include "picontainerx.h"
|
---|
10 | #include "picons.h"
|
---|
11 |
|
---|
12 | // $CHECK$ - Reza Aout 99
|
---|
13 | // Il y a un probleme sur la gestion de la taille de la fenetre principale
|
---|
14 | // SetBinding(elastic) ne marche correctement pas sur la fenetre principale
|
---|
15 | // et Des SetSize intempestive peuvent generer des comportements bizarres !
|
---|
16 |
|
---|
17 | // Pour rediriger stdout
|
---|
18 | #include <unistd.h>
|
---|
19 | #include <fcntl.h>
|
---|
20 | #include <sys/types.h>
|
---|
21 | #include <sys/stat.h>
|
---|
22 |
|
---|
23 | #include <iostream>
|
---|
24 |
|
---|
25 |
|
---|
26 | // #define DEBUG_APPLX
|
---|
27 |
|
---|
28 | static Cursor a_curs[3];
|
---|
29 | static bool a_fgcur=false;
|
---|
30 |
|
---|
31 | // Voir fichier pimenux.cc , Pour resoudre certains conflits avec les WindowManagers
|
---|
32 | void SetTopWdgForMenuX(SysDWdg mtw);
|
---|
33 |
|
---|
34 | static PIApplication* cur_piapp = NULL;
|
---|
35 | PIApplication* PIApplicationGetApp() { return cur_piapp; }
|
---|
36 |
|
---|
37 |
|
---|
38 | // Classe de container special pour top-container de Application X
|
---|
39 |
|
---|
40 | class PITopContAppX : public PIContainer {
|
---|
41 | public :
|
---|
42 | PITopContAppX(PIContainer *par, char *nom, PIWdg* topw,
|
---|
43 | int sx=10, int sy=10, int px=0, int py=0);
|
---|
44 | virtual ~PITopContAppX();
|
---|
45 |
|
---|
46 | virtual void SetSize(int sx, int sy);
|
---|
47 | virtual void SetPos(int px, int py);
|
---|
48 |
|
---|
49 | PIWdg* mTopWdg;
|
---|
50 | };
|
---|
51 |
|
---|
52 |
|
---|
53 | /* --Methode-- */
|
---|
54 | PITopContAppX::PITopContAppX(PIContainer *par, char *nom, PIWdg* topw, int sx, int sy, int px, int py)
|
---|
55 | : PIContainer(par, nom, (sx>10)?sx:10, (sy>10)?sy:10, px, py)
|
---|
56 | {
|
---|
57 | mTopWdg = topw;
|
---|
58 | SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* --Methode-- */
|
---|
62 | PITopContAppX::~PITopContAppX()
|
---|
63 | {
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* --Methode-- */
|
---|
67 | void PITopContAppX::SetSize(int sx, int sy)
|
---|
68 | {
|
---|
69 | if ((sx <10) || (sy < 10) ) return;
|
---|
70 |
|
---|
71 | int px = XPos();
|
---|
72 | int py = YPos();
|
---|
73 | // Probleme sur Sun - Reza 10/99 , REGLE 08/2000
|
---|
74 |
|
---|
75 | mTopWdg->SetSize(sx+px, sy+py);
|
---|
76 |
|
---|
77 | XtConfigureWidget(Parent()->XtWdg(), 0, 0, sx+px, sy+py, 0);
|
---|
78 | XtConfigureWidget(XtWdg(), px, py, sx, sy, 0);
|
---|
79 | // SetPos(px, py);
|
---|
80 | // PIContainer::SetSize(sx, sy);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /* --Methode-- */
|
---|
85 | void PITopContAppX::SetPos(int px, int py)
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 | // ---------------------------------------------
|
---|
90 | // Fonction pour gestion des close-window
|
---|
91 |
|
---|
92 | /* Nouvelle-Fonction */
|
---|
93 | static int fgactl = 0;
|
---|
94 |
|
---|
95 | static void CloseWindow (Widget w, XEvent*, String*,Cardinal*)
|
---|
96 | {
|
---|
97 | XtCallCallbacks(w, XtNpopdownCallback, NULL);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* Nouvelle-Fonction */
|
---|
101 | static void popdwn_cb_app(Widget /*w*/, XtPointer *usd, XtPointer * )
|
---|
102 | {
|
---|
103 | PIApplicationX * app = (PIApplicationX *) usd;
|
---|
104 | app->SendSelf(0,PIMsg_Close, NULL) ;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /* --Methode-- */
|
---|
109 | PIApplicationX::PIApplicationX(int sx, int sy, int narg, char *arg[])
|
---|
110 | : PIApplicationGen()
|
---|
111 | {
|
---|
112 | int sxt, syt;
|
---|
113 |
|
---|
114 | #ifdef DEBUG_APPLX
|
---|
115 | puts("PIApplicationX::PIApplicationX()_info : App creation");
|
---|
116 | #endif
|
---|
117 | mStop = true;
|
---|
118 |
|
---|
119 |
|
---|
120 | topwdg = new PIWdgX(narg, arg);
|
---|
121 |
|
---|
122 | int msx,msy;
|
---|
123 | PrefCompSize(msx, msy);
|
---|
124 | msy+=5;
|
---|
125 | topwdg->SetSize(sx, sy+msy);
|
---|
126 | //DBG printf("PIApplicationX-DBG:: Size=%d %d \n", topwdg->XSize(), topwdg->YSize());
|
---|
127 |
|
---|
128 | intcont = new PIContainerX((PIMsgHandler *)this, topwdg, "MBCont",
|
---|
129 | sx, sy+msy, 0, 0);
|
---|
130 | // Pb avec les Popup MenuX et certains Window Manager , voir pimenux.cc
|
---|
131 | SetTopWdgForMenuX(intcont->XtWdg());
|
---|
132 |
|
---|
133 | menubar = new PIMenubar(this, "DefMenubar");
|
---|
134 | Menubar()->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_free);
|
---|
135 | sxt = ( sx > Menubar()->XSize() ) ? sx : Menubar()->XSize();
|
---|
136 | syt = ( sy > 10 ) ? sy : 10;
|
---|
137 | // syt += Menubar()->YSize();
|
---|
138 | // syt += msy;
|
---|
139 | // topwdg->SetSize(sxt, syt);
|
---|
140 | // MBCont()->SetSize(sxt, syt);
|
---|
141 | if (sx < sxt) sx = sxt;
|
---|
142 | if (sy < 10) sy = 10;
|
---|
143 | topcont = new PITopContAppX(MBCont(), "TopLevelCont", topwdg,
|
---|
144 | sx, sy, 0, msy);
|
---|
145 | MBCont()->Show();
|
---|
146 | //DBG printf("PIApplicationX-DBG2:: MBContSize=%d %d \n", MBCont()->XSize(), MBCont()->YSize());
|
---|
147 |
|
---|
148 |
|
---|
149 | topcont->Show();
|
---|
150 |
|
---|
151 |
|
---|
152 | if (!a_fgcur)
|
---|
153 | {
|
---|
154 | Display *mdsp;
|
---|
155 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
156 | a_curs[0] = XCreateFontCursor(mdsp, XC_arrow);
|
---|
157 | a_curs[1] = XCreateFontCursor(mdsp, XC_X_cursor);
|
---|
158 | a_curs[2] = XCreateFontCursor(mdsp, XC_watch);
|
---|
159 | }
|
---|
160 |
|
---|
161 | topwdg->Manage();
|
---|
162 |
|
---|
163 | // ------------------------------------------------------
|
---|
164 | // Pour la gestion des close window des Window manager
|
---|
165 |
|
---|
166 | if (fgactl == 0) {
|
---|
167 | XtActionsRec desact = {(char *)"CloseWindow" ,CloseWindow};
|
---|
168 | int szx, szy, szf;
|
---|
169 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
170 | XtAppAddActions(*appctx, &desact, 1);
|
---|
171 | Display *mdsp;
|
---|
172 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
173 | Atom wmd;
|
---|
174 | wmd = XInternAtom(mdsp, "WM_DELETE_WINDOW",False);
|
---|
175 | XSetWMProtocols(mdsp, XtWindow(topwdg->XtWdg()), &wmd, 1);
|
---|
176 | fgactl = 1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | XtTranslations trans;
|
---|
180 | trans = XtParseTranslationTable("<Message>WM_PROTOCOLS:CloseWindow()");
|
---|
181 | XtOverrideTranslations(topwdg->XtWdg() , trans);
|
---|
182 | Arg warg[2];
|
---|
183 | XtSetArg(warg[0], XmNdeleteResponse, XmDO_NOTHING);
|
---|
184 | XtSetValues(topwdg->XtWdg(), warg, 1);
|
---|
185 |
|
---|
186 | XtAddCallback(topwdg->XtWdg(), XtNpopdownCallback,
|
---|
187 | (XtCallbackProc)popdwn_cb_app, (XtPointer)this);
|
---|
188 |
|
---|
189 | // ------------------------------------------------------
|
---|
190 |
|
---|
191 | cur_piapp = this;
|
---|
192 | mState = -1;
|
---|
193 | SetReady();
|
---|
194 | }
|
---|
195 |
|
---|
196 | /* --Methode-- */
|
---|
197 | PIApplicationX::~PIApplicationX()
|
---|
198 | {
|
---|
199 | #ifdef DEBUG_APPLX
|
---|
200 | puts("PIApplicationX::~PIApplicationX()_info : App delete");
|
---|
201 | #endif
|
---|
202 | Display *mdsp;
|
---|
203 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
204 | topwdg->UnManage();
|
---|
205 | delete menubar;
|
---|
206 | if (topcont != MBCont()) delete topcont;
|
---|
207 | delete intcont;
|
---|
208 | delete topwdg;
|
---|
209 | XtCloseDisplay(mdsp);
|
---|
210 | cur_piapp = NULL;
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* --Methode-- */
|
---|
215 | void PIApplicationX::Run()
|
---|
216 | {
|
---|
217 | XEvent evt;
|
---|
218 | #ifdef DEBUG_APPLX
|
---|
219 | puts("PIApplicationX::Run()_info : App Run ");
|
---|
220 | #endif
|
---|
221 |
|
---|
222 |
|
---|
223 | int szx, szy, szf;
|
---|
224 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
225 |
|
---|
226 | // Pour appeler FinishCreate() des objets dans la fenetre principale
|
---|
227 | if (mStop) { // C'est la premiere fois
|
---|
228 | topcont->SetSize(topcont->XSize(), topcont->YSize());
|
---|
229 | // topwdg->SetSize(MBCont()->XSize(), MBCont()->YSize());
|
---|
230 | MBCont()->FinishCreate();
|
---|
231 | }
|
---|
232 | else mStop = true; // On rerentre apres un stop
|
---|
233 | while (mStop)
|
---|
234 | {
|
---|
235 | XtAppNextEvent(*appctx, &evt);
|
---|
236 | XtDispatchEvent(&evt);
|
---|
237 | }
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | /* --Methode-- */
|
---|
243 | void PIApplicationX::SetReady()
|
---|
244 | {
|
---|
245 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
246 | if (mState != kReadyState)
|
---|
247 | {
|
---|
248 | mState = kReadyState;
|
---|
249 | Menubar()->SetSensitive();
|
---|
250 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
|
---|
251 | }
|
---|
252 | XFlush(mdsp);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /* --Methode-- */
|
---|
256 | void PIApplicationX::SetBusy()
|
---|
257 | {
|
---|
258 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
259 | if (mState != kBusyState)
|
---|
260 | {
|
---|
261 | mState = kBusyState;
|
---|
262 | Menubar()->SetSensitive();
|
---|
263 | // if ( XtIsRealized(topwdg->XtWdg()) )
|
---|
264 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
|
---|
265 | }
|
---|
266 | XFlush(mdsp);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /* --Methode-- */
|
---|
270 | void PIApplicationX::SetBlocked()
|
---|
271 | {
|
---|
272 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
273 | if (mState != kBlockedState)
|
---|
274 | {
|
---|
275 | mState = kBlockedState;
|
---|
276 | Menubar()->SetUnSensitive();
|
---|
277 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
|
---|
278 | }
|
---|
279 | XFlush(mdsp);
|
---|
280 | return;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /* --Methode-- */
|
---|
285 | void PIApplicationX::PrefCompSize(int& szx, int& szy)
|
---|
286 | {
|
---|
287 | int szf;
|
---|
288 | PIXtAppCtx(szx, szy, szf);
|
---|
289 | return;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /* --Methode-- */
|
---|
293 | void PIApplicationX::ScreenSize(int& szx, int& szy)
|
---|
294 | {
|
---|
295 | Display * dsp = PIXDisplay();
|
---|
296 | szx = DisplayWidth(dsp, DefaultScreen(dsp));
|
---|
297 | szy = DisplayHeight(dsp, DefaultScreen(dsp));
|
---|
298 | }
|
---|
299 |
|
---|
300 | /* --Methode-- */
|
---|
301 | void PIApplicationX::ScreenSizeMM(int& szx, int& szy)
|
---|
302 | {
|
---|
303 | Display * dsp = PIXDisplay();
|
---|
304 | szx = DisplayWidthMM(dsp, DefaultScreen(dsp));
|
---|
305 | szy = DisplayHeightMM(dsp, DefaultScreen(dsp));
|
---|
306 | }
|
---|
307 |
|
---|
308 | /* --Methode-- */
|
---|
309 | void PIApplicationX::ScreenResolution(int& resolx, int& resoly)
|
---|
310 | {
|
---|
311 | Display * dsp = PIXDisplay();
|
---|
312 | resolx = (int)((double)DisplayWidth(dsp, DefaultScreen(dsp))*10./
|
---|
313 | (double)DisplayWidthMM(dsp, DefaultScreen(dsp)));
|
---|
314 | resoly = (int)((double)DisplayHeight(dsp, DefaultScreen(dsp))*10./
|
---|
315 | (double)DisplayHeightMM(dsp, DefaultScreen(dsp)));
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 |
|
---|
320 |
|
---|
321 |
|
---|
322 | /* Call-Back - Fonction privee de ce fichier */
|
---|
323 | static void redirectstream_callback(XtPointer, int *, XtInputId*);
|
---|
324 |
|
---|
325 | static PIConsole* consstream[2] = {NULL, NULL};
|
---|
326 | static unsigned char streamva[2] = { PIVA_Def, PIVA_Ital};
|
---|
327 | static int streamno[2] = {0,1};
|
---|
328 | static XtInputId inputid[2];
|
---|
329 | static int origfiledes[2]={-1, -1}; // descripteurs de fichiers de depart
|
---|
330 | static bool fgcopieos[2]={false, false}; // Si true, recopie sur descripteurs de depart, en plus de redirection
|
---|
331 | static int readfiledes[2]={-1, -1}; // descripteurs de fichiers ouvert en lecture , si redirection a travers fichier
|
---|
332 | static XtIntervalId intervid[2];
|
---|
333 | static unsigned long tm_interv = 40; // intervalle timer = 40 ms
|
---|
334 |
|
---|
335 | //DBG static FILE * dbgfip = NULL;
|
---|
336 |
|
---|
337 | // call-back pour gestion de redirection stdout/stderr avec pipe
|
---|
338 | static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
|
---|
339 | {
|
---|
340 | char buff[512];
|
---|
341 | int nr, tnr;
|
---|
342 |
|
---|
343 | int idx = *((int*)cld);
|
---|
344 | if (idx != 1) idx = 0;
|
---|
345 | if (!consstream[idx]) return;
|
---|
346 | tnr = 0;
|
---|
347 | while ( (nr=read(*fd, buff, 511)) > 0 ) {
|
---|
348 | buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
|
---|
349 | tnr += nr;
|
---|
350 | }
|
---|
351 | if (tnr > 0) consstream[idx]->Refresh();
|
---|
352 | }
|
---|
353 |
|
---|
354 | // call-back pour gestion de redirection stdout/stderr avec pipe
|
---|
355 | static void redirectstream_timeout_callback(XtPointer cld, XtIntervalId * /*tid*/)
|
---|
356 | {
|
---|
357 | char buff[512];
|
---|
358 | int nr, tnr;
|
---|
359 |
|
---|
360 | int idx = *((int*)cld);
|
---|
361 | if (idx != 1) idx = 0;
|
---|
362 | if (!consstream[idx]) return;
|
---|
363 | tnr = 0;
|
---|
364 | while ( (nr=read(readfiledes[idx], buff, 511)) > 0 ) {
|
---|
365 | buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
|
---|
366 | tnr += nr;
|
---|
367 | if (fgcopieos[idx]) write(origfiledes[idx], buff, nr); // recopie sur filedesc original
|
---|
368 | }
|
---|
369 | if (tnr > 0) consstream[idx]->Refresh();
|
---|
370 | int szx, szy, szf;
|
---|
371 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
372 | intervid[idx] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback, (XtPointer)(streamno+idx));
|
---|
373 | }
|
---|
374 |
|
---|
375 | /* --Methode-- */
|
---|
376 | void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
|
---|
377 | {
|
---|
378 | if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD, 0);
|
---|
379 | if ( cons == consstream[0]) {
|
---|
380 | streamva[0] = va;
|
---|
381 | return;
|
---|
382 | }
|
---|
383 | if ( (consstream[0]) && (cons) ) { consstream[0] = cons; streamva[0] = va; return; }
|
---|
384 | else if (!cons) {
|
---|
385 | consstream[0] = NULL;
|
---|
386 | XtRemoveInput(inputid[0]);
|
---|
387 | dup2(origfiledes[0], 1);
|
---|
388 | return;
|
---|
389 | }
|
---|
390 |
|
---|
391 | consstream[0] = cons; streamva[0] = va;
|
---|
392 |
|
---|
393 | int p[2];
|
---|
394 | pipe(p);
|
---|
395 | // Redirection de stdout (fid=1) :
|
---|
396 | close(1);
|
---|
397 | dup(p[1]);
|
---|
398 | close(p[1]);
|
---|
399 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
400 |
|
---|
401 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
402 | setlinebuf(stdout);
|
---|
403 | #endif
|
---|
404 | ios::sync_with_stdio();
|
---|
405 |
|
---|
406 | int szx, szy, szf;
|
---|
407 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
408 | inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
409 | redirectstream_callback, (XtPointer) streamno);
|
---|
410 | //DBG if (dbgfip == NULL) dbgfip = fopen("debug.log","w");
|
---|
411 | }
|
---|
412 |
|
---|
413 | /* --Methode-- */
|
---|
414 | void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
|
---|
415 | {
|
---|
416 | if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD, 0);
|
---|
417 | if ( cons == consstream[1]) {
|
---|
418 | streamva[1] = va;
|
---|
419 | return;
|
---|
420 | }
|
---|
421 | if ( (consstream[1]) && (cons) ) { consstream[1] = cons; streamva[1] = va; return; }
|
---|
422 | else if (!cons) {
|
---|
423 | consstream[1] = NULL;
|
---|
424 | XtRemoveInput(inputid[1]);
|
---|
425 | dup2(origfiledes[1], 2);
|
---|
426 | return;
|
---|
427 | }
|
---|
428 |
|
---|
429 | consstream[1] = cons; streamva[1] = va;
|
---|
430 |
|
---|
431 | int p[2];
|
---|
432 | pipe(p);
|
---|
433 | // Redirection de stderr (fid=2) :
|
---|
434 | close(2);
|
---|
435 | dup(p[1]);
|
---|
436 | close(p[1]);
|
---|
437 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
438 |
|
---|
439 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
440 | setlinebuf(stderr);
|
---|
441 | #endif
|
---|
442 | ios::sync_with_stdio();
|
---|
443 |
|
---|
444 | int szx, szy, szf;
|
---|
445 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
446 | inputid[1] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
447 | redirectstream_callback, (XtPointer) (streamno+1));
|
---|
448 | }
|
---|
449 |
|
---|
450 | /* --Methode-- */
|
---|
451 | void PIApplicationX::RedirectOutStream(PIConsole* cons, string const & flnm, bool fgcpos, unsigned char va)
|
---|
452 | {
|
---|
453 | if (origfiledes[0] < 0) {
|
---|
454 | origfiledes[0] = dup(1);
|
---|
455 | if (origfiledes[0] < 0) { perror("RedirectOutStream()/ERROR ofd[0]<0 "); return; }
|
---|
456 | }
|
---|
457 | if (readfiledes[0] > -1) { close(readfiledes[0]); readfiledes[0] = -1;}
|
---|
458 | if (!cons) { // requete d'annulation de redirection
|
---|
459 | if (origfiledes[0] > -1) dup2(origfiledes[0], 1);
|
---|
460 | XtRemoveTimeOut(intervid[0]);
|
---|
461 | consstream[0] = NULL;
|
---|
462 | return;
|
---|
463 | }
|
---|
464 |
|
---|
465 | int fidso = open(flnm.c_str(), O_CREAT|O_WRONLY|O_NONBLOCK, S_IRUSR|S_IWUSR);
|
---|
466 | if (fidso < 0) {
|
---|
467 | printf("PIApplicationX::RedirectOutStream()/ERROR creating file %s \n", flnm.c_str());
|
---|
468 | return;
|
---|
469 | }
|
---|
470 | int rdfid = open(flnm.c_str(), O_RDONLY|O_NONBLOCK, 0);
|
---|
471 | if (rdfid < 0) {
|
---|
472 | printf("PIApplicationX::RedirectOutStream()/ERROR opening file %s for read \n", flnm.c_str());
|
---|
473 | return;
|
---|
474 | }
|
---|
475 | readfiledes[0] = rdfid;
|
---|
476 | dup2(fidso, 1); // close(1=stdout), recopie fidso en fd=1
|
---|
477 | close(fidso); // on ferme le fidso (on l'a recopie en fd=1)
|
---|
478 | consstream[0] = cons; streamva[0] = va; fgcopieos[0] = fgcpos;
|
---|
479 |
|
---|
480 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
481 | setlinebuf(stdout);
|
---|
482 | #endif
|
---|
483 | ios::sync_with_stdio();
|
---|
484 |
|
---|
485 | int szx, szy, szf;
|
---|
486 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
487 | intervid[0] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback,
|
---|
488 | (XtPointer)(streamno+0));
|
---|
489 |
|
---|
490 | //DBG if (dbgfip == NULL) dbgfip = fopen("debug.log","w");
|
---|
491 | }
|
---|
492 |
|
---|
493 | /* --Methode-- */
|
---|
494 | void PIApplicationX::RedirectErrStream(PIConsole* cons, string const & flnm, bool fgcpos, unsigned char va)
|
---|
495 | {
|
---|
496 | if (origfiledes[1] < 0) {
|
---|
497 | origfiledes[1] = dup(2);
|
---|
498 | if (origfiledes[1] < 0) { perror("RedirectOutStream()/ERROR ofd[1]<0 "); return; }
|
---|
499 | }
|
---|
500 | if (readfiledes[1] > -1) { close(readfiledes[1]); readfiledes[1] = -1;}
|
---|
501 | if (!cons) { // requete d'annulation de redirection
|
---|
502 | if (origfiledes[1] > -1) dup2(origfiledes[1], 2);
|
---|
503 | XtRemoveTimeOut(intervid[1]);
|
---|
504 | consstream[1] = NULL;
|
---|
505 | return;
|
---|
506 | }
|
---|
507 |
|
---|
508 | int fidso = open(flnm.c_str(), O_CREAT|O_WRONLY|O_NONBLOCK, S_IRUSR|S_IWUSR);
|
---|
509 | if (fidso < 0) {
|
---|
510 | printf("PIApplicationX::RedirectOutStream()/ERROR creating file %s \n", flnm.c_str());
|
---|
511 | return;
|
---|
512 | }
|
---|
513 | int rdfid = open(flnm.c_str(), O_RDONLY|O_NONBLOCK, 0);
|
---|
514 | if (rdfid < 0) {
|
---|
515 | printf("PIApplicationX::RedirectOutStream()/ERROR opening file %s for read \n", flnm.c_str());
|
---|
516 | return;
|
---|
517 | }
|
---|
518 | readfiledes[1] = rdfid;
|
---|
519 | dup2(fidso, 2); // close(2=stderr), recopie fidso en fd=2
|
---|
520 | close(fidso); // on ferme le fidso (on l'a recopie en fd=2)
|
---|
521 | consstream[1] = cons; streamva[1] = va; fgcopieos[1] = fgcpos;
|
---|
522 |
|
---|
523 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
524 | setlinebuf(stderr);
|
---|
525 | #endif
|
---|
526 | ios::sync_with_stdio();
|
---|
527 |
|
---|
528 | int szx, szy, szf;
|
---|
529 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
530 | intervid[1] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback,
|
---|
531 | (XtPointer)(streamno+1));
|
---|
532 | }
|
---|
533 |
|
---|