49 static GdkPixmap *pixmap =
NULL;
50 static GtkWidget *drawing_area;
53 #define DISPLAY_WIDTH 400
54 #define DISPLAY_HEIGHT 400
56 #define BASESTATION_SIZE 4
65 #define DOT_SIZE ether_strength()
66 #define DOT_INTENSITY 3
76 MEMB(dotsmem,
struct dot, 20000);
80 static int window_is_open;
82 static GdkGC *intensity_gcs[DOT_INTENSITY];
84 static GdkGC *intensity_clusterhead;
85 static GdkGC *intensity_clusterhead_lightgray;
86 static GdkGC *intensity_clusterhead_red;
88 static GdkGC *green, *red, *yellow, *black, *white;
90 static struct nodes_node *marked_node;
101 if(!window_is_open) {
105 gdk_draw_rectangle(pixmap,
109 drawing_area->allocation.width,
110 drawing_area->allocation.height);
113 for(i = 0; i < nodes_num(); ++i) {
128 if(n == marked_node) {
132 x * SCALE - MARK_SIZE * SCALE,
133 y * SCALE - MARK_SIZE * SCALE,
134 MARK_SIZE * 2 * SCALE, MARK_SIZE * 2 * SCALE,
140 for(i = 0; i < nodes_num(); ++i) {
169 if(strlen(n->text) > 0) {
170 gdk_draw_string(pixmap,
178 gdk_draw_rectangle(pixmap,
190 if(n->leds & LEDS_GREEN) {
191 gdk_draw_rectangle(pixmap,
198 if(n->leds & LEDS_YELLOW) {
199 gdk_draw_rectangle(pixmap,
206 if(n->leds & LEDS_RED) {
207 gdk_draw_rectangle(pixmap,
214 if(n->linex != 0 && n->liney != 0) {
215 gdk_draw_line(pixmap,
221 gdk_draw_rectangle(pixmap,
224 n->linex * SCALE - 2,
225 n->liney * SCALE - 2,
229 if(n->radio_status) {
233 x * SCALE - RADIO_SIZE * SCALE,
234 y * SCALE - RADIO_SIZE * SCALE,
235 RADIO_SIZE * 2 * SCALE, RADIO_SIZE * 2 * SCALE,
246 intensity_gcs[d->intensity - 1],
248 d->x * SCALE - d->size * SCALE,
249 d->y * SCALE - d->size * SCALE,
250 d->size * 2 * SCALE, d->size * 2 * SCALE,
255 gtk_widget_draw(drawing_area,
NULL);
263 struct ether_packet *p;
265 if(!window_is_open) {
299 for(p = ether_packets(); p !=
NULL; p = p->next) {
306 d->intensity = DOT_INTENSITY;
313 configure_event(GtkWidget *widget, GdkEventConfigure *event)
316 gdk_pixmap_unref(pixmap);
319 pixmap = gdk_pixmap_new(widget->window,
320 widget->allocation.width,
321 widget->allocation.height,
325 printf(
"gdk_pixmap_new == NULL\n");
328 gdk_draw_rectangle(pixmap,
329 widget->style->black_gc,
332 widget->allocation.width,
333 widget->allocation.height);
340 expose_event (GtkWidget * widget, GdkEventExpose * event)
343 gdk_draw_pixmap(widget->window,
344 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
346 event->area.x, event->area.y,
347 event->area.x, event->area.y,
348 event->area.width, event->area.height);
353 key_press_event (GtkWidget * widget, GdkEventKey * event)
365 if(event->keyval ==
'q') {
369 if(event->keyval ==
'p') {
370 display_output_fig();
376 key_release_event (GtkWidget * widget, GdkEventKey * event)
382 button_press_event (GtkWidget * widget, GdkEventKey * event)
385 struct sensor_data s;
386 GdkModifierType state;
389 gdk_window_get_pointer (event->window, &x, &y, &state);
391 x = ((GdkEventButton*)event)->x / SCALE;
392 y = ((GdkEventButton*)event)->y / SCALE;
394 if(state & GDK_BUTTON1_MASK) {
400 d->size = sensor_strength();
401 d->intensity = DOT_INTENSITY - 2;
404 sensor_data_init(&s);
408 ether_send_sensor_data(&s, x, y, sensor_strength());
409 }
else if(state & GDK_BUTTON2_MASK) {
410 sensor_data_init(&s);
414 if(marked_node !=
NULL) {
415 ether_send_sensor_data(&s, marked_node->x, marked_node->y, 1);
417 }
else if(state & GDK_BUTTON3_MASK) {
418 sensor_data_init(&s);
422 if(marked_node !=
NULL) {
423 ether_send_sensor_data(&s, marked_node->x, marked_node->y, 1);
431 pointer_motion_event (GtkWidget * widget, GdkEventMotion * event)
434 struct sensor_data s;
435 GdkModifierType state;
438 struct nodes_node *node, *closest;
447 gdk_window_get_pointer (event->window, &x, &y, &state);
452 if(state & GDK_BUTTON1_MASK) {
458 d->size = sensor_strength();
459 d->intensity = DOT_INTENSITY - 2;
462 sensor_data_init(&s);
464 ether_send_sensor_data(&s, x, y, sensor_strength());
471 for(i = 0; i < nodes_num(); ++i) {
472 node = nodes_node(i);
476 if(closest ==
NULL ||
477 (x - nodex) * (x - nodex) + (y - nodey) * (y - nodey) < dist) {
478 dist = (x - nodex) * (x - nodex) + (y - nodey) * (y - nodey);
482 marked_node = closest;
493 static void (* idle)(void);
495 idle_callback(gpointer data)
502 get_color(
unsigned short r,
unsigned short g,
unsigned short b)
512 if(gdk_colormap_alloc_color(gdk_colormap_get_system(),
516 values.foreground = color;
518 return gdk_gc_new_with_values(drawing_area->window,
524 stdin_callback(gpointer data, gint source, GdkInputCondition condition)
529 len = read(STDIN_FILENO, &buf,
sizeof(buf));
530 printf(
"read len %d\n", len);
532 ether_send_serial(buf);
536 display_init(
void (* idlefunc)(
void),
int time,
int with_gui)
550 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
551 gtk_window_set_title(GTK_WINDOW(window),
"Contiki simulation display");
553 vbox = gtk_vbox_new(
FALSE, 0);
554 gtk_container_add(GTK_CONTAINER (window), vbox);
555 gtk_widget_show(vbox);
557 gtk_signal_connect(GTK_OBJECT (window),
"destroy",
558 GTK_SIGNAL_FUNC (quit),
NULL);
560 font = gdk_font_load(
"-*-courier-medium-r-*-*-12-*-*-*-*-*-iso8859-1");
564 drawing_area = gtk_drawing_area_new();
565 gtk_drawing_area_size(GTK_DRAWING_AREA (drawing_area),
568 gtk_box_pack_start(GTK_BOX(vbox), drawing_area,
TRUE,
TRUE, 0);
570 gtk_widget_show(drawing_area);
574 gtk_signal_connect(GTK_OBJECT (drawing_area),
"expose_event",
575 (GtkSignalFunc) expose_event,
NULL);
576 gtk_signal_connect(GTK_OBJECT (drawing_area),
"configure_event",
577 (GtkSignalFunc) configure_event,
NULL);
581 gtk_signal_connect(GTK_OBJECT (window),
"key_press_event",
582 (GtkSignalFunc) key_press_event,
NULL);
583 gtk_signal_connect(GTK_OBJECT (window),
"key_release_event",
584 (GtkSignalFunc) key_release_event,
NULL);
586 gtk_signal_connect(GTK_OBJECT (window),
"button_press_event",
587 (GtkSignalFunc) button_press_event,
NULL);
589 gtk_signal_connect(GTK_OBJECT (window),
"motion_notify_event",
590 (GtkSignalFunc) pointer_motion_event,
NULL);
592 gtk_widget_set_events(drawing_area,GDK_KEY_PRESS_MASK
593 | GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK
594 | GDK_POINTER_MOTION_MASK);
598 gtk_widget_show(window);
599 window_is_open = with_gui;
604 gtk_timeout_add(time, idle_callback,
NULL);
608 for(i = 0; i < DOT_INTENSITY; ++i) {
611 color.green = ((DOT_INTENSITY + 1) * 0xffff) / (i + 1);
612 color.blue = ((DOT_INTENSITY + 1) * 0xffff) / (i + 1);
614 if(gdk_colormap_alloc_color(gdk_colormap_get_system(),
618 values.foreground = color;
620 intensity_gcs[i] = gdk_gc_new_with_values(drawing_area->window, &values,
626 color.green = 0xbfff;
629 if(gdk_colormap_alloc_color(gdk_colormap_get_system(),
633 values.foreground = color;
635 intensity_clusterhead = gdk_gc_new_with_values(drawing_area->window, &values,
640 color.green = 0xefff;
643 if(gdk_colormap_alloc_color(gdk_colormap_get_system(),
647 values.foreground = color;
649 intensity_clusterhead_lightgray = gdk_gc_new_with_values(drawing_area->window, &values,
657 if(gdk_colormap_alloc_color(gdk_colormap_get_system(),
661 values.foreground = color;
663 intensity_clusterhead_red = gdk_gc_new_with_values(drawing_area->window, &values,
667 red = get_color(0xffff, 0, 0);
668 green = get_color(0, 0xffff, 0);
669 yellow = get_color(0xffff, 0xffff, 0);
670 black = get_color(0, 0, 0);
671 white = get_color(0xffff, 0xffff, 0xffff);
674 gdk_input_add(STDIN_FILENO, GDK_INPUT_READ, stdin_callback,
NULL);
684 display_output_fig(
void)
687 struct nodes_node *n;
695 gettimeofday(&tv,
NULL);
696 snprintf(name,
sizeof(name),
"network-%lu.fig", tv.tv_sec);
698 fp = fopen(name,
"w");
699 fprintf(fp,
"#FIG 3.2\n"
710 for(i = 0; i < nodes_num(); ++i) {
715 fprintf(fp,
"1 3 1 1 0 7 50 -1 0 4.000 1 0.0000 %d %d %d %d %d %d %d %d\n",
717 dot_radius, dot_radius,
719 x + dot_radius, y + dot_radius);
721 if(strlen(n->text) > 0) {
722 fprintf(fp,
"4 0 0 50 -1 16 18 0.0000 4 135 720 %d %d %s\\001\n",
723 x + 2 * scale, y, n->text);
726 if(n->linex != 0 && n->liney != 0) {
727 fprintf(fp,
"2 1 1 1 0 7 50 -1 -1 0.000 0 0 -1 0 1 2\n"
728 "1 1 4.00 60.00 120.00\n"
731 n->linex * scale, n->liney * scale);