1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
32 #include "ctx->fonts.h"
35 void set_font(conky_context *ctx)
38 if (use_xft->ctx) return;
40 if (ctx->font_count > -1 && ctx->fonts[selected_font].font) {
41 XSetFont(display, window.gc, ctx->fonts[selected_font].font->fid);
45 void setup_fonts(conky_context *ctx)
47 if ((output_methods & TO_X) == 0) {
53 XftDrawDestroy(window.xftdraw);
56 window.xftdraw = XftDrawCreate(display, window.drawable,
57 DefaultVisual(display, screen), DefaultColormap(display, screen));
63 int add_font(conky_context *ctx, const char *data_in)
65 if ((output_methods & TO_X) == 0) {
68 if (ctx->font_count > MAX_FONTS) {
69 CRIT_ERR(NULL, NULL, "you don't need that many ctx->fonts, sorry.");
72 if (ctx->font_count == 0) {
73 if (ctx->fonts != NULL) {
76 if ((ctx->fonts = (struct font_list *) malloc(sizeof(struct font_list)))
78 CRIT_ERR(NULL, NULL, "malloc");
80 memset(ctx->fonts, 0, sizeof(struct font_list));
82 ctx->fonts = realloc(ctx->fonts, (sizeof(struct font_list) * (ctx->font_count + 1)));
83 memset(&ctx->fonts[ctx->font_count], 0, sizeof(struct font_list));
84 if (ctx->fonts == NULL) {
85 CRIT_ERR(NULL, NULL, "realloc in add_font");
87 // must account for null terminator
88 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
89 strncpy(ctx->fonts[ctx->font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
91 ctx->fonts[ctx->font_count].font_alpha = 0xffff;
94 CRIT_ERR(NULL, NULL, "Oops...looks like something overflowed in add_font().");
96 return ctx->font_count;
99 void set_first_font(conky_context *ctx, const char *data_in)
101 if ((output_methods & TO_X) == 0) {
104 if (ctx->font_count < 0) {
105 if ((ctx->fonts = (struct font_list *) malloc(sizeof(struct font_list)))
107 CRIT_ERR(NULL, NULL, "malloc");
109 memset(ctx->fonts, 0, sizeof(struct font_list));
112 if (strlen(data_in) > 1) {
113 strncpy(ctx->fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
115 ctx->fonts[0].font_alpha = 0xffff;
120 void free_fonts(conky_context *ctx)
124 if ((output_methods & TO_X) == 0) {
127 if(ctx->fontloaded == 0) {
131 for (i = 0; i <= ctx->font_count; i++) {
134 XftFontClose(display, ctx->fonts[i].xftfont);
135 ctx->fonts[i].xftfont = 0;
139 XFreeFont(display, ctx->fonts[i].font);
140 ctx->fonts[i].font = 0;
145 ctx->font_count = -1;
148 if (window.xftdraw) {
149 XftDrawDestroy(window.xftdraw);
155 void load_fonts(conky_context *ctx)
159 if ((output_methods & TO_X) == 0)
161 for (i = 0; i <= ctx->font_count; i++) {
164 if (use_xft->ctx && ctx->fonts[i].xftfont) {
166 } else if (use_xft->ctx) {
167 ctx->fonts[i].xftfont = XftFontOpenName(display, screen,
169 if (ctx->fonts[i].xftfont) {
173 NORM_ERR("can't load Xft font '%s'", ctx->fonts[i].name);
174 if ((ctx->fonts[i].xftfont = XftFontOpenName(display, screen,
175 "courier-12")) != NULL) {
179 NORM_ERR("can't load Xft font '%s'", "courier-12");
181 if ((ctx->fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
182 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
189 /* load normal font */
190 if (!ctx->fonts[i].font && (ctx->fonts[i].font = XLoadQueryFont(display, ctx->fonts[i].name)) == NULL) {
191 NORM_ERR("can't load font '%s'", ctx->fonts[i].name);
192 if ((ctx->fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
193 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");