summaryrefslogtreecommitdiff
path: root/clock/src/main.cc
blob: b808d22149525a34ab7a1e4572ce70425d1f3efd (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
/*
winnie - an experimental window system

Copyright (C) 2013 Eleni Maria Stea

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 of the License, 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/>.

Author: Eleni Maria Stea <elene.mst@gmail.com>
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "clockface.h"
#include "winnie.h"

static void display(Window *win);
static void keyboard(Window *win, int key, bool pressed);
static void button(Window *win, int bn, bool pressed, int x, int y);
static void motion(Window *win, int x, int y);
static void timer(Window *win);

static Pixmap *clockface;

extern "C" bool
init()
{
  Window *clock_win = new Window;
  clock_win->set_title("clock");
  clock_win->move(200, 100);
  clock_win->resize(200, 200);
  clock_win->set_display_callback(display);
  clock_win->set_keyboard_callback(keyboard);
  clock_win->set_mouse_button_callback(button);
  clock_win->set_mouse_motion_callback(motion);
  clock_win->set_timer_callback(timer);

  clockface = new Pixmap;
  clockface->set_image(gimp_clockface.width, gimp_clockface.height);

  for (int i = 0; i < clockface->width * clockface->height; i++) {
    clockface->pixels[i * 4] = gimp_clockface.pixel_data[i * 3];
    clockface->pixels[i * 4 + 1] = gimp_clockface.pixel_data[i * 3 + 1];
    clockface->pixels[i * 4 + 2] = gimp_clockface.pixel_data[i * 3 + 2];
  }

  wm->add_window(clock_win);

  set_window_timer(clock_win, 1000, TIMER_REPEAT);
  return true;
}

extern "C" void
cleanup()
{
  delete clockface;
}

static void
draw_hand(Pixmap *pixmap,
          int cx,
          int cy,
          float angle,
          float length,
          float width,
          int r,
          int g,
          int b)
{
  float varr_in[8]
      = { 0, width / 2.0f, -width / 2.0f, 0, 0, -length, width / 2.0f, 0 };
  int varr_out[8];

  float matrix[2][2] = { { (float) cos(angle), (float) -sin(angle) },
                         { (float) sin(angle), (float) cos(angle) } };

  for (int i = 0; i < 4; i++) {
    float x = varr_in[i * 2];
    float y = varr_in[i * 2 + 1];
    varr_out[i * 2] = matrix[0][0] * x + matrix[0][1] * y + cx;
    varr_out[i * 2 + 1] = matrix[1][0] * x + matrix[1][1] * y + cy;
  }

  draw_polygon(pixmap, varr_out, 0, 4, r, g, b);
}

static void
display(Window *win)
{
  time_t tm_sec = time(0);
  struct tm *tm = localtime(&tm_sec);

  Rect rect = win->get_absolute_rect();
  int cx = rect.x + rect.width / 2;
  int cy = rect.y + rect.height / 2;

  float hr_len = rect.width * 0.25;
  float min_len = rect.width * 0.4;
  float sec_len = rect.width * 0.38;

  float sec_angle = M_PI * (float) tm->tm_sec / 30.0f;
  float min_angle = M_PI * (float) tm->tm_min / 30.0f;
  float hour_angle = M_PI * (float) tm->tm_hour / 6.0f;

  Pixmap *fb = get_framebuffer_pixmap();
  blit(clockface->pixels,
       Rect(0, 0, clockface->width, clockface->height),
       fb->pixels,
       get_screen_size(),
       rect.x,
       rect.y);

  draw_hand(fb, cx, cy, hour_angle, hr_len, 16, 0, 0, 0);
  draw_hand(fb, cx, cy, min_angle, min_len, 12, 64, 64, 64);

  int x = (int) (sin(sec_angle) * sec_len);
  int y = (int) (-cos(sec_angle) * sec_len);
  draw_line(fb, cx, cy, cx + x, cy + y, 200, 64, 45);
  draw_line(fb, cx + 1, cy, cx + x + 1, cy + y, 200, 64, 45);
  draw_line(fb, cx, cy + 1, cx + x, cy + y + 1, 200, 64, 45);
}

static void
keyboard(Window *win, int key, bool pressed)
{
  printf("WINDOW(%p) keyboard %d %s\n",
         (void *) win,
         key,
         pressed ? "press" : "release");
}

static void
button(Window *win, int bn, bool pressed, int x, int y)
{
  printf("WINDOW(%p) button %d %s\n",
         (void *) win,
         bn,
         pressed ? "press" : "release");
}

static void
motion(Window *win, int x, int y)
{
  printf("WINDOW(%p) motion %d %d\n", (void *) win, x, y);
}

static void
timer(Window *win)
{
  wm->invalidate_region(win->get_absolute_rect());
}