24 |
#include <sys/wait.h> |
#include <sys/wait.h> |
25 |
#include <errno.h> |
#include <errno.h> |
26 |
|
|
27 |
#define COLOR_ERR "red" |
#define COLOR_ERR "red" |
28 |
#define COLOR_OK "darkgreen" |
#define COLOR_OK "darkgreen" |
29 |
|
#define COLOR_SYSTEM "darkblue" |
30 |
|
|
31 |
#define BUFFER_SIZE 1500 |
#define BUFFER_SIZE 256 |
32 |
|
|
33 |
typedef struct { |
typedef struct { |
34 |
appdata_t *appdata; |
appdata_t *appdata; |
54 |
char *buf = g_strdup_vprintf(fmt, args); |
char *buf = g_strdup_vprintf(fmt, args); |
55 |
va_end( args ); |
va_end( args ); |
56 |
|
|
57 |
|
printf("append: %s", buf); |
58 |
|
|
59 |
GtkTextTag *tag = NULL; |
GtkTextTag *tag = NULL; |
60 |
if(colname) |
if(colname) |
61 |
tag = gtk_text_buffer_create_tag(log->buffer, NULL, |
tag = gtk_text_buffer_create_tag(log->buffer, NULL, |
71 |
|
|
72 |
g_free(buf); |
g_free(buf); |
73 |
|
|
74 |
|
gtk_text_buffer_get_end_iter(log->buffer, &end); |
75 |
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(log->view), |
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(log->view), |
76 |
&end, 0.0, FALSE, 0, 0); |
&end, 0.0, FALSE, 0, 0); |
77 |
} |
} |
78 |
|
|
79 |
/* watch child process and receive events */ |
/* watch child process and receive events */ |
80 |
static void child_state_cb(GPid pid, gint status, gpointer data) { |
static void child_state_cb(GPid pid, gint status, gpointer data) { |
81 |
|
gt_context_t *context = (gt_context_t*)data; |
82 |
|
|
83 |
puts("child state"); |
puts("child state"); |
84 |
|
|
89 |
|
|
90 |
puts("gt exited"); |
puts("gt exited"); |
91 |
|
|
92 |
|
appendf(&context->log, COLOR_SYSTEM, "GeoToad finished\n"); |
93 |
|
|
94 |
|
appendf(&context->log, COLOR_SYSTEM, "TODO: free context!!!\n"); |
95 |
|
// printf("freeing context\n"); |
96 |
|
// g_free(context); |
97 |
|
|
98 |
/* Reap child if needed. */ |
/* Reap child if needed. */ |
99 |
waitpid (pid, NULL, WNOHANG); |
waitpid (pid, NULL, WNOHANG); |
100 |
} |
} |
101 |
|
|
102 |
static void child_input_cb(gpointer data, int fd, GdkInputCondition cond) { |
static gboolean child_input_cb(GIOChannel *source, |
103 |
|
GIOCondition condition, |
104 |
|
gpointer data) { |
105 |
gt_context_t *context = (gt_context_t*)data; |
gt_context_t *context = (gt_context_t*)data; |
106 |
|
int fd = g_io_channel_unix_get_fd(source); |
107 |
ssize_t bytes; |
ssize_t bytes; |
|
int errnosave=0; |
|
108 |
|
|
109 |
if(cond != GDK_INPUT_READ) { |
g_assert(condition == G_IO_IN); |
|
puts("fixme"); |
|
|
return; |
|
|
} |
|
110 |
|
|
111 |
/* append to current buffer content */ |
/* append to current buffer content */ |
112 |
while( (bytes = read(fd, context->buf+context->bused, |
while( (bytes = read(fd, context->buf+context->bused, |
122 |
char *p = strchr(ptr, '\n'); |
char *p = strchr(ptr, '\n'); |
123 |
*p = '\0'; |
*p = '\0'; |
124 |
|
|
125 |
appendf(&context->log, NULL, "%s\n", ptr); |
char *color = NULL; |
126 |
|
if(strstr(ptr, "Saved to ") != NULL) |
127 |
|
color = COLOR_OK; |
128 |
|
|
129 |
|
appendf(&context->log, color, "%s\n", ptr); |
130 |
|
|
131 |
ptr = p+1; |
ptr = p+1; |
132 |
} |
} |
142 |
} |
} |
143 |
} |
} |
144 |
|
|
145 |
/* save errno from read */ |
return TRUE; |
|
errnosave=errno; |
|
|
|
|
|
if(errnosave != EAGAIN && errnosave != 0) { |
|
|
/* we probably hit EOF */ |
|
|
puts("removing io"); |
|
|
|
|
|
// TODO: fixme, make sure process is dead! kill it otherwise, but dont |
|
|
// issue an disconnect(NULL) !!!! |
|
|
|
|
|
gdk_input_remove(context->stdout_tag); |
|
|
gdk_input_remove(context->stderr_tag); |
|
|
|
|
|
appendf(&context->log, COLOR_OK, "GeoToad finished\n"); |
|
|
|
|
|
appendf(&context->log, COLOR_ERR, "TODO: free context!!!\n"); |
|
|
// printf("freeing context\n"); |
|
|
// g_free(context); |
|
|
} |
|
146 |
} |
} |
147 |
|
|
148 |
static void run(gt_context_t *context) { |
static void run(gt_context_t *context) { |
157 |
/* build list of arguments to call geotoad */ |
/* build list of arguments to call geotoad */ |
158 |
GPtrArray *gt_argv = g_ptr_array_new(); |
GPtrArray *gt_argv = g_ptr_array_new(); |
159 |
g_ptr_array_add (gt_argv, "/usr/bin/geotoad"); |
g_ptr_array_add (gt_argv, "/usr/bin/geotoad"); |
160 |
g_ptr_array_add (gt_argv, "--distanceMax=1.5"); |
g_ptr_array_add (gt_argv, "--distanceMax=1.0"); |
161 |
g_ptr_array_add (gt_argv, "--output=gtoad.gpx"); |
g_ptr_array_add (gt_argv, "--output=gtoad.gpx"); |
162 |
g_ptr_array_add (gt_argv, "--password=winterblume"); |
g_ptr_array_add (gt_argv, "--password=winterblume"); |
163 |
g_ptr_array_add (gt_argv, "--queryType=coord"); |
g_ptr_array_add (gt_argv, "--queryType=coord"); |
179 |
context->appdata->proxy->host, |
context->appdata->proxy->host, |
180 |
context->appdata->proxy->port); |
context->appdata->proxy->port); |
181 |
|
|
182 |
appendf(&context->log, COLOR_OK, "Using proxy: %s\n", proxy); |
appendf(&context->log, COLOR_SYSTEM, "Using proxy: %s\n", proxy); |
183 |
g_ptr_array_add (gt_argv, proxy); |
g_ptr_array_add (gt_argv, proxy); |
184 |
} |
} |
185 |
|
|
|
|
|
186 |
g_ptr_array_add (gt_argv, "N49 00.000 E008 23.000"); |
g_ptr_array_add (gt_argv, "N49 00.000 E008 23.000"); |
187 |
g_ptr_array_add (gt_argv, NULL); |
g_ptr_array_add (gt_argv, NULL); |
188 |
|
|
204 |
g_ptr_array_free(gt_argv, TRUE); |
g_ptr_array_free(gt_argv, TRUE); |
205 |
if(proxy) g_free(proxy); |
if(proxy) g_free(proxy); |
206 |
appendf(&context->log, COLOR_ERR, |
appendf(&context->log, COLOR_ERR, |
207 |
_("GeoToad failed to start: '%s'"), error->message); |
_("GeoToad failed to start!\n%s\n"), error->message); |
208 |
g_error_free(error); |
g_error_free(error); |
209 |
return; |
return; |
210 |
} |
} |
211 |
|
|
212 |
g_ptr_array_free (gt_argv, TRUE); |
g_ptr_array_free (gt_argv, TRUE); |
213 |
if(proxy) g_free(proxy); |
if(proxy) g_free(proxy); |
214 |
|
|
215 |
gt_watch = g_child_watch_source_new(pid); |
gt_watch = g_child_watch_source_new(pid); |
216 |
g_source_set_callback(gt_watch, (GSourceFunc) child_state_cb, NULL, NULL); |
g_source_set_callback(gt_watch, (GSourceFunc) child_state_cb, context, NULL); |
217 |
|
|
218 |
g_source_attach(gt_watch, NULL); |
g_source_attach(gt_watch, NULL); |
219 |
g_source_unref(gt_watch); |
g_source_unref(gt_watch); |
225 |
if(fcntl(context->stderr_fd, F_SETFL, O_NONBLOCK) == -1) |
if(fcntl(context->stderr_fd, F_SETFL, O_NONBLOCK) == -1) |
226 |
perror("fcntl failed"); |
perror("fcntl failed"); |
227 |
|
|
228 |
/* use gdk to monitor read end of stdout */ |
GIOChannel *ioc = g_io_channel_unix_new(context->stdout_fd); |
229 |
context->stdout_tag = gdk_input_add(context->stdout_fd, |
g_io_channel_set_close_on_unref (ioc, TRUE); |
230 |
GDK_INPUT_READ, child_input_cb, context); |
g_io_channel_set_encoding (ioc, NULL, NULL); |
231 |
context->stderr_tag = gdk_input_add(context->stderr_fd, |
g_io_add_watch(ioc, G_IO_IN, child_input_cb, context); |
232 |
GDK_INPUT_READ, child_input_cb, context); |
g_io_channel_unref(ioc); |
233 |
|
|
234 |
|
// ioc = g_io_channel_unix_new(context->stderr_fd); |
235 |
|
// g_io_add_watch(ioc, G_IO_IN, child_input_cb, context); |
236 |
|
// g_io_channel_unref(ioc); |
237 |
} |
} |
238 |
|
|
239 |
void geotoad(appdata_t *appdata) { |
void geotoad(appdata_t *appdata) { |
285 |
|
|
286 |
gtk_widget_show_all(dialog); |
gtk_widget_show_all(dialog); |
287 |
|
|
288 |
appendf(&context->log, COLOR_OK, "Running GeoToad\n"); |
appendf(&context->log, COLOR_SYSTEM, "Running GeoToad\n"); |
289 |
run(context); |
run(context); |
290 |
|
|
291 |
gtk_dialog_run(GTK_DIALOG(dialog)); |
gtk_dialog_run(GTK_DIALOG(dialog)); |