2009-07-28 21:44:22 +00:00
|
|
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
2009-09-12 10:50:51 +00:00
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2009-02-22 16:53:30 +00:00
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2009-03-30 04:55:30 +00:00
|
|
|
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
|
2009-02-22 16:53:30 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "conky.h"
|
2009-08-07 07:21:56 +00:00
|
|
|
#include "fonts.h"
|
2009-02-22 16:53:30 +00:00
|
|
|
#include "logging.h"
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
int selected_font = 0;
|
|
|
|
int font_count = -1;
|
|
|
|
struct font_list *fonts = NULL;
|
|
|
|
char fontloaded = 0;
|
|
|
|
|
|
|
|
void set_font(void)
|
2009-06-10 20:08:39 +00:00
|
|
|
{
|
|
|
|
#ifdef XFT
|
2009-08-07 07:21:56 +00:00
|
|
|
if (use_xft) return;
|
2009-06-10 20:08:39 +00:00
|
|
|
#endif /* XFT */
|
2009-08-07 07:21:56 +00:00
|
|
|
if (font_count > -1 && fonts[selected_font].font) {
|
|
|
|
XSetFont(display, window.gc, fonts[selected_font].font->fid);
|
2009-06-10 20:08:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
void setup_fonts(void)
|
2009-02-22 16:53:30 +00:00
|
|
|
{
|
2009-05-24 23:49:50 +00:00
|
|
|
if ((output_methods & TO_X) == 0) {
|
2009-02-22 16:53:30 +00:00
|
|
|
return;
|
2009-05-24 23:49:50 +00:00
|
|
|
}
|
2009-02-22 16:53:30 +00:00
|
|
|
#ifdef XFT
|
2009-08-07 07:21:56 +00:00
|
|
|
if (use_xft) {
|
2009-06-10 00:39:26 +00:00
|
|
|
if (window.xftdraw) {
|
|
|
|
XftDrawDestroy(window.xftdraw);
|
2009-07-15 14:33:18 +00:00
|
|
|
window.xftdraw = 0;
|
2009-06-10 00:39:26 +00:00
|
|
|
}
|
2009-02-22 16:53:30 +00:00
|
|
|
window.xftdraw = XftDrawCreate(display, window.drawable,
|
2009-06-07 04:53:40 +00:00
|
|
|
DefaultVisual(display, screen), DefaultColormap(display, screen));
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-06-10 20:08:39 +00:00
|
|
|
#endif /* XFT */
|
|
|
|
set_font();
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
int add_font(const char *data_in)
|
2009-02-22 16:53:30 +00:00
|
|
|
{
|
2009-05-24 23:49:50 +00:00
|
|
|
if ((output_methods & TO_X) == 0) {
|
2009-02-22 16:53:30 +00:00
|
|
|
return 0;
|
2009-05-24 23:49:50 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
if (font_count > MAX_FONTS) {
|
|
|
|
CRIT_ERR(NULL, NULL, "you don't need that many fonts, sorry.");
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
font_count++;
|
|
|
|
if (font_count == 0) {
|
|
|
|
if (fonts != NULL) {
|
|
|
|
free(fonts);
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
|
2009-02-22 16:53:30 +00:00
|
|
|
== NULL) {
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "malloc");
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
memset(fonts, 0, sizeof(struct font_list));
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
|
|
|
|
memset(&fonts[font_count], 0, sizeof(struct font_list));
|
|
|
|
if (fonts == NULL) {
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "realloc in add_font");
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
// must account for null terminator
|
|
|
|
if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
|
2009-08-07 07:21:56 +00:00
|
|
|
strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
|
2009-02-22 16:53:30 +00:00
|
|
|
#ifdef XFT
|
2009-08-07 07:21:56 +00:00
|
|
|
fonts[font_count].font_alpha = 0xffff;
|
2009-02-22 16:53:30 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "Oops...looks like something overflowed in add_font().");
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
return font_count;
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
void set_first_font(const char *data_in)
|
2009-02-22 16:53:30 +00:00
|
|
|
{
|
2009-05-24 23:49:50 +00:00
|
|
|
if ((output_methods & TO_X) == 0) {
|
2009-02-22 16:53:30 +00:00
|
|
|
return;
|
2009-05-24 23:49:50 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
if (font_count < 0) {
|
|
|
|
if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
|
2009-02-22 16:53:30 +00:00
|
|
|
== NULL) {
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "malloc");
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
memset(fonts, 0, sizeof(struct font_list));
|
|
|
|
font_count++;
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
if (strlen(data_in) > 1) {
|
2009-08-07 07:21:56 +00:00
|
|
|
strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
|
2009-02-22 16:53:30 +00:00
|
|
|
#ifdef XFT
|
2009-08-07 07:21:56 +00:00
|
|
|
fonts[0].font_alpha = 0xffff;
|
2009-02-22 16:53:30 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
void free_fonts(void)
|
2009-02-22 16:53:30 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2009-07-13 13:04:57 +00:00
|
|
|
if ((output_methods & TO_X) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
if(fontloaded == 0) {
|
|
|
|
free(fonts);
|
2009-02-22 16:53:30 +00:00
|
|
|
return;
|
2009-05-24 23:49:50 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
for (i = 0; i <= font_count; i++) {
|
2009-02-22 16:53:30 +00:00
|
|
|
#ifdef XFT
|
2009-08-07 07:21:56 +00:00
|
|
|
if (use_xft) {
|
|
|
|
XftFontClose(display, fonts[i].xftfont);
|
|
|
|
fonts[i].xftfont = 0;
|
2009-02-22 16:53:30 +00:00
|
|
|
} else
|
2009-06-07 04:53:40 +00:00
|
|
|
#endif /* XFT */
|
2009-02-22 16:53:30 +00:00
|
|
|
{
|
2009-08-07 07:21:56 +00:00
|
|
|
XFreeFont(display, fonts[i].font);
|
|
|
|
fonts[i].font = 0;
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
free(fonts);
|
|
|
|
fonts = 0;
|
|
|
|
font_count = -1;
|
2009-02-22 16:53:30 +00:00
|
|
|
selected_font = 0;
|
2009-06-07 04:53:40 +00:00
|
|
|
#ifdef XFT
|
|
|
|
if (window.xftdraw) {
|
|
|
|
XftDrawDestroy(window.xftdraw);
|
|
|
|
window.xftdraw = 0;
|
|
|
|
}
|
|
|
|
#endif /* XFT */
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
void load_fonts(void)
|
2009-02-22 16:53:30 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((output_methods & TO_X) == 0)
|
|
|
|
return;
|
2009-08-07 07:21:56 +00:00
|
|
|
for (i = 0; i <= font_count; i++) {
|
2009-02-22 16:53:30 +00:00
|
|
|
#ifdef XFT
|
|
|
|
/* load Xft font */
|
2009-08-07 07:21:56 +00:00
|
|
|
if (use_xft && fonts[i].xftfont) {
|
2009-02-22 16:53:30 +00:00
|
|
|
continue;
|
2009-08-07 07:21:56 +00:00
|
|
|
} else if (use_xft) {
|
|
|
|
fonts[i].xftfont = XftFontOpenName(display, screen,
|
|
|
|
fonts[i].name);
|
|
|
|
if (fonts[i].xftfont) {
|
2009-02-22 16:53:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
NORM_ERR("can't load Xft font '%s'", fonts[i].name);
|
|
|
|
if ((fonts[i].xftfont = XftFontOpenName(display, screen,
|
2009-02-22 16:53:30 +00:00
|
|
|
"courier-12")) != NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("can't load Xft font '%s'", "courier-12");
|
2009-02-22 16:53:30 +00:00
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
use_xft = 0;
|
2009-02-22 16:53:30 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* load normal font */
|
2009-08-07 07:21:56 +00:00
|
|
|
if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
|
|
|
|
NORM_ERR("can't load font '%s'", fonts[i].name);
|
|
|
|
if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-07 07:21:56 +00:00
|
|
|
fontloaded = 1;
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|