4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-2001
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
34 * Giving Russians Opium May Alter Current Situation
36 static char *SRCID_x11_c = "$Id$";
46 /* These colours will be mapped to black on a monochrome screen */
47 unsigned long BLACK,BLUE,GREEN,CYAN,RED,BROWN,GREY,DARKGREY;
49 /* These colours will be mapped to white on a monochrome screen */
50 unsigned long LIGHTBLUE,LIGHTGREEN,LIGHTGREY,LIGHTCYAN,LIGHTRED,VIOLET,YELLOW,WHITE;
52 static XFontStruct *XLQF(FILE *err, Display *disp, char *name)
54 XFontStruct *font=XLoadQueryFont(disp,name);
57 fprintf(err, "Loaded font %s\n",name);
62 static XFontStruct *GetFont(FILE *err, Display *disp, char *name)
64 static char *fontnames[] = {
65 "sansb12","8x13bold","8x13",
68 #define MAXNAMES (sizeof(fontnames)/sizeof(fontnames[0]))
76 bFont=(bool) (font=XLQF(err,disp,name));
80 for (i=0; (!bFont && (i<MAXNAMES)); i++)
81 bFont=(bool) (font=XLQF(err,disp,fontnames[i]));
84 fontlist=XListFonts(disp,"?",1,&count);
86 bFont=(bool) (font=XLQF(err,disp,fontlist[0]));
89 fprintf (err, "Cannot load any suitable font\n");
93 static GC GetGC(Display *disp, XFontStruct *font)
97 values.font = font->fid;
98 values.foreground = WhitePixel(disp,DefaultScreen(disp));
100 return XCreateGC(disp,DefaultRootWindow(disp),GCForeground|GCFont,&values);
103 void GetNamedColor(t_x11 *x11,char *name,unsigned long *col)
105 /* If name is found than col set to that colour else col is unchanged */
108 if (XAllocNamedColor(x11->disp,x11->cmap,name,&clr,&exact))
111 fprintf(x11->console,"No colour %s\n",name);
114 static t_wlist *GetWList(t_x11 *x11, Window w)
119 while (curs && (curs->w != w))
130 static Bool TestEvent(Display *disp,XEvent *event,char *arg)
134 fprintf(stderr,"TestEvent\n");
136 if ((event->xany.window==tp->w) && (event->type==ConfigureNotify)) {
143 static void MainLoop(t_x11 *x11)
150 for (bReturn=FALSE; (!bReturn); ) {
152 XNextEvent(x11->disp,&event);
154 curs=GetWList(x11,w);
158 switch (event.type) {
160 /* Filter out expose events with non-zero count field */
161 if (event.xexpose.count != 0)
164 case ConfigureNotify:
165 /* Check if more are coming...
166 if (XCheckTypedWindowEvent(x11->disp,w,ConfigureNotify,&config))
173 bReturn=(*curs->cb)(x11,&event,w,curs->data);
179 static void RegisterCallback(t_x11 *x11,Window w,Window Parent,
180 CallBack cb, void *data)
202 static void UnRegisterCallback(t_x11 *x11, Window w)
209 x11->wlist=curs->next;
213 while (curs->next && (curs->next->w != w))
216 t_wlist *tmp=curs->next;
218 curs->next=curs->next->next;
225 static void SetInputMask(t_x11 *x11, Window w, unsigned long mask)
229 curs=GetWList(x11,w);
232 XSelectInput(x11->disp,w,(long)mask);
235 fprintf(x11->console,"No such window (%d)\n",(int)w);
238 static unsigned long GetInputMask(t_x11 *x11, Window w)
242 curs=GetWList(x11,w);
249 static void CleanUp(t_x11 *x11)
255 x11->wlist=curs->next;
256 XDestroyWindow(x11->disp,curs->w);
260 XCloseDisplay(x11->disp);
263 static void Xrm(int *argc, char *argv[])
266 static XrmOptionDescRec opTable[] = {
267 {"-background", "*background",
268 XrmoptionSepArg, (caddr_t) NULL},
269 {"-bd", "*borderColor",
270 XrmoptionSepArg, (caddr_t) NULL},
271 {"-bg", "*background",
272 XrmoptionSepArg, (caddr_t) NULL},
273 {"-borderwidth", "*TopLevelShell.borderwidth",
274 XrmoptionSepArg, (caddr_t) NULL},
275 {"-bordercolor", "*borderColor",
276 XrmoptionSepArg, (caddr_t) NULL},
277 {"-bw", "*TopLevelShell.borderColor",
278 XrmoptionSepArg, (caddr_t) NULL},
279 {"-display", ".display",
280 XrmoptionSepArg, (caddr_t) NULL},
281 {"-fg", "*foreground",
282 XrmoptionSepArg, (caddr_t) NULL},
284 XrmoptionSepArg, (caddr_t) NULL},
286 XrmoptionSepArg, (caddr_t) NULL},
287 {"-foreground", "*foreground",
288 XrmoptionSepArg, (caddr_t) NULL},
289 {"-geometry", ".TopLevelShell.geometry",
290 XrmoptionSepArg, (caddr_t) NULL},
291 {"-iconic", ".TopLevelShell.iconic",
292 XrmoptionNoArg, (caddr_t) "on"},
294 XrmoptionSepArg, (caddr_t) NULL},
295 {"-reverse", "*reverseVideo",
296 XrmoptionNoArg, (caddr_t) "on"},
297 {"-rv", "*reverseVideo",
298 XrmoptionNoArg, (caddr_t) "on"},
299 {"-synchronous", ".synchronous",
300 XrmoptionNoArg, (caddr_t) "on"},
301 {"-title", ".TopLevelShell.title",
302 XrmoptionSepArg, (caddr_t) NULL},
304 XrmoptionSepArg, (caddr_t) NULL},
306 #define TABLELENGTH (sizeof(opTable)/sizeof(opTable[0]))
310 static void Flush(t_x11 *x11)
312 fflush(x11->console);
315 t_x11 *GetX11(int *argc, char *argv[])
317 static char *v_name[] = {
318 "DirectColor","TrueColor", "PseudoColor",
319 "StaticColor","GrayScale", "StaticGray"
321 static int v_class[] = {
322 DirectColor,TrueColor, PseudoColor,
323 StaticColor,GrayScale, StaticGray
325 #define NCLASS (sizeof(v_class)/sizeof(v_class[0]))
333 char *title,*FG=NULL,*BG=NULL;
337 title=strdup(argv[0]);
339 /* First check environment */
340 fontname=getenv("GMXFONT");
341 display=getenv("DISPLAY");
345 for(i=1; (i < *argc); i++) {
346 if (argv[i][0]=='-') {
347 if (strlen(argv[i]) > 1) {
368 title=strdup(argv[++i]);
374 ARGV[ARGC++]=argv[i];
380 ARGV[ARGC++]=argv[i];
382 for (i=1; (i<ARGC); i++)
388 x11->dispname=display;
392 if ((x11->console=fopen("/dev/null","w"))== NULL)
395 if ((x11->disp=XOpenDisplay(display))==NULL) {
397 fprintf(x11->console,"Display %s invalid\n",display);
401 if ((x11->font=GetFont(x11->console,x11->disp,fontname))==NULL)
403 if ((x11->gc=GetGC(x11->disp,x11->font))==NULL)
406 x11->root=DefaultRootWindow(x11->disp);
407 x11->screen=DefaultScreen(x11->disp);
408 x11->depth=DefaultDepth(x11->disp,x11->screen);
409 x11->cmap=DefaultColormap(x11->disp,x11->screen);
411 /* These colours will be mapped to black on a monochrome screen */
412 x11->fg=BLACK=BLUE=GREEN=CYAN=RED=BROWN=GREY=DARKGREY=
413 BlackPixel(x11->disp,x11->screen);
415 /* These colours will be mapped to white on a monochrome screen */
417 LIGHTBLUE=LIGHTGREY=LIGHTGREEN=LIGHTCYAN=LIGHTRED=VIOLET=YELLOW=WHITE=
418 WhitePixel(x11->disp,x11->screen);
420 if (x11->depth > 1) {
421 /* Not B & W, Look what kind of screen we've got... */
422 for (i=0; (i < NCLASS); i++)
423 if (!XMatchVisualInfo(x11->disp,x11->screen,x11->depth,
426 if ((i==4) || (i==5))
427 fprintf(x11->console,"Greyscale screen, using B & W only\n");
429 /* We have real color! */
430 fprintf(x11->console,"%s screen with depth %d.\n",
431 (i==NCLASS)?"Unknown":v_name[i],x11->depth);
432 GetNamedColor(x11,"midnight blue",&BLUE);
433 GetNamedColor(x11,"DarkGreen",&GREEN);
434 GetNamedColor(x11,"SeaGreen",&CYAN);
435 GetNamedColor(x11,"red4",&RED);
436 GetNamedColor(x11,"Gray",&GREY);
437 GetNamedColor(x11,"Gray",&DARKGREY);
438 GetNamedColor(x11,"LightGray",&LIGHTGREY);
439 GetNamedColor(x11,"green",&LIGHTGREEN);
440 GetNamedColor(x11,"cyan",&LIGHTCYAN);
441 GetNamedColor(x11,"tomato1",&LIGHTRED);
442 GetNamedColor(x11,"violet",&VIOLET);
443 GetNamedColor(x11,"yellow",&YELLOW);
444 GetNamedColor(x11,"brown",&BROWN);
445 GetNamedColor(x11,"CornFlowerBlue",&LIGHTBLUE);
449 fprintf(x11->console,"Monochrome screen.\n");
451 /* We should use Xrm here... */
453 GetNamedColor(x11,FG,&(x11->fg));
457 GetNamedColor(x11,BG,&(x11->bg));
460 x11->title=strdup(title);
463 x11->GetNamedColor=&GetNamedColor;
464 x11->MainLoop=&MainLoop;
465 x11->RegisterCallback=&RegisterCallback;
466 x11->UnRegisterCallback=&UnRegisterCallback;
467 x11->SetInputMask=&SetInputMask;
468 x11->GetInputMask=&GetInputMask;
469 x11->CleanUp=&CleanUp;