1/* swc: libswc/util.c
2 *
3 * Copyright (c) 2013, 2014 Michael Forney
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#ifndef SWC_UTIL_H
25#define SWC_UTIL_H
26
27#include "swc.h"
28
29#include <pixman.h>
30#include <stdbool.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/time.h>
35#include <wayland-util.h>
36
37#define EXPORT __attribute__((visibility("default")))
38
39#if ENABLE_DEBUG
40#define MESSAGE_SOURCE fprintf(stderr, "[swc:%s:%d] ", __FILE__, __LINE__);
41#else
42#define MESSAGE_SOURCE
43#endif
44
45#define MESSAGE(type, format, ...) \
46 do { \
47 MESSAGE_SOURCE \
48 fprintf(stderr, type ": " format, ##__VA_ARGS__); \
49 } while (false)
50
51#define WARNING(format, ...) MESSAGE("WARNING", format, ##__VA_ARGS__)
52#define ERROR(format, ...) MESSAGE("ERROR", format, ##__VA_ARGS__)
53
54#if ENABLE_DEBUG
55#define DEBUG(format, ...) MESSAGE("DEBUG", format, ##__VA_ARGS__)
56#else
57#define DEBUG(format, ...)
58#endif
59
60#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0])
61
62#define MIN(a, b) ((a) < (b) ? (a) : (b))
63#define MAX(a, b) ((a) > (b) ? (a) : (b))
64
65struct wl_resource;
66struct wl_client;
67
68void
69remove_resource(struct wl_resource *resource);
70void
71destroy_resource(struct wl_client *client, struct wl_resource *resource);
72
73static inline uint32_t
74get_time(void)
75{
76 struct timeval timeval;
77
78 gettimeofday(&timeval, NULL);
79 return timeval.tv_sec * 1000 + timeval.tv_usec / 1000;
80}
81
82extern pixman_box32_t infinite_extents;
83
84static inline bool
85rectangle_contains_point(const struct swc_rectangle *rectangle,
86 int32_t x,
87 int32_t y)
88{
89 return x > rectangle->x && x < rectangle->x + rectangle->width &&
90 y > rectangle->y && y < rectangle->y + rectangle->height;
91}
92
93static inline bool
94rectangle_overlap(const struct swc_rectangle *r1,
95 const struct swc_rectangle *r2)
96{
97 return (MAX(r1->x + r1->width, r2->x + r2->width) - MIN(r1->x, r2->x) <
98 r1->width + r2->width) &&
99 (MAX(r1->y + r1->height, r2->y + r2->height) - MIN(r1->y, r2->y) <
100 r1->height + r2->height);
101}
102
103static inline void
104array_remove(struct wl_array *array, void *item, size_t size)
105{
106 size_t bytes =
107 array->size - ((intptr_t)item + size - (intptr_t)array->data);
108 if (bytes > 0) {
109 memmove(item, (void *)((intptr_t)item + size), bytes);
110 }
111 array->size -= size;
112}
113
114#endif