diff --git a/inc/ztoolkit/log.h b/inc/ztoolkit/log.h new file mode 100644 index 0000000..d4b6643 --- /dev/null +++ b/inc/ztoolkit/log.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2020 Alexandros Theodotou + * + * This file is part of ZToolkit + * + * ZToolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ZToolkit 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU General Affero Public License + * along with ZToolkit. If not, see . + */ + +/** + * \file + * + * Logging utilities. + */ + +#ifndef __Z_TOOLKIT_LOG_H__ +#define __Z_TOOLKIT_LOG_H__ + +typedef enum ZtkLogLevel +{ + ZTK_LOG_LEVEL_DEBUG, + ZTK_LOG_LEVEL_MESSAGE, + ZTK_LOG_LEVEL_WARNING, + ZTK_LOG_LEVEL_ERROR, +} ZtkLogLevel; + +static int ztk_log_enabled = 1; + +/** + * Enables or disables logging. + */ +static inline void +ztk_log_set_enabled ( + int enabled) +{ + ztk_log_enabled = enabled; +} + +/** + * Logs a message. + * + * @param func Function name. + * @param level Log level. + * @param format The format of the message to log. + */ +void +ztk_log ( + const char * func, + ZtkLogLevel level, + const char * format, + ...); + +#define ztk_debug(msg) \ + ztk_log (__func__, ZTK_LOG_LEVEL_DEBUG, msg) + +#define ztk_message(msg) \ + ztk_log (__func__, ZTK_LOG_LEVEL_MESSAGE, msg) + +#define ztk_warning(msg) \ + ztk_log (__func__, ZTK_LOG_LEVEL_WARNING, msg) + +#define ztk_error(msg) \ + ztk_log (__func__, ZTK_LOG_LEVEL_ERROR, msg) + +#endif diff --git a/inc/ztoolkit/ztk_drawing_area.h b/inc/ztoolkit/ztk_drawing_area.h new file mode 100644 index 0000000..10d2fa4 --- /dev/null +++ b/inc/ztoolkit/ztk_drawing_area.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2020 Alexandros Theodotou + * + * This file is part of ZToolkit + * + * ZToolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ZToolkit 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU General Affero Public License + * along with ZToolkit. If not, see . + */ + +/** + * \file + * + * Widget for custom drawing. + */ + +#ifndef __Z_TOOLKIT_ZTK_DRAWING_AREA_H__ +#define __Z_TOOLKIT_ZTK_DRAWING_AREA_H__ + +#include "ztoolkit/ztk_widget.h" + +/** + * @addtogroup ztoolkit + * + * @{ + */ + +/** + * Drawing area widget. + * + * This is used for custom drawing. + */ +typedef struct ZtkDrawingArea +{ + /** Base widget. */ + ZtkWidget base; + +} ZtkDrawingArea; + +/** + * Creates a new ZtkDrawingArea. + * + * @param update_cb Optional callback to be called + * for updating the user data based on the current + * state of the widget. See ztk_knob.c for an + * example. + * @param draw_cb Draw callback. This will be called + * when the widget needs to redraw itself. + * @param dispose_cb Optional callback to be called + * when disposing the widget. This will be called + * right before freeing the widget. + * @param data User data. + */ +ZtkDrawingArea * +ztk_drawing_area_new ( + PuglRect * rect, + ZtkWidgetCallback update_cb, + ZtkWidgetCallback draw_cb, + ZtkWidgetCallback dispose_cb, + void * data); + +/** + * @} + */ + +#endif diff --git a/inc/ztoolkit/ztk_widget.h b/inc/ztoolkit/ztk_widget.h index 6d6ac64..ef86490 100644 --- a/inc/ztoolkit/ztk_widget.h +++ b/inc/ztoolkit/ztk_widget.h @@ -1,20 +1,20 @@ /* - * Copyright (C) 2019 Alexandros Theodotou + * Copyright (C) 2019-2020 Alexandros Theodotou * - * This file is part of ZPlugins + * This file is part of ZToolkit * - * ZPlugins is free software: you can redistribute it and/or modify + * ZToolkit is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * - * ZPlugins is distributed in the hope that it will be useful, + * ZToolkit 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 Affero General Public License for more details. * * You should have received a copy of the GNU General Affero Public License - * along with this program. If not, see . + * along with ZToolkit. If not, see . */ /** @@ -62,15 +62,26 @@ typedef enum ZtkWidgetState ZTK_WIDGET_STATE_PRESSED = 1 << 3, } ZtkWidgetState; -//typedef enum ZtkWidgetType -//{ - //ZTK_WIDGET_TYPE_KNOB, - //ZTK_WIDGET_TYPE_CUSTOM, -//} ZtkWidgetType; -// +typedef enum ZtkWidgetType +{ + ZTK_WIDGET_TYPE_NONE, + ZTK_WIDGET_TYPE_LABEL, + ZTK_WIDGET_TYPE_KNOB, + ZTK_WIDGET_TYPE_DRAWING_AREA, +} ZtkWidgetType; typedef struct ZtkWidget ZtkWidget; +/** + * Prototype for callbacks. + * + * @param widget The ZtkWidget instance. + * @param data User data passed when instantiating the drawing area. + */ +typedef void (*ZtkWidgetCallback) ( + ZtkWidget * widget, + void * data); + /** * Base widget. */ @@ -86,11 +97,8 @@ typedef struct ZtkWidget /** * The type this widget is. - * - * Custom widgets should be - * ZTK_WIDGET_TYPE_CUSTOM + increasing numbers. */ - //ZtkWidgetType type; + ZtkWidgetType type; /** Update callback (called right before drawing) * (required. */ @@ -147,6 +155,9 @@ typedef struct ZtkWidget cairo_t * cached_cr; cairo_surface_t * cached_surface; + /** User data. */ + void * user_data; + } ZtkWidget; /** @@ -172,6 +183,14 @@ ztk_widget_is_hit ( double x, double y); +/** + * Sets the user data. + */ +void +ztk_widget_set_user_data ( + ZtkWidget * self, + void * data); + /** * Draws the widget. */ diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..d2218cf --- /dev/null +++ b/src/log.c @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2020 Alexandros Theodotou + * + * This file is part of ZToolkit + * + * ZToolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ZToolkit 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU General Affero Public License + * along with ZToolkit. If not, see . + */ + +#include +#include +#include +#include +#include + +#include + +/* ANSI color codes */ +#define COLOR_RED "\x1b[31m" +#define COLOR_GREEN "\x1b[32m" +#define COLOR_YELLOW "\x1b[33m" +#define COLOR_BLUE "\x1b[34m" +#define COLOR_MAGENTA "\x1b[35m" +#define COLOR_CYAN "\x1b[36m" +#define COLOR_RESET "\x1b[0m" + +/** + * Fills in the timestamp as a string into \ref buf. + */ +static void +get_timestamp ( + char * buf) +{ + struct timeval curTime; + gettimeofday(&curTime, NULL); + int milli = curTime.tv_usec / 1000; + + char buffer [80]; + strftime ( + buffer, 80, "%H:%M:%S", + localtime (&curTime.tv_sec)); + + sprintf(buf, "%s:%03d", buffer, milli); +} + +/** + * Logs a message. + * + * @param func Function name. + * @param level Log level. + * @param format The format of the message to log. + */ +void +ztk_log ( + const char * func, + ZtkLogLevel level, + const char * format, + ...) +{ + va_list arg; + + char buf[6000] = "** "; + + switch (level) + { + case ZTK_LOG_LEVEL_DEBUG: + strcat ( + buf, COLOR_CYAN "DEBUG: " COLOR_RESET); + break; + case ZTK_LOG_LEVEL_MESSAGE: + strcat ( + buf, + COLOR_GREEN "MESSAGE: " COLOR_RESET); + break; + case ZTK_LOG_LEVEL_WARNING: + strcat ( + buf, + COLOR_YELLOW "WARNING: " COLOR_RESET); + break; + case ZTK_LOG_LEVEL_ERROR: + strcat ( + buf, COLOR_RED "ERROR: " COLOR_RESET); + break; + default: + break; + } + + /* get current time as string*/ + char cur_time[200]; + get_timestamp (cur_time); + + strcat (buf, cur_time); + strcat (buf, ": "); + + /* format message */ + char log_msg[6000]; + va_start (arg, format); + vsprintf (log_msg, format, arg); + va_end (arg); + + strcat (buf, log_msg); + fprintf (stderr, "%s\n", buf); +} diff --git a/src/meson.build b/src/meson.build index 5fd4d87..4b41063 100644 --- a/src/meson.build +++ b/src/meson.build @@ -16,6 +16,7 @@ # along with ZToolkit. If not, see . ztoolkit_srcs = files([ + 'log.c', 'ztk_app.c', 'ztk_color.c', 'ztk_knob.c', diff --git a/src/ztk_widget.c b/src/ztk_widget.c index 4c53706..3088cc1 100644 --- a/src/ztk_widget.c +++ b/src/ztk_widget.c @@ -1,20 +1,20 @@ /* - * Copyright (C) 2019 Alexandros Theodotou + * Copyright (C) 2019-2020 Alexandros Theodotou * - * This file is part of ZPlugins + * This file is part of ZToolkit * - * ZPlugins is free software: you can redistribute it and/or modify + * ZToolkit is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * - * ZPlugins is distributed in the hope that it will be useful, + * ZToolkit 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 Affero General Public License for more details. * * You should have received a copy of the GNU General Affero Public License - * along with this program. If not, see . + * along with ZToolkit. If not, see . */ #include "ztoolkit/ztk_widget.h" @@ -55,6 +55,17 @@ ztk_widget_is_hit ( y <= self->rect.y + self->rect.height; } +/** + * Sets the user data. + */ +void +ztk_widget_set_user_data ( + ZtkWidget * self, + void * data) +{ + self->user_data = data; +} + /** * Draws the widget. */ diff --git a/tests/helper.h b/tests/helper.h new file mode 100644 index 0000000..abf10aa --- /dev/null +++ b/tests/helper.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 Alexandros Theodotou + + * This file is part of ZToolkit + + * ZToolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * ZToolkit 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 Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with ZToolkit. If not, see . + */ + +#ifndef __Z_TOOLKIT_TESTS_HELPER_H__ +#define __Z_TOOLKIT_TESTS_HELPER_H__ + +#include +#include +#include +#include + +#include + +#define ztk_assert(x) \ + if (!(x)) \ + { \ + ztk_error ("Assertion failed: %s", #x); \ + exit (1); \ + } + +int +str_endswith ( + const char *s, const char *t) +{ + size_t ls = strlen(s); // find length of s + size_t lt = strlen(t); // find length of t + if (ls >= lt) // check if t can fit in s + { + // point s to where t should start and compare the strings from there + return (0 == memcmp(t, s + (ls - lt), lt)); + } + return 0; // t was longer than s +} + +#endif diff --git a/tests/log.c b/tests/log.c new file mode 100644 index 0000000..6d3cc5b --- /dev/null +++ b/tests/log.c @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2020 Alexandros Theodotou + * + * This file is part of ZToolkit + * + * ZToolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ZToolkit 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with ZToolkit. If not, see . + */ + +#include "helper.h" + +int main ( + int argc, const char* argv[]) +{ + ztk_debug ("debug test"); + ztk_message ("message test"); + ztk_warning ("warning test"); + ztk_error ("error test"); + + return 0; +} diff --git a/tests/meson.build b/tests/meson.build index 1c4f2ce..7494554 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -15,4 +15,9 @@ # You should have received a copy of the GNU Affero General Public License # along with ZToolkit. If not, see . -# TODO +e = executable ( + 'log_exe', 'log.c', + include_directories: inc_dirs, + link_with: ztoolkit_lib, + ) +test ('log_test', e)