summaryrefslogtreecommitdiff
path: root/support/dktools/DkClockData.cpt
blob: 600529818611e0029e6067d2677286af101429b0 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
%%	options

%%	header

/**	Data of interest for the DkClockView class.
*/
typedef struct {
  int		h;	/**< Current hour. */
  int		m;	/**< Current minute. */
  int		s;	/**< Current second. */
  int		a;	/**< Flag: In alert. */
} clockview_data_t;

/**	Data for DkClock.
*/
class DkClockData
{
  protected:

    /**	Protect data against concurrent access.
    */
    wxMutex		mxProtectData;

    /**	Current time.
    */
    dk3_tm_t		tmCurrent;

    /**	Output data structure.
    */
    clockview_data_t	clvd;

    /**	Hour of alert.
    */
    int			iAlertHour;

    /**	Minute of alert.
    */
    int			iAlertMinute;

    /**	Flag: Alert enabled.
    */
    int			iAlertEnabled;

    /**	Alert state (0=before alert, 1=in alert, 2=after alert).
    */
    int			iAlertState;

  public:

    /**	Constructor.
    */
    DkClockData();

    /**	Retrieve data from object into buffer.
    	@param	dptr	Buffer pointer.
    */
    void getData(clockview_data_t *dptr);

    /**	Get alert settings.
    	@param	en	Pointer to enabled variable.
	@param	h	Pointer to hour variable.
	@param	m	Pointer to minute variable.
    */
    void getAlertData(int *en, int *h, int *m);

    /**	Set new current time.
    	@param	ct	Current time.
	@return	1 to maximize and raise the window, 0 to do nothing.
    */
    int setTime(dk3_time_t *ct);

    /**	Enable or disable alert, set time when enabling.
    	@param	flag	Flag to enable or disable alert.
	@param	hour	Hour of alert.
	@param	min	Minute of alert.
    */
    void setAlert(bool flag, int hour, int min);

    /**	End a running alert.
    */
    void	endAlert(void);
};

%%	module

#include "wxdkclock.h"


$!trace-include


DkClockData::DkClockData()
{
  $? "+ DkClockData::DkClockData"
  clvd.h = 0;
  clvd.m = 0;
  clvd.s = 0;
  clvd.a = 0;
  iAlertHour = 0;
  iAlertMinute = 0;
  iAlertEnabled = 0;
  iAlertState = 0;
  tmCurrent.Y = 0;
  tmCurrent.M = 0;
  tmCurrent.D = 0;
  tmCurrent.h = 0;
  tmCurrent.m = 0;
  tmCurrent.s = 0;
  $? "- DkClockData::DkClockData"
}



void
DkClockData::getData(clockview_data_t *dptr)
{
  $? "+ DkClockData::getData PTR=%d", TR_IPTR(dptr)
  if(dptr) {
    wxMutexLocker	lock(mxProtectData);
    dk3mem_cpy((void *)dptr, (void *)(&clvd), sizeof(clockview_data_t));
  }
  $? "- DkClockData::getData"
}



int
DkClockData::setTime(dk3_time_t *ct)
{
  dk3_tm_t	lct;
  int		back		= 0;
  $? "+ DkClockData::setTime"
  if(dk3sf_localtime_app(&lct, ct, NULL)) {
    wxMutexLocker		lock(mxProtectData);
    /*
    	Check date change.
    */
    if(2 == iAlertState) {
      if(lct.Y != tmCurrent.Y) {
        iAlertState = 0;
      } else {
        if(lct.M != tmCurrent.M) {
	  iAlertState = 0;
	} else {
	  if(lct.D != tmCurrent.D) {
	    iAlertState = 0;
	  }
	}
      }
    }
    /*
    	Check alert state change.
    */
    if((0 != iAlertEnabled) && (0 == iAlertState)) {
      if(lct.h > iAlertHour) {
        iAlertState = 1; back = 1;
      } else {
        if(lct.h == iAlertHour) {
	  if(lct.m >= iAlertMinute) {
	    iAlertState = 1; back = 1;
	  }
	}
      }
    }
    /*
    	Copy current time.
    */
    dk3mem_cpy((void *)(&tmCurrent), (void *)(&lct), sizeof(dk3_tm_t));
    /*
    	Copy to output structure.
    */
    clvd.h = lct.h;
    clvd.m = lct.m;
    clvd.s = lct.s;
    clvd.a = ((1 == iAlertState) ? 1 : 0);
  } $? "- DkClockData::setTime %d", back
  return back;
}



void
DkClockData::setAlert(bool flag, int hour, int min)
{
  $? "+ DkClockData::setAlert %d %d %d", ((flag) ? 1 : 0), hour, min
  iAlertState = 0;
  {
    wxMutexLocker         lock(mxProtectData);
    if(flag) {				$? ". enabled"
      if((0 <= hour) && (23 >= hour) && (0 <= min) && (59 >= min)) {
        iAlertEnabled = 1;		$? ". values ok"
        iAlertHour = hour;
        iAlertMinute = min;
        if(tmCurrent.h > hour) {
          iAlertState = 2;
        } else {
          if(tmCurrent.h == hour) {
	    if(tmCurrent.m >= min) {
	      iAlertState = 2;
	    }
	  }
        }
      } else {				$? "! wrong values"
        iAlertEnabled = 0;
        iAlertHour = 0;
        iAlertMinute = 0;
      }
    } else {				$? ". disabled"
      iAlertEnabled = 0;
      iAlertHour = 0;
      iAlertMinute = 0;
    }
  } $? "- DkClockData::setAlert"
}



void
DkClockData::endAlert(void)
{
  $? "+ DkClockData::endAlert"
  {
    wxMutexLocker		lock(mxProtectData);
    if(1 == iAlertState) {
      iAlertState = 2;
    }
  }$? "- DkClockData::endAlert"
}



void
DkClockData::getAlertData(int *en, int *h, int *m)
{
  $? "+ DkClockData::getAlertData"
  if((en) && (h) && (m)) {
    wxMutexLocker		lock(mxProtectData);
    *en = iAlertEnabled;	$? ". iAlertEnabled %d", iAlertEnabled
    *h  = iAlertHour;		$? ". iAlertHour    %d", iAlertHour
    *m  = iAlertMinute;		$? ". iAlertMinute  %d", iAlertMinute
  } $? "- DkClockData::getAlertData"
}