Revision 1074

View differences:

branches/simulator/projects/simulator/libsim/libsim.c
14 14

  
15 15
void dragonfly_init(int config)
16 16
{
17
  struct itimerval iv;
18
  int ret;
19

  
20 17
  shared_state = shmat(atoi(getenv("memory_id")), NULL, 0);
21 18

  
22 19
  if(shared_state < 0)
......
37 34
{
38 35
     if(raise(SIGTSTP)<0)
39 36
          fprintf(stderr, "ERROR: could not kill self\n");
40

  
41 37
}
38

  
branches/simulator/projects/simulator/simulator/gui/gtk_gui.c
5 5
#include "gtk_gui.h"
6 6
#include "gtk_environment_view.h"
7 7

  
8
#include "robot.h"
9

  
10
#define NEW_ROBOT 1
11
#define PAUSE 2
12
#define RESUME 3
13

  
8 14
GtkWidget* window;
9 15
GtkWidget* view = NULL;
10 16

  
11 17
void destroy_callback(int arg)
12 18
{
19
	robots_cleanup();
13 20
	gtk_main_quit();
14 21
}
15 22

  
23
void make_new_robot(void)
24
{
25
	GtkWidget *dialog;
26
	
27
	gdk_threads_leave();
28
	robots_pause();
29
	gdk_threads_enter();
30
	
31
	dialog = gtk_file_chooser_dialog_new ("Open File",
32
		GTK_WINDOW(window),
33
		GTK_FILE_CHOOSER_ACTION_OPEN,
34
		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
35
		GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
36
		NULL);
37

  
38
	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
39
	{
40
		char *filename;
41
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
42
		robot_create(filename);
43
		g_free (filename);
44
	}
45
	gtk_widget_destroy (dialog);
46
	
47
	gdk_threads_leave();
48
	robots_resume();
49
	gdk_threads_enter();
50
}
51

  
52
static gboolean menuhandler(int type)
53
{
54
	switch (type)
55
	{
56
		case NEW_ROBOT:
57
			make_new_robot();
58
			break;
59
		case PAUSE:
60
			gdk_threads_leave();
61
			robots_pause();
62
			gdk_threads_enter();
63
			break;
64
		case RESUME:
65
			gdk_threads_leave();
66
			robots_resume();
67
			gdk_threads_enter();
68
			break;
69
	}
70

  
71
	return TRUE;
72
}
73

  
74
GtkWidget* make_menu_bar(GtkWidget* window, GtkWidget* other)
75
{
76
	GtkWidget* menu_bar;
77
	GtkWidget* file_item;
78
	GtkWidget* file_menu;
79
	GtkWidget* menu_item;
80
	GtkWidget* vbox;
81

  
82
	file_menu = gtk_menu_new();
83
	gtk_widget_show(file_menu);
84

  
85
	menu_item = gtk_menu_item_new_with_label("New Robot");
86
	gtk_widget_show(menu_item);
87
	gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
88
	g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
89
		G_CALLBACK(menuhandler), (gint*)NEW_ROBOT);
90
	
91
	menu_item = gtk_menu_item_new_with_label("Pause");
92
	gtk_widget_show(menu_item);
93
	gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
94
	g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
95
		G_CALLBACK(menuhandler), (gint*)PAUSE);
96
	
97
	menu_item = gtk_menu_item_new_with_label("Resume");
98
	gtk_widget_show(menu_item);
99
	gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
100
	g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
101
		G_CALLBACK(menuhandler), (gint*)RESUME);
102

  
103
	file_item = gtk_menu_item_new_with_label("File");
104
	gtk_widget_show(file_item);
105
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
106
	menu_bar = gtk_menu_bar_new();
107

  
108
	vbox = gtk_vbox_new (FALSE, 0);
109
	gtk_container_add (GTK_CONTAINER (window), vbox);
110
	gtk_widget_show (vbox);
111
	gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 2);
112
	gtk_widget_show(menu_bar);
113

  
114
	gtk_box_pack_end (GTK_BOX (vbox), other, TRUE, TRUE, 2);
115
	gtk_widget_show(other);
116

  
117
	gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), file_item);
118

  
119
	return menu_bar;
