reload_config() fix, fix default net graph args.
[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
35 void setup_fonts(void)
36 {
37         if ((output_methods & TO_X) == 0) {
38                 return;
39         }
40 #ifdef XFT
41         if (use_xft && !window.xftdraw) {
42                 window.xftdraw = XftDrawCreate(display, window.drawable,
43                                 DefaultVisual(display, screen), DefaultColormap(display, screen));
44         }
45 #endif
46 }
47
48 int add_font(const char *data_in)
49 {
50         if ((output_methods & TO_X) == 0) {
51                 return 0;
52         }
53         if (font_count > MAX_FONTS) {
54                 CRIT_ERR("you don't need that many fonts, sorry.");
55         }
56         font_count++;
57         if (font_count == 0) {
58                 if (fonts != NULL) {
59                         free(fonts);
60                 }
61                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
62                                 == NULL) {
63                         CRIT_ERR("malloc");
64                 }
65                 memset(fonts, 0, sizeof(struct font_list));
66         }
67         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
68         memset(&fonts[font_count], 0, sizeof(struct font_list));
69         if (fonts == NULL) {
70                 CRIT_ERR("realloc in add_font");
71         }
72         // must account for null terminator
73         if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
74                 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
75 #ifdef XFT
76                 fonts[font_count].font_alpha = 0xffff;
77 #endif
78         } else {
79                 CRIT_ERR("Oops...looks like something overflowed in add_font().");
80         }
81         return font_count;
82 }
83
84 void set_first_font(const char *data_in)
85 {
86         if ((output_methods & TO_X) == 0) {
87                 return;
88         }
89         if (font_count < 0) {
90                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
91                                 == NULL) {
92                         CRIT_ERR("malloc");
93                 }
94                 memset(fonts, 0, sizeof(struct font_list));
95                 font_count++;
96         }
97         if (strlen(data_in) > 1) {
98                 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
99 #ifdef XFT
100                 fonts[0].font_alpha = 0xffff;
101 #endif
102         }
103 }
104
105 void free_fonts(void)
106 {
107         int i;
108
109         if ((output_methods & TO_X) == 0) {
110                 return;
111         }
112         for (i = 0; i <= font_count; i++) {
113 #ifdef XFT
114                 if (use_xft) {
115                         XftFontClose(display, fonts[i].xftfont);
116                         fonts[i].xftfont = 0;
117                 } else
118 #endif /* XFT */
119                 {
120                         XFreeFont(display, fonts[i].font);
121                         fonts[i].font = 0;
122                 }
123         }
124         free(fonts);
125         fonts = 0;
126         font_count = -1;
127         selected_font = 0;
128 #ifdef XFT
129         if (window.xftdraw) {
130                 XftDrawDestroy(window.xftdraw);
131                 window.xftdraw = 0;
132         }
133 #endif /* XFT */
134 }
135
136 void load_fonts(void)
137 {
138         int i;
139
140         if ((output_methods & TO_X) == 0)
141                 return;
142         for (i = 0; i <= font_count; i++) {
143 #ifdef XFT
144                 /* load Xft font */
145                 if (use_xft && fonts[i].xftfont) {
146                         continue;
147                 } else if (use_xft) {
148                         fonts[i].xftfont = XftFontOpenName(display, screen,
149                                         fonts[i].name);
150                         if (fonts[i].xftfont) {
151                                 continue;
152                         }
153
154                         ERR("can't load Xft font '%s'", fonts[i].name);
155                         if ((fonts[i].xftfont = XftFontOpenName(display, screen,
156                                         "courier-12")) != NULL) {
157                                 continue;
158                         }
159
160                         ERR("can't load Xft font '%s'", "courier-12");
161
162                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
163                                 CRIT_ERR("can't load font '%s'", "fixed");
164                         }
165                         use_xft = 0;
166
167                         continue;
168                 }
169 #endif
170                 /* load normal font */
171                 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
172                         ERR("can't load font '%s'", fonts[i].name);
173                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
174                                 CRIT_ERR("can't load font '%s'", "fixed");
175                         }
176                 }
177         }
178 }