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