2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
50 /* These colours will be mapped to black on a monochrome screen */
51 unsigned long BLACK
,BLUE
,GREEN
,CYAN
,RED
,BROWN
,GREY
,DARKGREY
;
53 /* These colours will be mapped to white on a monochrome screen */
54 unsigned long LIGHTBLUE
,LIGHTGREEN
,LIGHTGREY
,LIGHTCYAN
,LIGHTRED
,VIOLET
,YELLOW
,WHITE
;
56 static XFontStruct
*XLQF(FILE *err
, Display
*disp
, const char *name
)
58 XFontStruct
*font
=XLoadQueryFont(disp
,name
);
61 fprintf(err
, "Loaded font %s\n",name
);
66 static XFontStruct
*GetFont(FILE *err
, Display
*disp
, char *name
)
68 static const char *fontnames
[] = {
69 "sansb12","8x13bold","8x13",
72 #define MAXNAMES (sizeof(fontnames)/sizeof(fontnames[0]))
77 gmx_bool bFont
= FALSE
;
80 bFont
=(gmx_bool
) ((font
=XLQF(err
,disp
,name
))!=NULL
);
84 for (i
=0; (!bFont
&& (i
<MAXNAMES
)); i
++)
85 bFont
=(gmx_bool
) ((font
=XLQF(err
,disp
,fontnames
[i
]))!=NULL
);
88 fontlist
=XListFonts(disp
,"?",1,&count
);
90 bFont
=(gmx_bool
) ((font
=XLQF(err
,disp
,fontlist
[0]))!=NULL
);
93 fprintf (err
, "Cannot load any suitable font\n");
97 static GC
GetGC(Display
*disp
, XFontStruct
*font
)
101 values
.font
= font
->fid
;
102 values
.foreground
= WhitePixel(disp
,DefaultScreen(disp
));
104 return XCreateGC(disp
,DefaultRootWindow(disp
),GCForeground
|GCFont
,&values
);
107 void GetNamedColor(t_x11
*x11
,const char *name
,unsigned long *col
)
109 /* If name is found than col set to that colour else col is unchanged */
112 if (XAllocNamedColor(x11
->disp
,x11
->cmap
,name
,&clr
,&exact
))
115 fprintf(x11
->console
,"No colour %s\n",name
);
118 static t_wlist
*GetWList(t_x11
*x11
, Window w
)
123 while (curs
&& (curs
->w
!= w
))
134 static Bool
TestEvent(Display
*disp
,XEvent
*event
,char *arg
)
138 fprintf(stderr
,"TestEvent\n");
140 if ((event
->xany
.window
==tp
->w
) && (event
->type
==ConfigureNotify
)) {
147 static void MainLoop(t_x11
*x11
)
154 for (bReturn
=FALSE
; (!bReturn
); ) {
156 XNextEvent(x11
->disp
,&event
);
158 curs
=GetWList(x11
,w
);
162 switch (event
.type
) {
164 /* Filter out expose events with non-zero count field */
165 if (event
.xexpose
.count
!= 0)
168 case ConfigureNotify
:
169 /* Check if more are coming...
170 if (XCheckTypedWindowEvent(x11->disp,w,ConfigureNotify,&config))
177 bReturn
=(*curs
->cb
)(x11
,&event
,w
,curs
->data
);
183 static void RegisterCallback(t_x11
*x11
,Window w
,Window Parent
,
184 CallBack cb
, void *data
)
206 static void UnRegisterCallback(t_x11
*x11
, Window w
)
213 x11
->wlist
=curs
->next
;
217 while (curs
->next
&& (curs
->next
->w
!= w
))
220 t_wlist
*tmp
=curs
->next
;
222 curs
->next
=curs
->next
->next
;
229 static void SetInputMask(t_x11
*x11
, Window w
, unsigned long mask
)
233 curs
=GetWList(x11
,w
);
236 XSelectInput(x11
->disp
,w
,(long)mask
);
239 fprintf(x11
->console
,"No such window (%d)\n",(int)w
);
242 static unsigned long GetInputMask(t_x11
*x11
, Window w
)
246 curs
=GetWList(x11
,w
);
253 static void CleanUp(t_x11
*x11
)
259 x11
->wlist
=curs
->next
;
260 XDestroyWindow(x11
->disp
,curs
->w
);
264 XCloseDisplay(x11
->disp
);
267 static void Xrm(int *argc
, char *argv
[])
270 static XrmOptionDescRec opTable[] = {
271 {"-background", "*background",
272 XrmoptionSepArg, (caddr_t) NULL},
273 {"-bd", "*borderColor",
274 XrmoptionSepArg, (caddr_t) NULL},
275 {"-bg", "*background",
276 XrmoptionSepArg, (caddr_t) NULL},
277 {"-borderwidth", "*TopLevelShell.borderwidth",
278 XrmoptionSepArg, (caddr_t) NULL},
279 {"-bordercolor", "*borderColor",
280 XrmoptionSepArg, (caddr_t) NULL},
281 {"-bw", "*TopLevelShell.borderColor",
282 XrmoptionSepArg, (caddr_t) NULL},
283 {"-display", ".display",
284 XrmoptionSepArg, (caddr_t) NULL},
285 {"-fg", "*foreground",
286 XrmoptionSepArg, (caddr_t) NULL},
288 XrmoptionSepArg, (caddr_t) NULL},
290 XrmoptionSepArg, (caddr_t) NULL},
291 {"-foreground", "*foreground",
292 XrmoptionSepArg, (caddr_t) NULL},
293 {"-geometry", ".TopLevelShell.geometry",
294 XrmoptionSepArg, (caddr_t) NULL},
295 {"-iconic", ".TopLevelShell.iconic",
296 XrmoptionNoArg, (caddr_t) "on"},
298 XrmoptionSepArg, (caddr_t) NULL},
299 {"-reverse", "*reverseVideo",
300 XrmoptionNoArg, (caddr_t) "on"},
301 {"-rv", "*reverseVideo",
302 XrmoptionNoArg, (caddr_t) "on"},
303 {"-synchronous", ".synchronous",
304 XrmoptionNoArg, (caddr_t) "on"},
305 {"-title", ".TopLevelShell.title",
306 XrmoptionSepArg, (caddr_t) NULL},
308 XrmoptionSepArg, (caddr_t) NULL},
310 #define TABLELENGTH (sizeof(opTable)/sizeof(opTable[0]))
314 static void Flush(t_x11
*x11
)
316 fflush(x11
->console
);
319 t_x11
*GetX11(int *argc
, char *argv
[])
321 static const char *v_name
[] = {
322 "DirectColor","TrueColor", "PseudoColor",
323 "StaticColor","GrayScale", "StaticGray"
325 static int v_class
[] = {
326 DirectColor
,TrueColor
, PseudoColor
,
327 StaticColor
,GrayScale
, StaticGray
329 #define NCLASS (sizeof(v_class)/sizeof(v_class[0]))
337 char *title
,*FG
=NULL
,*BG
=NULL
;
338 gmx_bool bVerbose
=FALSE
;
341 title
=strdup(argv
[0]);
343 /* First check environment */
344 fontname
=getenv("GMXFONT");
345 display
=getenv("DISPLAY");
349 for(i
=1; (i
< *argc
); i
++) {
350 if (argv
[i
][0]=='-') {
351 if (strlen(argv
[i
]) > 1) {
372 title
=strdup(argv
[++i
]);
378 ARGV
[ARGC
++]=argv
[i
];
384 ARGV
[ARGC
++]=argv
[i
];
386 for (i
=1; (i
<ARGC
); i
++)
392 x11
->dispname
=display
;
396 if ((x11
->console
=fopen("/dev/null","w"))== NULL
)
399 if ((x11
->disp
=XOpenDisplay(display
))==NULL
) {
401 fprintf(x11
->console
,"Display %s invalid\n",display
);
405 if ((x11
->font
=GetFont(x11
->console
,x11
->disp
,fontname
))==NULL
)
407 if ((x11
->gc
=GetGC(x11
->disp
,x11
->font
))==NULL
)
410 x11
->root
=DefaultRootWindow(x11
->disp
);
411 x11
->screen
=DefaultScreen(x11
->disp
);
412 x11
->depth
=DefaultDepth(x11
->disp
,x11
->screen
);
413 x11
->cmap
=DefaultColormap(x11
->disp
,x11
->screen
);
415 /* These colours will be mapped to black on a monochrome screen */
416 x11
->fg
=BLACK
=BLUE
=GREEN
=CYAN
=RED
=BROWN
=GREY
=DARKGREY
=
417 BlackPixel(x11
->disp
,x11
->screen
);
419 /* These colours will be mapped to white on a monochrome screen */
421 LIGHTBLUE
=LIGHTGREY
=LIGHTGREEN
=LIGHTCYAN
=LIGHTRED
=VIOLET
=YELLOW
=WHITE
=
422 WhitePixel(x11
->disp
,x11
->screen
);
424 if (x11
->depth
> 1) {
425 /* Not B & W, Look what kind of screen we've got... */
426 for (i
=0; (i
< NCLASS
); i
++)
427 if (!XMatchVisualInfo(x11
->disp
,x11
->screen
,x11
->depth
,
430 if ((i
==4) || (i
==5))
431 fprintf(x11
->console
,"Greyscale screen, using B & W only\n");
433 /* We have real color! */
434 fprintf(x11
->console
,"%s screen with depth %d.\n",
435 (i
==NCLASS
)?"Unknown":v_name
[i
],x11
->depth
);
436 GetNamedColor(x11
,"midnight blue",&BLUE
);
437 GetNamedColor(x11
,"DarkGreen",&GREEN
);
438 GetNamedColor(x11
,"SeaGreen",&CYAN
);
439 GetNamedColor(x11
,"red4",&RED
);
440 GetNamedColor(x11
,"Gray",&GREY
);
441 GetNamedColor(x11
,"Gray",&DARKGREY
);
442 GetNamedColor(x11
,"LightGray",&LIGHTGREY
);
443 GetNamedColor(x11
,"green",&LIGHTGREEN
);
444 GetNamedColor(x11
,"cyan",&LIGHTCYAN
);
445 GetNamedColor(x11
,"tomato1",&LIGHTRED
);
446 GetNamedColor(x11
,"violet",&VIOLET
);
447 GetNamedColor(x11
,"yellow",&YELLOW
);
448 GetNamedColor(x11
,"brown",&BROWN
);
449 GetNamedColor(x11
,"CornFlowerBlue",&LIGHTBLUE
);
453 fprintf(x11
->console
,"Monochrome screen.\n");
455 /* We should use Xrm here... */
457 GetNamedColor(x11
,FG
,&(x11
->fg
));
461 GetNamedColor(x11
,BG
,&(x11
->bg
));
464 x11
->title
=strdup(title
);
467 x11
->GetNamedColor
=&GetNamedColor
;
468 x11
->MainLoop
=&MainLoop
;
469 x11
->RegisterCallback
=&RegisterCallback
;
470 x11
->UnRegisterCallback
=&UnRegisterCallback
;
471 x11
->SetInputMask
=&SetInputMask
;
472 x11
->GetInputMask
=&GetInputMask
;
473 x11
->CleanUp
=&CleanUp
;