[2] | 1 | #include <stdio.h>
|
---|
[18] | 2 | #include <X11/cursorfont.h>
|
---|
[2] | 3 | #include "piapplx.h"
|
---|
| 4 | #include "picontainerx.h"
|
---|
| 5 |
|
---|
| 6 |
|
---|
[78] | 7 | // Pour rediriger stdout
|
---|
| 8 | #include <unistd.h>
|
---|
| 9 | #include <fcntl.h>
|
---|
[2] | 10 |
|
---|
[135] | 11 | #include <iostream.h>
|
---|
[78] | 12 |
|
---|
[135] | 13 |
|
---|
[78] | 14 | // #define DEBUG_APPLX
|
---|
| 15 |
|
---|
[18] | 16 | static Cursor a_curs[3];
|
---|
| 17 | static bool a_fgcur=false;
|
---|
| 18 |
|
---|
[60] | 19 | // Voir fichier pimenux.cc , Pour resoudre certains conflits avec les WindowManagers
|
---|
| 20 | void SetTopWdgForMenuX(SysDWdg mtw);
|
---|
| 21 |
|
---|
[111] | 22 | static PIApplication* cur_piapp = NULL;
|
---|
| 23 | PIApplication* PIApplicationGetApp() { return cur_piapp; }
|
---|
| 24 |
|
---|
[126] | 25 |
|
---|
[132] | 26 | // Classe de container special pour top-container de Application X
|
---|
[126] | 27 |
|
---|
[132] | 28 | class PITopContAppX : public PIContainer {
|
---|
| 29 | public :
|
---|
| 30 | PITopContAppX(PIContainer *par, char *nom,
|
---|
| 31 | int sx=10, int sy=10, int px=0, int py=0);
|
---|
| 32 | virtual ~PITopContAppX();
|
---|
| 33 |
|
---|
| 34 | virtual void SetSize(int sx, int sy);
|
---|
| 35 | virtual void SetPos(int px, int py);
|
---|
| 36 | };
|
---|
| 37 |
|
---|
[2] | 38 | /* --Methode-- */
|
---|
[132] | 39 | PITopContAppX::PITopContAppX(PIContainer *par, char *nom, int sx, int sy, int px, int py)
|
---|
| 40 | : PIContainer(par, nom, sx, sy, px, py)
|
---|
| 41 | {
|
---|
| 42 | SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /* --Methode-- */
|
---|
| 46 | PITopContAppX::~PITopContAppX()
|
---|
| 47 | {
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /* --Methode-- */
|
---|
| 51 | void PITopContAppX::SetSize(int sx, int sy)
|
---|
| 52 | {
|
---|
| 53 | Parent()->SetSize(sx+XPos(), sy+YPos());
|
---|
| 54 | PIContainer::SetSize(sx, sy);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /* --Methode-- */
|
---|
| 58 | void PITopContAppX::SetPos(int px, int py)
|
---|
| 59 | {
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /* --Methode-- */
|
---|
[2] | 63 | PIApplicationX::PIApplicationX(int sx, int sy, int narg, char *arg[])
|
---|
| 64 | : PIApplicationGen()
|
---|
| 65 | {
|
---|
| 66 | int sxt, syt;
|
---|
| 67 |
|
---|
| 68 | #ifdef DEBUG_APPLX
|
---|
| 69 | puts("PIApplicationX::PIApplicationX()_info : App creation");
|
---|
| 70 | #endif
|
---|
| 71 | mStop = true;
|
---|
| 72 | topwdg = new PIWdgX(narg, arg);
|
---|
| 73 |
|
---|
| 74 | intcont = new PIContainerX((PIMsgHandler *)this, topwdg, "MBCont",
|
---|
| 75 | 10, 10, 0, 0);
|
---|
[60] | 76 | // Pb avec les Popup MenuX et certains Window Manager , voir pimenux.cc
|
---|
| 77 | SetTopWdgForMenuX(intcont->XtWdg());
|
---|
| 78 |
|
---|
[2] | 79 | menubar = new PIMenubar(this, "DefMenubar");
|
---|
[23] | 80 | Menubar()->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_free);
|
---|
[2] | 81 | sxt = ( sx > Menubar()->XSize() ) ? sx : Menubar()->XSize();
|
---|
| 82 | syt = ( sy > 0 ) ? sy : 0;
|
---|
[92] | 83 | int msx,msy;
|
---|
| 84 | PrefCompSz(msx, msy);
|
---|
[7] | 85 | //syt += Menubar()->YSize();
|
---|
[92] | 86 | syt += msy;
|
---|
[2] | 87 | MBCont()->SetSize(sxt, syt);
|
---|
[132] | 88 | if (sx < sxt) sx = sxt;
|
---|
| 89 | if (sy < 10) sy = 10;
|
---|
| 90 | topcont = new PITopContAppX(MBCont(), "TopLevelCont",
|
---|
| 91 | sx, sy, 0, msy);
|
---|
| 92 | topcont->Show();
|
---|
[2] | 93 |
|
---|
| 94 | MBCont()->Show();
|
---|
| 95 | topwdg->Manage();
|
---|
[18] | 96 |
|
---|
| 97 | if (!a_fgcur)
|
---|
| 98 | {
|
---|
| 99 | Display *mdsp;
|
---|
| 100 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
| 101 | a_curs[0] = XCreateFontCursor(mdsp, XC_arrow);
|
---|
| 102 | a_curs[1] = XCreateFontCursor(mdsp, XC_X_cursor);
|
---|
| 103 | a_curs[2] = XCreateFontCursor(mdsp, XC_watch);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[111] | 106 | cur_piapp = this;
|
---|
[18] | 107 | mState = -1;
|
---|
| 108 | SetReady();
|
---|
[2] | 109 | }
|
---|
| 110 |
|
---|
| 111 | /* --Methode-- */
|
---|
| 112 | PIApplicationX::~PIApplicationX()
|
---|
| 113 | {
|
---|
| 114 | #ifdef DEBUG_APPLX
|
---|
| 115 | puts("PIApplicationX::~PIApplicationX()_info : App delete");
|
---|
| 116 | #endif
|
---|
[18] | 117 | Display *mdsp;
|
---|
| 118 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[52] | 119 | topwdg->UnManage();
|
---|
[6] | 120 | delete menubar;
|
---|
| 121 | if (topcont != MBCont()) delete topcont;
|
---|
| 122 | delete intcont;
|
---|
[2] | 123 | delete topwdg;
|
---|
[18] | 124 | XtCloseDisplay(mdsp);
|
---|
[111] | 125 | cur_piapp = NULL;
|
---|
[18] | 126 | return;
|
---|
[2] | 127 | }
|
---|
| 128 |
|
---|
| 129 | /* --Methode-- */
|
---|
| 130 | void PIApplicationX::Run()
|
---|
| 131 | {
|
---|
| 132 | XEvent evt;
|
---|
| 133 | #ifdef DEBUG_APPLX
|
---|
| 134 | puts("PIApplicationX::Run()_info : App Run ");
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|
| 137 | topwdg->SetSize(MBCont()->XSize(), MBCont()->YSize());
|
---|
| 138 |
|
---|
[114] | 139 | int szx, szy, szf;
|
---|
| 140 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 141 |
|
---|
[110] | 142 | // Pour appeler FinishCreate() des objets dans la fenetre principale
|
---|
[141] | 143 | if (mStop) MBCont()->FinishCreate(); // C'est la premiere fois
|
---|
| 144 | else mStop = true; // On rerentre apres un stop
|
---|
[2] | 145 | while (mStop)
|
---|
| 146 | {
|
---|
[90] | 147 | XtAppNextEvent(*appctx, &evt);
|
---|
[2] | 148 | XtDispatchEvent(&evt);
|
---|
| 149 | }
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
[18] | 152 |
|
---|
| 153 |
|
---|
| 154 | /* --Methode-- */
|
---|
| 155 | void PIApplicationX::SetReady()
|
---|
| 156 | {
|
---|
[138] | 157 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[18] | 158 | if (mState != kReadyState)
|
---|
| 159 | {
|
---|
| 160 | mState = kReadyState;
|
---|
[106] | 161 | Menubar()->SetSensitive();
|
---|
[18] | 162 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
|
---|
| 163 | }
|
---|
[138] | 164 | XFlush(mdsp);
|
---|
[18] | 165 | }
|
---|
| 166 |
|
---|
| 167 | /* --Methode-- */
|
---|
| 168 | void PIApplicationX::SetBusy()
|
---|
| 169 | {
|
---|
[138] | 170 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[18] | 171 | if (mState != kBusyState)
|
---|
| 172 | {
|
---|
| 173 | mState = kBusyState;
|
---|
[106] | 174 | Menubar()->SetSensitive();
|
---|
[18] | 175 | // if ( XtIsRealized(topwdg->XtWdg()) )
|
---|
| 176 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
|
---|
| 177 | }
|
---|
[138] | 178 | XFlush(mdsp);
|
---|
[18] | 179 | }
|
---|
| 180 |
|
---|
| 181 | /* --Methode-- */
|
---|
| 182 | void PIApplicationX::SetBlocked()
|
---|
| 183 | {
|
---|
[138] | 184 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[18] | 185 | if (mState != kBlockedState)
|
---|
| 186 | {
|
---|
| 187 | mState = kBlockedState;
|
---|
[106] | 188 | Menubar()->SetUnSensitive();
|
---|
[18] | 189 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
|
---|
| 190 | }
|
---|
[138] | 191 | XFlush(mdsp);
|
---|
| 192 | return;
|
---|
[18] | 193 | }
|
---|
[75] | 194 |
|
---|
| 195 |
|
---|
| 196 | /* --Methode-- */
|
---|
[90] | 197 | void PIApplicationX::PrefCompSz(int& szx, int& szy)
|
---|
[75] | 198 | {
|
---|
[114] | 199 | int szf;
|
---|
| 200 | PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 201 | return;
|
---|
[75] | 202 | }
|
---|
| 203 |
|
---|
[92] | 204 | /* --Methode-- */
|
---|
| 205 | void PIApplicationX::ScreenSz(int& szx, int& szy)
|
---|
| 206 | {
|
---|
| 207 | Display * dsp = PIXDisplay();
|
---|
| 208 | szx = DisplayWidth(dsp, DefaultScreen(dsp));
|
---|
| 209 | szy = DisplayHeight(dsp, DefaultScreen(dsp));
|
---|
| 210 | }
|
---|
[75] | 211 |
|
---|
[92] | 212 |
|
---|
| 213 |
|
---|
[78] | 214 | /* Call-Back - Fonction privee de ce fichier */
|
---|
[81] | 215 | static void redirectstream_callback(XtPointer, int *, XtInputId*);
|
---|
[78] | 216 |
|
---|
[81] | 217 | static PIConsole* consstream[2] = {NULL, NULL};
|
---|
| 218 | static unsigned char streamva[2] = { PIVA_Def, PIVA_Ital};
|
---|
| 219 | static int streamno[2] = {0,1};
|
---|
[135] | 220 | static XtInputId inputid[2];
|
---|
| 221 | static int origfiledes[2]={-1, -1}; // descripteurs de fichiers de depart
|
---|
[78] | 222 |
|
---|
[81] | 223 | static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
|
---|
[78] | 224 | {
|
---|
| 225 | char buff[128];
|
---|
| 226 | int nr;
|
---|
[81] | 227 |
|
---|
| 228 | int idx = *((int*)cld);
|
---|
| 229 | if (idx != 1) idx = 0;
|
---|
[135] | 230 | if (!consstream[idx]) return;
|
---|
[78] | 231 | while ( (nr=read(*fd, buff, 127)) > 0 ) {
|
---|
[81] | 232 | buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
|
---|
[78] | 233 | }
|
---|
[81] | 234 | consstream[idx]->Refresh();
|
---|
[78] | 235 | }
|
---|
| 236 |
|
---|
| 237 | /* --Methode-- */
|
---|
[81] | 238 | void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
|
---|
[78] | 239 | {
|
---|
[135] | 240 | if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD);
|
---|
| 241 | if ( cons == consstream[0]) return;
|
---|
| 242 | if ( (consstream[0]) && (cons) ) { consstream[0] = cons; streamva[0] = va; return; }
|
---|
| 243 | else if (!cons) {
|
---|
| 244 | consstream[0] = NULL;
|
---|
| 245 | XtRemoveInput(inputid[0]);
|
---|
| 246 | dup2(origfiledes[0], 1);
|
---|
| 247 | return;
|
---|
| 248 | }
|
---|
[78] | 249 |
|
---|
[81] | 250 | consstream[0] = cons; streamva[0] = va;
|
---|
| 251 |
|
---|
[78] | 252 | int p[2];
|
---|
| 253 | pipe(p);
|
---|
[90] | 254 | // Redirection de stdout (fid=1) :
|
---|
[78] | 255 | close(1);
|
---|
| 256 | dup(p[1]);
|
---|
| 257 | close(p[1]);
|
---|
| 258 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
[135] | 259 |
|
---|
[85] | 260 | setlinebuf(stdout);
|
---|
[135] | 261 | ios::sync_with_stdio();
|
---|
[78] | 262 |
|
---|
[114] | 263 | int szx, szy, szf;
|
---|
| 264 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[135] | 265 | inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
| 266 | redirectstream_callback, (XtPointer) streamno);
|
---|
[78] | 267 | }
|
---|
[81] | 268 |
|
---|
| 269 | /* --Methode-- */
|
---|
| 270 | void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
|
---|
| 271 | {
|
---|
[135] | 272 | if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD);
|
---|
| 273 | if ( cons == consstream[1]) return;
|
---|
| 274 | if ( (consstream[1]) && (cons) ) { consstream[1] = cons; streamva[1] = va; return; }
|
---|
| 275 | else if (!cons) {
|
---|
| 276 | consstream[1] = NULL;
|
---|
| 277 | XtRemoveInput(inputid[1]);
|
---|
| 278 | dup2(origfiledes[1], 2);
|
---|
| 279 | return;
|
---|
| 280 | }
|
---|
[81] | 281 |
|
---|
| 282 | consstream[1] = cons; streamva[1] = va;
|
---|
| 283 |
|
---|
| 284 | int p[2];
|
---|
| 285 | pipe(p);
|
---|
[135] | 286 | // Redirection de stderr (fid=2) :
|
---|
[81] | 287 | close(2);
|
---|
| 288 | dup(p[1]);
|
---|
| 289 | close(p[1]);
|
---|
| 290 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
[135] | 291 |
|
---|
[85] | 292 | setlinebuf(stderr);
|
---|
[135] | 293 | ios::sync_with_stdio();
|
---|
[81] | 294 |
|
---|
[114] | 295 | int szx, szy, szf;
|
---|
| 296 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[135] | 297 | inputid[1] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
| 298 | redirectstream_callback, (XtPointer) (streamno+1));
|
---|
[81] | 299 | }
|
---|
| 300 |
|
---|