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-2013, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
45 #include "gromacs/utility/smalloc.h"
49 /* These colours will be mapped to black on a monochrome screen */
50 unsigned long BLACK, BLUE, GREEN, CYAN, RED, BROWN, GREY, DARKGREY;
52 /* These colours will be mapped to white on a monochrome screen */
53 unsigned long LIGHTBLUE, LIGHTGREEN, LIGHTGREY, LIGHTCYAN, LIGHTRED, VIOLET, YELLOW, WHITE;
55 static XFontStruct *XLQF(FILE gmx_unused *err, Display *disp, const char *name)
57 XFontStruct *font = XLoadQueryFont(disp, name);
61 fprintf(err, "Loaded font %s\n", name);
67 static XFontStruct *GetFont(FILE *err, Display *disp, char *name)
69 static const char *fontnames[] = {
70 "sansb12", "8x13bold", "8x13",
71 "9x15", "6x13", "fixed"
73 #define MAXNAMES (sizeof(fontnames)/sizeof(fontnames[0]))
82 bFont = ((font = XLQF(err, disp, name)) != NULL);
89 for (i = 0; (!bFont && (i < MAXNAMES)); i++)
91 bFont = ((font = XLQF(err, disp, fontnames[i])) != NULL);
96 fontlist = XListFonts(disp, "?", 1, &count);
99 bFont = ((font = XLQF(err, disp, fontlist[0])) != NULL);
104 fprintf (err, "Cannot load any suitable font\n");
109 static GC GetGC(Display *disp, XFontStruct *font)
113 values.font = font->fid;
114 values.foreground = WhitePixel(disp, DefaultScreen(disp));
116 return XCreateGC(disp, DefaultRootWindow(disp), GCForeground|GCFont, &values);
119 void GetNamedColor(t_x11 *x11, const char *name, unsigned long *col)
121 /* If name is found than col set to that colour else col is unchanged */
124 if (XAllocNamedColor(x11->disp, x11->cmap, name, &clr, &exact))
130 fprintf(x11->console, "No colour %s\n", name);
134 static t_wlist *GetWList(t_x11 *x11, Window w)
139 while (curs && (curs->w != w))
147 static void MainLoop(t_x11 *x11)
154 for (bReturn = false; (!bReturn); )
158 XNextEvent(x11->disp, &event);
159 w = event.xany.window;
160 curs = GetWList(x11, w);
170 /* Filter out expose events with non-zero count field */
171 if (event.xexpose.count != 0)
176 case ConfigureNotify:
177 /* Check if more are coming...
178 if (XCheckTypedWindowEvent(x11->disp,w,ConfigureNotify,&config))
186 bReturn = (*curs->cb)(x11, &event, w, curs->data);
193 static void RegisterCallback(t_x11 *x11, Window w, Window Parent,
194 CallBack cb, void *data)
196 t_wlist *curs, *item;
200 item->Parent = Parent;
221 static void UnRegisterCallback(t_x11 *x11, Window w)
230 x11->wlist = curs->next;
235 while (curs->next && (curs->next->w != w))
241 t_wlist *tmp = curs->next;
243 curs->next = curs->next->next;
250 static void SetInputMask(t_x11 *x11, Window w, unsigned long mask)
254 curs = GetWList(x11, w);
258 XSelectInput(x11->disp, w, (long)mask);
262 fprintf(x11->console, "No such window (%d)\n", (int)w);
266 static unsigned long GetInputMask(t_x11 *x11, Window w)
270 curs = GetWList(x11, w);
281 static void CleanUp(t_x11 *x11)
288 x11->wlist = curs->next;
289 XDestroyWindow(x11->disp, curs->w);
293 XCloseDisplay(x11->disp);
296 static void Flush(t_x11 *x11)
298 fflush(x11->console);
301 t_x11 *GetX11(int *argc, char *argv[])
303 static const char *v_name[] = {
304 "DirectColor", "TrueColor", "PseudoColor",
305 "StaticColor", "GrayScale", "StaticGray"
307 static int v_class[] = {
308 DirectColor, TrueColor, PseudoColor,
309 StaticColor, GrayScale, StaticGray
311 #define NCLASS (sizeof(v_class)/sizeof(v_class[0]))
319 char *title, *FG = NULL, *BG = NULL;
320 bool bVerbose = false;
323 title = strdup(argv[0]);
325 /* First check environment */
326 fontname = getenv("GMX_FONT");
327 display = getenv("DISPLAY");
331 for (i = 1; (i < *argc); i++)
333 if (argv[i][0] == '-')
335 if (strlen(argv[i]) > 1)
351 fontname = argv[++i];
360 title = strdup(argv[++i]);
366 ARGV[ARGC++] = argv[i];
374 ARGV[ARGC++] = argv[i];
377 for (i = 1; (i < ARGC); i++)
385 x11->dispname = display;
388 x11->console = stderr;
391 if ((x11->console = fopen("/dev/null", "w")) == NULL)
393 x11->console = stderr;
396 if ((x11->disp = XOpenDisplay(display)) == NULL)
400 fprintf(x11->console, "Display %s invalid\n", display);
405 if ((x11->font = GetFont(x11->console, x11->disp, fontname)) == NULL)
409 if ((x11->gc = GetGC(x11->disp, x11->font)) == NULL)
414 x11->root = DefaultRootWindow(x11->disp);
415 x11->screen = DefaultScreen(x11->disp);
416 x11->depth = DefaultDepth(x11->disp, x11->screen);
417 x11->cmap = DefaultColormap(x11->disp, x11->screen);
419 /* These colours will be mapped to black on a monochrome screen */
420 x11->fg = BLACK = BLUE = GREEN = CYAN = RED = BROWN = GREY = DARKGREY =
421 BlackPixel(x11->disp, x11->screen);
423 /* These colours will be mapped to white on a monochrome screen */
425 LIGHTBLUE = LIGHTGREY = LIGHTGREEN = LIGHTCYAN = LIGHTRED = VIOLET = YELLOW = WHITE =
426 WhitePixel(x11->disp, x11->screen);
430 /* Not B & W, Look what kind of screen we've got... */
431 for (i = 0; (i < (int)NCLASS); i++)
433 if (!XMatchVisualInfo(x11->disp, x11->screen, x11->depth,
434 v_class[i], &v_info))
439 if ((i == 4) || (i == 5))
441 fprintf(x11->console, "Greyscale screen, using B & W only\n");
445 /* We have real color! */
446 fprintf(x11->console, "%s screen with depth %d.\n",
447 (i == NCLASS) ? "Unknown" : v_name[i], x11->depth);
448 GetNamedColor(x11, "midnight blue", &BLUE);
449 GetNamedColor(x11, "DarkGreen", &GREEN);
450 GetNamedColor(x11, "SeaGreen", &CYAN);
451 GetNamedColor(x11, "red4", &RED);
452 GetNamedColor(x11, "Gray", &GREY);
453 GetNamedColor(x11, "Gray", &DARKGREY);
454 GetNamedColor(x11, "LightGray", &LIGHTGREY);
455 GetNamedColor(x11, "green", &LIGHTGREEN);
456 GetNamedColor(x11, "cyan", &LIGHTCYAN);
457 GetNamedColor(x11, "tomato1", &LIGHTRED);
458 GetNamedColor(x11, "violet", &VIOLET);
459 GetNamedColor(x11, "yellow", &YELLOW);
460 GetNamedColor(x11, "brown", &BROWN);
461 GetNamedColor(x11, "CornFlowerBlue", &LIGHTBLUE);
466 fprintf(x11->console, "Monochrome screen.\n");
469 /* We should use Xrm here... */
472 GetNamedColor(x11, FG, &(x11->fg));
480 GetNamedColor(x11, BG, &(x11->bg));
486 x11->title = strdup(title);
489 x11->GetNamedColor = &GetNamedColor;
490 x11->MainLoop = &MainLoop;
491 x11->RegisterCallback = &RegisterCallback;
492 x11->UnRegisterCallback = &UnRegisterCallback;
493 x11->SetInputMask = &SetInputMask;
494 x11->GetInputMask = &GetInputMask;
495 x11->CleanUp = &CleanUp;