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