-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotif.c
185 lines (156 loc) · 4.36 KB
/
notif.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "notif.h"
#include "config.h"
#include <stdlib.h>
#include <string.h>
struct _word {
char* word;
uint32_t length;
xcb_query_text_extents_cookie_t cookie;
};
static xcb_char2b_t* to_char2b(char* text)
{
xcb_char2b_t* ret;
uint32_t i;
ret = malloc(strlen(text) * sizeof(xcb_char2b_t));
for(i = 0; i < strlen(text); ++i) {
ret[i].byte1 = text[i];
ret[i].byte2 = 0;
}
return ret;
}
static struct _word* cut_in_words(xcb_connection_t* c, char* text, xcb_font_t font)
{
struct _word* words = malloc(10 * sizeof(struct _word));
uint32_t size = 10;
uint32_t i = 0;
char* word;
xcb_char2b_t* xcbword;
xcb_query_text_extents_reply_t* reply;
word = strtok(text, " ");
while(word) {
words[i].word = word;
words[i].length = 0;
xcbword = to_char2b(word);
words[i].cookie = xcb_query_text_extents(c, font, strlen(word), xcbword);
free(xcbword);
word = strtok(NULL, " ");
++i;
if(i >= size) {
size += 10;
words = realloc(words, sizeof(struct _word) * size);
}
}
words[i].word = NULL;
i = 0;
while(words[i].word) {
reply = xcb_query_text_extents_reply(c, words[i].cookie, NULL);
words[i].length = reply->overall_width;
free(reply);
++i;
}
return words;
}
static uint32_t space_length(xcb_connection_t* c, xcb_font_t font)
{
xcb_query_text_extents_reply_t* reply;
xcb_query_text_extents_cookie_t cookie;
uint32_t space;
xcb_char2b_t sp;
sp.byte1 = ' ';
sp.byte1 = 0;
cookie = xcb_query_text_extents(c, font, 1, &sp);
reply = xcb_query_text_extents_reply(c, cookie, NULL);
space = reply->overall_width;
free(reply);
return space;
}
static void add_line(srv_notif_t* notif, struct _word* words, uint32_t beg, uint32_t end, uint32_t line)
{
uint32_t length, j;
char* aline;
length = 0;
for(j = beg; j < end; ++j)
length += strlen(words[j].word) + 1;
notif->lines[line] = malloc(length);
aline = notif->lines[line];
for(j = beg; j < end; ++j) {
strcpy(aline, words[j].word);
aline += strlen(words[j].word);
aline[0] = ' ';
++aline;
}
--aline;
aline[0] = '\0';
}
srv_notif_t* create_notif(xcb_connection_t* c, srv_screen_t* scr, uint32_t y, const char* name, const char* text)
{
struct _word* words;
char* string;
char buffer[256];
uint32_t width, height, x;
srv_gcontext_t gc;
uint32_t i, size, last_i, length, line;
uint32_t space;
srv_notif_t* notif;
if(!get_gcontext(name, &gc))
return NULL;
notif = malloc(sizeof(srv_notif_t));
notif->gc = gc;
string = malloc(strlen(text) + 1);
strcpy(string, text);
words = cut_in_words(c, string, gc.font);
space = space_length(c, gc.font);
snprintf(buffer, 256, "%s.width", name);
width = 200;
if(has_entry(buffer))
width = get_int(buffer);
else if(has_entry("global.width"))
width = get_int("global.width");
width -= gc.width * 2;
size = 10;
notif->lines = malloc(sizeof(char*) * size);
last_i = length = 0;
line = 0;
for(i = 0; words[i].word; ++i) {
length += words[i].length;
if(length >= width) {
add_line(notif, words, last_i, i, line);
last_i = i;
++line;
length = words[i].length;
}
if(length != 0)
length += space;
if(line >= size) {
size += 10;
notif->lines = realloc(notif->lines, size);
}
}
add_line(notif, words, last_i, i, line);
++line;
if(line >= size) {
size += 1;
notif->lines = realloc(notif->lines, size);
}
notif->lines[line] = NULL;
height = line * gc.font_height + gc.width * 2;
width += gc.width * 2;
x = scr->w - width - 50;
notif->win = open_window(c, scr->xcbscr, x, y, width, height, name);
free(string);
free(words);
return notif;
}
void draw_notif(xcb_connection_t* c, srv_notif_t* notif)
{
display_notif(c, ¬if->win, notif->gc, (const char**)notif->lines);
}
void free_notif(xcb_connection_t* c, srv_notif_t* notif)
{
uint32_t i;
for(i = 0; notif->lines[i]; ++i)
free(notif->lines[i]);
free(notif->lines);
close_window(c, notif->win);
free(notif);
}