scroll: outsource code
authorPhil Sutter <phil@nwl.cc>
Tue, 6 Oct 2009 23:04:32 +0000 (01:04 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 00:50:28 +0000 (01:50 +0100)
src/Makefile.am
src/conky.c
src/conky.h
src/core.c
src/scroll.c [new file with mode: 0644]
src/scroll.h [new file with mode: 0644]

index 7904637..a03232f 100644 (file)
@@ -52,9 +52,10 @@ endif # BUILD_CONFIG_OUTPUT
 mandatory_sources = colours.c colours.h common.c common.h conky.c conky.h \
                core.c core.h diskio.c diskio.h exec.c exec.h fs.c fs.h logging.h \
                mail.c mail.h mixer.c mixer.h template.c template.h timed_thread.c \
-               timed_thread.h mboxscan.c mboxscan.h read_tcp.c read_tcp.h specials.c \
-               specials.h tailhead.c tailhead.h temphelper.c temphelper.h \
-               text_object.c text_object.h timeinfo.c timeinfo.h algebra.c algebra.h
+               timed_thread.h mboxscan.c mboxscan.h read_tcp.c read_tcp.h scroll.c \
+               scroll.h specials.c specials.h tailhead.c tailhead.h temphelper.c \
+               temphelper.h text_object.c text_object.h timeinfo.c timeinfo.h \
+               algebra.c algebra.h
 
 # source files only needed when the apropriate option is enabled
 audacious = audacious.c audacious.h
index 67b73ba..4fcad2c 100644 (file)
@@ -89,6 +89,7 @@
 #include "mail.h"
 #include "mboxscan.h"
 #include "read_tcp.h"
+#include "scroll.h"
 #include "specials.h"
 #include "temphelper.h"
 #include "template.h"
@@ -173,8 +174,6 @@ char** argv_copy;
 static void signal_handler(int);
 static void print_version(void) __attribute__((noreturn));
 static void reload_config(void);
-static void generate_text_internal(char *, int, struct text_object,
-                                   struct information *);
 
 static void print_version(void)
 {
@@ -832,7 +831,7 @@ void substitute_newlines(char *p, long l)
        }
 }
 
-static void generate_text_internal(char *p, int p_max_size,
+void generate_text_internal(char *p, int p_max_size,
                struct text_object root, struct information *cur)
 {
        struct text_object *obj;
@@ -2528,71 +2527,7 @@ static void generate_text_internal(char *p, int p_max_size,
                                snprintf(p, p_max_size, "%s", buf);
                        }
                        OBJ(scroll) {
-                               unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
-                               char *pwithcolors;
-                               char buf[max_user_text];
-                               generate_text_internal(buf, max_user_text,
-                                                      *obj->sub, cur);
-                               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
-                               if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
-                                       snprintf(p, p_max_size, "%s", buf);
-                                       break;
-                               }
-                               //make sure a colorchange at the front is not part of the string we are going to show
-                               while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
-                                       obj->data.scroll.start++;
-                               }
-                               //place all chars that should be visible in p, including colorchanges
-                               for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
-                                       p[j] = *(buf + obj->data.scroll.start + j);
-                                       if(p[j] == SPECIAL_CHAR) {
-                                               visibcolorchanges++;
-                                       }
-                                       //if there is still room fill it with spaces
-                                       if( ! p[j]) break;
-                               }
-                               for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
-                                       p[j] = ' ';
-                               }
-                               p[j] = 0;
-                               //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
-                               for(j = 0; j < obj->data.scroll.start; j++) {
-                                       if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
-                               }
-                               pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
-                               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
-                               obj->data.scroll.start += obj->data.scroll.step;
-                               if(buf[obj->data.scroll.start] == 0){
-                                        obj->data.scroll.start = 0;
-                               }
-#ifdef X11
-                               //reset color when scroll is finished
-                               new_fg(p + strlen(p), obj->data.scroll.resetcolor);
-#endif
+                               print_scroll(obj, p, p_max_size, cur);
                        }
                        OBJ(combine) {
                                char buf[2][max_user_text];
index fa8c5b2..62a4ac3 100644 (file)
@@ -367,4 +367,6 @@ void set_update_interval(double interval);
 void parse_conky_vars(struct text_object *, const char *,
                        char *, struct information *);
 
+void generate_text_internal(char *, int, struct text_object,
+                                   struct information *);
 #endif /* _conky_h_ */
index a1df38d..51412c1 100644 (file)
@@ -48,6 +48,7 @@
 #include "mail.h"
 #include "mboxscan.h"
 #include "read_tcp.h"
+#include "scroll.h"
 #include "specials.h"
 #include "temphelper.h"
 #include "template.h"
@@ -1442,30 +1443,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                obj->sub = malloc(sizeof(struct text_object));
                extract_variable_text_internal(obj->sub, arg);
        END OBJ(scroll, 0)
-               int n1 = 0, n2 = 0;
-
-               obj->data.scroll.resetcolor = get_current_text_color();
-               obj->data.scroll.step = 1;
-               if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
-                       sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
-                       if (*(arg + n1 + n2)) {
-                               n1 += n2;
-                       } else {
-                               obj->data.scroll.step = 1;
-                       }
-                       obj->data.scroll.text = malloc(strlen(arg + n1) + obj->data.scroll.show + 1);
-                       for(n2 = 0; (unsigned int) n2 < obj->data.scroll.show; n2++) {
-                               obj->data.scroll.text[n2] = ' ';
-                       }
-                       obj->data.scroll.text[n2] = 0;
-                       strcat(obj->data.scroll.text, arg + n1);
-                       obj->data.scroll.start = 0;
-                       obj->sub = malloc(sizeof(struct text_object));
-                       extract_variable_text_internal(obj->sub,
-                                       obj->data.scroll.text);
-               } else {
-                       CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
-               }
+               parse_scroll_arg(obj, arg, free_at_crash);
        END OBJ_ARG(combine, 0, "combine needs arguments: <text1> <text2>")
                unsigned int i,j;
                unsigned int indenting = 0;     //vars can be used as args for other vars
