3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2013, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Gyas ROwers Mature At Cryogenic Speed
47 /* These colours will be mapped to black on a monochrome screen */
48 unsigned long BLACK, BLUE, GREEN, CYAN, RED, BROWN, GREY, DARKGREY;
50 /* These colours will be mapped to white on a monochrome screen */
51 unsigned long LIGHTBLUE, LIGHTGREEN, LIGHTGREY, LIGHTCYAN, LIGHTRED, VIOLET, YELLOW, WHITE;
53 static XFontStruct *XLQF(FILE *err, Display *disp, const char *name)
55 XFontStruct *font = XLoadQueryFont(disp, name);
59 fprintf(err, "Loaded font %s\n", name);
65 static XFontStruct *GetFont(FILE *err, Display *disp, char *name)
67 static const char *fontnames[] = {
68 "sansb12", "8x13bold", "8x13",
69 "9x15", "6x13", "fixed"
71 #define MAXNAMES (sizeof(fontnames)/sizeof(fontnames[0]))
80 bFont = ((font = XLQF(err, disp, name)) != NULL);
87 for (i = 0; (!bFont && (i < MAXNAMES)); i++)
89 bFont = ((font = XLQF(err, disp, fontnames[i])) != NULL);
94 fontlist = XListFonts(disp, "?", 1, &count);
97 bFont = ((font = XLQF(err, disp, fontlist[0])) != NULL);
102 fprintf (err, "Cannot load any suitable font\n");
107 static GC GetGC(Display *disp, XFontStruct *font)
111 values.font = font->fid;
112 values.foreground = WhitePixel(disp, DefaultScreen(disp));
114 return XCreateGC(disp, DefaultRootWindow(disp), GCForeground|GCFont, &values);
117 void GetNamedColor(t_x11 *x11, const char *name, unsigned long *col)
119 /* If name is found than col set to that colour else col is unchanged */
122 if (XAllocNamedColor(x11->disp, x11->cmap, name, &clr, &exact))
128 fprintf(x11->console, "No colour %s\n", name);
132 static t_wlist *GetWList(t_x11 *x11, Window w)
137 while (curs && (curs->w != w))
145 static void MainLoop(t_x11 *x11)
152 for (bReturn = false; (!bReturn); )
156 XNextEvent(x11->disp, &event);
157 w = event.xany.window;
158 curs = GetWList(x11, w);
168 /* Filter out expose events with non-zero count field */
169 if (event.xexpose.count != 0)
174 case ConfigureNotify:
175 /* Check if more are coming...
176 if (XCheckTypedWindowEvent(x11->disp,w,ConfigureNotify,&config))
184 bReturn = (*curs->cb)(x11, &event, w, curs->data);
191 static void RegisterCallback(t_x11 *x11, Window w, Window Parent,
192 CallBack cb, void *data)
194 t_wlist *curs, *item;
198 item->Parent = Parent;
219 static void UnRegisterCallback(t_x11 *x11, Window w)
228 x11->wlist = curs->next;
233 while (curs->next && (curs->next->w != w))
239 t_wlist *tmp = curs->next;
241 curs->next = curs->next->next;
248 static void SetInputMask(t_x11 *x11, Window w, unsigned long mask)
252 curs = GetWList(x11, w);
256 XSelectInput(x11->disp, w, (long)mask);
260 fprintf(x11->console, "No such window (%d)\n", (int)w);
264 static unsigned long GetInputMask(t_x11 *x11, Window w)
268 curs = GetWList(x11, w);
279 static void CleanUp(t_x11 *x11)
286 x11->wlist = curs->next;
287 XDestroyWindow(x11->disp, curs->w);
291 XCloseDisplay(x11->disp);
294 static void Flush(t_x11 *x11)
296 fflush(x11->console);
299 t_x11 *GetX11(int *argc, char *argv[])
301 static const char *v_name[] = {
302 "DirectColor", "TrueColor", "PseudoColor",
303 "StaticColor", "GrayScale", "StaticGray"
305 static int v_class[] = {
306 DirectColor, TrueColor, PseudoColor,
307 StaticColor, GrayScale, StaticGray
309 #define NCLASS (sizeof(v_class)/sizeof(v_class[0]))
317 char *title, *FG = NULL, *BG = NULL;
318 bool bVerbose = false;
321 title = strdup(argv[0]);
323 /* First check environment */
324 fontname = getenv("GMXFONT");
325 display = getenv("DISPLAY");
329 for (i = 1; (i < *argc); i++)
331 if (argv[i][0] == '-')
333 if (strlen(argv[i]) > 1)
349 fontname = argv[++i];
358 title = strdup(argv[++i]);
364 ARGV[ARGC++] = argv[i];
372 ARGV[ARGC++] = argv[i];
375 for (i = 1; (i < ARGC); i++)
383 x11->dispname = display;
386 x11->console = stderr;
389 if ((x11->console = fopen("/dev/null", "w")) == NULL)
391 x11->console = stderr;
394 if ((x11->disp = XOpenDisplay(display)) == NULL)
398 fprintf(x11->console, "Display %s invalid\n", display);
403 if ((x11->font = GetFont(x11->console, x11->disp, fontname)) == NULL)
407 if ((x11->gc = GetGC(x11->disp, x11->font)) == NULL)
412 x11->root = DefaultRootWindow(x11->disp);
413 x11->screen = DefaultScreen(x11->disp);
414 x11->depth = DefaultDepth(x11->disp, x11->screen);
415 x11->cmap = DefaultColormap(x11->disp, x11->screen);
417 /* These colours will be mapped to black on a monochrome screen */
418 x11->fg = BLACK = BLUE = GREEN = CYAN = RED = BROWN = GREY = DARKGREY =
419 BlackPixel(x11->disp, x11->screen);
421 /* These colours will be mapped to white on a monochrome screen */
423 LIGHTBLUE = LIGHTGREY = LIGHTGREEN = LIGHTCYAN = LIGHTRED = VIOLET = YELLOW = WHITE =
424 WhitePixel(x11->disp, x11->screen);
428 /* Not B & W, Look what kind of screen we've got... */
429 for (i = 0; (i < (int)NCLASS); i++)
431 if (!XMatchVisualInfo(x11->disp, x11->screen, x11->depth,
432 v_class[i], &v_info))
437 if ((i == 4) || (i == 5))
439 fprintf(x11->console, "Greyscale screen, using B & W only\n");
443 /* We have real color! */
444 fprintf(x11->console, "%s screen with depth %d.\n",
445 (i == NCLASS) ? "Unknown" : v_name[i], x11->depth);
446 GetNamedColor(x11, "midnight blue", &BLUE);
447 GetNamedColor(x11, "DarkGreen", &GREEN);
448 GetNamedColor(x11, "SeaGreen", &CYAN);
449 GetNamedColor(x11, "red4", &RED);
450 GetNamedColor(x11, "Gray", &GREY);
451 GetNamedColor(x11, "Gray", &DARKGREY);
452 GetNamedColor(x11, "LightGray", &LIGHTGREY);
453 GetNamedColor(x11, "green", &LIGHTGREEN);
454 GetNamedColor(x11, "cyan", &LIGHTCYAN);
455 GetNamedColor(x11, "tomato1", &LIGHTRED);
456 GetNamedColor(x11, "violet", &VIOLET);
457 GetNamedColor(x11, "yellow", &YELLOW);
458 GetNamedColor(x11, "brown", &BROWN);
459 GetNamedColor(x11, "CornFlowerBlue", &LIGHTBLUE);
464 fprintf(x11->console, "Monochrome screen.\n");
467 /* We should use Xrm here... */
470 GetNamedColor(x11, FG, &(x11->fg));
478 GetNamedColor(x11, BG, &(x11->bg));
484 x11->title = strdup(title);
487 x11->GetNamedColor = &GetNamedColor;
488 x11->MainLoop = &MainLoop;
489 x11->RegisterCallback = &RegisterCallback;
490 x11->UnRegisterCallback = &UnRegisterCallback;
491 x11->SetInputMask = &SetInputMask;
492 x11->GetInputMask = &GetInputMask;
493 x11->CleanUp = &CleanUp;