Graphviz 14.1.2~dev.20260123.1158
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "builddate.h"
12#include "config.h"
13// windows.h for win machines
14#if defined(_WIN32) && !defined(__CYGWIN__)
15#include <windows.h>
16#include <windowsx.h>
17#endif
18#include "glexpose.h"
19#include "gltemplate.h"
20#include "glutrender.h"
21#include "gui/frmobjectui.h"
22#include "gui/gui.h"
23#include "gui/menucallbacks.h"
24#include "gvprpipe.h"
25#include "viewport.h"
26#include <assert.h>
27#include <glade/glade.h>
28#include <gtk/gtk.h>
29#include <gtk/gtkgl.h>
30
31#include <getopt.h>
32#include <stdlib.h>
33#include <string.h>
34#include <util/agxbuf.h>
35#include <util/alloc.h>
36#include <util/exit.h>
37#include <util/gv_find_me.h>
38#include <util/path.h>
39
40static char *smyrnaDir;
41static char *smyrnaGlade;
42
43/* smyrnaPath:
44 * Construct pathname for smyrna data file.
45 * Base file name is given as suffix.
46 * The function resolves the directory containing the data files,
47 * and constructs a complete pathname.
48 * The returned string is malloced, so the application should free
49 * it later.
50 */
51char *smyrnaPath(char *suffix) {
52 const char pathSep = PATH_SEPARATOR;
53 assert(smyrnaDir);
54
55 agxbuf buf = {0};
56 agxbprint(&buf, "%s%c%s", smyrnaDir, pathSep, suffix);
57 return agxbdisown(&buf);
58}
59
60static char *useString = "Usage: smyrna [-v?] <file>\n\
61 -f<WxH:bits@rate> - full-screen mode\n\
62 -e - draw edges as splines if available\n\
63 -v - verbose\n\
64 -? - print usage\n";
65
66static void usage(int v) {
67 fputs(useString, stdout);
69}
70
71static char *Info[] = {
72 "smyrna", /* Program */
73 PACKAGE_VERSION, /* Version */
74 BUILDDATE /* Build Date */
75};
76
77static char *parseArgs(int argc, char *argv[], ViewInfo *viewinfo) {
78 int c;
79
80 while ((c = getopt(argc, argv, ":eKf:txvV?")) != -1) {
81 switch (c) {
82 case 'e':
83 viewinfo->drawSplines = 1;
84 break;
85 case 'v': // FIXME: deprecate and remove -v in future
86 break;
87 case 'f':
88 viewinfo->guiMode = GUI_FULLSCREEN;
89 viewinfo->optArg = optarg;
90 break;
91
92 case 'V':
93 fprintf(stderr, "%s version %s (%s)\n", Info[0], Info[1], Info[2]);
95 break;
96 case '?':
97 if (optopt == '\0' || optopt == '?')
98 usage(0);
99 else {
100 fprintf(stderr, "smyrna: option -%c unrecognized\n", optopt);
101 usage(1);
102 }
103 break;
104 }
105 }
106
107 if (optind < argc)
108 return argv[optind];
109 else
110 return NULL;
111}
112
113static void windowedMode(int argc, char *argv[]) {
114 GdkGLConfig *glconfig;
115 /*combo box to show loaded graphs */
116 GtkComboBox *graphComboBox;
117
118 gtk_set_locale();
119 gtk_init(&argc, &argv);
120 if (!(smyrnaGlade))
121 smyrnaGlade = smyrnaPath("smyrna.glade");
122 xml = glade_xml_new(smyrnaGlade, NULL, NULL);
123
124 GtkWidget *gladewidget = glade_xml_get_widget(xml, "frmMain");
125 gtk_widget_show(gladewidget);
126 g_signal_connect(gladewidget, "destroy", G_CALLBACK(mQuitSlot), NULL);
127 glade_xml_signal_autoconnect(xml);
128 gtk_gl_init(0, 0);
129 /* Configure OpenGL framebuffer. */
130 glconfig = configure_gl();
131 gladewidget = glade_xml_get_widget(xml, "hbox11");
132
133 gtk_widget_hide(glade_xml_get_widget(xml, "vbox13"));
134 gtk_window_set_deletable(
135 (GtkWindow *)glade_xml_get_widget(xml, "dlgSettings"), 0);
136 gtk_window_set_deletable((GtkWindow *)glade_xml_get_widget(xml, "frmTVNodes"),
137 0);
138 create_window(glconfig, gladewidget);
139 change_cursor(GDK_TOP_LEFT_ARROW);
140
141#ifndef _WIN32
142 glutInit(&argc, argv);
143#endif
144
145 gladewidget = glade_xml_get_widget(xml, "hbox13");
146 graphComboBox = (GtkComboBox *)gtk_combo_box_new_text();
147 gtk_box_pack_end((GtkBox *)gladewidget, (GtkWidget *)graphComboBox, 1, 1, 10);
148 gtk_widget_show((GtkWidget *)graphComboBox);
149 view->graphComboBox = graphComboBox;
150
152 gtk_main();
153}
154
156static char *find_share(void) {
157
158 // find the path to the `smyrna` binary
159 char *smyrna_exe = gv_find_me();
160 if (smyrna_exe == NULL) {
161 fputs("failed to find path to self\n", stderr);
162 graphviz_exit(EXIT_FAILURE);
163 }
164
165 // assume it is of the form …/bin/smyrna[.exe] and construct
166 // …/share/graphviz/smyrna
167
168 char *slash = strrchr(smyrna_exe, PATH_SEPARATOR);
169 if (slash == NULL) {
170 fprintf(stderr, "no path separator in path to self, %s\n", smyrna_exe);
171 free(smyrna_exe);
172 graphviz_exit(EXIT_FAILURE);
173 }
174
175 *slash = '\0';
176 slash = strrchr(smyrna_exe, PATH_SEPARATOR);
177 if (slash == NULL) {
178 fprintf(stderr, "no path separator in directory containing self, %s\n",
179 smyrna_exe);
180 free(smyrna_exe);
181 graphviz_exit(EXIT_FAILURE);
182 }
183
184 *slash = '\0';
185 size_t share_len = strlen(smyrna_exe) + strlen("/share/graphviz/smyrna") + 1;
186 char *share = gv_alloc(share_len);
187 snprintf(share, share_len, "%s%cshare%cgraphviz%csmyrna", smyrna_exe,
189 free(smyrna_exe);
190
191 return share;
192}
193
194int main(int argc, char *argv[]) {
195 smyrnaDir = getenv("SMYRNA_PATH");
196 if (!smyrnaDir) {
198 }
199
200 char *package_locale_dir;
201#ifdef G_OS_WIN32
202 {
203 char *package_prefix =
204 g_win32_get_package_installation_directory(NULL, NULL);
205 package_locale_dir =
206 g_build_filename(package_prefix, "share", "locale", NULL);
207 g_free(package_prefix);
208 }
209#else
210 package_locale_dir = g_build_filename(smyrnaDir, "locale", NULL);
211#endif /* # */
212 view = gv_alloc(sizeof(ViewInfo));
214 view->initFileName = parseArgs(argc, argv, view);
215 if (view->initFileName)
216 view->initFile = 1;
217
219 cb_glutinit(800, 600, &argc, argv, view->optArg);
220 else
221 windowedMode(argc, argv);
222 g_free(package_locale_dir);
223 graphviz_exit(0);
224}
225
Dynamically expanding string buffers.
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:252
static char * agxbdisown(agxbuf *xb)
Definition agxbuf.h:345
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
char * suffix
Definition bcomps.c:66
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
GdkGLConfig * configure_gl(void)
Definition gltemplate.c:314
void create_window(GdkGLConfig *glconfig, GtkWidget *vbox)
Definition gltemplate.c:343
int cb_glutinit(int w, int h, int *argcp, char *argv[], char *optArg)
Definition glutrender.c:221
void free(void *)
node NULL
Definition grammar.y:181
GladeXML * xml
Definition gui.c:24
char * gv_find_me(void)
Definition gv_find_me.c:69
platform abstraction for finding the path to yourself
static const char * usage
Definition gvpr.c:54
static char * smyrnaGlade
Definition main.c:41
static char * Info[]
Definition main.c:71
char * smyrnaPath(char *suffix)
Definition main.c:51
static char * smyrnaDir
path to directory containing smyrna data files
Definition main.c:40
static char * useString
Definition main.c:60
static char * parseArgs(int argc, char *argv[], ViewInfo *viewinfo)
Definition main.c:77
static void windowedMode(int argc, char *argv[])
Definition main.c:113
static char * find_share(void)
find an absolute path to where Smyrna auxiliary files are stored
Definition main.c:156
void change_cursor(GdkCursorType C)
void mQuitSlot(GtkWidget *widget, void *user_data)
File system path helpers.
#define PATH_SEPARATOR
character for separating directory components in a file system path
Definition path.h:10
#define GUI_FULLSCREEN
Definition smyrnadefs.h:203
ViewInfo * view
Definition viewport.c:40
char * optArg
Definition smyrnadefs.h:337
GtkComboBox * graphComboBox
Definition smyrnadefs.h:330
char * initFileName
Definition smyrnadefs.h:325
int drawSplines
Definition smyrnadefs.h:327
int main()
void init_viewport(ViewInfo *vi)
Definition viewport.c:174