home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.eri.u-tokyo.ac.jp
/
2014.03.ftp.eri.u-tokyo.ac.jp.zip
/
ftp.eri.u-tokyo.ac.jp
/
pub
/
seisv
/
src
/
3.02
/
device.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-11-10
|
15KB
|
728 lines
/* SeisView alone/client Copyright (C) 1992,1998 K.Koketsu
<history>
98-11-03 Remove CR.
98-10-06 Printers appear again.
Some global variables are replaced by constants.
98-10-02 MONOCHROME drops from the support list.
Correct backspace().
98-02-26 Abandon DOS, but take Windows 95/NT.
96-06-19 remedy for BSD/386.
Support RISC NEWS (thanks to Dr Shiobara). */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#ifdef BSDI
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#ifdef RISC_NEWS
#include <varargs.h>
#endif
#include "ctlname.h"
#include "seis.h"
#ifdef WIN32
#include <io.h>
#endif
extern char em$[51]; /* error message on menu */
extern char drv$[65], drvo$[65]; /* parameters for disks */
extern char prn$[5], port$[65]; /* printer type etc. */
extern char z$[11], w$[81]; /* work string */
extern int psflag;
char *iseg[10];
unsigned nseg; /* # of work segments for selected events */
int oldx = -1, oldy; /* old cursor position */
void show_machine()
{
#ifdef WIN32
strcpy(w$+28, "for Windows 95/98/NT");
#else
strcpy(w$+29, "for UNIX/X Window");
#endif
}
void get_printer_defaults(s, t)
char *s, *t;
{
#ifdef WIN32
strcpy(s, "NEC "); strcpy(t, "HP Laserjet 4M");
#else
strcpy(s, "PS "); strcpy(t, "ps");
#endif
}
unsigned char *cx, *cy, *cz;
void keep_prt_buffer()
{
cx = (unsigned char *)malloc(2560);
cy = (unsigned char *)malloc(1280);
cz = (unsigned char *)malloc( 640);
}
int alloc_mem()
{
int i;
keep_prt_buffer();
for(i=0; i<8; i++) {
if((iseg[i] = malloc(0x10000)) == NULL) break;
}
nseg = i;
return( i );
}
/************ Text I/O (compatible to curses) ****************************/
short row = 0, col = 0;
char mz[2000], mv[2000];
void move(y, x)
{
row = y;
col = x;
}
void move_left() { move(row, --col); }
void move_right() { move(row, ++col); }
void move_down() { move(++row, col); }
void move_up() { move(--row, col); }
int get_position() { return(row*80 + col); }
void set_writable(y, x)
short y, x;
{
mv[y*80+x] = 1;
}
_iswritable(y, x)
int y, x;
{
if(mv[y*80+x] < 0) return( 0 );
else return( 1 );
}
iswritable() { return( _iswritable(row, col) ); }
void addstr( s )
char *s;
{
short i, n;
n = strlen( s );
for(i=0; i<n; i++) addch( s[i] );
}
void mvaddstr(y, x, s)
int y, x;
char *s;
{
move(y, x);
addstr( s );
}
void addch( c )
char c;
{
char cc[2];
cc[0] = c; cc[1] = 0;
xouttext(row, col, cc, 1);
mz[row*80+col] = c;
move(row, col+1);
}
void mvaddch(y, x, c)
short y, x, c;
{
move(y, x);
addch( c );
}
mvinch(y, x)
int y, x;
{
return( mz[y*80+x] );
}
#ifdef RISC_NEWS
void printw(va_alist)
va_dcl
{
va_list args;
char *fmt, s[81];
va_start(args);
fmt= va_arg(args, char *);
(void)vsprintf(s, fmt, args);
addstr(s);
va_end(args);
}
#else
void printw(fmt, A1, A2, A3, A4, A5, A6)
char *fmt;
long A1, A2, A3, A4, A5, A6;
{
char s[81];
sprintf(s, fmt, A1, A2, A3, A4, A5, A6);
addstr( s );
}
#endif
void wreverse() { _setcolor( 0 ); _setbkcolor( 7 ); }
void wblink() { _setcolor( 4 ); _setbkcolor( 9 ); }
void wrblink() { _setcolor( 4 ); _setbkcolor( 7 ); }
void standend() { _setcolor( 0 ); _setbkcolor( 9 ); }
void clrtoeol()
{
short y, x, i;
char s[81];
y = row;
x = col;
for(i=x; i<80; i++) s[i-x] = ' ';
s[i-x] = 0;
addstr( s );
move(y, x);
}
void clear( clr )
int clr;
{
short i;
for(i=0; i<2000; i++) mz[i] = ' '; /* Assign blanks */
for(i=0; i<2000; i++) mv[i] = -1; /* Assign unwritable flags */
physical_clear( clr );
}
void backspace()
{
short i, colold;
if( _iswritable(row, col-1) ) {
colold = col;
i = col;
do mvaddch(row, i-1, mz[row*80+i]);
while( _iswritable(row, ++i) );
addch( ' ' );
move(row, colold-1);
}
else beep();
}
short getchr()
{
short c;
wreverse();
_setbkcolor( 2 );
if( iswritable() ) _small_font(); else _normal_font();
addch( mz[row*80+col] );
move(row, --col);
c = getcchr();
if( iswritable() ) { wreverse(); _small_font(); }
else { standend(); _normal_font(); }
addch( mz[row*80+col] );
move(row, --col);
restore_box(col, row);
if( iswritable() ) { wreverse(); _small_font(); }
else { standend(); _normal_font(); }
return( c );
}
void avoid_cr( s )
char s[];
{
/* Avoid CR at the end of line */
#ifdef WIN32
s[strlen(s)-1] = 0;
#else
s[strlen(s)-2] = 0;
#endif
}
int bload(sg, off, fname)
char *sg, *fname;
unsigned off;
{
int handle, len, rcod;
char *s, fn[78];
/* seismicity data */
if(strstr(fname,".B") || strstr(fname,".b")) strcpy(fn, drv$);
/* misc. data */
else strcpy(fn, drvo$);
add_separator( fn );
/* Don't change dummy strings. This results in segmentation fault on Linux.*/
/* strlwr( fname ); strcat(fn, fname); */
len = strlen( fn ); strcat(fn, fname); strlwr( fn+len );
#ifdef WIN32
handle = open(fn, O_BINARY|O_RDONLY);
#else
handle = open(fn, O_RDONLY);
#endif
if(handle == -1) {
sprintf(em$, "File '%s' does not exist!", fn);
return( 0 );
}
s = sg + off;
/* Windows ceck possibility of poking beyond the buffer end
(UNIX never does so). */
rcod = read(handle, s, 0xffff-off);
if(rcod == -1) rcod = 0;
close( handle );
return( rcod );
}
unsigned peek(off, sg) unsigned off; unsigned char *sg;
{ return( (unsigned)sg[off] ); }
unsigned qeek(off, sg) unsigned off; unsigned char *sg;
{ return((unsigned)sg[off] + ((unsigned)sg[off+1] << 8)); }
void poke(byt, off, sg) unsigned byt, off; unsigned char *sg;
{ sg[off] = byt; }
/******************* Printers *********************/
/*************** Windows ****************/
#ifdef WIN32
#define FontHeight 16
#define FontWidth 8
#define LineHeight (FontHeight+1)
extern HWND hwnd;
extern HDC hdc;
extern HFONT hFont, hSmallFont, hMenuFont;
extern HBRUSH hBrush[];
extern int widespace;
int PrinterWidth, PrinterHeight;
HDC pdc, pmemdc;
HBITMAP pbit;
HPEN pPen[2];
DOCINFO di;
double Xratio;
RECT r;
int prow, pcol, plines;
int lpinit()
{
double mXPPI, pXPPI;
prow = 0; pcol = 0;
pdc = CreateDC("WINSPOOL", port$, NULL, NULL);
if(pdc == NULL){
sprintf(em$, "Printer `%s' is not found!", port$);
return( 0 );
}
if(!(GetDeviceCaps(pdc,RASTERCAPS) & RC_BITBLT)) {
sprintf(em$, "Printer `%s' cannot print raster images!",
port$);
return( 0 );
}
PrinterWidth = GetDeviceCaps(pdc, HORZRES);
PrinterHeight = GetDeviceCaps(pdc, VERTRES);
pmemdc = CreateCompatibleDC( pdc );
pbit = CreateCompatibleBitmap(pdc, PrinterWidth, PrinterHeight);
SelectObject(pmemdc, pbit);
PatBlt(pmemdc, 0, 0, PrinterWidth, PrinterHeight, WHITENESS);
SelectObject(pmemdc, hFont); /* normal font */
SelectObject(pmemdc, hBrush[12]); /* hollow brush */
GetClientRect(hwnd, &r);
mXPPI = GetDeviceCaps(hdc, LOGPIXELSX);
pXPPI = GetDeviceCaps(pdc, LOGPIXELSX);
Xratio = pXPPI / mXPPI;
plines = (int)(PrinterHeight / Xratio / LineHeight);
memset(&di, 0, sizeof(DOCINFO));
di.cbSize = sizeof( DOCINFO );
di.lpszDocName = "SeisView hard copy";
StartDoc(pdc, &di);
StartPage( pdc );
return( 1 );
}
void build_image()
{
int factor;
if(psflag == 2) factor = 2;
else factor = 1;
StretchBlt(pdc, 0, 0, PrinterWidth/factor, PrinterHeight/factor,
pmemdc, 0, 0, (int)(PrinterWidth/Xratio),
(int)(PrinterHeight/Xratio), SRCCOPY);
}
void lpclose(){
build_image();
EndPage( pdc );
EndDoc( pdc );
DeleteDC( pdc );
}
void newpage()
{
build_image();
EndPage( pdc );
PatBlt(pmemdc, 0, 0, PrinterWidth, PrinterHeight, WHITENESS);
StartPage( pdc );
prow = 0;
}
void lpputn(s, n)
int n;
char *s;
{
int yw, xw;
if( widespace ) yw = yy(prow * LineHeight);
else yw = yy(prow * FontHeight);
xw = xx(pcol * FontWidth);
TextOut(pmemdc, xw, yw, s, n);
pcol = pcol + strlen(s);
}
void lpputs( s )
char *s;
{
unsigned int i; char t[2];
for(i=0; i<strlen(s); i++) {
if(s[i] == '\r') pcol = 0;
else if(s[i] == '\n') {
if(++prow > plines-1) newpage();
}
else {
t[0] = s[i]; t[1] = 0;
lpputn(t, 1);
}
}
}
/*************** Simulated PostScript ****************/
extern short xoff, yoff;
void psinit()
{
pPen[0] = CreatePen(PS_SOLID , 1, 0);
pPen[1] = CreatePen(PS_DASHDOT, 1, 0);
}
void psviewport(x1, y1, x2, y2)
short x1, y1, x2, y2;
{
HRGN hrgn;
int left, right, top, bottom;
if(x1==0 && y1==0 && x2==639 && y2==YPixels-1) { /* Full Screen */
left = 0;
top = 0;
right = PrinterWidth;
bottom = PrinterHeight;
}
else {
left = xx( x1 );
top = yy( y1 );
right = xx( x2 );
bottom = yy( y2 );
}
hrgn = CreateRectRgn(left, top, right, bottom);
SelectClipRgn(pmemdc, hrgn);
xoff = x1;
yoff = y1;
}
void psmoveto(x, y) short x, y;
{ MoveToEx(pmemdc, xx(x), yy(y), NULL); }
void pslineto(x, y) short x, y;
{ LineTo(pmemdc, xx(x), yy(y)); }
void pspen(ipen, idash)
int ipen, idash;
{
if(idash == 1) SelectObject(pmemdc, pPen[1]);
else SelectObject(pmemdc, pPen[0]);
}
void psnormal_font() { SelectObject(pmemdc, hFont); }
void pssmall_font() { SelectObject(pmemdc, hSmallFont); }
void pstext(x, y, s)
int x, y;
char *s;
{ TextOut(pmemdc, xx(x), yy(y), s, strlen(s)); }
void pscircle(x, y, r)
short x, y, r;
{
if(r <= 1) SetPixel(pmemdc, xx(x), yy(y), 0);
else Ellipse(pmemdc, xx(x)-r, yy(y)-r, xx(x)+r, yy(y)+r);
}
void psbox(flag, x1, y1, x2, y2)
short flag, x1, y1, x2, y2;
{ Rectangle(pmemdc, xx(x1), yy(y1), xx(x2), yy(y2)); }
void pshcopy( n )
short n;
{
lpclose();
}
/*************** UNIX ****************/
#else
FILE *fq;
int pfile_open( s )
char *s;
{
fq = fopen("seis.prn", s);
if(fq == NULL) return( 0 );
else return( 1 );
}
void pfile_close() { fclose( fq ); }
char *strupr( s )
char *s;
{
char *t;
t = s;
while(*t != 0) *(t++) = toupper( (int)*t );
return( s );
}
char *strlwr( s )
char *s;
{
char *t;
t = s;
while(*t != 0) *(t++) = tolower( (int)*t );
return( s );
}
char *itoa(i, s, radix)
int i, radix;
char *s;
{ /* Assume radix = 10. */
sprintf(s, "%d", i);
return( s );
}
int lpinit()
{
int rc;
rc = pfile_open("w");
if(rc == 0) strcpy(em$, "Printer file cannot be opened!");
return( rc );
}
void lpputs( s ) char *s; { fputs(s, fq); }
void lpclose()
{ pfile_close(); lpsubmit(); }
void lpputn(s, n) char *s; int n;
{ int i; for(i=0; i<n; i++) fputc(*(s++), fq); }
void lpsubmit()
{
char cmd[25];
strcpy(cmd, "lpr -P"); strcat(cmd, port$);
strcat(cmd," seis.prn");
system( cmd );
}
/*************** PostScript ****************/
#define PSXOffset 60.0
#define PSYOffset 110.0
#define XOffset 4
#define YOffset 5
double PSXFactor, PSYFactor, PSFontSize = 15.95;
short psxoff, psyoff;
double xps( x ) short x;
{ return((x+psxoff)*PSXFactor + PSXOffset); }
double yps( y ) short y;
{ return((YPixels-y-psyoff)*PSYFactor + PSYOffset); }
void psinit()
{
fputs("%!PS-Adobe-2.0 EPSF-2.0\n", fq);
fputs("%%BoundingBox: \n", fq);
fputs("initmatrix\n", fq);
fputs("/pM {stroke newpath moveto} def\n", fq);
fputs("/pL {lineto} def\n", fq);
fputs("/pQ {moveto} def\n", fq);
fputs("2 setlinejoin\n", fq);
fputs("/tR {/Times-Roman findfont exch scalefont setfont} def\n", fq);
fputs("/tI {/Times-Italic findfont exch scalefont setfont} def\n", fq);
fputs("/tB {/Times-Bold findfont exch scalefont setfont} def\n", fq);
fputs("/hV {/Helvetica-Bold findfont exch scalefont setfont}def\n",fq);
fputs("590 5 translate\n90 rotate\n", fq);
fputs(" scale\n", fq);
pspen(2, 0);
fputs("gsave\nnewpath\n", fq);
PSYFactor = 440. / YPixels;
PSXFactor = PSYFactor;
}
void psterm()
{
fputs("stroke\n", fq);
fputs("showpage\n", fq);
}
void psviewport(x1, y1, x2, y2)
short x1, y1, x2, y2;
{
double xp1, yp1, xp2, yp2;
psxoff = x1;
psyoff = y1;
if(x1==0 && y1==0 && x2==639 && y2==YPixels-1) {
fputs("stroke\ngrestore\ngsave\n", fq);
}
else {
xp1 = x1*PSXFactor + PSXOffset;
yp1 = (YPixels-y1)*PSYFactor + PSYOffset;
xp2 = x2*PSXFactor + PSXOffset;
yp2 = (YPixels-y2)*PSYFactor + PSYOffset;
fprintf(fq, "/viewpath { newpath %6.1f %6.1f pM\n", xp1, yp1);
fprintf(fq, "%6.1f %6.1f pL\n", xp2, yp1);
fprintf(fq, "%6.1f %6.1f lineto\n", xp2, yp2);
fprintf(fq, "%6.1f %6.1f lineto\n", xp1, yp2);
fputs("closepath } def\n", fq);
fputs("grestore\ngsave\nviewpath clip\n", fq);
}
}
void psmoveto(x, y) short x, y;
{ fprintf(fq, "%6.1f %6.1f pM\n", xps(x), yps(y)); }
void pslineto(x, y) short x, y;
{ fprintf(fq, "%6.1f %6.1f pL\n", xps(x), yps(y)); }
void psbox(flag, x1, y1, x2, y2)
short flag, x1, y1, x2, y2;
{
if(_getcolor() > 0) {
if( flag ) fputs("stroke newpath\n", fq);
psmoveto(x1, y1);
pslineto(x2, y1);
pslineto(x2, y2);
pslineto(x1, y2);
pslineto(x1, y1);
if( flag ) fputs("gsave stroke grestore fill\n", fq);
}
}
void pscircle(x, y, r)
short x, y, r;
{
psmoveto(x+r, y);
fprintf(fq, "%6.1f %6.1f %6.1f %6.1f %6.1f arc\n",
xps(x), yps(y), r*PSXFactor, 0., 360.);
}
void pstext(x, y, s)
int x, y;
char *s;
{
unsigned i;
fprintf(fq, "0 %6.1f rmoveto\n", -(PSFontSize-2)*PSYFactor);
fprintf(fq, "%6.1f tR (", PSFontSize);
for(i=0; i<strlen(s); i++) {
if(s[i]=='(' || s[i]==')') fputc('\\', fq);
fputc(s[i], fq);
}
fputs(") show\n", fq);
}
void psnormal_font() { PSFontSize = 15.95; }
void pssmall_font() { PSFontSize = 10.; }
void pspen(ipen, idash)
int ipen, idash;
{
double width;
if( psflag ) {
if(ipen>=0 || idash>=0) fputs("stroke\n", fq);
if(ipen >= 0) {
width = ipen * 0.2 + 0.1;
if(ipen == 0) width = 0.;
fprintf(fq, "%6.2f setlinewidth\n", width);
}
if(idash == 1) fputs("[6 2 2 2] 0 setdash\n", fq);
else if(idash == 0) fputs("[] 0 setdash\n", fq);
}
}
void pshcopy( n )
short n;
{
short i;
long m;
psterm(); pfile_close();
/* `open as a+ and rewind' does not work on BSD/386. */
if( pfile_open("r+") ) {
m = 0;
for(i=0; i<13; i++) { fgets(w$,81,fq); m += strlen(w$); }
fseek(fq, m, 0);
fprintf(fq, "%3.1f %3.1f", 0.5*(3-n), 0.5*(3-n));
pfile_close();
lpsubmit();
}
}
#endif
/****************** common routines **********************/
int tcopy()
{
/* c[1]=0 is required for TextOut of Windows. */
int i, j; char c[2];
blueF1_2( 0 );
if(lpinit() == 0) return( 0 );
c[1] = 0;
for(i=0; i<25; i++) {
for(j=0; j<80; j++) {
c[0] = mz[i*80+j];
if(! isprint(c[0])) c[0] = ' ';
lpputn(c, 1);
}
lpputs( "\r\n" );
}
lpclose();
return( 1 );
}
void hcopy(){}