-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.c
50 lines (44 loc) · 1.37 KB
/
util.c
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
// Copyright 2020 - 2024, project-repo and the cagebreak contributors
// SPDX-License-Identifier: MIT
#include <wlr/util/box.h>
#include "util.h"
#include <math.h>
#include <stdlib.h>
int
scale_length(int length, int offset, double scale) {
/**
* One does not simply multiply the width by the scale. We allow fractional
* scaling, which means the resulting scaled width might be a decimal.
* So we round it.
*
* But even this can produce undesirable results depending on the X or Y
* offset of the box. For example, with a scale of 1.5, a box with
* width=1 should not scale to 2px if its X coordinate is 1, because the
* X coordinate would have scaled to 2px.
*/
return (int)(round((offset + length) * scale) - round(offset * scale));
}
void
scale_box(struct wlr_box *box, double scale) {
box->width = scale_length(box->width, box->x, scale);
box->height = scale_length(box->height, box->y, scale);
box->x = (int)round(box->x * scale);
box->y = (int)round(box->y * scale);
}
char *
malloc_vsprintf_va_list(const char *fmt, va_list ap) {
va_list ap2;
va_copy(ap2, ap);
int len = vsnprintf(NULL, 0, fmt, ap);
char *ret = malloc(sizeof(char) * (len + 1));
vsnprintf(ret, len + 1, fmt, ap2);
va_end(ap2);
return ret;
}
char *
malloc_vsprintf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char *ret = malloc_vsprintf_va_list(fmt, args);
return ret;
}