File: Time.c

package info (click to toggle)
ppxp 0.99120923-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,812 kB
  • ctags: 3,704
  • sloc: ansic: 24,532; tcl: 3,992; makefile: 517; sh: 80
file content (301 lines) | stat: -rw-r--r-- 8,514 bytes parent folder | download | duplicates (3)
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
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Vendor.h>
#include <X11/xpm.h>
#include <stdio.h>
#include "TimeP.h"

#include "pixmap/t0.xpm"
#include "pixmap/t1.xpm"
#include "pixmap/t2.xpm"
#include "pixmap/t3.xpm"
#include "pixmap/t4.xpm"
#include "pixmap/t5.xpm"
#include "pixmap/t6.xpm"
#include "pixmap/t7.xpm"
#include "pixmap/t8.xpm"
#include "pixmap/t9.xpm"
#include "pixmap/tn.xpm"
#include "pixmap/tm.xpm"
#include "pixmap/ca.xpm"
#include "pixmap/cb.xpm"

#define TIME_WIDTH  (47)
#define TIME_HEIGHT (14)
#define BASE_COLOR  "#282828"

#define sDisp      XtDisplay(tw)
#define sScreen    XtScreen(tw)
#define sRoot      XRootWindowOfScreen(XtScreen(tw))
#define sWidth     tw->core.width
#define sHeight    tw->core.height
#define sPixmap    tw->time.pixmap
#define sParts(i)  tw->time.parts_pixmap[i]
#define sAttr(i)   tw->time.parts_attr[i]
#define sGC        tw->time.gc

static char** pixmap_name[] =
{
        t0_xpm, t1_xpm, t2_xpm, t3_xpm, t4_xpm,
        t5_xpm, t6_xpm, t7_xpm, t8_xpm, t9_xpm,
        tn_xpm, tm_xpm, ca_xpm, cb_xpm
};

static int xpos_num[] =
{
        1, 8, 17, 24, 33, 40
};

#define offset(field) XtOffsetOf(TimeRec, field)
static XtResource resources[] = {
  { XtNtime, XtCTime, XtRInt, sizeof(int),
            offset(time.time), XtRImmediate, (XtPointer)-1 },
};
#undef offset

static void Initialize();
static void Resize();
static void Redisplay();
static Boolean SetValues();
static void Destroy();
static XtGeometryResult QueryGeometry();
static char defaultTranslation[] = "";
static XtActionsRec actions[] = {{NULL, NULL}};
static void ShowTime();

TimeClassRec timeClassRec = {
  {
  /* superclass             */ (WidgetClass) &coreClassRec,
  /* class_name             */ "Time",
  /* widget_size            */ sizeof(TimeRec),
  /* class_initialize       */ NULL,
  /* class_part_initialize  */ NULL,
  /* class_inited           */ FALSE,
  /* initialize             */ Initialize,
  /* initialize_hook        */ NULL,
  /* realize                */ XtInheritRealize,
  /* actions                */ actions,
  /* num_actions            */ XtNumber(actions),
  /* resources              */ resources,
  /* num_resourece          */ XtNumber(resources),
  /* xrm_class              */ NULLQUARK,
  /* compress_motion        */ TRUE,
  /* compress_exposure      */ TRUE,
  /* compress_enterleave    */ TRUE,
  /* visible_interest       */ FALSE,
  /* destroy                */ Destroy,
  /* resize                 */ Resize,
  /* expose                 */ Redisplay,
  /* set_values             */ SetValues,
  /* set_values_hook        */ NULL,
  /* set_values_almoset     */ XtInheritSetValuesAlmost,
  /* get_values_hook        */ NULL,
  /* accept_focus           */ NULL,
  /* version                */ XtVersion,
  /* callback_private       */ NULL,
  /* tm_table               */ defaultTranslation,
  /* query_geometry         */ QueryGeometry,
  /* display_accelator      */ XtInheritDisplayAccelerator,
  /* extention              */ NULL
  },{
  /* foo                    */ 0
  }
};
WidgetClass timeWidgetClass = (WidgetClass)&timeClassRec;

static void
GetGC(TimeWidget tw)
{
        XGCValues values;
        XtGCMask  mask;
        XColor exact;
        Colormap cmap;

        cmap=DefaultColormapOfScreen(sScreen);
        XAllocNamedColor(sDisp, cmap, BASE_COLOR, &(tw->time.color), &exact);
        values.foreground = tw->time.color.pixel;
        values.background = tw->core.background_pixel;
        values.graphics_exposures = False;
        mask = GCForeground | GCBackground | GCGraphicsExposures;
        sGC = XCreateGC(sDisp, sPixmap, mask, &values);
}

