Fix a lot of unimportant memleaks caused by a CRIT_ERR
[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                         window.xftdraw = 0;
56                 }
57                 window.xftdraw = XftDrawCreate(display, window.drawable,
58                                 DefaultVisual(display, screen), DefaultColormap(display, screen));
59         }
60 #endif /* XFT */
61         set_font();
62 }
63
64 int add_font(const char *data_in)
65 {
66         if ((output_methods & TO_X) == 0) {
67                 return 0;
68         }
69         if (font_count > MAX_FONTS) {
70                 CRIT_ERR(NULL, NULL, "you don't need that many fonts, sorry.");
71         }
72         font_count++;
73         if (font_count == 0) {
74                 if (fonts != NULL) {
75                         free(fonts);
76                 }
77                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
78                                 == NULL) {
79                         CRIT_ERR(NULL, NULL, "malloc");
80                 }
81                 memset(fonts, 0, sizeof(struct font_list));
82         }
83         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
84         memset(&fonts[font_count], 0, sizeof(struct font_list));
85         if (fonts == NULL) {
86                 CRIT_ERR(NULL, NULL, "realloc in add_font");
87         }
88         // must account for null terminator
89         if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
90                 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
91 #ifdef XFT
92                 fonts[font_count].font_alpha = 0xffff;
93 #endif
94         } else {
95                 CRIT_ERR(NULL, NULL, "Oops...looks like something overflowed in add_font().");
96         }
97         return font_count;
98 }
99
100 void set_first_font(const char *data_in)
101 {
102         if ((output_methods & TO_X) == 0) {
103                 return;
104         }
105         if (font_count < 0) {
106                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
107                                 == NULL) {
108                         CRIT_ERR(NULL, NULL, "malloc");
109                 }
110                 memset(fonts, 0, sizeof(struct font_list));
111                 font_count++;
112         }
113         if (strlen(data_in) > 1) {
114                 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
115 #ifdef XFT
116                 fonts[0].font_alpha = 0xffff;
117 #endif
118         }
119 }
120
121 void free_fonts(void)
122 {
123         int i;
124
125         if ((output_methods & TO_X) == 0) {
126                 return;
127         }
128         if(fontloaded == 0) {
129                 free(fonts);
130                 return;
131         }
132         for (i = 0; i <= font_count; i++) {
133 #ifdef XFT
134                 if (use_xft) {
135                         XftFontClose(display, fonts[i].xftfont);
136                         fonts[i].xftfont = 0;
137                 } else
138 #endif /* XFT */
139                 {
140                         XFreeFont(display, fonts[i].font);
141                         fonts[i].font = 0;
142                 }
143         }
144         free(fonts);
145         fonts = 0;
146         font_count = -1;
147         selected_font = 0;
148 #ifdef XFT
149         if (window.xftdraw) {
150                 XftDrawDestroy(window.xftdraw);
151                 window.xftdraw = 0;
152         }
153 #endif /* XFT */
154 }
155
156 void load_fonts(void)
157 {
158         int i;
159
160         if ((output_methods & TO_X) == 0)
161                 return;
162         for (i = 0; i <= font_count; i++) {
163 #ifdef XFT
164                 /* load Xft font */
165                 if (use_xft && fonts[i].xftfont) {
166                         continue;
167                 } else if (use_xft) {
168                         fonts[i].xftfont = XftFontOpenName(display, screen,
169                                         fonts[i].name);
170                         if (fonts[i].xftfont) {
171                                 continue;
172                         }
173
174                         ERR("can't load Xft font '%s'", fonts[i].name);
175                         if ((fonts[i].xftfont = XftFontOpenName(display, screen,
176                                         "courier-12")) != NULL) {
177                                 continue;
178                         }
179
180                         ERR("can't load Xft font '%s'", "courier-12");
181
182                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
183                                 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
184                         }
185                         use_xft = 0;
186
187                         continue;
188                 }
189 #endif
190                 /* load normal font */
191                 if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
192                         ERR("can't load font '%s'", fonts[i].name);
193                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
194                                 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
195                         }
196                 }
197         }
198         fontloaded = 1;
199 }