summaryrefslogtreecommitdiff
path: root/src/utils.h
blob: 5a47c44bd0f1eff5ad6e1fed44f5cc1e85e829fe (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
#pragma once

#include <wayland-client.h>
#include <wayland-egl.h>

#include <cstdint>
#include <cstdio>
#include <cstdlib>

#include "xdg-shell.h"

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

typedef float f32;
typedef double f64;

#if UINT64_MAX == UINTPTR_MAX
typedef u64 usize;
typedef i64 isize;
#elif UINT32_MAX == UINTPTR_MAX
typedef u32 usize;
typedef i32 isize;
#else
#error "Unsupported pointer size"
#endif

typedef struct wl_display* wl_display_t;
typedef struct wl_registry* wl_registry_t;
typedef struct wl_compositor* wl_compositor_t;
typedef struct wl_surface* wl_surface_t;

typedef struct xdg_wm_base* xdg_wm_base_t;
typedef struct xdg_surface* xdg_surface_t;
typedef struct xdg_toplevel* xdg_toplevel_t;

typedef struct wl_egl_window* wl_egl_window_t;

static inline u32 wayc_min(u32 a, u32 b) { return a > b ? a : b; }

[[noreturn]] static inline void wayc_panic_impl(const char* file, int line,
                                                const char* func,
                                                const char* fmt, ...) {
  fprintf(stderr, "Panic at %s:%d in %s: ", file, line, func);
  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, fmt, args);
  va_end(args);
  fprintf(stderr, "\n");
  exit(EXIT_FAILURE);
}

#define wayc_panic(...) \
  wayc_panic_impl(__FILE__, __LINE__, __func__, __VA_ARGS__)

#define wayc_assert(expr)                                   \
  do {                                                      \
    if (!(expr)) wayc_panic("Assertion failed: %s", #expr); \
  } while (0)

#define wayc_notnull(expr) wayc_assert(expr != nullptr)

#define WAYC_CONCAT_IMPL(a, b) a##b
#define WAYC_CONCAT(a, b) WAYC_CONCAT_IMPL(a, b)

#define WAYC_UNIQUE(base) WAYC_CONCAT(base, WAYC_CONCAT(__LINE__, __COUNTER__))
#define WAYC_FRAC_BITS 6
#define WAYC_SCALE (1 << WAYC_FRAC_BITS)

#define WAYC_X(v) ((v)[0])
#define WAYC_Y(v) ((v)[1])

#define WAYC_TRIANGLE_NVERTS 3

static inline usize wayc_max(usize a, usize b) { return a > b ? a : b; }

template <typename A, typename B>
struct is_same {
  static constexpr bool value = false;
};

template <typename A>
struct is_same<A, A> {
  static constexpr bool value = true;
};

template <typename Func>
struct defer_s {
  Func func;

  defer_s(Func func) : func(func) {}

  ~defer_s() { func(); }
};

#define wayc_defer(func) auto WAYC_UNIQUE(_defer_) = defer_s([&]() { func; })

template <typename Func, typename S>
struct defer_cond_s {
  Func func;
  S* signal;
  S expected;

  defer_cond_s(Func func, S* cond, S expected)
      : func(func), signal(cond), expected(expected) {}

  ~defer_cond_s() {
    if (*signal == expected) return;
    func();
  }
};

#define wayc_defer_cond(func, cond, expected) \
  auto WAYC_UNIQUE(_defer_) = defer_cond_s([&]() { func; }, &cond, expected)

struct string_s {
  char* data;
  usize length;
};

#define WAYC_STRING_INIT(str, len) \
  string_s { str, len }

static inline usize wayc_string_length(const struct string_s* str) {
  wayc_notnull(str);
  return str->length;
}

static inline char* wayc_string_data(const struct string_s* str) {
  wayc_notnull(str);
  return str->data;
}