OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
mape.c
Go to the documentation of this file.
1 /*
2  * mape - C4 Landscape.txt editor
3  *
4  * Copyright (c) 2005-2009, Armin Burgmeier
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include <stdlib.h>
17 #include <signal.h>
18 #include <glib.h>
19 #include <gtk/gtk.h>
20 #include "mape/editview.h"
21 #include "mape/window.h"
22 #include "mape/mapgen.h"
23 #include "mape/random.h"
24 
25 static MapeWindow** _global_wnd = NULL;
26 
27 static const gchar* SEGV_MSG =
28  "Mape saved the currently opened file to %s\n\n"
29  "If the crash is reproducible, I would like to fix it. Drop an email "
30  "with steps to reproduce and a possible file that caused the crash to "
31  "Armin Burgmeier <armin@arbur.net>";
32 
33 static const gchar* SEGV_MSG_ERR =
34  "Mape failed to save the currently opened file to %s: %s\n\n"
35  "If the crash is reproducible, I would like to fix it. Drop an email "
36  "with steps to reproduce and a possible file that caused the crash to "
37  "Armin Burgmeier <armin@arbur.net>";
38 
39 static void segv_handler()
40 {
41  GtkWidget* error_dialog;
42  gboolean result;
43  MapeWindow* wnd;
44 
45  gchar* filename;
46  gchar* utf8_file;
47  gchar* current;
48 
49  GError* error;
50  error = NULL;
51 
52  if(_global_wnd == NULL)
53  return;
54 
55  wnd = *_global_wnd;
56  if(wnd->edit_view->file_path)
57  {
58  filename = g_strdup(wnd->edit_view->file_path);
59  }
60  else
61  {
62  current = g_get_current_dir();
63  filename = g_build_filename(
64  current,
65  "Landscape_crash.txt",
66  NULL
67  );
68 
69  g_free(current);
70  }
71 
72  result = mape_edit_view_save(wnd->edit_view, filename, &error);
73 
74  utf8_file = g_filename_to_utf8(
75  filename,
76  -1,
77  NULL,
78  NULL,
79  NULL
80  );
81 
82  g_free(filename);
83 
84  error_dialog = gtk_message_dialog_new(
85  NULL,
86  GTK_DIALOG_MODAL,
87  GTK_MESSAGE_ERROR,
88  GTK_BUTTONS_OK,
89  "The application crashed!"
90  );
91 
92  if(result == TRUE)
93  {
94  gtk_message_dialog_format_secondary_text(
95  GTK_MESSAGE_DIALOG(error_dialog),
96  SEGV_MSG,
97  utf8_file
98  );
99  }
100  else
101  {
102  gtk_message_dialog_format_secondary_text(
103  GTK_MESSAGE_DIALOG(error_dialog),
104  SEGV_MSG_ERR,
105  utf8_file,
106  error->message
107  );
108 
109  g_error_free(error);
110  }
111 
112  g_free(utf8_file);
113 
114  gtk_dialog_run(GTK_DIALOG(error_dialog) );
115  gtk_widget_destroy(error_dialog);
116 
117  exit(EXIT_FAILURE);
118 }
119 
120 static void mape_main_window_destroy(GtkWidget* widget,
121  gpointer data)
122 {
123  gtk_main_quit();
124 }
125 
126 int main(int argc,
127  char* argv[])
128 {
129  MapeWindow* window;
130  GError* error = NULL;
131  GtkWidget* error_dialog;
132 
133  _global_wnd = &window;
134 
135  signal(SIGSEGV, segv_handler);
136 
137  g_thread_init(NULL);
138  gtk_init(&argc, &argv);
139 
140  mape_random_seed(time(NULL));
141  mape_mapgen_init(&error);
142 
143  if(error == NULL)
144  {
145  /* TODO: Set global application icon */
146  window = mape_window_new(argc, argv, &error);
147  }
148 
149  if(error != NULL)
150  {
151  error_dialog = gtk_message_dialog_new(
152  NULL,
153  GTK_DIALOG_MODAL,
154  GTK_MESSAGE_ERROR,
155  GTK_BUTTONS_OK,
156  "Application initialization failed"
157  );
158 
159  gtk_message_dialog_format_secondary_text(
160  GTK_MESSAGE_DIALOG(error_dialog),
161  "%s",
162  error->message
163  );
164 
165  gtk_window_set_title(GTK_WINDOW(error_dialog), "Mape");
166 
167  g_error_free(error);
168  gtk_dialog_run(GTK_DIALOG(error_dialog) );
169  gtk_widget_destroy(error_dialog);
170 
171  return EXIT_FAILURE;
172  }
173 
174  g_signal_connect(
175  G_OBJECT(window->window),
176  "destroy",
177  G_CALLBACK(mape_main_window_destroy),
178  (gpointer)window
179  );
180 
181  gtk_main();
182  mape_window_destroy(window);
184 
185  return EXIT_SUCCESS;
186 }
int main(int argc, char *argv[])
Definition: mape.c:126
void mape_window_destroy(MapeWindow *wnd)
Definition: window.c:881
gboolean mape_edit_view_save(MapeEditView *view, const gchar *filename, GError **error)
Definition: editview.c:632
gchar * file_path
Definition: editview.h:34
MapeEditView * edit_view
Definition: window.h:41
void mape_random_seed(unsigned int seed)
Definition: random.c:42
MapeWindow * mape_window_new(int argc, char *argv[], GError **error)
Definition: window.c:517
gboolean mape_mapgen_init(GError **error)
Definition: mapgen.c:144
void mape_mapgen_deinit()
Definition: mapgen.c:156
GtkWidget * window
Definition: window.h:27