[2] | 1 | #include <stdio.h>
|
---|
[18] | 2 | #include <X11/cursorfont.h>
|
---|
[1074] | 3 | #include <X11/IntrinsicP.h>
|
---|
[1263] | 4 | #include <X11/Shell.h>
|
---|
| 5 | #include <X11/StringDefs.h>
|
---|
| 6 | #include <Xm/Xm.h>
|
---|
[2615] | 7 | #include "sopnamsp.h"
|
---|
[2] | 8 | #include "piapplx.h"
|
---|
| 9 | #include "picontainerx.h"
|
---|
[3475] | 10 | #include "picons.h"
|
---|
[2] | 11 |
|
---|
[365] | 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 !
|
---|
[2] | 16 |
|
---|
[78] | 17 | // Pour rediriger stdout
|
---|
| 18 | #include <unistd.h>
|
---|
| 19 | #include <fcntl.h>
|
---|
[3369] | 20 | #include <sys/types.h>
|
---|
| 21 | #include <sys/stat.h>
|
---|
[2] | 22 |
|
---|
[2322] | 23 | #include <iostream>
|
---|
[78] | 24 |
|
---|
[135] | 25 |
|
---|
[78] | 26 | // #define DEBUG_APPLX
|
---|
| 27 |
|
---|
[18] | 28 | static Cursor a_curs[3];
|
---|
| 29 | static bool a_fgcur=false;
|
---|
| 30 |
|
---|
[60] | 31 | // Voir fichier pimenux.cc , Pour resoudre certains conflits avec les WindowManagers
|
---|
| 32 | void SetTopWdgForMenuX(SysDWdg mtw);
|
---|
| 33 |
|
---|
[111] | 34 | static PIApplication* cur_piapp = NULL;
|
---|
| 35 | PIApplication* PIApplicationGetApp() { return cur_piapp; }
|
---|
| 36 |
|
---|
[126] | 37 |
|
---|
[132] | 38 | // Classe de container special pour top-container de Application X
|
---|
[126] | 39 |
|
---|
[132] | 40 | class PITopContAppX : public PIContainer {
|
---|
| 41 | public :
|
---|
[457] | 42 | PITopContAppX(PIContainer *par, char *nom, PIWdg* topw,
|
---|
[132] | 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);
|
---|
[457] | 48 |
|
---|
| 49 | PIWdg* mTopWdg;
|
---|
[132] | 50 | };
|
---|
| 51 |
|
---|
[1074] | 52 |
|
---|
[2] | 53 | /* --Methode-- */
|
---|
[457] | 54 | PITopContAppX::PITopContAppX(PIContainer *par, char *nom, PIWdg* topw, int sx, int sy, int px, int py)
|
---|
[365] | 55 | : PIContainer(par, nom, (sx>10)?sx:10, (sy>10)?sy:10, px, py)
|
---|
[132] | 56 | {
|
---|
[457] | 57 | mTopWdg = topw;
|
---|
[365] | 58 | SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
|
---|
[132] | 59 | }
|
---|
| 60 |
|
---|
| 61 | /* --Methode-- */
|
---|
| 62 | PITopContAppX::~PITopContAppX()
|
---|
| 63 | {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /* --Methode-- */
|
---|
| 67 | void PITopContAppX::SetSize(int sx, int sy)
|
---|
| 68 | {
|
---|
[365] | 69 | if ((sx <10) || (sy < 10) ) return;
|
---|
[1074] | 70 |
|
---|
| 71 | int px = XPos();
|
---|
| 72 | int py = YPos();
|
---|
[1133] | 73 | // Probleme sur Sun - Reza 10/99 , REGLE 08/2000
|
---|
| 74 |
|
---|
[1074] | 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);
|
---|
[132] | 81 | }
|
---|
| 82 |
|
---|
[365] | 83 |
|
---|
[132] | 84 | /* --Methode-- */
|
---|
| 85 | void PITopContAppX::SetPos(int px, int py)
|
---|
| 86 | {
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[1263] | 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 |
|
---|
[132] | 108 | /* --Methode-- */
|
---|
[2] | 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;
|
---|
[365] | 118 |
|
---|
| 119 |
|
---|
[2] | 120 | topwdg = new PIWdgX(narg, arg);
|
---|
| 121 |
|
---|
[365] | 122 | int msx,msy;
|
---|
[1592] | 123 | PrefCompSize(msx, msy);
|
---|
[365] | 124 | msy+=5;
|
---|
| 125 | topwdg->SetSize(sx, sy+msy);
|
---|
[3216] | 126 | //DBG printf("PIApplicationX-DBG:: Size=%d %d \n", topwdg->XSize(), topwdg->YSize());
|
---|
[365] | 127 |
|
---|
[2] | 128 | intcont = new PIContainerX((PIMsgHandler *)this, topwdg, "MBCont",
|
---|
[365] | 129 | sx, sy+msy, 0, 0);
|
---|
[60] | 130 | // Pb avec les Popup MenuX et certains Window Manager , voir pimenux.cc
|
---|
| 131 | SetTopWdgForMenuX(intcont->XtWdg());
|
---|
| 132 |
|
---|
[2] | 133 | menubar = new PIMenubar(this, "DefMenubar");
|
---|
[23] | 134 | Menubar()->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_free);
|
---|
[2] | 135 | sxt = ( sx > Menubar()->XSize() ) ? sx : Menubar()->XSize();
|
---|
[365] | 136 | syt = ( sy > 10 ) ? sy : 10;
|
---|
| 137 | // syt += Menubar()->YSize();
|
---|
| 138 | // syt += msy;
|
---|
| 139 | // topwdg->SetSize(sxt, syt);
|
---|
| 140 | // MBCont()->SetSize(sxt, syt);
|
---|
[132] | 141 | if (sx < sxt) sx = sxt;
|
---|
| 142 | if (sy < 10) sy = 10;
|
---|
[457] | 143 | topcont = new PITopContAppX(MBCont(), "TopLevelCont", topwdg,
|
---|
[132] | 144 | sx, sy, 0, msy);
|
---|
[163] | 145 | MBCont()->Show();
|
---|
[3216] | 146 | //DBG printf("PIApplicationX-DBG2:: MBContSize=%d %d \n", MBCont()->XSize(), MBCont()->YSize());
|
---|
[2] | 147 |
|
---|
[1074] | 148 |
|
---|
[365] | 149 | topcont->Show();
|
---|
[18] | 150 |
|
---|
[365] | 151 |
|
---|
[18] | 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 |
|
---|
[365] | 161 | topwdg->Manage();
|
---|
[1263] | 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 |
|
---|
[111] | 191 | cur_piapp = this;
|
---|
[18] | 192 | mState = -1;
|
---|
| 193 | SetReady();
|
---|
[2] | 194 | }
|
---|
| 195 |
|
---|
| 196 | /* --Methode-- */
|
---|
| 197 | PIApplicationX::~PIApplicationX()
|
---|
| 198 | {
|
---|
| 199 | #ifdef DEBUG_APPLX
|
---|
| 200 | puts("PIApplicationX::~PIApplicationX()_info : App delete");
|
---|
| 201 | #endif
|
---|
[18] | 202 | Display *mdsp;
|
---|
| 203 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[52] | 204 | topwdg->UnManage();
|
---|
[6] | 205 | delete menubar;
|
---|
| 206 | if (topcont != MBCont()) delete topcont;
|
---|
| 207 | delete intcont;
|
---|
[2] | 208 | delete topwdg;
|
---|
[18] | 209 | XtCloseDisplay(mdsp);
|
---|
[111] | 210 | cur_piapp = NULL;
|
---|
[18] | 211 | return;
|
---|
[2] | 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 |
|
---|
[114] | 223 | int szx, szy, szf;
|
---|
| 224 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 225 |
|
---|
[110] | 226 | // Pour appeler FinishCreate() des objets dans la fenetre principale
|
---|
[365] | 227 | if (mStop) { // C'est la premiere fois
|
---|
[1074] | 228 | topcont->SetSize(topcont->XSize(), topcont->YSize());
|
---|
| 229 | // topwdg->SetSize(MBCont()->XSize(), MBCont()->YSize());
|
---|
[365] | 230 | MBCont()->FinishCreate();
|
---|
| 231 | }
|
---|
[141] | 232 | else mStop = true; // On rerentre apres un stop
|
---|
[2] | 233 | while (mStop)
|
---|
| 234 | {
|
---|
[90] | 235 | XtAppNextEvent(*appctx, &evt);
|
---|
[2] | 236 | XtDispatchEvent(&evt);
|
---|
| 237 | }
|
---|
| 238 | return;
|
---|
| 239 | }
|
---|
[18] | 240 |
|
---|
| 241 |
|
---|
| 242 | /* --Methode-- */
|
---|
| 243 | void PIApplicationX::SetReady()
|
---|
| 244 | {
|
---|
[138] | 245 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[18] | 246 | if (mState != kReadyState)
|
---|
| 247 | {
|
---|
| 248 | mState = kReadyState;
|
---|
[106] | 249 | Menubar()->SetSensitive();
|
---|
[18] | 250 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
|
---|
| 251 | }
|
---|
[138] | 252 | XFlush(mdsp);
|
---|
[18] | 253 | }
|
---|
| 254 |
|
---|
| 255 | /* --Methode-- */
|
---|
| 256 | void PIApplicationX::SetBusy()
|
---|
| 257 | {
|
---|
[138] | 258 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[18] | 259 | if (mState != kBusyState)
|
---|
| 260 | {
|
---|
| 261 | mState = kBusyState;
|
---|
[106] | 262 | Menubar()->SetSensitive();
|
---|
[18] | 263 | // if ( XtIsRealized(topwdg->XtWdg()) )
|
---|
| 264 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
|
---|
| 265 | }
|
---|
[138] | 266 | XFlush(mdsp);
|
---|
[18] | 267 | }
|
---|
| 268 |
|
---|
| 269 | /* --Methode-- */
|
---|
| 270 | void PIApplicationX::SetBlocked()
|
---|
| 271 | {
|
---|
[138] | 272 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[18] | 273 | if (mState != kBlockedState)
|
---|
| 274 | {
|
---|
| 275 | mState = kBlockedState;
|
---|
[106] | 276 | Menubar()->SetUnSensitive();
|
---|
[18] | 277 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
|
---|
| 278 | }
|
---|
[138] | 279 | XFlush(mdsp);
|
---|
| 280 | return;
|
---|
[18] | 281 | }
|
---|
[75] | 282 |
|
---|
| 283 |
|
---|
| 284 | /* --Methode-- */
|
---|
[1592] | 285 | void PIApplicationX::PrefCompSize(int& szx, int& szy)
|
---|
[75] | 286 | {
|
---|
[114] | 287 | int szf;
|
---|
| 288 | PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 289 | return;
|
---|
[75] | 290 | }
|
---|
| 291 |
|
---|
[92] | 292 | /* --Methode-- */
|
---|
[1592] | 293 | void PIApplicationX::ScreenSize(int& szx, int& szy)
|
---|
[92] | 294 | {
|
---|
| 295 | Display * dsp = PIXDisplay();
|
---|
| 296 | szx = DisplayWidth(dsp, DefaultScreen(dsp));
|
---|
| 297 | szy = DisplayHeight(dsp, DefaultScreen(dsp));
|
---|
| 298 | }
|
---|
[75] | 299 |
|
---|
[1592] | 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 | }
|
---|
[92] | 307 |
|
---|
[1592] | 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 | }
|
---|
[92] | 317 |
|
---|
[1592] | 318 |
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 |
|
---|
[78] | 322 | /* Call-Back - Fonction privee de ce fichier */
|
---|
[81] | 323 | static void redirectstream_callback(XtPointer, int *, XtInputId*);
|
---|
[78] | 324 |
|
---|
[81] | 325 | static PIConsole* consstream[2] = {NULL, NULL};
|
---|
| 326 | static unsigned char streamva[2] = { PIVA_Def, PIVA_Ital};
|
---|
| 327 | static int streamno[2] = {0,1};
|
---|
[135] | 328 | static XtInputId inputid[2];
|
---|
| 329 | static int origfiledes[2]={-1, -1}; // descripteurs de fichiers de depart
|
---|
[3369] | 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
|
---|
[78] | 334 |
|
---|
[3369] | 335 | //DBG static FILE * dbgfip = NULL;
|
---|
| 336 |
|
---|
| 337 | // call-back pour gestion de redirection stdout/stderr avec pipe
|
---|
[81] | 338 | static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
|
---|
[78] | 339 | {
|
---|
[2607] | 340 | char buff[512];
|
---|
[3369] | 341 | int nr, tnr;
|
---|
[81] | 342 |
|
---|
| 343 | int idx = *((int*)cld);
|
---|
| 344 | if (idx != 1) idx = 0;
|
---|
[135] | 345 | if (!consstream[idx]) return;
|
---|
[3369] | 346 | tnr = 0;
|
---|
| 347 | while ( (nr=read(*fd, buff, 511)) > 0 ) {
|
---|
[81] | 348 | buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
|
---|
[3369] | 349 | tnr += nr;
|
---|
[78] | 350 | }
|
---|
[3369] | 351 | if (tnr > 0) consstream[idx]->Refresh();
|
---|
[78] | 352 | }
|
---|
| 353 |
|
---|
[3369] | 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 |
|
---|
[78] | 375 | /* --Methode-- */
|
---|
[81] | 376 | void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
|
---|
[78] | 377 | {
|
---|
[3369] | 378 | if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD, 0);
|
---|
| 379 | if ( cons == consstream[0]) {
|
---|
| 380 | streamva[0] = va;
|
---|
| 381 | return;
|
---|
| 382 | }
|
---|
[135] | 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 | }
|
---|
[78] | 390 |
|
---|
[81] | 391 | consstream[0] = cons; streamva[0] = va;
|
---|
| 392 |
|
---|
[78] | 393 | int p[2];
|
---|
| 394 | pipe(p);
|
---|
[90] | 395 | // Redirection de stdout (fid=1) :
|
---|
[78] | 396 | close(1);
|
---|
| 397 | dup(p[1]);
|
---|
| 398 | close(p[1]);
|
---|
| 399 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
[135] | 400 |
|
---|
[365] | 401 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
[85] | 402 | setlinebuf(stdout);
|
---|
[163] | 403 | #endif
|
---|
[135] | 404 | ios::sync_with_stdio();
|
---|
[78] | 405 |
|
---|
[114] | 406 | int szx, szy, szf;
|
---|
| 407 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[135] | 408 | inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
| 409 | redirectstream_callback, (XtPointer) streamno);
|
---|
[3369] | 410 | //DBG if (dbgfip == NULL) dbgfip = fopen("debug.log","w");
|
---|
[78] | 411 | }
|
---|
[81] | 412 |
|
---|
| 413 | /* --Methode-- */
|
---|
| 414 | void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
|
---|
| 415 | {
|
---|
[3369] | 416 | if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD, 0);
|
---|
| 417 | if ( cons == consstream[1]) {
|
---|
| 418 | streamva[1] = va;
|
---|
| 419 | return;
|
---|
| 420 | }
|
---|
[135] | 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 | }
|
---|
[81] | 428 |
|
---|
| 429 | consstream[1] = cons; streamva[1] = va;
|
---|
| 430 |
|
---|
| 431 | int p[2];
|
---|
| 432 | pipe(p);
|
---|
[135] | 433 | // Redirection de stderr (fid=2) :
|
---|
[81] | 434 | close(2);
|
---|
| 435 | dup(p[1]);
|
---|
| 436 | close(p[1]);
|
---|
| 437 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
[135] | 438 |
|
---|
[365] | 439 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
[85] | 440 | setlinebuf(stderr);
|
---|
[163] | 441 | #endif
|
---|
[135] | 442 | ios::sync_with_stdio();
|
---|
[81] | 443 |
|
---|
[114] | 444 | int szx, szy, szf;
|
---|
| 445 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[135] | 446 | inputid[1] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
| 447 | redirectstream_callback, (XtPointer) (streamno+1));
|
---|
[81] | 448 | }
|
---|
| 449 |
|
---|
[3369] | 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
|
---|
[3372] | 502 | if (origfiledes[1] > -1) dup2(origfiledes[1], 2);
|
---|
[3369] | 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 |
|
---|