@@ -2147,9 +2125,7 @@ void free_text_objects(struct text_object *root, int internal)
                                }
                                break;
                        case OBJ_scroll:
-                               free(data.scroll.text);
-                               free_text_objects(obj->sub, 1);
-                               free(obj->sub);
+                               free_scroll(obj);
                                break;
                        case OBJ_combine:
                                free(data.combine.left);
diff --git a/src/scroll.c b/src/scroll.c
new file mode 100644 (file)
index 0000000..5ffb223
--- /dev/null
@@ -0,0 +1,139 @@
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ *
+ * 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
+ * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ *     (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"
+
+void parse_scroll_arg(struct text_object *obj, const char *arg, void *free_at_crash)
+{
+       int n1 = 0, n2 = 0;
+
+       obj->data.scroll.resetcolor = get_current_text_color();
+       obj->data.scroll.step = 1;
+       if (!arg || sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) <= 0)
+               CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
+
+       sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
+       if (*(arg + n1 + n2)) {
+               n1 += n2;
+       } else {
+               obj->data.scroll.step = 1;
+       }
+       obj->data.scroll.text = malloc(strlen(arg + n1) + obj->data.scroll.show + 1);
+       for(n2 = 0; (unsigned int) n2 < obj->data.scroll.show; n2++) {
+               obj->data.scroll.text[n2] = ' ';
+       }
+       obj->data.scroll.text[n2] = 0;
+       strcat(obj->data.scroll.text, arg + n1);
+       obj->data.scroll.start = 0;
+       obj->sub = malloc(sizeof(struct text_object));
+       extract_variable_text_internal(obj->sub, obj->data.scroll.text);
+}
+
+void print_scroll(struct text_object *obj, char *p, int p_max_size, struct information *cur)
+{
+       unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
+       char *pwithcolors;
+       char buf[max_user_text];
+
+       generate_text_internal(buf, max_user_text, *obj->sub, cur);
+       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
+       if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
+               snprintf(p, p_max_size, "%s", buf);
+               return;
+       }
+       //make sure a colorchange at the front is not part of the string we are going to show
+       while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
+               obj->data.scroll.start++;
+       }
+       //place all chars that should be visible in p, including colorchanges
+       for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
+               p[j] = *(buf + obj->data.scroll.start + j);
+               if(p[j] == SPECIAL_CHAR) {
+                       visibcolorchanges++;
+               }
+               //if there is still room fill it with spaces
+               if( ! p[j]) return;
+       }
+       for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
+               p[j] = ' ';
+       }
+       p[j] = 0;
+       //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
+       for(j = 0; j < obj->data.scroll.start; j++) {
+               if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
+       }
+       pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
+       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
+       obj->data.scroll.start += obj->data.scroll.step;
+       if(buf[obj->data.scroll.start] == 0){
+               obj->data.scroll.start = 0;
+       }
+#ifdef X11
+       //reset color when scroll is finished
+       new_fg(p + strlen(p), obj->data.scroll.resetcolor);
+#endif
+}
+
+void free_scroll(struct text_object *obj)
+{
+       if (obj->data.scroll.text)
+               free(obj->data.scroll.text);
+       if (obj->sub) {
+               free_text_objects(obj->sub, 1);
+               free(obj->sub);
+               obj->sub = NULL;
+       }
+}
diff --git a/src/scroll.h b/src/scroll.h
new file mode 100644 (file)
index 0000000..4c33eb8
--- /dev/null
@@ -0,0 +1,36 @@
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ *
+ * 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
+ * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ *     (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/>.
+ *
+ */
+#ifndef _SCROLL_H
+#define _SCROLL_H
+
+void parse_scroll_arg(struct text_object *, const char *, void *);
+void print_scroll(struct text_object *, char *, int, struct information *);
+void free_scroll(struct text_object *);
+
+#endif /* _SCROLL_H */