aboutsummaryrefslogtreecommitdiff
path: root/src/grid.c
blob: f4a75e1cec7d91403b966505f5740c5263ab5906 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* This file is part of tagr.
   Copyright (C) 2009 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <unistd.h>
#include <tagr.h>

struct grid
{
  struct grid_class *class;
  queue_t *q;
  unsigned long vmin;
  unsigned long vmax;
  unsigned long cur;
  char *str;
  void *data;
};

grid_t
grid_create (struct grid_class *class, queue_t *q,
	     unsigned long vmin,  unsigned long vmax, void *cdata)
{
  grid_t grid;

  if (!class)
    return NULL;
  grid = xmalloc (sizeof (*grid));
  grid->class = class;
  grid->q = q;
  grid->vmin = vmin;
  grid->vmax = vmax;
  grid->cur = 0;
  if (class->create)
    grid->data = class->create (grid, cdata);
  else
    grid->data = NULL;
  grid->str = NULL;
  return grid;
}
    
unsigned long
grid_next (grid_t grid, char **str, int *mark)
{
  return grid->class->next (grid, str, mark);
}

void
grid_destroy (grid_t grid)
{
  if (grid)
    {
      if (grid->str)
	free (grid->str);
      if (grid->class->dealloc)
	grid->class->dealloc (grid->data);
      free (grid);
    }
}

static void
grid_free_data (void *ptr)
{
  free (ptr);
}


struct ygrid_data
{
  unsigned long step;
};

static void *
ygrid_create (grid_t grid, void *cdata)
{
  struct monitor *mon = cdata;
  struct ygrid_data *gr = xmalloc (sizeof (*gr));
  gr->step = mon->ystep;
  return gr;
}
    
static unsigned long
ygrid_next (grid_t grid, char **str, int *mark)
{
  struct ygrid_data *gr = grid->data;
  if (mark)
    *mark = 0;
  grid->cur += gr->step;
  if (grid->cur < grid->vmax)
    {
      if (grid->str)
	{
	  free (grid->str);
	  grid->str = NULL;
	}
      asprintf (&grid->str, "%6.1f", (double) grid->cur);
      *str = grid->str;
    }
  return grid->cur;
}

struct grid_class grid_class_y = {
  ygrid_create,
  ygrid_next,
  grid_free_data
};



struct xgrid_data
{
  time_t start;
  unsigned off;
  unsigned unit;
};

#define TWOHRS (2*3600)

static void *
xgrid_2h_create (grid_t grid, void *cdata)
{
  time_t start = *(time_t*)cdata;
  struct xgrid_data *gr = xmalloc (sizeof (*gr));
  unsigned long frac = start % 3600;
  unsigned long h;
  struct tm *tm;

  gr->start = start - frac;
  tm = localtime (&gr->start);
  h = tm->tm_hour % 2;
  gr->off = frac + h * TWOHRS;
  gr->unit = tm->tm_hour - h;
  
  return gr;
}

static unsigned long
xgrid_2h_next (grid_t grid, char **str, int *mark)
{
  struct xgrid_data *gr = grid->data;
  unsigned long ret = gr->off;

  if (grid->str)
    {
      free (grid->str);
      grid->str = NULL;
    }
  asprintf (&grid->str, "%02d", gr->unit);
  *str = grid->str;
  if (mark)
    *mark = gr->unit == 0;
  
  gr->off += TWOHRS;
  gr->unit = (gr->unit + 24 - 2) % 24;
  return ret;
}

struct grid_class grid_class_x_2h = {
  xgrid_2h_create,
  xgrid_2h_next,
  grid_free_data
};


#define TWENTYFOURHRS 86400

