open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2011, Gabriel Dos Reis.4All rights reserved.56Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions are8met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in15the documentation and/or other materials provided with the16distribution.1718- Neither the name of The Numerical Algorithms Group Ltd. nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS23IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED24TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A25PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER26OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435#define _MAIN2D_C36#include "openaxiom-c-macros.h"3738#include <string.h>39#include <stdlib.h>40#include <stdio.h>41#include <unistd.h>42#include <signal.h>4344#include "cfuns.h"45#include "header2.h"46#include "bsdsignal.h"47#include "globals2.h"48#include "all_2d.H1"49#include "Gfun.H1"50#include "util.H1"51#include "XSpadFill.h"5253using namespace OpenAxiom;545556/**********************/57/** global variables **/58/**********************/59Window rtWindow,viewman;60Display *dsply;61XFontStruct *globalFont,62*buttonFont,63*headerFont,64*titleFont,65*graphFont,66*unitFont,67*serverFont;68GC globalGC1,69globalGC2,70anotherGC,71globGC,72trashGC,73controlMessageGC,74graphGC,75unitGC,76processGC;77XGCValues gcVals;78HashTable *table;79Colormap colorMap;8081XrmDatabase rDB; /* Resource database */828384char scaleXReport[5],85scaleYReport[5],86deltaXReport[5],87deltaYReport[5],88errorStr[80],89filename[256], /** For writing viewport data out to a file **/90*xDefault; /** used for accessing .XDefaults **/91const char* s;929394unsigned long *spadColors;95unsigned long foregroundColor, backgroundColor;9697int followMouse = no,98viewportKeyNum = 0,99scrn,100Socket = 1,101ack = 1,102someInt,103drawMore,104spadMode=no, /* yes if receiving OpenAxiom command and calling drawViewport */105spadDraw=no, /* yes if drawing viewport because of a OpenAxiom command */106pointsON = yes, /* these would affect the choices in buttons.c */107connectON = yes,108splineON = no,109axesON = yes,110unitsON = no,111zoomXON = yes,112zoomYON = yes,113transXON = yes,114transYON = yes,115currentGraph = 0, /* last graph selected */116queriedGraph = 0, /* current graph queried */117picking=0,118dropping=0,119viewAloned, /** if not connected to OpenAxiom **/120mono,121totalColors,122totalSolid,123totalDithered,124maxGreyShade,125totalHues,126totalSolidShades,127totalDitheredAndSolids,128totalShades;129/* totalShades is initially set to totalShadesConst (probably 8).130If X cannot allocate 8 shades for each hue, totalShades is131decremented. There is currently only a check for this value132to be positive. ---> something to add: change over to monochrome133if totalShades=0. Just modify the spadcolors.c file.134spadcolors.c has been modified so that it returns the value for135totalShades. Since the return value had previously been unused,136a modification in this way ensures continued support of other137routines calling this function (e.g. hyperDoc stuff). */138139viewPoints *viewport;140controlPanelStruct *control;141jmp_buf jumpFlag;142graphStruct graphArray[maxGraphs];143graphStateStruct graphStateArray[maxGraphs],144graphStateBackupArray[maxGraphs];145xPointStruct xPointsArray[maxGraphs];146float aspectR = 1.0;147/* global ps variables */148int psInit=no; /* need to call globalInitPs() each run */149GCptr GChead=NULL; /* ptr to head of ps GC linked list */150char *PSfilename, /* output file name used in user directory */151*envAXIOM; /* used as ps file pathnames */152153int154main(void)155{156157XGCValues controlGCVals;158int i;159view2DStruct viewData;160161char property[256];162char *prop = &property[0];163char *str_type[20];164XrmValue value;165166167/**** Set up display ****/168if ((dsply = XOpenDisplay(oa_getenv("DISPLAY"))) == NULL)169fprintf(stderr,"Could not open the display.\n");170scrn = DefaultScreen(dsply);171rtWindow = RootWindow(dsply,scrn);172173/**** link Xwindows to viewports - X10 feature ****/174table = XCreateAssocTable(nbuckets);175176/**** Create OpenAxiom color map ****/177totalColors = XInitSpadFill(dsply,scrn,&colorMap,178&totalHues,&totalSolidShades,179&totalDitheredAndSolids,&totalShades);180181if (totalColors < 0) {182fprintf(stderr,">>Error: Could not allocate all the necessary colors.\n");183exitWithAck(RootWindow(dsply,scrn),Window,-1);184}185186mergeDatabases();187188189/*** Determine whether monochrome or color is used ***/190if (XrmGetResource(rDB,"Axiom.2D.monochrome","",str_type,&value) == True)191(void) strncpy(prop,value.addr,(int)value.size);192else193(void) strcpy(prop, "off");194195mono = ((totalSolid == 2) || (strcmp(prop,"on") == 0));196197if (XrmGetResource(rDB,"Axiom.2D.inverse","",str_type,&value) == True)198(void) strncpy(prop,value.addr,(int)value.size);199else200(void) strcpy(prop, "off");201202if (mono)203if (strcmp(prop,"on") == 0) { /* 0 if equal (inverse video) */204foregroundColor = WhitePixel(dsply,scrn);205backgroundColor = BlackPixel(dsply,scrn);206} else { /* off (no inverse video) */207foregroundColor = BlackPixel(dsply,scrn);208backgroundColor = WhitePixel(dsply,scrn);209}210else /* inverse of inverse in color (for some strange reason) */211if (strcmp(prop,"on") == 0) { /* 0 if equal (inverse video) */212foregroundColor = WhitePixel(dsply,scrn);213backgroundColor = BlackPixel(dsply,scrn);214} else { /* off (no inverse video) */215foregroundColor = BlackPixel(dsply,scrn);216backgroundColor = WhitePixel(dsply,scrn);217}218219220/* read default file name for postScript output */221if (XrmGetResource(rDB,222"Axiom.2D.postscriptFile",223"",224str_type, &value) == True)225(void) strncpy(prop,value.addr,(int)value.size);226else227(void) strcpy(prop, "axiom2D.ps");228229PSfilename = (char *)malloc(strlen(prop)+1);230strcpy(PSfilename,prop);231232233234/**** Open global fonts ****/235serverFont = XQueryFont(dsply,XGContextFromGC(DefaultGC(dsply,scrn)));236237if (XrmGetResource(rDB,238"Axiom.2D.messageFont",239"Axiom.2D.Font",240str_type, &value) == True)241(void) strncpy(prop,value.addr,(int)value.size);242else243(void) strcpy(prop,messageFontDefault);244if ((globalFont = XLoadQueryFont(dsply, prop)) == NULL) {245fprintf(stderr,246"Warning: could not get the %s font for messageFont\n",prop);247globalFont = serverFont;248}249250if (XrmGetResource(rDB,251"Axiom.2D.buttonFont",252"Axiom.2D.Font",253str_type, &value) == True)254(void) strncpy(prop,value.addr,(int)value.size);255else256(void) strcpy(prop,buttonFontDefault);257if ((buttonFont = XLoadQueryFont(dsply, prop)) == NULL) {258fprintf(stderr,259"Warning: could not get the %s font for buttonFont\n",prop);260buttonFont = serverFont;261}262263if (XrmGetResource(rDB,264"Axiom.2D.headerFont",265"Axiom.2D.Font",266str_type, &value) == True)267(void) strncpy(prop,value.addr,(int)value.size);268else269(void) strcpy(prop,headerFontDefault);270271if ((headerFont = XLoadQueryFont(dsply, prop)) == NULL) {272fprintf(stderr,273"Warning: could not get the %s font for headerFont\n",prop);274headerFont = serverFont;275}276277if (XrmGetResource(rDB,278"Axiom.2D.titleFont",279"Axiom.2D.Font",280str_type,&value) == True)281(void) strncpy(prop,value.addr,(int)value.size);282else283(void) strcpy(prop,titleFontDefault);284285if ((titleFont = XLoadQueryFont(dsply, prop)) == NULL) {286fprintf(stderr,287"Warning: could not get the %s font for titleFont\n",prop);288titleFont = serverFont;289}290291if (XrmGetResource(rDB,292"Axiom.2D.graphFont",293"Axiom.2D.Font",294str_type,&value) == True)295(void) strncpy(prop,value.addr,(int)value.size);296else297(void) strcpy(prop,graphFontDefault);298299if ((graphFont = XLoadQueryFont(dsply, prop)) == NULL) {300fprintf(stderr,301"Warning: could not get the %s font for graphFont\n",prop);302graphFont = serverFont;303}304305if (XrmGetResource(rDB,306"Axiom.2D.unitFont",307"Axiom.2D.Font",308str_type,&value) == True)309(void) strncpy(prop,value.addr,(int)value.size);310else311(void) strcpy(prop,unitFontDefault);312313if ((unitFont = XLoadQueryFont(dsply, prop)) == NULL) {314fprintf(stderr,315"Warning: could not get the %s font for unitFont\n",prop);316unitFont = serverFont;317}318319320/**** Create widely used Graphic Contexts ****/321PSGlobalInit();322/* must initiate before using any G/PS functions323need character name: used as postscript GC variable324need to create ps GCs for all GCs used by drawings in viewWindow */325326/* globalGC1 */327328controlGCVals.foreground = monoColor(axesColorDefault);329controlGCVals.background = backgroundColor;330globalGC1 = XCreateGC(dsply,rtWindow,GCForeground | GCBackground ,331&controlGCVals);332carefullySetFont(globalGC1,globalFont);333334335/* create the equivalent GCs for ps */336PSCreateContext(globalGC1, "globalGC1", psNormalWidth, psButtCap,337psMiterJoin, psWhite, psBlack);338339/* controlMessageGC */340341controlGCVals.foreground = controlMessageColor;342controlMessageGC = XCreateGC(dsply,rtWindow,GCForeground | GCBackground343,&controlGCVals);344carefullySetFont(controlMessageGC,globalFont);345346/* globalGC2 */347348controlGCVals.foreground = monoColor(labelColor);349controlGCVals.background = backgroundColor;350globalGC2 = XCreateGC(dsply,rtWindow,GCForeground | GCBackground,351&controlGCVals);352carefullySetFont(globalGC2,buttonFont);353PSCreateContext(globalGC2, "globalGC2", psNormalWidth, psButtCap,354psMiterJoin, psWhite, psBlack);355356/* trashGC */357358trashGC = XCreateGC(dsply,rtWindow,0,&controlGCVals);359carefullySetFont(trashGC,buttonFont);360PSCreateContext(trashGC, "trashGC", psNormalWidth, psButtCap,361psMiterJoin, psWhite, psBlack);362363/* globGC */364365globGC = XCreateGC(dsply,rtWindow,0,&controlGCVals);366carefullySetFont(globGC,headerFont);367PSCreateContext(globGC, "globGC", psNormalWidth, psButtCap,368psMiterJoin, psWhite, psBlack);369370/* anotherGC */371372controlGCVals.line_width = colorWidth;373anotherGC = XCreateGC(dsply,rtWindow,GCBackground,&controlGCVals);374carefullySetFont(anotherGC,titleFont);375PSCreateContext(anotherGC, "anotherGC", psNormalWidth, psButtCap,376psMiterJoin, psWhite, psBlack);377378/* processGC */379380gcVals.background = backgroundColor;381processGC = XCreateGC(dsply,rtWindow,GCBackground ,&gcVals);382carefullySetFont(processGC,buttonFont);383384/* graphGC */385386graphGC = XCreateGC(dsply,rtWindow,GCBackground,&gcVals);387carefullySetFont(graphGC,graphFont);388PSCreateContext(graphGC, "graphGC", psNormalWidth, psButtCap,389psMiterJoin, psWhite, psBlack);390391/* unitGC */392393unitGC = XCreateGC(dsply,rtWindow,GCBackground ,&gcVals);394carefullySetFont(unitGC,unitFont);395PSCreateContext(unitGC, "unitGC", psNormalWidth, psButtCap,396psMiterJoin, psWhite, psBlack);397398/**** Initialize Graph States ****/399400for (i=0; i<maxGraphs; i++) {401graphStateArray[i].scaleX = 0.9;402graphStateArray[i].scaleY = 0.9;403graphStateArray[i].deltaX = 0.0;404graphStateArray[i].deltaY = 0.0;405graphStateArray[i].centerX = 0.0;406graphStateArray[i].centerY = 0.0;407graphStateArray[i].pointsOn = yes;408graphStateArray[i].connectOn = yes;409graphStateArray[i].splineOn = no;410graphStateArray[i].axesOn = yes;411graphStateArray[i].unitsOn = no;412graphStateArray[i].showing = no;413graphStateArray[i].selected = no;414graphStateBackupArray[i] = graphStateArray[i];415}416417/**** Get Data from the Viewport Manager ****/418419i = 123;420check(write(Socket,&i,intSize));421422/* Check if I am getting stuff from OpenAxiom or, if I am viewAlone. */423readViewman(&viewAloned,intSize);424readViewman(&viewData,sizeof(view2DStruct));425readViewman(&i,intSize);426427if (!(viewData.title = (char *)malloc(i))) {428fprintf(stderr,429"ERROR: Ran out of memory trying to receive the title.\n");430exitWithAck(RootWindow(dsply,scrn),Window,-1);431}432readViewman(viewData.title,i);433434for (i=0; i<maxGraphs; i++) {435readViewman(&(graphArray[i].key),intSize);436if (graphArray[i].key) { /** this graph slot has data **/437getGraphFromViewman(i);438} /* if graph exists (graphArray[i].key is not zero) */439} /* for i in graphs */440441viewport = makeView2D(&viewData);442control = viewport->controlPanel;443444bsdSignal(SIGTERM,goodbye,DontRestartSystemCalls);445446/* send acknowledgement to viewport manager */447i = 345;448check(write(Socket,&(viewport->viewWindow),sizeof(Window)));449450processEvents();451452goodbye(-1);453return(0); /* control never reaches here but compiler complains */454} /* main() */455456void457mergeDatabases(void)458{459/* using global460rDB461dsply462*/463XrmDatabase homeDB,serverDB,applicationDB;464char filenamebuf[1024];465char *filename = &filenamebuf[0];466const char *classname = "OpenAxiom";467char name[255];468469(void) XrmInitialize();470(void) strcpy(name, "/usr/lib/X11/app-defaults/");471(void) strcat(name, classname);472applicationDB = XrmGetFileDatabase(name);473(void) XrmMergeDatabases(applicationDB, &rDB);474475if (XResourceManagerString(dsply) != NULL)476serverDB = XrmGetStringDatabase(XResourceManagerString(dsply));477else {478(void) strcpy(filename,oa_getenv("HOME"));479(void) strcat(filename,"/.Xdefaults");480serverDB = XrmGetFileDatabase(filename);481}482XrmMergeDatabases(serverDB,&rDB);483if ( oa_getenv ("XENVIRONMENT") == NULL) {484int len;485(void) strcpy(filename,oa_getenv("HOME"));486(void) strcat(filename,"/.Xdefaults-");487len = strlen(filename);488(void) gethostname(filename+len,1024-len);489}490else491(void) strcpy (filename,oa_getenv ("XENVIRONMENT"));492493homeDB = XrmGetFileDatabase(filename);494XrmMergeDatabases(homeDB,&rDB);495}496497498499