120
}
121

  
16 122
int gtk_gui_run(int argc, char** argv)
17 123
{
18
	//g_thread_init(NULL);
19 124
	gdk_threads_init();
20 125
	gdk_threads_enter();
21 126
	gtk_init(&argc, &argv);
......
23 128
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
24 129
	gtk_window_set_title(GTK_WINDOW(window), "Colony Simulator");
25 130
	gtk_window_maximize(GTK_WINDOW(window));
26
	//g_signal_connect(G_OBJECT(window), "delete_event",
27
	//		G_CALLBACK(delete_event), NULL);
28
	
131
	gtk_container_set_border_width(GTK_CONTAINER(window), 0);
29 132
	g_signal_connect(G_OBJECT(window), "destroy",
30 133
			G_CALLBACK(destroy_callback), NULL);
31
	
32
	gtk_container_set_border_width(GTK_CONTAINER(window), 0);
33 134

  
34 135
	view = gtk_environment_view_new();
35
	gtk_container_add(GTK_CONTAINER(window), view);
36
	gtk_widget_show(view);
37 136

  
137
	make_menu_bar(window, view);
38 138
	gtk_widget_show(window);
39 139

  
40 140
	gtk_main();
branches/simulator/projects/simulator/simulator/core/main.c
67 67

  
68 68

  
69 69
	robot_create(argv[optind]);
70
	/*robot_create(argv[optind]);
70 71
	robot_create(argv[optind]);
71 72
	robot_create(argv[optind]);
72 73
	robot_create(argv[optind]);
......
84 85
	robot_create(argv[optind]);
85 86
	robot_create(argv[optind]);
86 87
	robot_create(argv[optind]);
87
	robot_create(argv[optind]);
88
	robot_create(argv[optind]);
88
	robot_create(argv[optind]);*/
89 89

  
90 90

  
91 91
	sigset_t set;
branches/simulator/projects/simulator/simulator/core/robot.c
53 53
pthread_cond_t all_finished_cond;
54 54
int finished = 0;
55 55

  
56
int pausing = 0;
57
int paused = 0;
58

  
56 59
int robots_initialize(void)
57 60
{
58 61
	int i;
......
74 77
	return 0;
75 78
}
76 79

  
80
int robots_cleanup(void)
81
{
82
	robots_pause();
83
	//TODO: clean things up
84
	return 0;
85
}
86

  
87
void robots_pause(void)
88
{
89
	pausing = 1;
90
	while (!paused) usleep(10000);
91
}
92

  
93
void robots_resume(void)
94
{
95
	pausing = 0;
96
}
97

  
77 98
/**
78 99
 * Creates a new robot. Returns its id on
79 100
 * success, or a negative integer on failure.
......
287 308
			  err(errno, "error waiting on condition variable");
288 309
		}
289 310
		finished = 0;
311
		
312
		ret = pthread_mutex_unlock(&all_finished_mutex);
290 313

  
291
    //sleep to avoid 100% CPU usage
292
    usleep(10000);
314
		//sleep to avoid 100% CPU usage
315
		usleep(10000);
316
		while (pausing)
317
		{
318
			paused = 1;
319
			usleep(50000);
320
		}
321
		paused = 0;
293 322

  
294
		ret = pthread_mutex_unlock(&all_finished_mutex);
295 323
		if(ret)
296 324
		  fprintf(stderr, "hmmm, error unlocking. errno: %d", errno);
297 325

  
......
301 329
			{
302 330
				robot_update(i);
303 331

  
304
				printf("commit %d\n", i);
305 332
				commit(&robots[i],i,timeStep);
306 333
			}
307 334
		for (i = 0; i < robots_size; i++)
......
313 340
			  warn("error: could not kill resume robot proc.");
314 341
		}
315 342

  
316
		gui_refresh();
343
		if (timeStep % 5 == 0)
344
			gui_refresh();
317 345
	}
318 346
}
319 347

  
branches/simulator/projects/simulator/simulator/core/robot.h
32 32
} Robot;
33 33

  
34 34
int robots_initialize(void);
35
int robots_cleanup(void);
35 36
int robot_create(char *execname);
36 37
int robot_destroy(int id);
37 38

  
......
41 42
void* robot_event_loop(void* arg);
42 43
void* logger_step_loop(void* arg);
43 44

  
45
void robots_pause(void);
46
void robots_resume(void);
47

  
44 48
#endif
45 49

  

Also available in: Unified diff