[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 |
|
---|
[78] | 11 |
|
---|
| 12 | // #define DEBUG_APPLX
|
---|
| 13 |
|
---|
[18] | 14 | static Cursor a_curs[3];
|
---|
| 15 | static bool a_fgcur=false;
|
---|
| 16 |
|
---|
[60] | 17 | // Voir fichier pimenux.cc , Pour resoudre certains conflits avec les WindowManagers
|
---|
| 18 | void SetTopWdgForMenuX(SysDWdg mtw);
|
---|
| 19 |
|
---|
[111] | 20 | static PIApplication* cur_piapp = NULL;
|
---|
| 21 | PIApplication* PIApplicationGetApp() { return cur_piapp; }
|
---|
| 22 |
|
---|
[126] | 23 |
|
---|
| 24 |
|
---|
[2] | 25 | /* --Methode-- */
|
---|
| 26 | PIApplicationX::PIApplicationX(int sx, int sy, int narg, char *arg[])
|
---|
| 27 | : PIApplicationGen()
|
---|
| 28 | {
|
---|
| 29 | int sxt, syt;
|
---|
| 30 |
|
---|
| 31 | #ifdef DEBUG_APPLX
|
---|
| 32 | puts("PIApplicationX::PIApplicationX()_info : App creation");
|
---|
| 33 | #endif
|
---|
| 34 | mStop = true;
|
---|
| 35 | topwdg = new PIWdgX(narg, arg);
|
---|
| 36 |
|
---|
| 37 | intcont = new PIContainerX((PIMsgHandler *)this, topwdg, "MBCont",
|
---|
| 38 | 10, 10, 0, 0);
|
---|
[60] | 39 | // Pb avec les Popup MenuX et certains Window Manager , voir pimenux.cc
|
---|
| 40 | SetTopWdgForMenuX(intcont->XtWdg());
|
---|
| 41 |
|
---|
[2] | 42 | menubar = new PIMenubar(this, "DefMenubar");
|
---|
[23] | 43 | Menubar()->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_free);
|
---|
[2] | 44 | sxt = ( sx > Menubar()->XSize() ) ? sx : Menubar()->XSize();
|
---|
| 45 | syt = ( sy > 0 ) ? sy : 0;
|
---|
[92] | 46 | int msx,msy;
|
---|
| 47 | PrefCompSz(msx, msy);
|
---|
[7] | 48 | //syt += Menubar()->YSize();
|
---|
[92] | 49 | syt += msy;
|
---|
[2] | 50 | MBCont()->SetSize(sxt, syt);
|
---|
| 51 | if ( (sx > 0) && (sy > 0) )
|
---|
| 52 | {
|
---|
| 53 | topcont = new PIContainerX(MBCont(), "TopLevelCont",
|
---|
[92] | 54 | sx, sy, 0, msy);
|
---|
[23] | 55 | topcont->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
|
---|
[2] | 56 | topcont->Show();
|
---|
| 57 | }
|
---|
| 58 | else topcont = MBCont();
|
---|
| 59 |
|
---|
| 60 | MBCont()->Show();
|
---|
| 61 | topwdg->Manage();
|
---|
[18] | 62 |
|
---|
| 63 | if (!a_fgcur)
|
---|
| 64 | {
|
---|
| 65 | Display *mdsp;
|
---|
| 66 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
| 67 | a_curs[0] = XCreateFontCursor(mdsp, XC_arrow);
|
---|
| 68 | a_curs[1] = XCreateFontCursor(mdsp, XC_X_cursor);
|
---|
| 69 | a_curs[2] = XCreateFontCursor(mdsp, XC_watch);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[111] | 72 | cur_piapp = this;
|
---|
[18] | 73 | mState = -1;
|
---|
| 74 | SetReady();
|
---|
[2] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /* --Methode-- */
|
---|
| 78 | PIApplicationX::~PIApplicationX()
|
---|
| 79 | {
|
---|
| 80 | #ifdef DEBUG_APPLX
|
---|
| 81 | puts("PIApplicationX::~PIApplicationX()_info : App delete");
|
---|
| 82 | #endif
|
---|
[18] | 83 | Display *mdsp;
|
---|
| 84 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
[52] | 85 | topwdg->UnManage();
|
---|
[6] | 86 | delete menubar;
|
---|
| 87 | if (topcont != MBCont()) delete topcont;
|
---|
| 88 | delete intcont;
|
---|
[2] | 89 | delete topwdg;
|
---|
[18] | 90 | XtCloseDisplay(mdsp);
|
---|
[111] | 91 | cur_piapp = NULL;
|
---|
[18] | 92 | return;
|
---|
[2] | 93 | }
|
---|
| 94 |
|
---|
| 95 | /* --Methode-- */
|
---|
| 96 | void PIApplicationX::Run()
|
---|
| 97 | {
|
---|
| 98 | XEvent evt;
|
---|
| 99 | #ifdef DEBUG_APPLX
|
---|
| 100 | puts("PIApplicationX::Run()_info : App Run ");
|
---|
| 101 | #endif
|
---|
| 102 |
|
---|
| 103 | topwdg->SetSize(MBCont()->XSize(), MBCont()->YSize());
|
---|
| 104 |
|
---|
[114] | 105 | int szx, szy, szf;
|
---|
| 106 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 107 |
|
---|
[110] | 108 | // Pour appeler FinishCreate() des objets dans la fenetre principale
|
---|
| 109 | MBCont()->FinishCreate();
|
---|
[2] | 110 | while (mStop)
|
---|
| 111 | {
|
---|
[90] | 112 | XtAppNextEvent(*appctx, &evt);
|
---|
[2] | 113 | XtDispatchEvent(&evt);
|
---|
| 114 | }
|
---|
| 115 | return;
|
---|
| 116 | }
|
---|
[18] | 117 |
|
---|
| 118 |
|
---|
| 119 | /* --Methode-- */
|
---|
| 120 | void PIApplicationX::SetReady()
|
---|
| 121 | {
|
---|
| 122 | if (mState != kReadyState)
|
---|
| 123 | {
|
---|
| 124 | Display * mdsp;
|
---|
| 125 | mState = kReadyState;
|
---|
[106] | 126 | Menubar()->SetSensitive();
|
---|
[18] | 127 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
| 128 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
|
---|
| 129 | XFlush(mdsp);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /* --Methode-- */
|
---|
| 134 | void PIApplicationX::SetBusy()
|
---|
| 135 | {
|
---|
| 136 | if (mState != kBusyState)
|
---|
| 137 | {
|
---|
| 138 | Display * mdsp;
|
---|
| 139 | mState = kBusyState;
|
---|
[106] | 140 | Menubar()->SetSensitive();
|
---|
[18] | 141 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
| 142 | // if ( XtIsRealized(topwdg->XtWdg()) )
|
---|
| 143 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
|
---|
| 144 | XFlush(mdsp);
|
---|
| 145 | }
|
---|
| 146 | return;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /* --Methode-- */
|
---|
| 150 | void PIApplicationX::SetBlocked()
|
---|
| 151 | {
|
---|
| 152 | if (mState != kBlockedState)
|
---|
| 153 | {
|
---|
| 154 | Display * mdsp;
|
---|
| 155 | mState = kBlockedState;
|
---|
[106] | 156 | Menubar()->SetUnSensitive();
|
---|
[18] | 157 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
| 158 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
|
---|
| 159 | XFlush(mdsp);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
[75] | 162 |
|
---|
| 163 |
|
---|
| 164 | /* --Methode-- */
|
---|
[90] | 165 | void PIApplicationX::PrefCompSz(int& szx, int& szy)
|
---|
[75] | 166 | {
|
---|
[114] | 167 | int szf;
|
---|
| 168 | PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 169 | return;
|
---|
[75] | 170 | }
|
---|
| 171 |
|
---|
[92] | 172 | /* --Methode-- */
|
---|
| 173 | void PIApplicationX::ScreenSz(int& szx, int& szy)
|
---|
| 174 | {
|
---|
| 175 | Display * dsp = PIXDisplay();
|
---|
| 176 | szx = DisplayWidth(dsp, DefaultScreen(dsp));
|
---|
| 177 | szy = DisplayHeight(dsp, DefaultScreen(dsp));
|
---|
| 178 | }
|
---|
[75] | 179 |
|
---|
[92] | 180 |
|
---|
| 181 |
|
---|
[78] | 182 | /* Call-Back - Fonction privee de ce fichier */
|
---|
[81] | 183 | static void redirectstream_callback(XtPointer, int *, XtInputId*);
|
---|
[78] | 184 |
|
---|
[81] | 185 | static PIConsole* consstream[2] = {NULL, NULL};
|
---|
| 186 | static unsigned char streamva[2] = { PIVA_Def, PIVA_Ital};
|
---|
| 187 | static int streamno[2] = {0,1};
|
---|
[78] | 188 |
|
---|
[81] | 189 | static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
|
---|
[78] | 190 | {
|
---|
| 191 | char buff[128];
|
---|
| 192 | int nr;
|
---|
[81] | 193 |
|
---|
| 194 | int idx = *((int*)cld);
|
---|
| 195 | if (idx != 1) idx = 0;
|
---|
[78] | 196 | while ( (nr=read(*fd, buff, 127)) > 0 ) {
|
---|
[81] | 197 | buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
|
---|
[78] | 198 | }
|
---|
[81] | 199 | consstream[idx]->Refresh();
|
---|
[78] | 200 | }
|
---|
| 201 |
|
---|
| 202 | /* --Methode-- */
|
---|
[81] | 203 | void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
|
---|
[78] | 204 | {
|
---|
| 205 | if (!cons) return;
|
---|
| 206 |
|
---|
[81] | 207 | if (consstream[0]) { consstream[0] = cons; streamva[0] = va; return; }
|
---|
| 208 | consstream[0] = cons; streamva[0] = va;
|
---|
| 209 |
|
---|
[78] | 210 | int p[2];
|
---|
| 211 | pipe(p);
|
---|
[90] | 212 | // Redirection de stdout (fid=1) :
|
---|
[78] | 213 | close(1);
|
---|
| 214 | dup(p[1]);
|
---|
| 215 | close(p[1]);
|
---|
| 216 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
[85] | 217 | setlinebuf(stdout);
|
---|
[78] | 218 |
|
---|
[114] | 219 | int szx, szy, szf;
|
---|
| 220 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 221 | XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask, redirectstream_callback, (XtPointer) streamno);
|
---|
[78] | 222 | }
|
---|
[81] | 223 |
|
---|
| 224 | /* --Methode-- */
|
---|
| 225 | void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
|
---|
| 226 | {
|
---|
| 227 | if (!cons) return;
|
---|
| 228 |
|
---|
| 229 | if (consstream[1]) { consstream[1] = cons; streamva[1] = va; return; }
|
---|
| 230 | consstream[1] = cons; streamva[1] = va;
|
---|
| 231 |
|
---|
| 232 | int p[2];
|
---|
| 233 | pipe(p);
|
---|
[90] | 234 | // Redirection de stderr (fid=0) :
|
---|
[81] | 235 | close(2);
|
---|
| 236 | dup(p[1]);
|
---|
| 237 | close(p[1]);
|
---|
| 238 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
[85] | 239 | setlinebuf(stderr);
|
---|
[81] | 240 |
|
---|
[114] | 241 | int szx, szy, szf;
|
---|
| 242 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
[90] | 243 | XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask, redirectstream_callback, (XtPointer) (streamno+1));
|
---|
[81] | 244 | }
|
---|
| 245 |
|
---|