-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.c
179 lines (142 loc) · 4.09 KB
/
sim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**********************************************************************
* sim.c - definition of the sim object - controls the simulation
*
* Copyright 1993, David Nedde
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is granted
* provided that the above copyright notice appears in all copies.
* It is provided "as is" without express or implied warranty.
**********************************************************************/
/* Local headers */
#include "sim.h"
#include "misc.h"
#include "names.h"
/* System Headers */
#include <malloc.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
/* Macros */
#define INVALID_WPID 0
#ifndef DELAY
#define DELAY 0
#endif
/* Structures */
typedef struct {
int delay;
} app_data,*app_data_ptr;
static app_data res_data;
static XtResource resources[] = {
{ "delay", "Delay", XtRInt, sizeof(int),
XtOffset(app_data_ptr,delay), XtRImmediate, (caddr_t)DELAY}
};
/* Functions */
static Boolean background_processing(/*sim*/);
static int delay();
static int process_invisible_backwards(/*datap_ptr*/);
/* Public object methods */
/* Create the simulation object */
sim_type sim__create(w, menu, items, room, demo)
Widget w;
menu_type menu;
items_type items;
room_type room;
demo_type demo;
{
sim_type sim = (sim_type)malloc( sizeof( sim_struct_type));
sim->menu = menu;
sim->items = items;
sim->room = room;
sim->demo = demo;
XtGetApplicationResources(w, (XtPointer)&res_data,
resources, XtNumber(resources),
(ArgList)NULL,(Cardinal)0);
sim->step_simulation = False;
sim->delay = res_data.delay;
sim->wpid = INVALID_WPID;
return sim;
}
/* Destroys the simulation object */
void sim__destroy(sim)
sim_type sim;
{
XtRemoveWorkProc(sim->wpid);
free( sim);
}
/* Steps the simulation one iteration */
void sim__step_mcb(w, sim, call_data, extra)
Widget w;
sim_type sim;
caddr_t call_data;
char * extra;
{
background_processing( sim);
}
/* Halts the simulation */
void sim__halt_mcb(w, sim, call_data, extra)
Widget w;
sim_type sim;
menuCallbackStruct * call_data;
char * extra;
{
sim->step_simulation = call_data->set;
if (!sim->step_simulation)
{
/* Stepping currently turned off */
/* Add background processing to perform simulation in real-time */
sim->wpid = XtAppAddWorkProc(XtWidgetToApplicationContext(w),
background_processing, (char *)sim);
/* The 'Step simulation' menu item is greyed since sim is running */
menu__set_sensitivity( sim->menu, STEP_SIM, /*sensitive=*/False);
}
else
{
/* Stepping turned on - remove the work proc if it has been started */
if (sim->wpid != INVALID_WPID)
XtRemoveWorkProc(sim->wpid);
sim->wpid = INVALID_WPID;
/* The 'Step simulation' menu item is ungreyed since sim not running */
menu__set_sensitivity( sim->menu, STEP_SIM, /*sensitive=*/True);
}
}
/* Private object methods */
/* Performs one iteration of the demo and simulation */
static Boolean background_processing(sim)
sim_type sim;
{
room_type room = sim->room;
if (demo__running(sim->demo))
demo__process( sim->demo);
/* Delay */
delay();
items__move_items( sim->items, room);
return /*remove work procedure=*/False;
}
/* Slow the simulation some amount */
/* Slow more if there are less balls, less if there are more balls */
static int delay(
)
{
static int first_flag = 1;
static struct timeval last = {0,0};
struct timeval tv;
struct timezone tz;
const int STEP = (10 * 1000); //10mSec
gettimeofday(&tv, &tz);
if (first_flag) {
first_flag = 0;
last = tv;
}
// Compute required delay
int delta = tv.tv_usec - last.tv_usec;
if (tv.tv_sec != last.tv_sec) {
delta += (tv.tv_sec - last.tv_sec) * 1000000;
}
if (delta < STEP) {
usleep(STEP - delta);
}
last = tv;
}