4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_x11_c = "$Id$";
40 /* These colours will be mapped to black on a monochrome screen */
41 unsigned long BLACK,BLUE,GREEN,CYAN,RED,BROWN,GREY,DARKGREY;
43 /* These colours will be mapped to white on a monochrome screen */
44 unsigned long LIGHTBLUE,LIGHTGREEN,LIGHTGREY,LIGHTCYAN,LIGHTRED,VIOLET,YELLOW,WHITE;
46 static XFontStruct *XLQF(FILE *err, Display *disp, char *name)
48 XFontStruct *font=XLoadQueryFont(disp,name);
51 fprintf(err, "Loaded font %s\n",name);
56 static XFontStruct *GetFont(FILE *err, Display *disp, char *name)
58 static char *fontnames[] = {
59 "sansb12","8x13bold","8x13",
62 #define MAXNAMES (sizeof(fontnames)/sizeof(fontnames[0]))
70 bFont=(bool) (font=XLQF(err,disp,name));
74 for (i=0; (!bFont && (i<MAXNAMES)); i++)
75 bFont=(bool) (font=XLQF(err,disp,fontnames[i]));
78 fontlist=XListFonts(disp,"?",1,&count);
80 bFont=(bool) (font=XLQF(err,disp,fontlist[0]));
83 fprintf (err, "Cannot load any suitable font\n");
87 static GC GetGC(Display *disp, XFontStruct *font)
91 values.font = font->fid;
92 values.foreground = WhitePixel(disp,DefaultScreen(disp));
94 return XCreateGC(disp,DefaultRootWindow(disp),GCForeground|GCFont,&values);
97 void GetNamedColor(t_x11 *x11,char *name,unsigned long *col)
99 /* If name is found than col set to that colour else col is unchanged */
102 if (XAllocNamedColor(x11->disp,x11->cmap,name,&clr,&exact))
105 fprintf(x11->console,"No colour %s\n",name);
108 static t_wlist *GetWList(t_x11 *x11, Window w)
113 while (curs && (curs->w != w))
124 static Bool TestEvent(Display *disp,XEvent *event,char *arg)
128 fprintf(stderr,"TestEvent\n");
130 if ((event->xany.window==tp->w) && (event->type==ConfigureNotify)) {
137 static void MainLoop(t_x11 *x11)
144 for (bReturn=FALSE; (!bReturn); ) {
146 XNextEvent(x11->disp,&event);
148 curs=GetWList(x11,w);
152 switch (event.type) {
154 /* Filter out expose events with non-zero count field */
155 if (event.xexpose.count != 0)
158 case ConfigureNotify:
159 /* Check if more are coming...
160 if (XCheckTypedWindowEvent(x11->disp,w,ConfigureNotify,&config))
167 bReturn=(*curs->cb)(x11,&event,w,curs->data);
173 static void RegisterCallback(t_x11 *x11,Window w,Window Parent,
174 CallBack cb, void *data)
196 static void UnRegisterCallback(t_x11 *x11, Window w)
203 x11->wlist=curs->next;
207 while (curs->next && (curs->next->w != w))
210 t_wlist *tmp=curs->next;
212 curs->next=curs->next->next;
219 static void SetInputMask(t_x11 *x11, Window w, unsigned long mask)
223 curs=GetWList(x11,w);
226 XSelectInput(x11->disp,w,(long)mask);
229 fprintf(x11->console,"No such window (%d)\n",(int)w);
232 static unsigned long GetInputMask(t_x11 *x11, Window w)
236 curs=GetWList(x11,w);
243 static void CleanUp(t_x11 *x11)
249 x11->wlist=curs->next;
250 XDestroyWindow(x11->disp,curs->w);
254 XCloseDisplay(x11->disp);
257 static void Xrm(int *argc, char *argv[])
260 static XrmOptionDescRec opTable[] = {
261 {"-background", "*background",
262 XrmoptionSepArg, (caddr_t) NULL},
263 {"-bd", "*borderColor",
264 XrmoptionSepArg, (caddr_t) NULL},
265 {"-bg", "*background",
266 XrmoptionSepArg, (caddr_t) NULL},
267 {"-borderwidth", "*TopLevelShell.borderwidth",
268 XrmoptionSepArg, (caddr_t) NULL},
269 {"-bordercolor", "*borderColor",
270 XrmoptionSepArg, (caddr_t) NULL},
271 {"-bw", "*TopLevelShell.borderColor",
272 XrmoptionSepArg, (caddr_t) NULL},
273 {"-display", ".display",
274 XrmoptionSepArg, (caddr_t) NULL},
275 {"-fg", "*foreground",
276 XrmoptionSepArg, (caddr_t) NULL},
278 XrmoptionSepArg, (caddr_t) NULL},
280 XrmoptionSepArg, (caddr_t) NULL},
281 {"-foreground", "*foreground",
282 XrmoptionSepArg, (caddr_t) NULL},
283 {"-geometry", ".TopLevelShell.geometry",
284 XrmoptionSepArg, (caddr_t) NULL},
285 {"-iconic", ".TopLevelShell.iconic",
286 XrmoptionNoArg, (caddr_t) "on"},
288 XrmoptionSepArg, (caddr_t) NULL},
289 {"-reverse", "*reverseVideo",
290 XrmoptionNoArg, (caddr_t) "on"},
291 {"-rv", "*reverseVideo",
292 XrmoptionNoArg, (caddr_t) "on"},
293 {"-synchronous", ".synchronous",
294 XrmoptionNoArg, (caddr_t) "on"},
295 {"-title", ".TopLevelShell.title",
296 XrmoptionSepArg, (caddr_t) NULL},
298 XrmoptionSepArg, (caddr_t) NULL},
300 #define TABLELENGTH (sizeof(opTable)/sizeof(opTable[0]))
304 static void Flush(t_x11 *x11)
306 fflush(x11->console);
309 t_x11 *GetX11(int *argc, char *argv[])
311 static char *v_name[] = {
312 "DirectColor","TrueColor", "PseudoColor",
313 "StaticColor","GrayScale", "StaticGray"
315 static int v_class[] = {
316 DirectColor,TrueColor, PseudoColor,
317 StaticColor,GrayScale, StaticGray
319 #define NCLASS (sizeof(v_class)/sizeof(v_class[0]))
327 char *title,*FG=NULL,*BG=NULL;
331 title=strdup(argv[0]);
333 /* First check environment */
334 fontname=getenv("GMXFONT");
335 display=getenv("DISPLAY");
339 for(i=1; (i < *argc); i++) {
340 if (argv[i][0]=='-') {
341 if (strlen(argv[i]) > 1) {
362 title=strdup(argv[++i]);
368 ARGV[ARGC++]=argv[i];
374 ARGV[ARGC++]=argv[i];
376 for (i=1; (i<ARGC); i++)
382 x11->dispname=display;
386 if ((x11->console=fopen("/dev/null","w"))== NULL)
389 if ((x11->disp=XOpenDisplay(display))==NULL) {
391 fprintf(x11->console,"Display %s invalid\n",display);
395 if ((x11->font=GetFont(x11->console,x11->disp,fontname))==NULL)
397 if ((x11->gc=GetGC(x11->disp,x11->font))==NULL)
400 x11->root=DefaultRootWindow(x11->disp);
401 x11->screen=DefaultScreen(x11->disp);
402 x11->depth=DefaultDepth(x11->disp,x11->screen);
403 x11->cmap=DefaultColormap(x11->disp,x11->screen);
405 /* These colours will be mapped to black on a monochrome screen */
406 x11->fg=BLACK=BLUE=GREEN=CYAN=RED=BROWN=GREY=DARKGREY=
407 BlackPixel(x11->disp,x11->screen);
409 /* These colours will be mapped to white on a monochrome screen */
411 LIGHTBLUE=LIGHTGREY=LIGHTGREEN=LIGHTCYAN=LIGHTRED=VIOLET=YELLOW=WHITE=
412 WhitePixel(x11->disp,x11->screen);
414 if (x11->depth > 1) {
415 /* Not B & W, Look what kind of screen we've got... */
416 for (i=0; (i < NCLASS); i++)
417 if (!XMatchVisualInfo(x11->disp,x11->screen,x11->depth,
420 if ((i==4) || (i==5))
421 fprintf(x11->console,"Greyscale screen, using B & W only\n");
423 /* We have real color! */
424 fprintf(x11->console,"%s screen with depth %d.\n",
425 (i==NCLASS)?"Unknown":v_name[i],x11->depth);
426 GetNamedColor(x11,"midnight blue",&BLUE);
427 GetNamedColor(x11,"DarkGreen",&GREEN);
428 GetNamedColor(x11,"SeaGreen",&CYAN);
429 GetNamedColor(x11,"red4",&RED);
430 GetNamedColor(x11,"Gray",&GREY);
431 GetNamedColor(x11,"Gray",&DARKGREY);
432 GetNamedColor(x11,"LightGray",&LIGHTGREY);
433 GetNamedColor(x11,"green",&LIGHTGREEN);
434 GetNamedColor(x11,"cyan",&LIGHTCYAN);
435 GetNamedColor(x11,"tomato1",&LIGHTRED);
436 GetNamedColor(x11,"violet",&VIOLET);
437 GetNamedColor(x11,"yellow",&YELLOW);
438 GetNamedColor(x11,"brown",&BROWN);
439 GetNamedColor(x11,"CornFlowerBlue",&LIGHTBLUE);
443 fprintf(x11->console,"Monochrome screen.\n");
445 /* We should use Xrm here... */
447 GetNamedColor(x11,FG,&(x11->fg));
451 GetNamedColor(x11,BG,&(x11->bg));
454 x11->title=strdup(title);
457 x11->GetNamedColor=&GetNamedColor;
458 x11->MainLoop=&MainLoop;
459 x11->RegisterCallback=&RegisterCallback;
460 x11->UnRegisterCallback=&UnRegisterCallback;
461 x11->SetInputMask=&SetInputMask;
462 x11->GetInputMask=&GetInputMask;
463 x11->CleanUp=&CleanUp;