From f4453d104dcf0742af0949225cada72a6cc65d05 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 21 Jun 2017 12:10:07 -0400 Subject: Reorganize wlr-common --- CMakeLists.txt | 2 +- backend/CMakeLists.txt | 2 +- backend/backend.c | 2 +- backend/drm/backend.c | 4 +- backend/drm/drm.c | 2 +- backend/egl.c | 3 +- backend/libinput/backend.c | 2 +- backend/libinput/events.c | 4 +- backend/libinput/keyboard.c | 3 +- backend/libinput/pointer.c | 3 +- backend/libinput/tablet_pad.c | 3 +- backend/libinput/tablet_tool.c | 3 +- backend/libinput/touch.c | 3 +- backend/multi/backend.c | 3 +- backend/udev.c | 2 +- backend/wayland/backend.c | 8 ++-- backend/wayland/output.c | 2 +- backend/wayland/registry.c | 2 +- backend/wayland/wl_seat.c | 2 +- common/CMakeLists.txt | 6 --- common/list.c | 91 ------------------------------------------ common/log.c | 77 ----------------------------------- example/touch.c | 2 +- include/backend/drm.h | 2 +- include/backend/libinput.h | 4 +- include/backend/multi.h | 2 +- include/backend/wayland.h | 2 +- include/common/log.h | 27 ------------- include/wlr/common/list.h | 31 -------------- include/wlr/common/log.h | 19 --------- include/wlr/types/wlr_output.h | 2 +- include/wlr/util/list.h | 31 ++++++++++++++ include/wlr/util/log.h | 39 ++++++++++++++++++ render/gles3/renderer.c | 2 +- render/gles3/util.c | 2 +- session/CMakeLists.txt | 2 +- session/direct.c | 2 +- session/logind.c | 2 +- session/session.c | 2 +- types/CMakeLists.txt | 2 +- types/wlr_input_device.c | 3 +- types/wlr_keyboard.c | 1 - types/wlr_output.c | 2 +- types/wlr_pointer.c | 1 - types/wlr_tablet_pad.c | 1 - types/wlr_tablet_tool.c | 1 - types/wlr_touch.c | 1 - util/CMakeLists.txt | 6 +++ util/list.c | 91 ++++++++++++++++++++++++++++++++++++++++++ util/log.c | 77 +++++++++++++++++++++++++++++++++++ 50 files changed, 284 insertions(+), 304 deletions(-) delete mode 100644 common/CMakeLists.txt delete mode 100644 common/list.c delete mode 100644 common/log.c delete mode 100644 include/common/log.h delete mode 100644 include/wlr/common/list.h delete mode 100644 include/wlr/common/log.h create mode 100644 include/wlr/util/list.h create mode 100644 include/wlr/util/log.h create mode 100644 util/CMakeLists.txt create mode 100644 util/list.c create mode 100644 util/log.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5529ae32..ae2397b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,9 +58,9 @@ include(Manpage) include_directories(include) add_subdirectory(backend) -add_subdirectory(common) add_subdirectory(types) add_subdirectory(session) add_subdirectory(render) +add_subdirectory(util) add_subdirectory(example) diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 37cee892..5486b302 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -28,7 +28,7 @@ add_library(wlr-backend ) target_link_libraries(wlr-backend - wlr-common + wlr-util wlr-types ${WAYLAND_LIBRARIES} ${DRM_LIBRARIES} diff --git a/backend/backend.c b/backend/backend.c index e0c52a7e..a391b272 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -10,9 +10,9 @@ #include #include #include +#include #include "backend/libinput.h" #include "backend/udev.h" -#include "common/log.h" struct wlr_backend *wlr_backend_create(const struct wlr_backend_impl *impl, struct wlr_backend_state *state) { diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 1a8099c0..34c03eea 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -8,12 +8,12 @@ #include #include #include -#include #include #include +#include +#include #include "backend/udev.h" #include "backend/drm.h" -#include "common/log.h" static bool wlr_drm_backend_init(struct wlr_backend_state *state) { wlr_drm_scan_connectors(state); diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 8a9ae63f..e4790dec 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -14,8 +14,8 @@ #include #include #include +#include #include "backend/drm.h" -#include "common/log.h" static const char *conn_name[] = { [DRM_MODE_CONNECTOR_Unknown] = "Unknown", diff --git a/backend/egl.c b/backend/egl.c index 1ce2d8a7..f46a0c83 100644 --- a/backend/egl.c +++ b/backend/egl.c @@ -2,9 +2,8 @@ #include #include #include // GBM_FORMAT_XRGB8888 - +#include #include "backend/egl.h" -#include "common/log.h" const char *egl_error(void) { switch (eglGetError()) { diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index fedab148..a3523dc0 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -3,9 +3,9 @@ #include #include #include +#include #include "backend/udev.h" #include "backend/libinput.h" -#include "common/log.h" static int wlr_libinput_open_restricted(const char *path, int flags, void *_state) { diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 1f8f51c7..6cd33e3d 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -2,10 +2,10 @@ #include #include #include -#include #include +#include +#include #include "backend/libinput.h" -#include "common/log.h" struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index 26d4309c..f188b09e 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -4,9 +4,8 @@ #include #include #include -#include +#include #include "backend/libinput.h" -#include "common/log.h" struct wlr_keyboard_state { struct libinput_device *device; diff --git a/backend/libinput/pointer.c b/backend/libinput/pointer.c index d4d3aec0..436c826a 100644 --- a/backend/libinput/pointer.c +++ b/backend/libinput/pointer.c @@ -4,9 +4,8 @@ #include #include #include -#include +#include #include "backend/libinput.h" -#include "common/log.h" struct wlr_pointer *wlr_libinput_pointer_create( struct libinput_device *device) { diff --git a/backend/libinput/tablet_pad.c b/backend/libinput/tablet_pad.c index 3c34b643..fdb4775e 100644 --- a/backend/libinput/tablet_pad.c +++ b/backend/libinput/tablet_pad.c @@ -4,9 +4,8 @@ #include #include #include -#include +#include #include "backend/libinput.h" -#include "common/log.h" struct wlr_tablet_pad *wlr_libinput_tablet_pad_create( struct libinput_device *device) { diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c index 8cf2933b..e9e2951f 100644 --- a/backend/libinput/tablet_tool.c +++ b/backend/libinput/tablet_tool.c @@ -4,9 +4,8 @@ #include #include #include -#include +#include #include "backend/libinput.h" -#include "common/log.h" struct wlr_tablet_tool *wlr_libinput_tablet_tool_create( struct libinput_device *device) { diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c index d0048ff0..b19597a3 100644 --- a/backend/libinput/touch.c +++ b/backend/libinput/touch.c @@ -4,9 +4,8 @@ #include #include #include -#include +#include #include "backend/libinput.h" -#include "common/log.h" struct wlr_touch *wlr_libinput_touch_create( struct libinput_device *device) { diff --git a/backend/multi/backend.c b/backend/multi/backend.c index e3d87ccc..d33d137c 100644 --- a/backend/multi/backend.c +++ b/backend/multi/backend.c @@ -1,9 +1,8 @@ #include #include #include -#include +#include #include "backend/multi.h" -#include "common/log.h" struct subbackend_state { struct wlr_backend *backend; diff --git a/backend/udev.c b/backend/udev.c index c84b8e31..1fecaee4 100644 --- a/backend/udev.c +++ b/backend/udev.c @@ -9,8 +9,8 @@ #include #include #include +#include #include "backend/udev.h" -#include "common/log.h" /* Tests if 'path' is KMS compatible by trying to open it. * It leaves the open device in *fd_out it it succeeds. diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 88dd2ef1..a58c8691 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -1,14 +1,14 @@ #include #include -#include #include +#include +#include +#include #include #include #include +#include #include "backend/wayland.h" -#include "common/log.h" -#include -#include static int dispatch_events(int fd, uint32_t mask, void *data) { struct wlr_backend_state *state = data; diff --git a/backend/wayland/output.c b/backend/wayland/output.c index 551f9052..a01ff015 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -6,8 +6,8 @@ #include #include #include +#include #include "backend/wayland.h" -#include "common/log.h" static struct wl_callback_listener frame_listener; diff --git a/backend/wayland/registry.c b/backend/wayland/registry.c index 5859c2b4..ddd2a79b 100644 --- a/backend/wayland/registry.c +++ b/backend/wayland/registry.c @@ -2,8 +2,8 @@ #include #include #include +#include #include "backend/wayland.h" -#include "common/log.h" static void registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index a7505f94..5e6583ba 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -8,8 +8,8 @@ #include #include #include +#include #include "backend/wayland.h" -#include "common/log.h" static void wlr_wl_device_destroy(struct wlr_input_device_state *state) { free(state); diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt deleted file mode 100644 index d8d52681..00000000 --- a/common/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(wlr-common STATIC - list.c - log.c -) - -target_link_libraries(wlr-common m) diff --git a/common/list.c b/common/list.c deleted file mode 100644 index 095fbced..00000000 --- a/common/list.c +++ /dev/null @@ -1,91 +0,0 @@ -#include -#include -#include -#include -#include - -list_t *list_create(void) { - list_t *list = malloc(sizeof(list_t)); - list->capacity = 10; - list->length = 0; - list->items = malloc(sizeof(void*) * list->capacity); - return list; -} - -static void list_resize(list_t *list) { - if (list->length == list->capacity) { - list->capacity += 10; - list->items = realloc(list->items, sizeof(void*) * list->capacity); - } -} - -void list_free(list_t *list) { - if (list == NULL) { - return; - } - free(list->items); - free(list); -} - -void list_foreach(list_t *list, void (*callback)(void *item)) { - if (list == NULL || callback == NULL) { - return; - } - for (size_t i = 0; i < list->length; i++) { - callback(list->items[i]); - } -} - -void list_add(list_t *list, void *item) { - list_resize(list); - list->items[list->length++] = item; -} - -void list_push(list_t *list, void *item) { - list_add(list, item); -} - -void list_insert(list_t *list, size_t index, void *item) { - list_resize(list); - memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); - list->length++; - list->items[index] = item; -} - -void list_del(list_t *list, size_t index) { - list->length--; - memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); -} - -void *list_pop(list_t *list) { - void *_ = list->items[list->length - 1]; - list_del(list, list->length - 1); - return _; -} - -void *list_peek(list_t *list) { - return list->items[list->length - 1]; -} - -void list_cat(list_t *list, list_t *source) { - size_t i; - for (i = 0; i < source->length; ++i) { - list_add(list, source->items[i]); - } -} - -void list_qsort(list_t* list, int compare(const void *left, const void *right)) { - qsort(list->items, list->length, sizeof(void *), compare); -} - -int list_seq_find(list_t *list, - int compare(const void *item, const void *data), - const void *data) { - for (size_t i = 0; i < list->length; i++) { - void *item = list->items[i]; - if (compare(item, data) == 0) { - return i; - } - } - return -1; -} diff --git a/common/log.c b/common/log.c deleted file mode 100644 index 90eedf4c..00000000 --- a/common/log.c +++ /dev/null @@ -1,77 +0,0 @@ -#define _POSIX_C_SOURCE 1 -#include -#include -#include -#include -#include -#include -#include -#include "wlr/common/log.h" -#include "common/log.h" - -static bool colored = true; -static log_callback_t log_callback = wlr_log_stderr; - -static const char *verbosity_colors[] = { - [L_SILENT] = "", - [L_ERROR ] = "\x1B[1;31m", - [L_INFO ] = "\x1B[1;34m", - [L_DEBUG ] = "\x1B[1;30m", -}; - -void wlr_log_init(log_callback_t callback) { - log_callback = callback; -} - -void wlr_log_stderr(log_importance_t verbosity, const char *fmt, va_list args) { - // prefix the time to the log message - struct tm result; - time_t t = time(NULL); - struct tm *tm_info = localtime_r(&t, &result); - char buffer[26]; - - // generate time prefix - strftime(buffer, sizeof(buffer), "%F %T - ", tm_info); - fprintf(stderr, "%s", buffer); - - unsigned c = (verbosity < L_LAST) ? verbosity : L_LAST - 1; - - if (colored && isatty(STDERR_FILENO)) { - fprintf(stderr, "%s", verbosity_colors[c]); - } - - vfprintf(stderr, fmt, args); - - if (colored && isatty(STDERR_FILENO)) { - fprintf(stderr, "\x1B[0m"); - } - fprintf(stderr, "\n"); -} - -void _wlr_vlog(log_importance_t verbosity, const char *fmt, va_list args) { - log_callback(verbosity, fmt, args); -} - -void _wlr_log(log_importance_t verbosity, const char *fmt, ...) { - va_list args; - va_start(args, fmt); - log_callback(verbosity, fmt, args); - va_end(args); -} - -// strips the path prefix from filepath -// will try to strip WLR_SRC_DIR as well as a relative src dir -// e.g. '/src/build/wlroots/backend/wayland/backend.c' and -// '../backend/wayland/backend.c' will both be stripped to -// 'backend/wayland/backend.c' -const char *_strip_path(const char *filepath) { - static int srclen = strlen(WLR_SRC_DIR) + 1; - if(*filepath == '.') { - while(*filepath == '.' || *filepath == '/') { - ++filepath; - } - } else { - filepath += srclen; - } - return filepath; -} diff --git a/example/touch.c b/example/touch.c index 554c46c0..d2280385 100644 --- a/example/touch.c +++ b/example/touch.c @@ -11,12 +11,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include "shared.h" #include "cat.h" diff --git a/include/backend/drm.h b/include/backend/drm.h index a4995fa9..2c78e3d9 100644 --- a/include/backend/drm.h +++ b/include/backend/drm.h @@ -11,8 +11,8 @@ #include #include -#include #include +#include #include "backend/egl.h" #include "backend/udev.h" diff --git a/include/backend/libinput.h b/include/backend/libinput.h index f424053a..bb83c749 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -1,10 +1,10 @@ #ifndef _WLR_BACKEND_LIBINPUT_INTERNAL_H #define _WLR_BACKEND_LIBINPUT_INTERNAL_H #include -#include -#include #include +#include #include +#include #include "backend/udev.h" struct wlr_backend_state { diff --git a/include/backend/multi.h b/include/backend/multi.h index 5e6930bb..efaf61b9 100644 --- a/include/backend/multi.h +++ b/include/backend/multi.h @@ -3,7 +3,7 @@ #include #include -#include +#include struct wlr_backend_state { struct wlr_backend *backend; diff --git a/include/backend/wayland.h b/include/backend/wayland.h index 02ea3482..bf65ec50 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -4,9 +4,9 @@ #include #include #include -#include #include #include +#include #include "backend/egl.h" struct wlr_backend_state { diff --git a/include/common/log.h b/include/common/log.h deleted file mode 100644 index 98e57900..00000000 --- a/include/common/log.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef _WLR_INTERNAL_COMMON_LOG_H -#define _WLR_INTERNAL_COMMON_LOG_H -#include -#include -#include -#include - -#ifdef __GNUC__ -#define ATTRIB_PRINTF(start, end) __attribute__((format(printf, start, end))) -#else -#define ATTRIB_PRINTF(start, end) -#endif - -void _wlr_log(log_importance_t verbosity, const char *format, ...) ATTRIB_PRINTF(2, 3); -void _wlr_vlog(log_importance_t verbosity, const char *format, va_list args) ATTRIB_PRINTF(2, 0); -const char *_strip_path(const char *filepath); - -#define wlr_log(verb, fmt, ...) \ - _wlr_log(verb, "[%s:%d] " fmt, _strip_path(__FILE__), __LINE__, ##__VA_ARGS__) - -#define wlr_vlog(verb, fmt, args) \ - _wlr_vlog(verb, "[%s:%d] " fmt, _strip_path(__FILE__), __LINE__, args) - -#define wlr_log_errno(verb, fmt, ...) \ - wlr_log(verb, fmt ": %s", ##__VA_ARGS__, strerror(errno)) - -#endif diff --git a/include/wlr/common/list.h b/include/wlr/common/list.h deleted file mode 100644 index 2bc82570..00000000 --- a/include/wlr/common/list.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef _WLR_LIST_H -#define _WLR_LIST_H - -#include - -typedef struct { - size_t capacity; - size_t length; - void **items; -} list_t; - -list_t *list_create(void); -void list_free(list_t *list); -void list_foreach(list_t *list, void (*callback)(void* item)); -void list_add(list_t *list, void *item); -void list_push(list_t *list, void *item); -void list_insert(list_t *list, size_t index, void *item); -void list_del(list_t *list, size_t index); -void *list_pop(list_t *list); -void *list_peek(list_t *list); -void list_cat(list_t *list, list_t *source); -// See qsort. Remember to use *_qsort functions as compare functions, -// because they dereference the left and right arguments first! -void list_qsort(list_t *list, int compare(const void *left, const void *right)); -// Return index for first item in list that returns 0 for given compare -// function or -1 if none matches. -int list_seq_find(list_t *list, - int compare(const void *item, const void *cmp_to), - const void *cmp_to); - -#endif diff --git a/include/wlr/common/log.h b/include/wlr/common/log.h deleted file mode 100644 index c93f9652..00000000 --- a/include/wlr/common/log.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef _WLR_COMMON_LOG_H -#define _WLR_COMMON_LOG_H -#include -#include - -typedef enum { - L_SILENT = 0, - L_ERROR = 1, - L_INFO = 2, - L_DEBUG = 3, - L_LAST, -} log_importance_t; - -typedef void (*log_callback_t)(log_importance_t importance, const char *fmt, va_list args); - -void wlr_init_log(log_callback_t callback); -void wlr_log_stderr(log_importance_t verbosity, const char *fmt, va_list args); - -#endif diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 66216d3e..8ccf87e1 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -1,7 +1,7 @@ #ifndef _WLR_TYPES_OUTPUT_H #define _WLR_TYPES_OUTPUT_H #include -#include +#include #include struct wlr_output_mode_state; diff --git a/include/wlr/util/list.h b/include/wlr/util/list.h new file mode 100644 index 00000000..c82fbf7a --- /dev/null +++ b/include/wlr/util/list.h @@ -0,0 +1,31 @@ +#ifndef _WLR_UTIL_LIST_H +#define _WLR_UTIL_LIST_H + +#include + +typedef struct { + size_t capacity; + size_t length; + void **items; +} list_t; + +list_t *list_create(void); +void list_free(list_t *list); +void list_foreach(list_t *list, void (*callback)(void* item)); +void list_add(list_t *list, void *item); +void list_push(list_t *list, void *item); +void list_insert(list_t *list, size_t index, void *item); +void list_del(list_t *list, size_t index); +void *list_pop(list_t *list); +void *list_peek(list_t *list); +void list_cat(list_t *list, list_t *source); +// See qsort. Remember to use *_qsort functions as compare functions, +// because they dereference the left and right arguments first! +void list_qsort(list_t *list, int compare(const void *left, const void *right)); +// Return index for first item in list that returns 0 for given compare +// function or -1 if none matches. +int list_seq_find(list_t *list, + int compare(const void *item, const void *cmp_to), + const void *cmp_to); + +#endif diff --git a/include/wlr/util/log.h b/include/wlr/util/log.h new file mode 100644 index 00000000..2acaa2ed --- /dev/null +++ b/include/wlr/util/log.h @@ -0,0 +1,39 @@ +#ifndef _WLR_UTIL_LOG_H +#define _WLR_UTIL_LOG_H +#include +#include +#include +#include + +typedef enum { + L_SILENT = 0, + L_ERROR = 1, + L_INFO = 2, + L_DEBUG = 3, + L_LAST, +} log_importance_t; + +typedef void (*log_callback_t)(log_importance_t importance, const char *fmt, va_list args); + +void wlr_init_log(log_callback_t callback); + +#ifdef __GNUC__ +#define ATTRIB_PRINTF(start, end) __attribute__((format(printf, start, end))) +#else +#define ATTRIB_PRINTF(start, end) +#endif + +void _wlr_log(log_importance_t verbosity, const char *format, ...) ATTRIB_PRINTF(2, 3); +void _wlr_vlog(log_importance_t verbosity, const char *format, va_list args) ATTRIB_PRINTF(2, 0); +const char *_strip_path(const char *filepath); + +#define wlr_log(verb, fmt, ...) \ + _wlr_log(verb, "[%s:%d] " fmt, _strip_path(__FILE__), __LINE__, ##__VA_ARGS__) + +#define wlr_vlog(verb, fmt, args) \ + _wlr_vlog(verb, "[%s:%d] " fmt, _strip_path(__FILE__), __LINE__, args) + +#define wlr_log_errno(verb, fmt, ...) \ + wlr_log(verb, fmt ": %s", ##__VA_ARGS__, strerror(errno)) + +#endif diff --git a/render/gles3/renderer.c b/render/gles3/renderer.c index d029c0fb..4270bd14 100644 --- a/render/gles3/renderer.c +++ b/render/gles3/renderer.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "render/gles3.h" static struct { diff --git a/render/gles3/util.c b/render/gles3/util.c index 1f216182..a3aa87f3 100644 --- a/render/gles3/util.c +++ b/render/gles3/util.c @@ -1,7 +1,7 @@ #include #include #include -#include "common/log.h" +#include #include "render/gles3.h" const char *gles3_strerror(GLenum err) { diff --git a/session/CMakeLists.txt b/session/CMakeLists.txt index bd83068b..23077c12 100644 --- a/session/CMakeLists.txt +++ b/session/CMakeLists.txt @@ -8,7 +8,7 @@ set(sources ) set(libs - wlr-common + wlr-util ${WAYLAND_LIBRARIES} ) diff --git a/session/direct.c b/session/direct.c index c2b155e5..74f2faf1 100644 --- a/session/direct.c +++ b/session/direct.c @@ -7,7 +7,7 @@ #include #include #include -#include "common/log.h" +#include const struct session_impl session_direct; diff --git a/session/logind.c b/session/logind.c index 191791f3..6eb837ed 100644 --- a/session/logind.c +++ b/session/logind.c @@ -12,7 +12,7 @@ #include #include #include -#include "common/log.h" +#include enum { DRM_MAJOR = 226 }; diff --git a/session/session.c b/session/session.c index dbdd8156..9de93b1d 100644 --- a/session/session.c +++ b/session/session.c @@ -2,7 +2,7 @@ #include #include #include -#include "common/log.h" +#include extern const struct session_impl session_logind; extern const struct session_impl session_direct; diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 58f8a651..72d885c9 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -14,6 +14,6 @@ add_library(wlr-types ) target_link_libraries(wlr-types - wlr-common + wlr-util ${WAYLAND_LIBRARIES} ) diff --git a/types/wlr_input_device.c b/types/wlr_input_device.c index 3a9611cc..f146c186 100644 --- a/types/wlr_input_device.c +++ b/types/wlr_input_device.c @@ -9,8 +9,7 @@ #include #include #include -#include -#include "common/log.h" +#include struct wlr_input_device *wlr_input_device_create( enum wlr_input_device_type type, diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index bd6718a7..26dd2977 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -3,7 +3,6 @@ #include #include #include -#include struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl, struct wlr_keyboard_state *state) { diff --git a/types/wlr_output.c b/types/wlr_output.c index f639a2c4..6daac31c 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include static const float transforms[][4] = { [WL_OUTPUT_TRANSFORM_NORMAL] = { diff --git a/types/wlr_pointer.c b/types/wlr_pointer.c index 2c9b8735..fc1ae808 100644 --- a/types/wlr_pointer.c +++ b/types/wlr_pointer.c @@ -3,7 +3,6 @@ #include #include #include -#include struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, struct wlr_pointer_state *state) { diff --git a/types/wlr_tablet_pad.c b/types/wlr_tablet_pad.c index 97c53bb4..80c6187d 100644 --- a/types/wlr_tablet_pad.c +++ b/types/wlr_tablet_pad.c @@ -3,7 +3,6 @@ #include #include #include -#include struct wlr_tablet_pad *wlr_tablet_pad_create(struct wlr_tablet_pad_impl *impl, struct wlr_tablet_pad_state *state) { diff --git a/types/wlr_tablet_tool.c b/types/wlr_tablet_tool.c index cca3e31d..1e939c3b 100644 --- a/types/wlr_tablet_tool.c +++ b/types/wlr_tablet_tool.c @@ -3,7 +3,6 @@ #include #include #include -#include struct wlr_tablet_tool *wlr_tablet_tool_create(struct wlr_tablet_tool_impl *impl, struct wlr_tablet_tool_state *state) { diff --git a/types/wlr_touch.c b/types/wlr_touch.c index a6b7d9ea..e698decd 100644 --- a/types/wlr_touch.c +++ b/types/wlr_touch.c @@ -3,7 +3,6 @@ #include #include #include -#include struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, struct wlr_touch_state *state) { diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt new file mode 100644 index 00000000..5a057889 --- /dev/null +++ b/util/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(wlr-util STATIC + list.c + log.c +) + +target_link_libraries(wlr-util m) diff --git a/util/list.c b/util/list.c new file mode 100644 index 00000000..94159036 --- /dev/null +++ b/util/list.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +list_t *list_create(void) { + list_t *list = malloc(sizeof(list_t)); + list->capacity = 10; + list->length = 0; + list->items = malloc(sizeof(void*) * list->capacity); + return list; +} + +static void list_resize(list_t *list) { + if (list->length == list->capacity) { + list->capacity += 10; + list->items = realloc(list->items, sizeof(void*) * list->capacity); + } +} + +void list_free(list_t *list) { + if (list == NULL) { + return; + } + free(list->items); + free(list); +} + +void list_foreach(list_t *list, void (*callback)(void *item)) { + if (list == NULL || callback == NULL) { + return; + } + for (size_t i = 0; i < list->length; i++) { + callback(list->items[i]); + } +} + +void list_add(list_t *list, void *item) { + list_resize(list); + list->items[list->length++] = item; +} + +void list_push(list_t *list, void *item) { + list_add(list, item); +} + +void list_insert(list_t *list, size_t index, void *item) { + list_resize(list); + memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); + list->length++; + list->items[index] = item; +} + +void list_del(list_t *list, size_t index) { + list->length--; + memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); +} + +void *list_pop(list_t *list) { + void *_ = list->items[list->length - 1]; + list_del(list, list->length - 1); + return _; +} + +void *list_peek(list_t *list) { + return list->items[list->length - 1]; +} + +void list_cat(list_t *list, list_t *source) { + size_t i; + for (i = 0; i < source->length; ++i) { + list_add(list, source->items[i]); + } +} + +void list_qsort(list_t* list, int compare(const void *left, const void *right)) { + qsort(list->items, list->length, sizeof(void *), compare); +} + +int list_seq_find(list_t *list, + int compare(const void *item, const void *data), + const void *data) { + for (size_t i = 0; i < list->length; i++) { + void *item = list->items[i]; + if (compare(item, data) == 0) { + return i; + } + } + return -1; +} diff --git a/util/log.c b/util/log.c new file mode 100644 index 00000000..760ef18a --- /dev/null +++ b/util/log.c @@ -0,0 +1,77 @@ +#define _POSIX_C_SOURCE 1 +#include +#include +#include +#include +#include +#include +#include +#include + +static bool colored = true; + +static const char *verbosity_colors[] = { + [L_SILENT] = "", + [L_ERROR ] = "\x1B[1;31m", + [L_INFO ] = "\x1B[1;34m", + [L_DEBUG ] = "\x1B[1;30m", +}; + +void wlr_log_stderr(log_importance_t verbosity, const char *fmt, va_list args) { + // prefix the time to the log message + struct tm result; + time_t t = time(NULL); + struct tm *tm_info = localtime_r(&t, &result); + char buffer[26]; + + // generate time prefix + strftime(buffer, sizeof(buffer), "%F %T - ", tm_info); + fprintf(stderr, "%s", buffer); + + unsigned c = (verbosity < L_LAST) ? verbosity : L_LAST - 1; + + if (colored && isatty(STDERR_FILENO)) { + fprintf(stderr, "%s", verbosity_colors[c]); + } + + vfprintf(stderr, fmt, args); + + if (colored && isatty(STDERR_FILENO)) { + fprintf(stderr, "\x1B[0m"); + } + fprintf(stderr, "\n"); +} + +static log_callback_t log_callback = wlr_log_stderr; + +void wlr_log_init(log_callback_t callback) { + log_callback = callback; +} + +void _wlr_vlog(log_importance_t verbosity, const char *fmt, va_list args) { + log_callback(verbosity, fmt, args); +} + +void _wlr_log(log_importance_t verbosity, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + log_callback(verbosity, fmt, args); + va_end(args); +} + +// strips the path prefix from filepath +// will try to strip WLR_SRC_DIR as well as a relative src dir +// e.g. '/src/build/wlroots/backend/wayland/backend.c' and +// '../backend/wayland/backend.c' will both be stripped to +// 'backend/wayland/backend.c' +const char *_strip_path(const char *filepath) { + static int srclen = strlen(WLR_SRC_DIR) + 1; + if(*filepath == '.') { + while(*filepath == '.' || *filepath == '/') { + ++filepath; + } + } else { + filepath += srclen; + } + return filepath; +} -- cgit v1.2.3