first working implementation

This commit is contained in:
Roland Hollós 2020-11-22 11:28:39 +01:00
parent abedc6b3e0
commit af18c1e9d2
2 changed files with 178 additions and 9 deletions

View File

@ -1,6 +1,6 @@
TARGET=drawOnRoot TARGET=fiboBG
SRC=main.c SRC=main.c
# DEBUG=-g # DEBUG=-g
DEBUG= DEBUG=
$(TARGET): main.c $(TARGET): main.c
gcc -Wall -Wextra $(DEBUG) $(SRC) `pkg-config --cflags --libs cairo x11` -o $(TARGET) gcc -Wall -Wextra $(DEBUG) $(SRC) `pkg-config --cflags --libs cairo x11` -o $(TARGET) -lm

183
main.c
View File

@ -1,20 +1,186 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <cairo.h> #include <cairo.h>
#include <cairo-xlib.h> #include <cairo-xlib.h>
int width, height; typedef struct _rectangle {
void draw(cairo_t *cr) { int pos_x;
int quarter_w = width / 4; int pos_y;
int quarter_h = height / 4; int width;
cairo_set_source_rgb(cr, 1.0, 1.0, 0.0); int height;
cairo_rectangle(cr, quarter_w, quarter_h, quarter_w * 2, quarter_h * 2); int color;
} rectangle;
typedef struct _myTime {
unsigned int hour;
unsigned int minute;
} myTime;
myTime getTime(void){
time_t raw_time;
time(&raw_time);
struct tm *currTime;
currTime = localtime(&raw_time);
myTime t;
t.hour = currTime->tm_hour;
t.minute = currTime->tm_min;
return t;
}
void print_current_time(void){
myTime t=getTime();
printf("%02d:%02d\n", t.hour, t.minute);
}
void getFibbTime(myTime t, int* res){
int minute = t.minute;
minute = (minute / 5) * 5;
int hour = t.hour % 12;
if(hour == 0){
hour = 12;
}
int fibBlocks[5] = {5,3,2,1,1};
int mins[5] = {0,0,0,0,0};
int hours[5] = {0,0,0,0,0};
int blockElement = 0;
while(minute != 0){
if(fibBlocks[blockElement] * 5 > minute){
blockElement++;
continue;
}
mins[blockElement] = 1;
minute -= fibBlocks[blockElement] * 5;
blockElement++;
}
blockElement = 0;
while(hour != 0){
if(fibBlocks[blockElement] > hour){
blockElement++;
continue;
}
hours[blockElement] = 1;
hour -= fibBlocks[blockElement];
blockElement++;
}
for(int i=0; i < 5; ++i){
*(res + i) = mins[i] + 2 * hours[i];
}
if((*(res + 3) == 3) && (*(res + 4) ==0)){
*(res + 3) = 2;
*(res + 4) = 1;
}
}
void drawRectangle(cairo_t* cr, rectangle R){
int color[3];
switch(R.color){
case 0:
color[0] = 1;
color[1] = 1;
color[2] = 1;
break;
case 1:
color[0] = 1;
color[1] = 0;
color[2] = 0;
break;
case 2:
color[0] = 0;
color[1] = 0;
color[2] = 1;
break;
case 3:
color[0] = 0;
color[1] = 1;
color[2] = 0;
break;
}
cairo_rectangle(cr, R.pos_x, R.pos_y, R.width, R.height); /* set rectangle */
cairo_set_source_rgb(cr, color[0], color[1], color[2]); /* set fill color */
cairo_fill(cr); /* fill rectangle */
}
void drawReminder(cairo_t* cr, int minRem){
float x_pos[4]={10, 810, 810, 10};
float y_pos[4]={10, 10, 510, 510};
int previous;
if(minRem == 0){
previous = 4;
} else {
previous = minRem - 1;
}
float radius=10;
for(int i = 0; i < 4; ++i){
cairo_arc(cr, x_pos[i], y_pos[i],10, 0, 2 * M_PI);
cairo_set_source_rgb(cr, 0.5, 0.5, 0.5); /* set fill color */
cairo_fill(cr);
}
cairo_arc(cr, x_pos[previous], y_pos[previous], 10, 0, 2 * M_PI);
cairo_set_source_rgb(cr, 0.5, 0.5, 0.5); /* set fill color */
cairo_fill(cr); cairo_fill(cr);
if(minRem != 0){
cairo_arc(cr, x_pos[minRem - 1], y_pos[minRem - 1], 7, 0, 2 * M_PI);
cairo_set_source_rgb(cr, 0, 0, 0); /* set fill color */
cairo_fill(cr);
}
}
void drawFibbTime(cairo_t* cr){
myTime t=getTime();
int colors[5];
getFibbTime(t, &colors);
rectangle Rs[5];
Rs[2].pos_x = 10;
Rs[2].pos_y = 10;
Rs[2].width = 195;
Rs[2].height = 195;
Rs[2].color = colors[2];
Rs[1].pos_x = 10;
Rs[1].pos_y = 210;
Rs[1].width = 295;
Rs[1].height = 295;
Rs[1].color = colors[1];
Rs[3].pos_x = 210;
Rs[3].pos_y = 10;
Rs[3].width = 95;
Rs[3].height = 95;
Rs[3].color = colors[3];
Rs[4].pos_x = 210;
Rs[4].pos_y = 110;
Rs[4].width = 95;
Rs[4].height = 95;
Rs[4].color = colors[4];
Rs[0].pos_x = 310;
Rs[0].pos_y = 10;
Rs[0].width = 495;
Rs[0].height = 495;
Rs[0].color = colors[0];
drawRectangle(cr, Rs[1]);
drawRectangle(cr, Rs[2]);
drawRectangle(cr, Rs[3]);
drawRectangle(cr, Rs[4]);
drawRectangle(cr, Rs[0]);
drawReminder(cr, (t.minute % 5));
} }
int main() { int main() {
int width;
int height;
Display *d = XOpenDisplay(NULL); Display *d = XOpenDisplay(NULL);
assert(d); assert(d);
@ -34,7 +200,8 @@ int main() {
DefaultVisual(d, s), DefaultVisual(d, s),
width, height); width, height);
cairo_t *cr = cairo_create(surf); cairo_t *cr = cairo_create(surf);
draw(cr); while(1){
drawFibbTime(cr);
XChangeProperty(d, /* connection to x server */ XChangeProperty(d, /* connection to x server */
w, /* window whose property we want to change */ w, /* window whose property we want to change */
//prop_root = XInternAtom(disp2, "_XROOTPMAP_ID", False); //prop_root = XInternAtom(disp2, "_XROOTPMAP_ID", False);
@ -49,6 +216,8 @@ int main() {
/* XSetWindowBackgroundPixmap(d, w, pix); // If we do not have compositor :) */ /* XSetWindowBackgroundPixmap(d, w, pix); // If we do not have compositor :) */
XClearWindow(d,w); XClearWindow(d,w);
XFlush(d); XFlush(d);
sleep(30);
}
/* cairo_destroy(cr); */ /* cairo_destroy(cr); */
/* cairo_surface_destroy(surf); */ /* cairo_surface_destroy(surf); */
/* XFreePixmap(d, pix); */ /* XFreePixmap(d, pix); */