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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
/*
pmserv.c
Geoffrey Furnish
22 September 1991
This program is the OS/2 PM server for PLPLOT. This program
is run in a seperate process, before the plplot app is run.
The app then communicates its requests to this server, which
renders the plot in a PM window. Since the plplot app need not
be a PM application, it can use normal printf output, and
need not exhibit the message based architecture common in PM apps.
Theory of Operation:
This program has two threads of execution. The main thread handles
the PM message queue, and basically just responds to messages to
keep the image window up to date. However, just before starting up
the message queue, it spawns a second process which manages the
pipe through which commands are accepted. This second thread
accepts connection requests from one client at a time, and routes
the drawing commands back to the first thread for display.
Although admittedly somewhat complex, this is actually a very efficient
way to implement the desired functionality. Threads with OS/2 are
really great!
Revision History:
1-24-92 Finally fixed the problem with the colors getting messed
up on a repaint. Also cleaning up the code a bit.
2-1-92 Finally fixed the problem with the final command in the
history buffer beeing fouled up in some cases. More
code cleaning. Fixed numerous compiler warnings.
2-1-92 Finally fixed the colors so they look about the same as
with the X Windows driver. However, I still haven't
messed with the RGB escape function yet. Later.
2-1-92 Fixed all compiler warnings.
2-2-92 Took out the majic number 200, and parameterized most
pipe things with constants defined in pmdefs.h. Also took
this opportunity to choose a more sensible name for the
pipe. Some optimization of pipe performance.
2-27-92 Finished the autoadvance code. Seems to work about as well
as could be hoped. The only thing wrong is that pages with
rgb never get drawn correctly, since they advance before they
ever get refreshed with correct colors. Sure would be nice
if I could get them to draw right in the first place...
3-13-92 Fixed the bug which prevented a page with RGB on it from ever
being plotted right if it was the last page in the file.
Anyway, looks like this is good enough to be called
Version 1.0!
*/
#include "plplot/plplot.h"
#define INCL_WIN
#define INCL_DOS
#define INCL_GPI
#include <os2.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include "plplot/pmdefs.h"
VOID CheckMenuItem( HWND hwnd, SHORT sMenuItem, BOOL fCheck );
void PipeManagerThread( void *stuff );
MRESULT EXPENTRY ClientWndProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2 );
void InitiatePipeManagement( void );
long pmcolor( long color );
static CHAR szClientClass[] = "PlDrawClass";
static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU |
FCF_SIZEBORDER | FCF_MINMAX |
FCF_SHELLPOSITION | FCF_TASKLIST |
FCF_MENU ;
HAB hab;
HMQ hmq;
HWND hwndFrame = NULLHANDLE, hwndClient = NULLHANDLE;
QMSG qmsg;
PLINT inbuflen = 0, inbuffer[ PIPE_BUFFER_SIZE ] = {};
int inbuffree = 1;
// I'm not really too clear on just how big this number should be. When I
// had it set to only 16000, I found that examles 1, 12 and 13 all exhibited
// the property of overflowing the history buffer. The code handles this
// correctly, in the sense that no error is generated, but clearly we want
// this number big enough for most graphs to work without a hitch.
#define MAX_LIST_LEN 60000
PLINT permbuflen = 0, permbufferfull=0;
PLINT permbuffer[ MAX_LIST_LEN ] = {};
#define WM_DRAW_MORE (WM_USER + 0)
#define WM_NEW_SESSION (WM_USER + 1)
/*----------------------------------------------------------------------------*\
* void main ( void )
*
* In this function we initialize the necessary data structures, spawn the
* thread which manages the incoming command pipe, and finally start up the
* PM message queue and process messages until all done.
*
* This is based on code from C. Petzold's book on OS/2 PM Programming.
\*----------------------------------------------------------------------------*/
void main ( void )
{
hab = WinInitialize(0);
hmq = WinCreateMsgQueue( hab, 0 );
WinRegisterClass( hab, szClientClass, (PFNWP) ClientWndProc, CS_SIZEREDRAW,
0 );
hwndFrame = WinCreateStdWindow( HWND_DESKTOP, WS_VISIBLE,
&flFrameFlags, szClientClass,
NULL,
0L, (HMODULE) 0, ID_RESOURCE, &hwndClient );
WinSendMsg( hwndFrame, WM_SETICON,
(MPARAM) WinQuerySysPointer( HWND_DESKTOP, SPTR_APPICON, FALSE ), NULL );
InitiatePipeManagement(); // spawns a new thread for this purpose.
while( WinGetMsg( hab, &qmsg, NULLHANDLE, 0, 0 ) )
WinDispatchMsg( hab, &qmsg );
WinDestroyWindow( hwndFrame );
WinDestroyMsgQueue( hmq );
WinTerminate( hab );
}
/*----------------------------------------------------------------------------*\
* MRESULT EXPENTRY ClientWndProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
*
* In this function we handle all the messages to the main window.
\*----------------------------------------------------------------------------*/
MRESULT EXPENTRY ClientWndProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2 )
{
long i=0, j = 0;
long startofset = 0, atendofimage = 0, commandlen = 0, range = 0,
pmrgb = 0, pmcolorindex = 0;
HPS hps;
POINTL ptl;
static SHORT cx = 0, cy = 0;
static float scalex = 0.0, scaley = 0.0;
static int rgbused = 0;
static int WaitingToAdvance = 0;
static int justadvanced = 0;
static int autoadvance = 0;
SHORT sMenuItem = 0;
static long resumewith = 0;
static PLINT oldx = 0, oldy = 0, oldcolor = CLR_WHITE, firstcolor = CLR_WHITE;
switch (msg) {
case WM_COMMAND:
switch ( SHORT1FROMMP (mp1) ) { // COMMANDMSG(&msg)->cmd ) {
case IDM_ADVANCE:
WinAlarm( HWND_DESKTOP, WA_ERROR );
if (WaitingToAdvance) {
WaitingToAdvance = 0;
justadvanced = 1;
WinPostMsg( hwndFrame, WM_DRAW_MORE, NULL, NULL );
}
return 0;
case IDM_AUTO_ADVANCE:
autoadvance = !autoadvance;
sMenuItem = IDM_AUTO_ADVANCE; // COMMANDMSG(&msg)->cmd;
CheckMenuItem( hwnd, sMenuItem, autoadvance );
return 0;
}
break;
case WM_SIZE:
cx = SHORT1FROMMP( mp2 );
cy = SHORT2FROMMP( mp2 );
scalex = cx / (float) PLMETA_X;
scaley = cy / (float) PLMETA_Y;
return 0;
case WM_PAINT:
hps = WinBeginPaint( hwnd, NULLHANDLE, NULL );
ptl.x = oldx = 0; ptl.y = oldy = 0;
GpiMove( hps, &ptl );
ptl.x = cx; ptl.y = cy;
// Some ballogney to clear the window. Gotta be a better way...
GpiSetPattern( hps, PATSYM_SOLID );
GpiSetColor( hps, CLR_BLACK );
GpiBox( hps, DRO_FILL, &ptl, 0, 0 );
// Now reset the color to the color which was the current color at the time
// of the last frame advance, i.e. the first color to use when drawing lines
// in this window. This is not necessarily the same as oldcolor, or the
// first color in the color map.
// Also, reset the command index i, and other flags.
GpiSetColor( hps, pmcolor(firstcolor) );
i = 0;
atendofimage = 0;
while ( i < (int) permbuflen && !atendofimage )
switch ( permbuffer[i++] ) {
case INITIALIZE:
break;
case SWITCH_TO_TEXT:
break;
case SWITCH_TO_GRAPH:
break;
case CLEAR:
atendofimage = 1;
break;
case PAGE:
break;
case NEW_COLOR:
oldcolor = permbuffer[i++];
GpiSetColor( hps, pmcolor(oldcolor) );
break;
case NEW_WIDTH:
i++;
break;
case LINETO:
oldx = ptl.x = (long) (permbuffer[i++] * scalex);
oldy = ptl.y = (long) (permbuffer[i++] * scaley);
GpiLine( hps, &ptl );
break;
case LINE:
ptl.x = (long) (permbuffer[i++] * scalex);
ptl.y = (long) (permbuffer[i++] * scaley);
GpiMove( hps, &ptl );
oldx = ptl.x = (long) (permbuffer[i++] * scalex);
oldy = ptl.y = (long) (permbuffer[i++] * scaley);
GpiLine( hps, &ptl );
break;
case CLOSE:
atendofimage = 1;
break;
case ESCAPE:
switch(permbuffer[i++]) {
case ESC_NOOP:
break;
case ESC_RGB:
pmrgb = permbuffer[i++];
// Do something to update the color here.
pmcolorindex = GpiQueryColorIndex( hps,
LCOLOPT_REALIZED, pmrgb );
if (pmcolorindex >= 0 && pmcolorindex <= 15) {
GpiSetColor( hps, pmcolorindex );
oldcolor = pmcolorindex;
}
break;
default:
printf( "*** UNRECOGNIZED COMMAND ***\n" );
WinAlarm( HWND_DESKTOP, WA_ERROR );
exit(0);
break;
}
break;
default:
WinAlarm( HWND_DESKTOP, WA_ERROR );
printf( "*** UNRECOGNIZED COMMAND ***\n" );
exit(0); // if bad here, then its bad bad news !!!
break;
}
WinEndPaint( hps );
return 0;
case WM_DRAW_MORE:
DosEnterCritSec();
inbuffree = 0; // Mark the buffer "off-limits".
// Get a PS, and restore prior state.
hps = WinGetPS( hwnd );
GpiSetColor( hps, pmcolor(oldcolor) );
GpiSetPattern( hps, PATSYM_SOLID );
ptl.x = oldx; ptl.y = oldy;
GpiMove( hps, &ptl );
if (justadvanced) {
i = resumewith;
resumewith = 0;
permbuflen = 0;
permbufferfull = 0;
ptl.x = oldx = 0; ptl.y = oldy = 0;
GpiMove( hps, &ptl );
ptl.x = cx; ptl.y = cy;
// Some ballogney to clear the window. Gotta be a better way...
GpiSetColor( hps, CLR_BLACK );
GpiBox( hps, DRO_FILL, &ptl, 0, 0 );
GpiSetColor( hps, pmcolor(oldcolor) );
firstcolor = oldcolor;
rgbused = 0;
justadvanced = 0;
}
// Now process the new message stream.
startofset = i;
range = 0;
while ( i < (int) inbuflen && !WaitingToAdvance ) {
commandlen = 1;
switch ( inbuffer[i++] ) {
case INITIALIZE:
break;
case SWITCH_TO_TEXT:
break;
case SWITCH_TO_GRAPH:
break;
case CLEAR:
resumewith = i;
WaitingToAdvance = 1;
if (rgbused)
WinInvalidateRect( hwnd, NULL, FALSE );
break;
case PAGE:
break;
case NEW_COLOR:
oldcolor = inbuffer[i++];
GpiSetColor( hps, pmcolor(oldcolor) );
commandlen++;
break;
case NEW_WIDTH:
i++;
commandlen++;
break;
case LINETO:
oldx = ptl.x = (long) (inbuffer[i++] * scalex);
oldy = ptl.y = (long) (inbuffer[i++] * scaley);
GpiLine( hps, &ptl );
commandlen += 2;
break;
case LINE:
ptl.x = (long) (inbuffer[i++] * scalex);
ptl.y = (long) (inbuffer[i++] * scaley);
GpiMove( hps, &ptl );
oldx = ptl.x = (long) (inbuffer[i++] * scalex);
oldy = ptl.y = (long) (inbuffer[i++] * scaley);
GpiLine( hps, &ptl );
commandlen += 4;
break;
case CLOSE:
if (rgbused)
WinInvalidateRect( hwnd, NULL, FALSE );
break;
case ESCAPE:
commandlen++;
switch(inbuffer[i++]) {
case ESC_NOOP:
break;
case ESC_RGB:
pmrgb = inbuffer[i++];
commandlen++;
// Do something to update the color here...
// Unfortunately, this is very hard to do
// here, since it keeps drawing in spurious
// colors. Only works right if done during
// the WM_PAINT message handling. So,
// jury rig plots with RGB specified colors
// to auto-repaint when done.
rgbused = 1;
break;
default:
printf( "*** UNRECOGNIZED ESC COMMAND ***%i\n", (int) inbuffer[i-1] );
WinAlarm( HWND_DESKTOP, WA_ERROR );
exit(0);
break;
}
break;
default:
printf( "*** UNRECOGNIZED COMMAND ***%i\n", (int) inbuffer[i-1] );
WinAlarm( HWND_DESKTOP, WA_ERROR );
exit(0);
break;
}
if (!permbufferfull && permbuflen + range + commandlen
< MAX_LIST_LEN)
range += commandlen;
else
permbufferfull = 1;
}
WinReleasePS( hps );
// Now add the commands processed this far to the permanent buffer list.
if ( range )
for( j=startofset; range; range-- )
permbuffer[ permbuflen++ ] = inbuffer[ j++ ];
if (!WaitingToAdvance)
inbuffree = 1;
// Should this next block be here, or just above the one above???
if (WaitingToAdvance && autoadvance) {
WaitingToAdvance = 0;
justadvanced = 1;
WinPostMsg( hwndFrame, WM_DRAW_MORE, NULL, NULL );
}
DosExitCritSec();
return 0;
case WM_NEW_SESSION:
WinAlarm( HWND_DESKTOP, WA_ERROR );
resumewith = 0;
justadvanced = 1;
return 0;
}
return WinDefWindowProc( hwnd, msg, mp1, mp2 );
}
/*----------------------------------------------------------------------------*\
* long pmcolor( long color )
*
* In this function we convert the PLPLOT requested color into a an actual
* PM color index value.
\*----------------------------------------------------------------------------*/
long pmcolor( long color )
{
static long pmcolors[] = {
CLR_BLACK, CLR_RED, CLR_YELLOW, CLR_GREEN,
CLR_DARKCYAN, CLR_PINK, CLR_DARKGRAY, CLR_PALEGRAY,
CLR_BROWN, CLR_BLUE, CLR_DARKBLUE, CLR_CYAN,
CLR_RED, CLR_PINK, CLR_YELLOW, CLR_WHITE
};
if (color < 0 || color > 15)
color = 15;
return pmcolors[color];
}
/*----------------------------------------------------------------------------*\
* void InitiatePipeManagement( void )
*
* In this function we start the thread which reads the pipe, and passes
* the graphics commands to the window procedure.
\*----------------------------------------------------------------------------*/
void InitiatePipeManagement( void )
{
static VOID *pThreadStack = NULL;
static TID tidPipeManager;
static PMSTUFF pmstuff;
pmstuff.a = 0; // Not currently used for anything...
if ( NULL == (pThreadStack = malloc( STACKSIZE )) ) {
printf( "Unable to allocate space for pipe manager thread.\n" );
exit(0);
}
if ( -1 == (tidPipeManager = _beginthread( PipeManagerThread,
pThreadStack, STACKSIZE, &pmstuff ))) {
printf( "Unable to spawn pipe manager thread.\n" );
exit(0);
}
}
/*----------------------------------------------------------------------------*\
* VOID FAR PipeManagerThread( PMSTUFF *pmstuff )
*
* In this function we read commands from the pipe, and pass them off to
* the window procedure.
\*----------------------------------------------------------------------------*/
void PipeManagerThread( void *stuff )
{
HPIPE hp;
USHORT bytes2 = 0, rv1, rv2, retries;
ULONG bytes1 = 0L, bytes3 = 0L;
PLINT buflen = 0, buffer[ PIPE_BUFFER_SIZE ] = {};
char *pbuffer = (char *) buffer;
PMSTUFF *pmstuff = (PMSTUFF *) stuff;
int isfree;
// Now open up the pipe and start handling it.
DosCreateNPipe( /*"\\pipe\\bubba"*/ PIPE_NAME, // pipe name.
&hp, // pipe handle.
NP_ACCESS_INBOUND | NP_NOINHERIT | NP_NOWRITEBEHIND,
1 | NP_WAIT | NP_READMODE_BYTE | NP_TYPE_BYTE,
512, // output-buffer size.
//512, // input-buffer size.
2*PIPE_BUFFER_SIZE, // input-buffer size.
500L ); // default timeout for DosWaitNmPipe
// 2-1-92
// Comments: In vol 1 of the OS/2 Programmer's Reference, the output
// and input buffer sizes are listed in the opposite order of what is
// shown in vol 3. I am taking 3 to be correct, since it is the complete
// spec for the function.
//
// Secondly, in a prior life, I had to boost the output buffer (whic I thought
// at the time was the input buffer) size from 512 to 2048. I've now (2-1-92)
// taken it back down to 512, and don't seem to have any problems with it.
// Really need to overhaul this next section. Should replace all those
// crazy diagnostics with some useful, effective actions.
do {
if (DosConnectNPipe( hp )) {
DosClose( hp );
exit(0);
}
WinPostMsg( hwndFrame, WM_NEW_SESSION, NULL, NULL );
do {
rv1 = DosRead( hp, &buflen, sizeof( PLINT ), &bytes1 );
if (bytes1 == 0) break;
// if (bytes1 != sizeof (PLINT) )
// printf( "Major screw up. buflen not read correctly.\n" );
bytes2 = 0;
retries = -1;
do {
retries++;
rv2 = DosRead( hp, &pbuffer[bytes2],
(USHORT) (buflen*sizeof(PLINT) - (PLINT) bytes2),
&bytes3 );
bytes2 += (PLINT) bytes3;
//if (!bytes3) printf( "No bytes returned!\n" );
//if (rv2) printf( "Bogus pipe read.\n" );
} while ( (PLINT) bytes2 < buflen*sizeof(PLINT) );
// printf( "buflen=%d, Read %d bytes, %d expected.\n",
// (int) buflen, (int) bytes2, (int) buflen*sizeof(PLINT) );
// if (retries) printf( "%d retries to get whole packet.\n", retries );
if (rv1 || rv2)
printf( "rv1=%d, rv2=%d \n", rv1, rv2 );
DosEnterCritSec();
isfree = inbuffree;
DosExitCritSec();
while (!isfree)
{
_sleep2(100);
DosEnterCritSec();
isfree = inbuffree;
DosExitCritSec();
}
DosEnterCritSec();
inbuflen = buflen;
memcpy( inbuffer, buffer, (int) buflen*sizeof(PLINT) );
inbuffree = 0;
DosExitCritSec();
WinPostMsg( hwndFrame, WM_DRAW_MORE, NULL, NULL );
} while( bytes2 );
DosDisConnectNPipe( hp );
// printf( "Connection closed.\n\n" );
} while(1);
_endthread();
}
/*----------------------------------------------------------------------------*\
* VOID CheckMenuItem( HWND hwnd, SHORT sMenuItem, BOOL fCheck )
*
* This function, obtained from Petzold's book, manages the checking and
* unchecking of a menu item.
\*----------------------------------------------------------------------------*/
VOID CheckMenuItem( HWND hwnd, SHORT sMenuItem, BOOL fCheck )
{
HWND hwndParent = WinQueryWindow( hwnd, QW_PARENT );
HWND hwndMenu = WinWindowFromID( hwndParent, FID_MENU );
WinSendMsg( hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT( sMenuItem, TRUE ),
MPFROM2SHORT( MIA_CHECKED, fCheck ? MIA_CHECKED : 0 ) );
}
|