static void *
xgrid_wday_create (grid_t grid, void *cdata)
{
  time_t start = *(time_t*)cdata;
  struct xgrid_data *gr = xmalloc (sizeof (*gr));
  struct tm *tm = localtime (&start);
  unsigned long frac;

  frac = (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec;
  gr->start = start - frac;
  gr->off = frac;
  gr->unit = tm->tm_wday;

  return gr;
}

static unsigned long
xgrid_wday_next (grid_t grid, char **str, int *mark)
{
  struct xgrid_data *gr = grid->data;
  unsigned long ret = gr->off;
  char buf[80];
  struct tm *tm = localtime (&gr->start);

  if (grid->str)
    {
      free (grid->str);
      grid->str = NULL;
    }
  strftime (buf, sizeof buf, "%a", tm);
  grid->str = xstrdup (buf);
  *str = grid->str;
  if (mark)
    *mark = tm->tm_wday == 0;
  
  gr->start -= TWENTYFOURHRS;
  gr->off += TWENTYFOURHRS;
  return ret;
}

struct grid_class grid_class_x_wday = {
  xgrid_wday_create,
  xgrid_wday_next,
  grid_free_data
};


#define SEVENDAYS (7*TWENTYFOURHRS)

static void *
xgrid_week_create (grid_t grid, void *cdata)
{
  time_t start = *(time_t*)cdata;
  struct xgrid_data *gr = xmalloc (sizeof (*gr));
  struct tm *tm = localtime (&start);
  unsigned long frac;

  frac = ((tm->tm_wday * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60
          + tm->tm_sec;
  gr->start = start - frac;
  gr->off = frac;
  gr->unit = 0;
  
  return gr;
}

static unsigned long
xgrid_week_next (grid_t grid, char **str, int *mark)
{
  struct xgrid_data *gr = grid->data;
  unsigned long ret = gr->off;
  struct tm *tm = localtime (&gr->start);

  if (tm->tm_mday && tm->tm_mday < 7 && mark && gr->unit == 0)
    {
      int diff = (tm->tm_mday - 1) * TWENTYFOURHRS;
      ret += diff;
      *mark = 1;
      *str = NULL;
      gr->unit = 1;
    }
  else
    {
      gr->unit = 0;
      if (grid->str)
	{
	  free (grid->str);
	  grid->str = NULL;
	}
      asprintf (&grid->str, _("Week %2d"), tm->tm_yday / 7 + 1);
      *str = grid->str;
      if (mark)
	*mark = 0;
  
      gr->start -= SEVENDAYS;
      gr->off += SEVENDAYS;
    }
  
  return ret;
}

struct grid_class grid_class_x_week = {
  xgrid_week_create,
  xgrid_week_next,
  grid_free_data
};


static void *
xgrid_month_create (grid_t grid, void *cdata)
{
  time_t start = *(time_t*)cdata;
  struct xgrid_data *gr = xmalloc (sizeof (*gr));
  struct tm *tm = localtime (&start);
  unsigned long frac;

  frac = (((tm->tm_mday - 1) * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60
          + tm->tm_sec;
  gr->start = start - frac;
  gr->off = frac;
  return gr;
}

static unsigned long
xgrid_month_next (grid_t grid, char **str, int *mark)
{
  struct xgrid_data *gr = grid->data;
  unsigned long ret = gr->off;
  unsigned long frac;
  struct tm *tm = localtime (&gr->start);
  int mon;
  char buf[80];

  if (grid->str)
    {
      free (grid->str);
      grid->str = NULL;
    }
  strftime (buf, sizeof buf, "%b", tm);
  grid->str = xstrdup (buf);
  *str = grid->str;
  if (mark)
    *mark = tm->tm_mon == 0;

  if (tm->tm_mon == 0)
    mon = 11;
  else
    mon = tm->tm_mon - 1;
  
  do
    {
      gr->start -= TWENTYFOURHRS;
      gr->off += TWENTYFOURHRS;
      tm = localtime (&gr->start);
    }      
  while (!(mon == tm->tm_mon && tm->tm_mday == 1));
  
  frac = (((tm->tm_mday - 1) * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60
          + tm->tm_sec;
  gr->start -= frac;
  gr->off += frac;

  return ret;
}

struct grid_class grid_class_x_month = {
  xgrid_month_create,
  xgrid_month_next,
  grid_free_data
};

Return to:

Send suggestions and report system problems to the System administrator.