2010-01-04 17:06:14 +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-10-06 23:04:32 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* 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-10-06 23:04:32 +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"
|
|
|
|
#include "core.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "specials.h"
|
|
|
|
#include "text_object.h"
|
2010-02-25 21:28:34 +00:00
|
|
|
#include "x11.h"
|
2010-01-04 17:06:14 +00:00
|
|
|
#include <vector>
|
2009-10-06 23:04:32 +00:00
|
|
|
|
2010-02-17 16:49:03 +00:00
|
|
|
#define SCROLL_LEFT true
|
|
|
|
#define SCROLL_RIGHT false
|
|
|
|
|
2009-10-06 23:04:32 +00:00
|
|
|
struct scroll_data {
|
|
|
|
char *text;
|
|
|
|
unsigned int show;
|
|
|
|
unsigned int step;
|
2010-02-17 16:49:03 +00:00
|
|
|
signed int start;
|
2009-10-06 23:04:32 +00:00
|
|
|
long resetcolor;
|
2010-02-17 16:49:03 +00:00
|
|
|
bool direction;
|
2009-10-06 23:04:32 +00:00
|
|
|
};
|
|
|
|
|
2010-02-15 15:02:29 +00:00
|
|
|
void parse_scroll_arg(struct text_object *obj, const char *arg, void *free_at_crash, char *free_at_crash2)
|
2009-10-06 23:04:32 +00:00
|
|
|
{
|
2009-10-06 23:04:32 +00:00
|
|
|
struct scroll_data *sd;
|
2009-10-06 23:04:32 +00:00
|
|
|
int n1 = 0, n2 = 0;
|
2010-02-17 16:49:03 +00:00
|
|
|
char dirarg[6];
|
2009-10-06 23:04:32 +00:00
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
sd = (struct scroll_data *)malloc(sizeof(struct scroll_data));
|
2009-10-06 23:04:32 +00:00
|
|
|
memset(sd, 0, sizeof(struct scroll_data));
|
|
|
|
|
|
|
|
sd->resetcolor = get_current_text_color();
|
|
|
|
sd->step = 1;
|
2010-02-17 16:49:03 +00:00
|
|
|
sd->direction = SCROLL_LEFT;
|
|
|
|
|
|
|
|
if (arg && sscanf(arg, "%5s %n", dirarg, &n1) == 1) {
|
|
|
|
if (strcasecmp(dirarg, "right") == 0 || strcasecmp(dirarg, "r") == 0)
|
|
|
|
sd->direction = SCROLL_RIGHT;
|
|
|
|
else if ( strcasecmp(dirarg, "left") != 0 && strcasecmp(dirarg, "l") != 0)
|
|
|
|
n1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!arg || sscanf(arg + n1, "%u %n", &sd->show, &n2) <= 0) {
|
2010-02-15 15:02:29 +00:00
|
|
|
free(sd);
|
|
|
|
#ifdef BUILD_X11
|
|
|
|
free(obj->next);
|
|
|
|
#endif
|
|
|
|
free(free_at_crash2);
|
2010-02-17 16:49:03 +00:00
|
|
|
CRIT_ERR(obj, free_at_crash, "scroll needs arguments: [left|right] <length> [<step>] <text>");
|
2010-02-15 15:02:29 +00:00
|
|
|
}
|
2010-02-17 16:49:03 +00:00
|
|
|
n1 += n2;
|
2009-10-06 23:04:32 +00:00
|
|
|
|
2010-02-17 16:49:03 +00:00
|
|
|
if(sscanf(arg + n1, "%u %n", &sd->step, &n2) == 1) {
|
2009-10-06 23:04:32 +00:00
|
|
|
n1 += n2;
|
|
|
|
} else {
|
2009-10-06 23:04:32 +00:00
|
|
|
sd->step = 1;
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|
2010-01-04 17:06:14 +00:00
|
|
|
sd->text = (char*)malloc(strlen(arg + n1) + sd->show + 1);
|
2010-01-01 22:51:46 +00:00
|
|
|
|
|
|
|
if (strlen(arg) > sd->show) {
|
|
|
|
for(n2 = 0; (unsigned int) n2 < sd->show; n2++) {
|
|
|
|
sd->text[n2] = ' ';
|
|
|
|
}
|
|
|
|
sd->text[n2] = 0;
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|
2010-01-01 22:51:46 +00:00
|
|
|
else
|
|
|
|
sd->text[0] = 0;
|
|
|
|
|
2009-10-06 23:04:32 +00:00
|
|
|
strcat(sd->text, arg + n1);
|
|
|
|
sd->start = 0;
|
2010-01-04 17:06:14 +00:00
|
|
|
obj->sub = (struct text_object *)malloc(sizeof(struct text_object));
|
2009-10-06 23:04:32 +00:00
|
|
|
extract_variable_text_internal(obj->sub, sd->text);
|
|
|
|
|
|
|
|
obj->data.opaque = sd;
|
2009-11-08 13:49:27 +00:00
|
|
|
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_X11
|
2009-11-08 13:49:27 +00:00
|
|
|
/* add a color object right after scroll to reset any color changes */
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_X11 */
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|
|
|
|
|
2009-11-08 18:50:27 +00:00
|
|
|
void print_scroll(struct text_object *obj, char *p, int p_max_size)
|
2009-10-06 23:04:32 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct scroll_data *sd = (struct scroll_data *)obj->data.opaque;
|
2009-10-06 23:04:32 +00:00
|
|
|
unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
|
|
|
|
char *pwithcolors;
|
2010-01-04 17:06:14 +00:00
|
|
|
std::vector<char> buf(max_user_text);
|
2009-10-06 23:04:32 +00:00
|
|
|
|
2009-10-06 23:04:32 +00:00
|
|
|
if (!sd)
|
|
|
|
return;
|
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
generate_text_internal(&(buf[0]), max_user_text, *obj->sub);
|
2009-10-06 23:04:32 +00:00
|
|
|
for(j = 0; buf[j] != 0; j++) {
|
|
|
|
switch(buf[j]) {
|
|
|
|
case '\n': //place all the lines behind each other with LINESEPARATOR between them
|
|
|
|
#define LINESEPARATOR '|'
|
|
|
|
buf[j]=LINESEPARATOR;
|
|
|
|
break;
|
|
|
|
case SPECIAL_CHAR:
|
|
|
|
colorchanges++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//no scrolling necessary if the length of the text to scroll is too short
|
2010-01-04 17:06:14 +00:00
|
|
|
if (strlen(&(buf[0])) - colorchanges <= sd->show) {
|
|
|
|
snprintf(p, p_max_size, "%s", &(buf[0]));
|
2009-10-06 23:04:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//make sure a colorchange at the front is not part of the string we are going to show
|
2010-01-04 17:06:14 +00:00
|
|
|
while(buf[sd->start] == SPECIAL_CHAR) {
|
2009-10-06 23:04:32 +00:00
|
|
|
sd->start++;
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|
|
|
|
//place all chars that should be visible in p, including colorchanges
|
2009-10-06 23:04:32 +00:00
|
|
|
for(j=0; j < sd->show + visibcolorchanges; j++) {
|
2010-01-04 17:06:14 +00:00
|
|
|
p[j] = buf[sd->start + j];
|
2009-10-06 23:04:32 +00:00
|
|
|
if(p[j] == SPECIAL_CHAR) {
|
|
|
|
visibcolorchanges++;
|
|
|
|
}
|
|
|
|
//if there is still room fill it with spaces
|
2009-11-09 01:38:20 +00:00
|
|
|
if( ! p[j]) break;
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|
2009-10-06 23:04:32 +00:00
|
|
|
for(; j < sd->show + visibcolorchanges; j++) {
|
2009-10-06 23:04:32 +00:00
|
|
|
p[j] = ' ';
|
|
|
|
}
|
|
|
|
p[j] = 0;
|
|
|
|
//count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
|
2010-02-17 16:49:03 +00:00
|
|
|
for(j = 0; j < (unsigned) sd->start; j++) {
|
2009-10-06 23:04:32 +00:00
|
|
|
if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
|
|
|
|
}
|
2010-01-04 17:06:14 +00:00
|
|
|
pwithcolors=(char*)malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
|
2009-10-06 23:04:32 +00:00
|
|
|
for(j = 0; j < frontcolorchanges; j++) {
|
|
|
|
pwithcolors[j] = SPECIAL_CHAR;
|
|
|
|
}
|
|
|
|
pwithcolors[j] = 0;
|
|
|
|
strcat(pwithcolors,p);
|
|
|
|
strend = strlen(pwithcolors);
|
|
|
|
//and place the colorchanges not in front or in the visible part behind the visible part
|
|
|
|
for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
|
|
|
|
pwithcolors[strend + j] = SPECIAL_CHAR;
|
|
|
|
}
|
|
|
|
pwithcolors[strend + j] = 0;
|
|
|
|
strcpy(p, pwithcolors);
|
|
|
|
free(pwithcolors);
|
|
|
|
//scroll
|
2010-02-17 16:49:03 +00:00
|
|
|
if(sd->direction == SCROLL_LEFT) {
|
|
|
|
sd->start += sd->step;
|
|
|
|
if(buf[sd->start] == 0 || (unsigned) sd->start > strlen(&(buf[0]))) {
|
|
|
|
sd->start = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(sd->start < 1) {
|
|
|
|
sd->start = strlen(&(buf[0]));
|
|
|
|
}
|
|
|
|
sd->start -= sd->step;
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|
2010-02-16 14:45:32 +00:00
|
|
|
#ifdef BUILD_X11
|
|
|
|
//reset color when scroll is finished
|
2010-02-25 21:28:34 +00:00
|
|
|
if (out_to_x.get(*state))
|
2010-02-16 14:45:32 +00:00
|
|
|
new_special(p + strlen(p), FG)->arg = sd->resetcolor;
|
|
|
|
#endif
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_scroll(struct text_object *obj)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct scroll_data *sd = (struct scroll_data *)obj->data.opaque;
|
2009-10-06 23:04:32 +00:00
|
|
|
|
|
|
|
if (!sd)
|
|
|
|
return;
|
|
|
|
|
2010-02-24 10:52:59 +00:00
|
|
|
free_and_zero(sd->text);
|
|
|
|
free_text_objects(obj->sub);
|
|
|
|
free_and_zero(obj->sub);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-10-06 23:04:32 +00:00
|
|
|
}
|