1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* Generic text scroller program.
*
* Scrolls any string of text on a line of the screen.
* Can use SysV shared memory to communicate with another
* program to get messages to display.
*
* By Joey Hess <joey@kitenet.net>, GPL copyright 1998-2002
* All rights reserved. See COPYING for full copyright information.
*/
#include "config.h"
#include "slang.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <stdlib.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <limits.h>
/* Where to display message. 1=bottom of screen, 0=top */
int scroll_pos=0;
/* message to display */
char *message=NULL;
/* the colors to display the message in. */
char *bgcolor="black";
char *fgcolor="white";
/* how long to delay between updates, in usecs */
long delay=1000000;
/* if 1, read new messages from stdin */
int read_shm=0;
/* how long to delay between checks of the shared memory segment */
int refresh=1;
/* the max size of the shared memory segment */
int shm_size=80;
/* this points to the shared memory segment */
char *shm_p=NULL;
/* Display an error. The parameters work like printf() */
void Error(const char *fmt, ...) {
va_list arglist;
va_start(arglist, fmt);
fprintf(stderr, "ticker: ");
vfprintf(stderr, fmt, arglist);
fprintf(stderr, "\n");
va_end(arglist);
exit(1);
}
/* Signal handler to exit safely. */
void Clean_Exit (int unused) {
SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
SLsmg_refresh();
SLang_reset_tty ();
SLsmg_reset_smg ();
if (read_shm)
shmdt(shm_p);
exit(0);
}
void Init_Screen () {
SLsig_block_signals();
SLtt_get_terminfo();
if (SLkp_init() == -1)
Error("unable to initialize keyboard");
SLang_init_tty(3, 1, 1); /* 3 = abort on ctrl-c */
SLang_set_abort_signal(Clean_Exit);
SLsmg_init_smg();
SLsig_unblock_signals();
}
void ClearLine (int xoffset, int yoffset) {
SLsmg_gotorc(yoffset,xoffset);
SLsmg_write_nstring(NULL,SLtt_Screen_Cols);
}
void Write_Message (char *message, int xoffset, int yoffset) {
SLsmg_gotorc(yoffset,xoffset);
SLsmg_write_string(message);
}
void Usage () {
printf("Usage: ticker [options] [message]\n"
" -h --help display this help\n"
" -u --upper scroll message on top of screen\n"
" -l --lower scroll message on bottom of screen\n"
" -fcolor --foreground=color foreground color to display message in\n"
" -bcolor --background=color background color to display message in\n"
" -dsecs --delay=secs number of seconds between updates (may use\n"
" decimals)\n"
" -snum --sysv=num read messages from shared memory segment with\n"
" id of num\n"
" -Snum --size=num size of the shared memory segment\n"
" -csecs --check=secs how often to shared memory segment for new\n"
" messages\n"
" message the text to scroll (optional if -s is used)\n");
exit(0);
}
void Parse_Params (int argc, char **argv) {
#ifdef HAVE_GETOPT_LONG
struct option long_options[] = {
{"help",0,NULL,'h'},
{"upper",0,NULL,'u'},
{"lower",0,NULL,'l'},
{"sysv",1,NULL,'s'},
{"foreground",1,NULL,'f'},
{"background",1,NULL,'b'},
{"delay",1,NULL,'d'},
{"check",1,NULL,'c'},
{0,0,0,0}
};
#endif
extern int optind;
extern char *optarg;
int shmid=0;
#if defined (HAVE_GETOPT_LONG) || defined (HAVE_GETOPT)
int c=0;
while (c != -1) {
#define PARAM_SHORT "hulf:b:d:s:c:S:"
#ifdef HAVE_GETOPT_LONG
c=getopt_long(argc,argv,PARAM_SHORT,long_options,NULL);
#elif HAVE_GETOPT
c=getopt(argc,argv,PARAM_SHORT);
#endif
switch (c) {
case 'h':
Usage(); /* exits program */
case 'u':
scroll_pos=0;
break;
case 's':
read_shm=1;
shmid=atoi(optarg);
break;
case 'l':
scroll_pos=1;
break;
case 'f':
fgcolor=malloc(strlen(optarg)+1);
strcpy(fgcolor,optarg);
break;
case 'b':
bgcolor=malloc(strlen(optarg)+1);
strcpy(bgcolor,optarg);
break;
case 'd':
delay=atof(optarg) * 1000000;
break;
case 'c':
refresh=atoi(optarg);
break;
case 'S':
shm_size=atoi(optarg);
}
}
#else
optind=1;
#endif /* have one of the getopts */
if (optind < argc) { /* we were passed a message to scroll */
/*
* malloc memory for the message, instead of just setting it
* equal for optind, because we want to always be able to free()
* message later.
*/
message=malloc(strlen(argv[optind])+1);
strcpy(message,argv[optind]);
}
else if (! read_shm) {
fprintf(stderr,"Must specify a message to display.\n");
Usage();
}
if (read_shm) {
shm_p=shmat(shmid,0,SHM_RDONLY);
/* [FIX] warning: cast-to-from-pointer-from-to-integer-of-different-size */
if ((long)shm_p == -1) {
perror("ticker: shmat error");
exit(1);
}
if (! (optind < argc)) {
message=malloc(shm_size+1);
strncpy(message,shm_p,shm_size);
}
}
}
int Is_ExtraLong(int len) {
if (len > SLtt_Screen_Cols - 2)
return 1;
else
return 0;
}
void Handle_KeyPress(int key) {
switch (key) {
case '+':
case SL_KEY_UP:
delay=delay / 1.25;
break;
case '-':
case SL_KEY_DOWN:
/*
* Anything less than 5 is a trap we cannot get out of with a
* multiplication factor of 1.25, because of rounding.
*/
if (delay < 5)
delay=5;
else if (delay * 1.25 <= LONG_MAX)
delay=delay * 1.25;
else
delay=LONG_MAX;
break;
case ' ':
/* Pause for a keypress */
while (! SLang_input_pending(600)) {}
SLkp_getkey(); /* discard */
break;
}
}
int main (int argc, char **argv) {
int x, len, extralong, ypos, cursorpos;
struct timeval tv;
time_t now, oldtime=time(NULL);
char *newmessage=NULL;
Parse_Params(argc,argv);
Init_Screen();
SLtt_set_color(1,"",fgcolor,bgcolor);
SLsmg_set_color(1);
if (scroll_pos) {
ypos=SLtt_Screen_Rows-1;
cursorpos=0;
}
else {
ypos=0;
cursorpos=SLtt_Screen_Rows-1;
}
len=strlen(message);
extralong=Is_ExtraLong(len);
x=SLtt_Screen_Cols;
while (1) {
x--;
/* See if it's time to reset x. */
if (x == -1 * len - 2) {
/*
* For strings shorter than the screen
* width, we need to behave one way, while
* we behave a different way for longer strings.
*/
if (extralong) {
x= -1;
}
else {
x=SLtt_Screen_Cols - len - 2;
}
/* See if there is a newmessage waiting to scroll on. */
if (newmessage != NULL) {
free(message);
message=newmessage;
newmessage=NULL;
len=strlen(message);
extralong=Is_ExtraLong(len);
x=SLtt_Screen_Cols;
}
}
/* See if we need to check the shared memory segment for new
* data now. */
if (read_shm && x == -1) {
now=time(NULL);
if (now >= oldtime + refresh) {
oldtime=now;
newmessage=malloc(shm_size+1);
strncpy(newmessage,shm_p,shm_size);
/* See if the message has actually changed. */
if (strcmp(message,newmessage) == 0) {
free(newmessage);
newmessage=NULL;
}
}
}
/*
* Accomplish what looks like continuous scrolling
* by actually scrolling 2 copies of the message.
*/
ClearLine(0,ypos);
Write_Message(message,x,ypos);
/*
* If a newmessage is waiting to come on, we don't scroll the
* second copy. This makes the screen clear eventually so the
* newmessage can come on.
*/
if (newmessage == NULL) {
/* In long string mode have to reposition the second
* string. */
if (extralong)
Write_Message(message,x+len+2,ypos);
else
Write_Message(message,x+SLtt_Screen_Cols,ypos);
}
SLsmg_gotorc(cursorpos,0);
SLsmg_refresh();
/* Pause. */
tv.tv_sec=0;
tv.tv_usec=delay;
select(0,0,0,0,&tv);
/*
* Check for keypress. Could use this as a pause, but it only
* has 1/10th second resolution, so don't.
*/
while (SLang_input_pending(0))
Handle_KeyPress(SLkp_getkey());
}
}
|