static void
GetPixmap(TimeWidget tw)
{
        int i;
        Pixmap Mask;
        int rst;

        for(i=0; i<14; i++) {
                sAttr(i).valuemask = XpmReturnInfos 
                                   | XpmReturnAllocPixels
                                   | XpmReturnExtensions;
                rst = XpmCreatePixmapFromData(sDisp, sRoot, pixmap_name[i],
                                        &sParts(i), &Mask, &sAttr(i));
                if (rst != 0) XpmError(rst);
                if (Mask) XFreePixmap(sDisp, Mask);
        }
}

static void
Initialize(Widget wreq, Widget wnew)
{
        TimeWidget tw = (TimeWidget) wnew;
        Pixmap Mask;

        if (sWidth == 0) {
                sWidth = TIME_WIDTH;
        }
        if (sHeight == 0) {
                sHeight = TIME_HEIGHT;
        }
        sPixmap = XCreatePixmap(sDisp, sRoot, sWidth, sHeight,
                               DefaultDepthOfScreen(sScreen));
        GetGC(tw);
        GetPixmap(tw);
        XFillRectangle(sDisp, sPixmap, sGC,  0, 0, sWidth, sHeight);
        ShowTime(tw);
}

static void
Redisplay(Widget w, XExposeEvent *event)
{
        TimeWidget tw = (TimeWidget) w;
        int x,y;
        int width, height;

        if (XtIsRealized(w)) {
                if (event) {
                        x = event->x;
                        y = event->y;
                        width = event->width;
                        height = event->height;
                } else {
                        x = 0;
                        y = 0;
                        width = sWidth;
                        height = sHeight;
                }
                XCopyArea(sDisp, sPixmap, XtWindow(tw), sGC,
                          x, y, width, height, x, y);
        }
}


static Boolean
SetValues(Widget wcur, Widget wreq, Widget wnew)
{
        TimeWidget cur = (TimeWidget) wcur;
        TimeWidget new = (TimeWidget) wnew;
        Boolean redraw = False;

        if (cur->time.time != new->time.time) {
                ShowTime(new);
        }

        return redraw;
}

static void
Destroy(Widget w)
{
        TimeWidget tw = (TimeWidget) w;
        Colormap cmap;
        int i;

        cmap=DefaultColormapOfScreen(sScreen);
        XFreeColors(sDisp, cmap, &tw->time.color.pixel, 1, 0);
        if (sGC) XFreeGC(sDisp, sGC);
        if (sPixmap)
                XFreePixmap(sDisp, sPixmap);

        for(i=0; i<14; i++) {
                if (sParts(i))
                        XFreePixmap(sDisp, sParts(i));
                XFreeColors(sDisp, cmap,
                            sAttr(i).alloc_pixels,
                            sAttr(i).nalloc_pixels, 0);
                XpmFreeAttributes(&sAttr(i));
        }
}

static void Resize(Widget w)
{
        TimeWidget tw = (TimeWidget) w;

        return;
}

static XtGeometryResult
QueryGeometry(Widget w, XtWidgetGeometry *proposed, XtWidgetGeometry *answer)
{
        TimeWidget tw = (TimeWidget) w;

        answer->request_mode = CWWidth | CWHeight;
        answer->width  = sWidth;
        answer->height = sHeight;
        return XtGeometryAlmost;
}

static void
ShowTime(TimeWidget tw)
{
        XExposeEvent rrect;
        static int old_num[6] = {-1, -1, -1, -1, -1};
        static int old_colon = -1;
        int num[6];
        int colon;
        int t;
        int redisp;
        int i;

        t = tw->time.time;
        redisp = False;

        if (t < 0) {
                for(i=0; i<6; i++) {
                        num[i]=10;
                }
                colon=0;
        } else if (t >= 86400) {
                for(i=0; i<6; i++) {
                        num[i]=11;
                }
                colon=1;
        } else {
                num[0] = (t /3600) / 10;
                num[1] = (t /3600) % 10;
                num[2] = ((t / 60) % 60) / 10;
                num[3] = ((t / 60) % 60) % 10;
                num[4] = (t % 60) / 10;
                num[5] = (t % 60) % 10;
                colon=1;
        }

        for(i=0; i<6; i++) {
                if (old_num[i] != num[i]) {
                        XCopyArea(sDisp, sParts(num[i]), sPixmap, sGC,
                                  0,0, 7,14, xpos_num[i], 0);
                        old_num[i] = num[i];
                redisp = True;
                }
        }

        if (old_colon != colon) {
                XCopyArea(sDisp, sParts(colon+12), sPixmap, sGC,
                          0,0, 2,14, 15, 0);
                XCopyArea(sDisp, sParts(colon+12), sPixmap, sGC,
                          0,0, 2,14, 31, 0);
                old_colon = colon;
                redisp = True;
        }

        if (redisp) {
                rrect.x = 0;
                rrect.y = 0;
                rrect.width = sWidth;
                rrect.height = sHeight;
                Redisplay((Widget)tw, &rrect);
        }
}