-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmisc.c
162 lines (137 loc) · 4.94 KB
/
misc.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// misc.c - miscellaneous functions for lualock
// Copyright ©2011 Guff <cassmacguff@gmail.com>
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "misc.h"
cairo_surface_t* create_surface(gint width, gint height) {
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
width ? : gdk_screen_get_width(lualock.scr),
height ? : gdk_screen_get_height(lualock.scr));
return surface;
}
layer_t* create_layer(gint width, gint height) {
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
width,
height);
layer_t *layer = malloc(sizeof(layer_t));
layer->surface = surface;
layer->width = width;
layer->height = height;
layer->scale_x = 1;
layer->scale_y = 1;
layer->x = 0;
layer->y = 0;
layer->angle = 0;
layer->show = FALSE;
return layer;
}
void add_layer(layer_t *layer) {
g_ptr_array_add(lualock.layers, layer);
}
void remove_layer(layer_t *layer) {
g_ptr_array_remove(lualock.layers, layer);
layer_destroy(layer);
}
void replace_layer(layer_t *old_layer, layer_t *new_layer) {
for (guint i = 0; i < lualock.layers->len; i++) {
if (g_ptr_array_index(lualock.layers, i) == old_layer) {
layer_destroy(old_layer);
lualock.layers->pdata[i] = new_layer;
return;
}
}
}
void layer_destroy(layer_t *layer) {
cairo_surface_destroy(layer->surface);
free(layer);
}
void parse_color(const gchar *color, gdouble *r, gdouble *g, gdouble *b, gdouble *a) {
if (!color) {
*r = 0, *g = 0, *b = 0, *a = 1;
return;
}
gint a_dummy;
gchar *color_buf = g_strdup(color);
if (color_buf[0] == '#' && strlen(color_buf) == 9)
sscanf(color_buf + 7, "%2x", &a_dummy);
else
a_dummy = 1 << 16;
GdkColor color_gdk;
gdk_color_parse(color_buf, &color_gdk);
*r = color_gdk.red / (float) ((1 << 16) - 1);
*g = color_gdk.green / (float) ((1 << 16) - 1);
*b = color_gdk.blue / (float) ((1 << 16) - 1);
*a = a_dummy / (float) ((1 << 8) - 1);
g_free(color_buf);
}
gchar* get_password_mask(void) {
gchar password_mask[strlen(lualock.password) + 1];
for (guint i = 0; i < strlen(lualock.password); i++)
password_mask[i] = '*';
password_mask[strlen(lualock.password)] = '\0';
return strdup(password_mask);
}
void get_abs_pos(gdouble rel_x, gdouble rel_y, gdouble *x, gdouble *y) {
*x = rel_x >= 1.0 ? rel_x : rel_x * gdk_screen_get_width(lualock.scr);
*y = rel_y >= 1.0 ? rel_y : rel_y * gdk_screen_get_height(lualock.scr);
}
void get_abs_pos_for_dims(gdouble dim_w, gdouble dim_h, gdouble rel_w, gdouble rel_h,
gdouble *w, gdouble *h) {
*w = rel_w >= 1.0 ? rel_w : rel_w * dim_w;
*h = rel_h >= 1.0 ? rel_h : rel_h * dim_h;
}
void add_timer(guint id) {
g_array_append_val(lualock.timers, id);
}
void remove_timer(guint id) {
if (!id)
return;
for (guint i = 0; i < lualock.timers->len; i++) {
if (g_array_index(lualock.timers, guint, i) == id) {
g_source_remove(id);
g_array_remove_index(lualock.timers, i);
}
}
}
void clear_timers(void) {
for (guint i = 0; i < lualock.timers->len; i++)
g_source_remove(g_array_index(lualock.timers, guint, i));
if (lualock.timers->len)
g_array_remove_range(lualock.timers, 0, lualock.timers->len);
}
void register_update(gdouble x, gdouble y, gdouble w, gdouble h) {
cairo_rectangle_int_t rect = { x, y, w, h };
cairo_region_union_rectangle(lualock.updates_needed, &rect);
}
void register_update_for_layer(layer_t *layer) {
gdouble x, y, width, height;
x = layer->x, y = layer->y;
width = layer->width * layer->scale_x;
height = layer->height * layer->scale_y;
register_update(x, y, width, height);
}
void clear_updates(void) {
cairo_region_t *region = cairo_region_create();
cairo_region_intersect(lualock.updates_needed, region);
cairo_region_destroy(region);
}
void update_screen(void) {
register_update(0, 0, gdk_screen_get_width(lualock.scr),
gdk_screen_get_height(lualock.scr));
}