1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #include "arch.hh"
|
---|
10 | #include "time/time.hh"
|
---|
11 | #include "time/timedev.hh"
|
---|
12 | #include "device/kernel.hh"
|
---|
13 | #include "init/init.hh"
|
---|
14 | #include "isllist.hh"
|
---|
15 | #include "time/time.hh"
|
---|
16 | #include "device/event.hh"
|
---|
17 |
|
---|
18 | i4_time_device_class i4_time_dev;
|
---|
19 | extern int i4_show_events;
|
---|
20 |
|
---|
21 | static i4_isl_list<i4_time_device_class::timed_event> events;
|
---|
22 |
|
---|
23 | i4_time_device_class::timed_event::~timed_event()
|
---|
24 | {
|
---|
25 | delete event;
|
---|
26 | }
|
---|
27 |
|
---|
28 | i4_time_device_class::timed_event::timed_event(i4_event_handler_class *send_to,
|
---|
29 | i4_event *event,
|
---|
30 | w32 milli_wait)
|
---|
31 | : send_to(send_to),
|
---|
32 | event(event->copy()),
|
---|
33 | milli_wait(milli_wait)
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 | class i4_time_device_adder_class : public i4_init_class
|
---|
38 | {
|
---|
39 | public :
|
---|
40 | void init()
|
---|
41 | {
|
---|
42 | i4_kernel.add_device(&i4_time_dev);
|
---|
43 | }
|
---|
44 | } i4_time_device_adder_instance;
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | i4_bool i4_time_device_class::cancel_event(const id &handle)
|
---|
49 | {
|
---|
50 | i4_isl_list<timed_event>::iterator i=events.begin(),last=events.end();
|
---|
51 | for (;i!=events.end();++i)
|
---|
52 | {
|
---|
53 | if (handle.reference==&*i)
|
---|
54 | {
|
---|
55 | if (last==events.end())
|
---|
56 | events.erase();
|
---|
57 | else
|
---|
58 | events.erase_after(last);
|
---|
59 | delete &*i;
|
---|
60 | return i4_T;
|
---|
61 | }
|
---|
62 | last=i;
|
---|
63 | }
|
---|
64 | return i4_F;
|
---|
65 | }
|
---|
66 |
|
---|
67 | i4_bool i4_time_device_class::process_events() // returns true if an event was dispatched
|
---|
68 | {
|
---|
69 | if (events.begin()==events.end())
|
---|
70 | return i4_F;
|
---|
71 |
|
---|
72 | i4_time_class current;
|
---|
73 | i4_bool ret=i4_F;
|
---|
74 |
|
---|
75 | i4_isl_list<timed_event>::iterator i=events.begin(),del=events.end(), last=events.end();
|
---|
76 |
|
---|
77 | for (;i!=events.end();)
|
---|
78 | {
|
---|
79 | if (current.milli_diff(i->start_time)>i->milli_wait)
|
---|
80 | {
|
---|
81 | if (last==events.end())
|
---|
82 | events.erase();
|
---|
83 | else
|
---|
84 | events.erase_after(last);
|
---|
85 |
|
---|
86 | #ifndef I4_RETAIL
|
---|
87 | if (i4_show_events)
|
---|
88 | i4_warning("sending : '%s' to '%s'",i->event->name(), i->send_to->name());
|
---|
89 | #endif
|
---|
90 | del=i;
|
---|
91 | ++i;
|
---|
92 |
|
---|
93 | del->send_to->receive_event(del->event);
|
---|
94 | delete &*del;
|
---|
95 |
|
---|
96 | ret=i4_T;
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | last=i;
|
---|
101 | ++i;
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|
105 | last=events.end();
|
---|
106 | return ret;
|
---|
107 | }
|
---|
108 |
|
---|
109 | i4_time_device_class::id
|
---|
110 | i4_time_device_class::request_event(i4_event_handler_class *send_to, // who to send the event to
|
---|
111 | i4_event *event, // what event to send
|
---|
112 | w32 milli_wait) // how much time to wait (in milli-seconds)
|
---|
113 | {
|
---|
114 | // first make sure this is not an event that needs to be sent right now
|
---|
115 | // these events usually are two-way events that have return codes inside
|
---|
116 | if (event->when()==i4_event::NOW)
|
---|
117 | i4_error("Cannot send NOW events throught the time device!");
|
---|
118 |
|
---|
119 | timed_event *ev=new timed_event(send_to,event,milli_wait);
|
---|
120 | events.insert_end(*ev);
|
---|
121 | return id(ev);
|
---|
122 | }
|
---|