2010-02-09 12:11:56 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
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
|
2010-01-01 23:46:17 +00:00
|
|
|
* Copyright (c) 2005-2010 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;
|
2010-09-08 15:30:04 +00:00
|
|
|
std::vector<font_list> fonts;
|
2009-08-07 07:21:56 +00:00
|
|
|
char fontloaded = 0;
|
|
|
|
|
2010-09-08 15:30:04 +00:00
|
|
|
void font_setting::lua_setter(lua::state &l, bool init)
|
|
|
|
{
|
|
|
|
lua::stack_sentry s(l, -2);
|
|
|
|
|
|
|
|
Base::lua_setter(l, init);
|
|
|
|
|
|
|
|
if(init && out_to_x.get(*state)) {
|
|
|
|
if(fonts.size() == 0)
|
|
|
|
fonts.resize(1);
|
|
|
|
fonts[0].name = do_convert(l, -1).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
|
|
|
font_setting font;
|
|
|
|
|
|
|
|
#ifdef BUILD_XFT
|
|
|
|
namespace {
|
|
|
|
class xftalpha_setting: public conky::simple_config_setting<float> {
|
|
|
|
typedef conky::simple_config_setting<float> Base;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void lua_setter(lua::state &l, bool init)
|
|
|
|
{
|
|
|
|
lua::stack_sentry s(l, -2);
|
|
|
|
|
|
|
|
Base::lua_setter(l, init);
|
|
|
|
|
|
|
|
if(init && out_to_x.get(*state)) {
|
|
|
|
fonts[0].font_alpha = do_convert(l, -1).first * 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
xftalpha_setting()
|
|
|
|
: Base("xftalpha", 1.0, false)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
xftalpha_setting xftalpha;
|
|
|
|
}
|
|
|
|
#endif /* BUILD_XFT */
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
void set_font(void)
|
2009-06-10 20:08:39 +00:00
|
|
|
{
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2010-04-21 17:33:39 +00:00
|
|
|
if (use_xft.get(*state)) return;
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2010-09-08 15:30:04 +00:00
|
|
|
if (fonts[selected_font].font) {
|
2009-08-07 07:21:56 +00:00
|
|
|
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
|
|
|
{
|
2010-02-25 21:28:34 +00:00
|
|
|
if (not out_to_x.get(*state)) {
|
2009-02-22 16:53:30 +00:00
|
|
|
return;
|
2009-05-24 23:49:50 +00:00
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2010-04-21 17:33:39 +00:00
|
|
|
if (use_xft.get(*state)) {
|
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,
|
2010-01-01 23:01:51 +00:00
|
|
|
window.visual, window.colourmap);
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2009-06-10 20:08:39 +00:00
|
|
|
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
|
|
|
{
|
2010-02-25 21:28:34 +00:00
|
|
|
if (not out_to_x.get(*state)) {
|
2009-02-22 16:53:30 +00:00
|
|
|
return 0;
|
2009-05-24 23:49:50 +00:00
|
|
|
}
|
2010-09-08 15:30:04 +00:00
|
|
|
fonts.push_back(font_list());
|
|
|
|
fonts.rbegin()->name = data_in;
|
2009-02-22 16:53:30 +00:00
|
|
|
|
2010-09-08 15:30:04 +00:00
|
|
|
return fonts.size()-1;
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:59:07 +00:00
|
|
|
void free_fonts(bool utf8) {
|
2010-02-25 21:28:34 +00:00
|
|
|
if (not out_to_x.get(*state)) {
|
2009-07-13 13:04:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-09-08 15:30:04 +00:00
|
|
|
for (size_t i = 0; i < fonts.size(); i++) {
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2010-04-21 17:33:39 +00:00
|
|
|
if (use_xft.get(*state)) {
|
2010-01-02 22:22:46 +00:00
|
|
|
/*
|
|
|
|
* Do we not need to close fonts with Xft? Unsure. Not freeing the
|
|
|
|
* fonts seems to incur a slight memory leak, but it also prevents
|
|
|
|
* a crash.
|
|
|
|
*
|
|
|
|
* XftFontClose(display, fonts[i].xftfont);
|
|
|
|
*/
|
2009-02-22 16:53:30 +00:00
|
|
|
} else
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2009-02-22 16:53:30 +00:00
|
|
|
{
|
2010-01-10 21:09:33 +00:00
|
|
|
if (fonts[i].font) {
|
|
|
|
XFreeFont(display, fonts[i].font);
|
|
|
|
}
|
2010-11-26 16:59:07 +00:00
|
|
|
if (utf8 && fonts[i].fontset) {
|
|
|
|
XFreeFontSet(display, fonts[i].fontset);
|
|
|
|
}
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-08 15:30:04 +00:00
|
|
|
fonts.clear();
|
2009-02-22 16:53:30 +00:00
|
|
|
selected_font = 0;
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2009-06-07 04:53:40 +00:00
|
|
|
if (window.xftdraw) {
|
|
|
|
XftDrawDestroy(window.xftdraw);
|
|
|
|
window.xftdraw = 0;
|
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:59:07 +00:00
|
|
|
void load_fonts(bool utf8) {
|
2010-02-25 21:28:34 +00:00
|
|
|
if (not out_to_x.get(*state))
|
2009-02-22 16:53:30 +00:00
|
|
|
return;
|
2010-10-31 17:17:22 +00:00
|
|
|
for (size_t i = 0; i < fonts.size(); i++) {
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2009-02-22 16:53:30 +00:00
|
|
|
/* load Xft font */
|
2010-04-21 17:33:39 +00:00
|
|
|
if (use_xft.get(*state)) {
|
|
|
|
if(not fonts[i].xftfont)
|
2010-09-08 15:30:04 +00:00
|
|
|
fonts[i].xftfont = XftFontOpenName(display, screen, fonts[i].name.c_str());
|
2010-04-21 17:33:39 +00:00
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
if (fonts[i].xftfont) {
|
2009-02-22 16:53:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-09-08 15:30:04 +00:00
|
|
|
NORM_ERR("can't load Xft font '%s'", fonts[i].name.c_str());
|
2009-08-07 07:21:56 +00:00
|
|
|
if ((fonts[i].xftfont = XftFontOpenName(display, screen,
|
2009-02-22 16:53:30 +00:00
|
|
|
"courier-12")) != NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-21 17:33:39 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "can't load Xft font '%s'", "courier-12");
|
2009-02-22 16:53:30 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-26 16:59:07 +00:00
|
|
|
if(utf8 && fonts[i].fontset == NULL) {
|
|
|
|
char** missing;
|
|
|
|
int missingnum;
|
|
|
|
char* missingdrawn;
|
|
|
|
fonts[i].fontset = XCreateFontSet(display, fonts[i].name.c_str(), &missing, &missingnum, &missingdrawn);
|
|
|
|
XFreeStringList(missing);
|
|
|
|
if(fonts[i].fontset == NULL) {
|
|
|
|
NORM_ERR("can't load font '%s'", fonts[i].name.c_str());
|
|
|
|
fonts[i].fontset = XCreateFontSet(display, "fixed", &missing, &missingnum, &missingdrawn);
|
|
|
|
if(fonts[i].fontset == NULL) {
|
|
|
|
CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 16:53:30 +00:00
|
|
|
/* load normal font */
|
2010-09-08 15:30:04 +00:00
|
|
|
if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name.c_str())) == NULL) {
|
|
|
|
NORM_ERR("can't load font '%s'", fonts[i].name.c_str());
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|