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,2015,2017,2019, 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/cstringutil.h"
46 #include "gromacs/utility/smalloc.h"
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 gmx_unused* err, Display* disp, const char* name)
58 XFontStruct* font = XLoadQueryFont(disp, name);
62 std::fprintf(err, "Loaded font %s\n", name);
68 static XFontStruct* GetFont(FILE* err, Display* disp, char* name)
70 static const char* fontnames[] = { "sansb12", "8x13bold", "8x13", "9x15", "6x13", "fixed" };
71 #define MAXNAMES (sizeof(fontnames) / sizeof(fontnames[0]))
80 bFont = ((font = XLQF(err, disp, name)) != nullptr);
87 for (i = 0; (!bFont && (i < MAXNAMES)); i++)
89 bFont = ((font = XLQF(err, disp, fontnames[i])) != nullptr);
94 fontlist = XListFonts(disp, "?", 1, &count);
97 bFont = ((font = XLQF(err, disp, fontlist[0])) != nullptr);
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 std::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))
183 bReturn = (*curs->cb)(x11, &event, w, curs->data);
190 static void RegisterCallback(t_x11* x11, Window w, Window Parent, CallBack cb, void* data)
192 t_wlist *curs, *item;
196 item->Parent = Parent;
200 item->next = nullptr;
217 static void UnRegisterCallback(t_x11* x11, Window w)
226 x11->wlist = curs->next;
231 while (curs->next && (curs->next->w != w))
237 t_wlist* tmp = curs->next;
239 curs->next = curs->next->next;
246 static void SetInputMask(t_x11* x11, Window w, unsigned long mask)
250 curs = GetWList(x11, w);
254 XSelectInput(x11->disp, w, (long)mask);
258 std::fprintf(x11->console, "No such window (%d)\n", (int)w);
262 static unsigned long GetInputMask(t_x11* x11, Window w)
266 curs = GetWList(x11, w);
277 static void CleanUp(t_x11* x11)
284 x11->wlist = curs->next;
285 XDestroyWindow(x11->disp, curs->w);
289 XCloseDisplay(x11->disp);
292 static void Flush(t_x11* x11)
294 std::fflush(x11->console);
297 t_x11* GetX11(int* argc, char* argv[])
299 static const char* v_name[] = { "DirectColor", "TrueColor", "PseudoColor",
300 "StaticColor", "GrayScale", "StaticGray" };
301 static int v_class[] = { DirectColor, TrueColor, PseudoColor, StaticColor, GrayScale, StaticGray };
302 #define NCLASS (sizeof(v_class) / sizeof(v_class[0]))
310 char * title, *FG = nullptr, *BG = nullptr;
311 bool bVerbose = false;
314 title = gmx_strdup(argv[0]);
316 /* First check environment */
317 fontname = std::getenv("GMX_FONT");
318 display = std::getenv("DISPLAY");
322 for (i = 1; (i < *argc); i++)
324 if (argv[i][0] == '-')
326 if (strlen(argv[i]) > 1)
332 case 'b': BG = argv[++i]; break;
333 case 'd': display = argv[++i]; break;
337 case 'o': fontname = argv[++i]; break;
338 case 'g': FG = argv[++i]; break;
343 title = gmx_strdup(argv[++i]);
345 case 'v': bVerbose = true; break;
346 default: ARGV[ARGC++] = argv[i]; break;
353 ARGV[ARGC++] = argv[i];
356 for (i = 1; (i < ARGC); i++)
361 argv[ARGC] = nullptr;
364 x11->dispname = display;
367 x11->console = stderr;
369 else if ((x11->console = std::fopen("/dev/null", "w")) == nullptr)
371 x11->console = stderr;
374 if ((x11->disp = XOpenDisplay(display)) == nullptr)
378 std::fprintf(x11->console, "Display %s invalid\n", display);
383 if ((x11->font = GetFont(x11->console, x11->disp, fontname)) == nullptr)
387 if ((x11->gc = GetGC(x11->disp, x11->font)) == nullptr)
392 x11->root = DefaultRootWindow(x11->disp);
393 x11->screen = DefaultScreen(x11->disp);
394 x11->depth = DefaultDepth(x11->disp, x11->screen);
395 x11->cmap = DefaultColormap(x11->disp, x11->screen);
397 /* These colours will be mapped to black on a monochrome screen */
398 x11->fg = BLACK = BLUE = GREEN = CYAN = RED = BROWN = GREY = DARKGREY =
399 BlackPixel(x11->disp, x11->screen);
401 /* These colours will be mapped to white on a monochrome screen */
402 x11->bg = LIGHTBLUE = LIGHTGREY = LIGHTGREEN = LIGHTCYAN = LIGHTRED = VIOLET = YELLOW = WHITE =
403 WhitePixel(x11->disp, x11->screen);
407 /* Not B & W, Look what kind of screen we've got... */
408 for (i = 0; (i < (int)NCLASS); i++)
410 if (!XMatchVisualInfo(x11->disp, x11->screen, x11->depth, v_class[i], &v_info))
415 if ((i == 4) || (i == 5))
417 std::fprintf(x11->console, "Greyscale screen, using B & W only\n");
421 /* We have real color! */
422 std::fprintf(x11->console, "%s screen with depth %d.\n",
423 (i == NCLASS) ? "Unknown" : v_name[i], x11->depth);
424 GetNamedColor(x11, "midnight blue", &BLUE);
425 GetNamedColor(x11, "DarkGreen", &GREEN);
426 GetNamedColor(x11, "SeaGreen", &CYAN);
427 GetNamedColor(x11, "red4", &RED);
428 GetNamedColor(x11, "Gray", &GREY);
429 GetNamedColor(x11, "Gray", &DARKGREY);
430 GetNamedColor(x11, "LightGray", &LIGHTGREY);
431 GetNamedColor(x11, "green", &LIGHTGREEN);
432 GetNamedColor(x11, "cyan", &LIGHTCYAN);
433 GetNamedColor(x11, "tomato1", &LIGHTRED);
434 GetNamedColor(x11, "violet", &VIOLET);
435 GetNamedColor(x11, "yellow", &YELLOW);
436 GetNamedColor(x11, "brown", &BROWN);
437 GetNamedColor(x11, "CornFlowerBlue", &LIGHTBLUE);
442 std::fprintf(x11->console, "Monochrome screen.\n");
445 /* We should use Xrm here... */
448 GetNamedColor(x11, FG, &(x11->fg));
456 GetNamedColor(x11, BG, &(x11->bg));
462 x11->title = gmx_strdup(title);
464 x11->wlist = nullptr;
465 x11->GetNamedColor = &GetNamedColor;
466 x11->MainLoop = &MainLoop;
467 x11->RegisterCallback = &RegisterCallback;
468 x11->UnRegisterCallback = &UnRegisterCallback;
469 x11->SetInputMask = &SetInputMask;
470 x11->GetInputMask = &GetInputMask;
471 x11->CleanUp = &CleanUp;