LMMS
Loading...
Searching...
No Matches
undo-history.cpp
Go to the documentation of this file.
1#include <deque>
2#include <cstring>
3#include <cstdio>
4#include <cassert>
5#include <ctime>
6#include <rtosc/rtosc.h>
8
9using std::pair;
10using std::make_pair;
11
12namespace rtosc {
14{
15 public:
18 {}
20 {
21 clear();
22 }
23 std::deque<pair<time_t, const char *>> history;
25 unsigned max_history_size;//XXX Expose this via a public API
26 std::function<void(const char*)> cb;
27
28 void rewind(const char *msg);
29 void replay(const char *msg);
30 bool mergeEvent(time_t t, const char *msg, char *buf, size_t N);
31 void clear(void);
32};
33
35{
37 impl->history_pos = 0;
38}
39
41{
42 delete impl;
43}
44
46{
47 //TODO Properly account for when you have traveled back in time.
48 //while this could result in another branch of history, the simple method
49 //would be to kill off any future redos when new history is recorded
50 if(impl->history.size() != (unsigned) impl->history_pos) {
51 impl->history.resize(impl->history_pos);
52 }
53
54 size_t len = rtosc_message_length(msg, -1);
55 char *data = new char[len];
56 time_t now = time(NULL);
57 //printf("now = '%ld'\n", now);
58 if(!impl->mergeEvent(now, msg, data, len)) {
59 memcpy(data, msg, len);
60 impl->history.push_back(make_pair(now, data));
61 impl->history_pos++;
62 if(impl->history.size() > impl->max_history_size)
63 {
64 delete[] impl->history[0].second;
65 impl->history.pop_front();
66 impl->history_pos--;
67 }
68 }
69
70}
71
73{
74 int i = 0;
75 for(auto s : impl->history)
76 printf("#%d type: %s dest: %s arguments: %s\n", i++,
77 s.second, rtosc_argument(s.second, 0).s, rtosc_argument_string(s.second));
78}
79
80static char tmp[256];
82{
83 memset(tmp, 0, sizeof(tmp));
87 &arg);
88 cb(tmp);
89}
90
92{
94 int len = rtosc_amessage(tmp, 256, rtosc_argument(msg,0).s,
96 &arg);
97
98 if(len)
99 cb(tmp);
100}
101
102const char *getUndoAddress(const char *msg)
103{
104 return rtosc_argument(msg,0).s;
105}
106
107bool UndoHistoryImpl::mergeEvent(time_t now, const char *msg, char *buf, size_t N)
108{
109 if(history_pos == 0)
110 return false;
111 for(int i=history_pos-1; i>=0; --i) {
112 if(difftime(now, history[i].first) > 2)
113 break;
114 if(!strcmp(getUndoAddress(msg),
116 {
117 //We can splice events together, merging them into one event
118 rtosc_arg_t args[3];
119 args[0] = rtosc_argument(msg, 0);
120 args[1] = rtosc_argument(history[i].second,1);
121 args[2] = rtosc_argument(msg, 2);
122
124
125 delete [] history[i].second;
126 history[i].second = buf;
127 history[i].first = now;
128 return true;
129 }
130 }
131 return false;
132}
133
135{
136 for(auto elm : history)
137 delete [] elm.second;
138 history.clear();
139 history_pos = 0;
140}
141
142
143
144void UndoHistory::seekHistory(int distance)
145{
146 //TODO print out the events that would need to take place to get to the
147 //final destination
148
149 //TODO limit the distance to be to applicable sizes
150 //ie ones that do not exceed the known history/future
151 long dest = impl->history_pos + distance;
152 if(dest < 0)
153 distance -= dest;
154 if(dest > (long) impl->history.size())
155 distance = impl->history.size() - impl->history_pos;
156 if(!distance)
157 return;
158
159 //TODO account for traveling back in time
160 if(distance<0)
161 while(distance++)
162 impl->rewind(impl->history[--impl->history_pos].second);
163 else
164 while(distance--)
165 impl->replay(impl->history[impl->history_pos++].second);
166}
167
168unsigned UndoHistory::getPos(void) const
169{
170 return impl->history_pos;
171}
172
173const char *UndoHistory::getHistory(int i) const
174{
175 return impl->history[i].second;
176}
177
178size_t UndoHistory::size() const
179{
180 return impl->history.size();
181}
182
183void UndoHistory::setCallback(std::function<void(const char*)> cb)
184{
185 impl->cb = cb;
186}
187};
#define NULL
Definition CarlaBridgeFormat.cpp:30
float arg(const fft_t *freqs, off_t x)
Definition OscilGen.cpp:58
void recordEvent(const char *msg)
Definition undo-history.cpp:45
unsigned getPos(void) const
Definition undo-history.cpp:168
const char * getHistory(int i) const
Definition undo-history.cpp:173
void showHistory(void) const
Definition undo-history.cpp:72
size_t size(void) const
Definition undo-history.cpp:178
void seekHistory(int distance)
Definition undo-history.cpp:144
class UndoHistoryImpl * impl
Definition undo-history.h:34
~UndoHistory(void)
Definition undo-history.cpp:40
UndoHistory(void)
Definition undo-history.cpp:34
void setCallback(std::function< void(const char *)> cb)
Definition undo-history.cpp:183
Definition undo-history.cpp:14
std::function< void(const char *)> cb
Definition undo-history.cpp:26
bool mergeEvent(time_t t, const char *msg, char *buf, size_t N)
Definition undo-history.cpp:107
long history_pos
Definition undo-history.cpp:24
UndoHistoryImpl(void)
Definition undo-history.cpp:16
std::deque< pair< time_t, const char * > > history
Definition undo-history.cpp:23
void clear(void)
Definition undo-history.cpp:134
void replay(const char *msg)
Definition undo-history.cpp:91
unsigned max_history_size
Definition undo-history.cpp:25
~UndoHistoryImpl(void)
Definition undo-history.cpp:19
void rewind(const char *msg)
Definition undo-history.cpp:81
struct huft * t
Definition inflate.c:943
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
JSAMPIMAGE data
Definition jpeglib.h:945
const char * msg
Definition missing_descriptor.c:20
Definition globals.h:37
static char tmp[256]
Definition undo-history.cpp:80
osc_element< 1, rtMsg< Types... > >::type second(rtMsg< Types... > &Tuple)
Definition typed-message.h:159
osc_element< 0, rtMsg< Types... > >::type first(rtMsg< Types... > &Tuple)
Definition typed-message.h:152
const char * getUndoAddress(const char *msg)
Definition undo-history.cpp:102
#define N
Definition nseel-cfunc.c:36
size_t rtosc_message_length(const char *msg, size_t len)
Definition rtosc.c:848
size_t rtosc_amessage(char *buffer, size_t len, const char *address, const char *arguments, const rtosc_arg_t *args)
Definition rtosc.c:515
const char * rtosc_argument_string(const char *msg)
Definition rtosc.c:11
rtosc_arg_t rtosc_argument(const char *msg, unsigned idx)
Definition rtosc.c:732
Definition rtosc.h:46
const char * s
Definition rtosc.h:54
memcpy(hh, h, RAND_HEAD_LEN)
#define void
Definition unzip.h:396