Graphviz 13.0.0~dev.20250424.1043
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 "frmobjectui.h"
19#include "glexpose.h"
20#include "gltemplate.h"
21#include "glutrender.h"
22#include "gui.h"
23#include "gvprpipe.h"
24#include "menucallbacks.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/alloc.h>
35#include <util/exit.h>
36#include <util/gv_find_me.h>
37
38static char *smyrnaDir;
39static char *smyrnaGlade;
40
41/* smyrnaPath:
42 * Construct pathname for smyrna data file.
43 * Base file name is given as suffix.
44 * The function resolves the directory containing the data files,
45 * and constructs a complete pathname.
46 * The returned string is malloced, so the application should free
47 * it later.
48 * Returns NULL on error.
49 */
50char *smyrnaPath(char *suffix) {
51 static size_t baselen;
52#ifdef _WIN32
53 char *pathSep = "\\";
54#else
55 char *pathSep = "/";
56#endif
57 assert(smyrnaDir);
58
59 if (baselen == 0) {
60 baselen = strlen(smyrnaDir) + 2;
61 }
62 size_t len = baselen + strlen(suffix);
63 char *buf = gv_calloc(len, sizeof(char));
64 snprintf(buf, len, "%s%s%s", smyrnaDir, pathSep, suffix);
65 return buf;
66}
67
68static char *useString = "Usage: smyrna [-v?] <file>\n\
69 -f<WxH:bits@rate> - full-screen mode\n\
70 -e - draw edges as splines if available\n\
71 -v - verbose\n\
72 -? - print usage\n";
73
74static void usage(int v) {
75 fputs(useString, stdout);
77}
78
79static char *Info[] = {
80 "smyrna", /* Program */
81 PACKAGE_VERSION, /* Version */
82 BUILDDATE /* Build Date */
83};
84
85static char *parseArgs(int argc, char *argv[], ViewInfo *viewinfo) {
86 int c;
87
88 while ((c = getopt(argc, argv, ":eKf:txvV?")) != -1) {
89 switch (c) {
90 case 'e':
91 viewinfo->drawSplines = 1;
92 break;
93 case 'v': // FIXME: deprecate and remove -v in future
94 break;
95 case 'f':
96 viewinfo->guiMode = GUI_FULLSCREEN;
97 viewinfo->optArg = optarg;
98 break;
99
100 case 'V':
101 fprintf(stderr, "%s version %s (%s)\n", Info[0], Info[1], Info[2]);
102 graphviz_exit(0);
103 break;
104 case '?':
105 if (optopt == '\0' || optopt == '?')
106 usage(0);
107 else {
108 fprintf(stderr, "smyrna: option -%c unrecognized\n", optopt);
109 usage(1);
110 }
111 break;
112 }
113 }
114
115 if (optind < argc)
116 return argv[optind];
117 else
118 return NULL;
119}
120
121static void windowedMode(int argc, char *argv[]) {
122 GdkGLConfig *glconfig;
123 /*combo box to show loaded graphs */
124 GtkComboBox *graphComboBox;
125
126 gtk_set_locale();
127 gtk_init(&argc, &argv);
128 if (!(smyrnaGlade))
129 smyrnaGlade = smyrnaPath("smyrna.glade");
130 xml = glade_xml_new(smyrnaGlade, NULL, NULL);
131
132 GtkWidget *gladewidget = glade_xml_get_widget(xml, "frmMain");
133 gtk_widget_show(gladewidget);
134 g_signal_connect(gladewidget, "destroy", G_CALLBACK(mQuitSlot), NULL);
135 glade_xml_signal_autoconnect(xml);
136 gtk_gl_init(0, 0);
137 /* Configure OpenGL framebuffer. */
138 glconfig = configure_gl();
139 gladewidget = glade_xml_get_widget(xml, "hbox11");
140
141 gtk_widget_hide(glade_xml_get_widget(xml, "vbox13"));
142 gtk_window_set_deletable(
143 (GtkWindow *)glade_xml_get_widget(xml, "dlgSettings"), 0);
144 gtk_window_set_deletable((GtkWindow *)glade_xml_get_widget(xml, "frmTVNodes"),
145 0);
146 create_window(glconfig, gladewidget);
147 change_cursor(GDK_TOP_LEFT_ARROW);
148
149#ifndef _WIN32
150 glutInit(&argc, argv);
151#endif
152
153 gladewidget = glade_xml_get_widget(xml, "hbox13");
154 graphComboBox = (GtkComboBox *)gtk_combo_box_new_text();
155 gtk_box_pack_end((GtkBox *)gladewidget, (GtkWidget *)graphComboBox, 1, 1, 10);
156 gtk_widget_show((GtkWidget *)graphComboBox);
157 view->graphComboBox = graphComboBox;
158
160 gtk_main();
161}
162
164static char *find_share(void) {
165
166#ifdef _WIN32
167 const char PATH_SEPARATOR = '\\';
168#else
169 const char PATH_SEPARATOR = '/';
170#endif
171
172 // find the path to the `smyrna` binary
173 char *smyrna_exe = gv_find_me();
174 if (smyrna_exe == NULL) {
175 fputs("failed to find path to self\n", stderr);
176 graphviz_exit(EXIT_FAILURE);
177 }
178
179 // assume it is of the form …/bin/smyrna[.exe] and construct
180 // …/share/graphviz/smyrna
181
182 char *slash = strrchr(smyrna_exe, PATH_SEPARATOR);
183 if (slash == NULL) {
184 fprintf(stderr, "no path separator in path to self, %s\n", smyrna_exe);
185 free(smyrna_exe);
186 graphviz_exit(EXIT_FAILURE);
187 }
188
189 *slash = '\0';
190 slash = strrchr(smyrna_exe, PATH_SEPARATOR);
191 if (slash == NULL) {
192 fprintf(stderr, "no path separator in directory containing self, %s\n",
193 smyrna_exe);
194 free(smyrna_exe);
195 graphviz_exit(EXIT_FAILURE);
196 }
197
198 *slash = '\0';
199 size_t share_len = strlen(smyrna_exe) + strlen("/share/graphviz/smyrna") + 1;
200 char *share = gv_alloc(share_len);
201 snprintf(share, share_len, "%s%cshare%cgraphviz%csmyrna", smyrna_exe,
202 PATH_SEPARATOR, PATH_SEPARATOR, PATH_SEPARATOR);
203 free(smyrna_exe);
204
205 return share;
206}
207
208int main(int argc, char *argv[]) {
209 smyrnaDir = getenv("SMYRNA_PATH");
210 if (!smyrnaDir) {
212 }
213
214 char *package_locale_dir;
215#ifdef G_OS_WIN32
216 {
217 char *package_prefix =
218 g_win32_get_package_installation_directory(NULL, NULL);
219 package_locale_dir =
220 g_build_filename(package_prefix, "share", "locale", NULL);
221 g_free(package_prefix);
222 }
223#else
224 package_locale_dir = g_build_filename(smyrnaDir, "locale", NULL);
225#endif /* # */
226 view = gv_alloc(sizeof(ViewInfo));
228 view->initFileName = parseArgs(argc, argv, view);
229 if (view->initFileName)
230 view->initFile = 1;
231
233 cb_glutinit(800, 600, &argc, argv, view->optArg);
234 else
235 windowedMode(argc, argv);
236 g_free(package_locale_dir);
237 graphviz_exit(0);
238}
239
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
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:310
void create_window(GdkGLConfig *glconfig, GtkWidget *vbox)
Definition gltemplate.c:339
static double len(glCompPoint p)
Definition glutils.c:150
int cb_glutinit(int w, int h, int *argcp, char *argv[], char *optArg)
Definition glutrender.c:222
void free(void *)
node NULL
Definition grammar.y:163
GladeXML * xml
Definition gui.c:22
char * gv_find_me(void)
Definition gv_find_me.c:65
platform abstraction for finding the path to yourself
static const char * usage
Definition gvpr.c:47
static char * smyrnaGlade
Definition main.c:39
static char * Info[]
Definition main.c:79
char * smyrnaPath(char *suffix)
Definition main.c:50
static char * smyrnaDir
path to directory containing smyrna data files
Definition main.c:38
static char * useString
Definition main.c:68
static char * parseArgs(int argc, char *argv[], ViewInfo *viewinfo)
Definition main.c:85
static void windowedMode(int argc, char *argv[])
Definition main.c:121
static char * find_share(void)
find an absolute path to where Smyrna auxiliary files are stored
Definition main.c:164
void change_cursor(GdkCursorType C)
void mQuitSlot(GtkWidget *widget, void *user_data)
#define GUI_FULLSCREEN
Definition smyrnadefs.h:210
ViewInfo * view
Definition viewport.c:37
char * optArg
Definition smyrnadefs.h:348
GtkComboBox * graphComboBox
Definition smyrnadefs.h:341
char * initFileName
Definition smyrnadefs.h:336
int drawSplines
Definition smyrnadefs.h:338
int main()
void init_viewport(ViewInfo *vi)
Definition viewport.c:189