Fix: free_fonts tries sometimes tries to free much
[monky] / src / fonts.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27 #include "conky.h"
28 #include "fonts.h"
29 #include "logging.h"
30
31 int selected_font = 0;
32 int font_count = -1;
33 struct font_list *fonts = NULL;
34 char fontloaded = 0;
35
36 void set_font(void)
37 {
38 #ifdef XFT
39         if (use_xft) return;
40 #endif /* XFT */
41         if (font_count > -1 && fonts[selected_font].font) {
42                 XSetFont(display, window.gc, fonts[selected_font].font->fid);
43         }
44 }
45
46 void setup_fonts(void)
47 {
48         if ((output_methods & TO_X) == 0) {
49                 return;
50         }
51 #ifdef XFT
52         if (use_xft) {
53                 if (window.xftdraw) {
54                         XftDrawDestroy(window.xftdraw);
55                 }
56                 window.xftdraw = XftDrawCreate(display, window.drawable,
57                                 DefaultVisual(display, screen), DefaultColormap(display, screen));
58         }
59 #endif /* XFT */
60         set_font();
61 }
62
63 int add_font(const char *data_in)
64 {
65         if ((output_methods & TO_X) == 0) {
66                 return 0;
67         }
68         if (font_count > MAX_FONTS) {
69                 CRIT_ERR("you don't need that many fonts, sorry.");
70         }
71         font_count++;
72         if (font_count == 0) {
73                 if (fonts != NULL) {
74                         free(fonts);
75                 }
76                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
77                                 == NULL) {
78                         CRIT_ERR("malloc");
79                 }
80                 memset(fonts, 0, sizeof(struct font_list));
81         }
82         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
83         memset(&fonts[font_count], 0, sizeof(struct font_list));
84         if (fonts == NULL) {
85                 CRIT_ERR("realloc in add_font");
86         }
87         // must account for null terminator
88         if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
89                 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
90 #ifdef XFT
91                 fonts[font_count].font_alpha = 0xffff;
92 #endif
93         } else {
94                 CRIT_ERR("Oops...looks like something overflowed in add_font().");
95         }
96         return font_count;
97 }
98
99 void set_first_font(const char *data_in)
100 {
101         if ((output_methods & TO_X) == 0) {
102                 return;
103         }
104         if (font_count < 0) {
105                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
106                                 == NULL) {
107                         CRIT_ERR("malloc");
108                 }
109                 memset(fonts, 0, sizeof(struct font_list));
110                 font_count++;
111         }
112         if (strlen(data_in) > 1) {
113                 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
114 #ifdef XFT
115                 fonts[0].font_alpha = 0xffff;
116 #endif
117         }
118 }
119
120 void free_fonts(void)
121 {
122         int i;
123
124         if ((output_methods & TO_X) == 0 || fontloaded == 0) {
125                 return;
126         }
127         for (i = 0; i <= font_count; i++) {
128 #ifdef XFT
129                 if (use_xft) {
130                         XftFontClose(display, fonts[i].xftfont);
131                         fonts[i].xftfont = 0;
132                 } else
133 #endif /* XFT */
134                 {
135                         XFreeFont(display, fonts[i].font);
136                         fonts[i].font = 0;
137                 }
138         }
139         free(fonts);
140         fonts = 0;
141         font_count = -1;
142         selected_font = 0;
143 #ifdef XFT
144         if (window.xftdraw) {
145                 XftDrawDestroy(window.xftdraw);
146                 window.xftdraw = 0;
147         }
148 #endif /* XFT */
149 }
150
151 void load_fonts(void)
152 {
153         int i;
154
155         if ((output_methods & TO_X) == 0)
156                 return;
157         for (i = 0; i <= font_count; i++) {
158 #ifdef XFT
159                 /* load Xft font */
160                 if (use_xft && fonts[i].xftfont) {
161                         continue;
162                 } else if (use_xft) {
163                         fonts[i].xftfont = XftFontOpenName(display, screen,
164                                         fonts[i].name);
165                         if (fonts[i].xftfont) {
166                                 continue;
167                         }
168
169                         ERR("can't load Xft font '%s'", fonts[i].name);
170                         if ((fonts[i].xftfont = XftFontOpenName(display, screen,
171                                         "courier-12")) != NULL) {
172                                 continue;
173                         }
174
175                         ERR("can't load Xft font '%s'", "courier-12");
176
177                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
178                                 CRIT_ERR("can't load font '%s'", "fixed");
179                         }
180                         use_xft = 0;
181
182                         continue;
183                 }
184 #endif
185                 /* load normal font */
186                 if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
187                         ERR("can't load font '%s'", fonts[i].name);
188                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
189                                 CRIT_ERR("can't load font '%s'", "fixed");
190                         }
191                 }
192         }
193         fontloaded = 1;
194 }