Turning cursor off in consol window

Booting, installing, newbie
Post Reply
Message
Author
gemera
Posts: 14
Joined: Fri 17 Feb 2012, 19:19

Turning cursor off in consol window

#1 Post by gemera »

I'm learning some c using puppy and I would like to be able to turn off
the cursor in the consol window for a program I'm writing.

I asked the question in a c forum and it was suggested to try following:

Code: Select all


printf("\033[?251");

But it doesn't seem to work with puppy.

Any Ideas?

User avatar
wk57
Posts: 6
Joined: Mon 17 Dec 2012, 14:59

#2 Post by wk57 »

I don't know the answer to your question, or whether it's possible, but it's going to depend on the capabilities of the rxvt terminal emulator (or whatever terminal emulator you may be using; I think rxvt is the default one in Puppy.) You may find the manual entries for "tput" and "terminfo" interesting -

Code: Select all

man tput
man terminfo
from the command line.

gemera
Posts: 14
Joined: Fri 17 Feb 2012, 19:19

#3 Post by gemera »

Wow! a bit more info than I was expecting! :)

This is new stuff for me, but as far as I can gather its part of ncurses
etc which is not something I want to get involved with at this stage
and also I couldn't find mention among the info of being able to disable
the cursor anyway.

I'm really just programming with basic escape codes at the moment and
was hoping there would be a simple method of using them to disable the
cursor? Or even a simple command line argument that could be used in a
system() call in the program? i.e.

Code: Select all


system("argument") 

Basically I'm writing a Linux version of a Windows program that uses
graphics. The only barrier to completion is the cursor which gets in the way to the point of altering the result.


Otherwise I believe I'm stumped!

User avatar
wk57
Posts: 6
Joined: Mon 17 Dec 2012, 14:59

#4 Post by wk57 »

No, I don't think there's going to be any simple way to do this based on what it says in those manual entries; as you may be able to see depending on which version of the text you're looking at, there appear to be dozens and dozens of different terminal types with different functionality and capabilities and only some of them respond to the particular escape code you were trying to use. Ncurses and the termcap database appear to be designed to try to abstract away the differences but I don't see any general functionality that would appear to do what you want.

(Which doesn't really surprise me; if you're familiar with the origin of all this stuff, in the Dream Time of computing a terminal could actually be a printer instead of any kind of electronic display and so the equivalent to the cursor was actually just wherever the print head was at the moment... no way you're going to make the print head invisible. :? )

Though I was noticing that in the rxvt settings on Puppy you can set the color of the cursor. So you could set it to the background color to make it invisible in that particular context but there won't be any way to do that from within the normal channels of communicating with the terminal.

gemera
Posts: 14
Joined: Fri 17 Feb 2012, 19:19

#5 Post by gemera »

The default cursor in puppy is black, and I'm using a black background in the program so no joy there.

It looks as if I'm just going to have to leave it for now.

But thanks for your help.

gemera
Posts: 14
Joined: Fri 17 Feb 2012, 19:19

#6 Post by gemera »

Well I almost gave up, but decided to go ahead and introduce another thread
expecting a mess to ensue.Instead all the problems disappeared and the program
works as expected.

But this is probably more of a hack than a solution.

If all that is required is a flashing sign, it's possible to change the 2nd thread
to just printing a character the same colour as the background say at position
(0,0), and that is enough for the synchronisation of the main thread to come good.

Really just a programming excercise but for anyone interested - this is how to give
Puppy a (naff?) festive look-

Code: Select all


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <string.h>

#define  ALL_NORMAL "\033[0m"

#define  FG_BLACK   "\033[22;30m"	// dark colours
#define  FG_MAROON  "\033[22;31m"     
#define  FG_GREEN   "\033[22;32m"
#define  FG_OLIVE   "\033[22;33m"     
#define  FG_NAVY    "\033[22;34m"     
#define  FG_PURPLE  "\033[22;35m"
#define  FG_TEAL    "\033[22;36m"     
#define  FG_SILVER  "\033[22;37m"     

#define  FG_GRAY    "\033[1;30m" 	// light colours
#define  FG_RED     "\033[1;31m"      
#define  FG_LIME    "\033[1;32m"      
#define  FG_YELLOW  "\033[1;33m"
#define  FG_BLUE    "\033[1;34m"
#define  FG_FUCHSIA "\033[1;35m"      
#define  FG_AQUA    "\033[1;36m"      
#define  FG_WHITE   "\033[1;37m"

#define  BG_BLACK   "\033[40m"

#define BLACKBACK BG_BLACK " " ALL_NORMAL
#define MAGENTA  FG_FUCHSIA BG_BLACK "#" ALL_NORMAL
#define CYAN    FG_TEAL BG_BLACK "#" ALL_NORMAL
#define RED     FG_RED BG_BLACK "#" ALL_NORMAL
#define YELLOW  FG_YELLOW BG_BLACK "#" ALL_NORMAL

#define BRIGHT_RED BG_BLACK FG_RED "M" ALL_NORMAL
#define BRIGHT_GREEN BG_BLACK FG_LIME "E" ALL_NORMAL
#define BRIGHT_YELLOW BG_BLACK FG_YELLOW "R" ALL_NORMAL
#define BRIGHT_BLUE BG_BLACK FG_BLUE "R" ALL_NORMAL
#define BRIGHT_MAGENTA BG_BLACK FG_FUCHSIA "Y" ALL_NORMAL
#define BRIGHT_MAGENTA2 BG_BLACK FG_FUCHSIA "X" ALL_NORMAL
#define BRIGHT_CYAN BG_BLACK FG_AQUA "M" ALL_NORMAL
#define BRIGHT_WHITE BG_BLACK FG_WHITE "A" ALL_NORMAL
#define BRIGHT_RED2 BG_BLACK FG_RED "S" ALL_NORMAL

#define DIM_RED BG_BLACK FG_MAROON "M" ALL_NORMAL
#define DIM_GREEN BG_BLACK FG_GREEN "E" ALL_NORMAL
#define DIM_YELLOW BG_BLACK FG_OLIVE "R" ALL_NORMAL
#define DIM_BLUE BG_BLACK FG_NAVY "R" ALL_NORMAL
#define DIM_MAGENTA BG_BLACK FG_PURPLE "Y" ALL_NORMAL
#define DIM_MAGENTA2 BG_BLACK FG_PURPLE "X" ALL_NORMAL
#define DIM_CYAN BG_BLACK FG_TEAL "M" ALL_NORMAL
#define DIM_WHITE BG_BLACK FG_SILVER "A" ALL_NORMAL
#define DIM_RED2 BG_BLACK FG_MAROON "S" ALL_NORMAL

/********************************************************/
/*                                                      */
/*                  Flashing XMAS Sign.                 */
/*                                                      */
/*  compile with: cc -Wall -g xmas.c -o xmas -lpthread  */
/*                                                      */
/*                                                      */
/*                                                      */
/*             gemera            23rd Dec 2012          */
/*                                                      */
/********************************************************/

#define COLS 80
#define ROWS 25

#define START_X 8               	// x start position of sign
#define START_Y 9                 	// y start position of sign

void set_coord(int x, int y);
void plot_char(int x,int y,char *colour);
void* tinsel_func(void * id);
void sleep_seconds(long seconds);

pthread_mutex_t mutex1;

int main(void)
{
	int x,y;
	pthread_t thread1;
	
	pthread_mutex_init(&mutex1,NULL);

	srand(time(NULL));
   
    	system("clear");
	

	for(y=0;y<ROWS;y++)					//set background black 
    	{
		for(x=0;x<COLS;x++)
		{
			plot_char(x,y,BLACKBACK);
										
		}
	}
	
	pthread_create(&thread1,NULL,&tinsel_func,NULL);

	while(1)
	{
		pthread_mutex_lock(&mutex1);

		plot_char(START_X,START_Y,BRIGHT_RED);
    		plot_char(START_X+16,START_Y,BRIGHT_GREEN);
    		plot_char(START_X+32,START_Y,BRIGHT_YELLOW);
    		plot_char(START_X+48,START_Y,BRIGHT_BLUE);
    		plot_char(START_X+64,START_Y,BRIGHT_MAGENTA);
    		plot_char(START_X+8,START_Y+6,BRIGHT_MAGENTA2);
    		plot_char(START_X+24,START_Y+6,BRIGHT_CYAN);
    		plot_char(START_X+40,START_Y+6,BRIGHT_WHITE);
    		plot_char(START_X+56,START_Y+6,BRIGHT_RED2);

		pthread_mutex_unlock(&mutex1);
		
		sleep_seconds(2);
		
		pthread_mutex_lock(&mutex1);
		
		/*plot_char(START_X,START_Y,DIM_RED);			// alternate
    		plot_char(START_X+16,START_Y,DIM_GREEN);
    		plot_char(START_X+32,START_Y,DIM_YELLOW);
    		plot_char(START_X+48,START_Y,DIM_BLUE);
    		plot_char(START_X+64,START_Y,DIM_MAGENTA);
		plot_char(START_X+8,START_Y+6,DIM_MAGENTA);
    		plot_char(START_X+24,START_Y+6,DIM_CYAN);
    		plot_char(START_X+40,START_Y+6,DIM_WHITE);
    		plot_char(START_X+56,START_Y+6,DIM_RED2);*/
    	
    		plot_char(START_X,START_Y,BLACKBACK);			
    		plot_char(START_X+16,START_Y,BLACKBACK);
    		plot_char(START_X+32,START_Y,BLACKBACK);
    		plot_char(START_X+48,START_Y,BLACKBACK);
    		plot_char(START_X+64,START_Y,BLACKBACK);
		plot_char(START_X+8,START_Y+6,BLACKBACK);
    		plot_char(START_X+24,START_Y+6,BLACKBACK);
    		plot_char(START_X+40,START_Y+6,BLACKBACK);
    		plot_char(START_X+56,START_Y+6,BLACKBACK);

		pthread_mutex_unlock(&mutex1);
		
		sleep_seconds(1);
	}
        
    	return 0;
}

void set_coord(int x, int y)
{	
    printf("\033[%d;%dH",y,x);	/* move cursor (row y, col x) */
}

void plot_char(int x,int y,char * colour)
{
    set_coord(x,y);
    
    fputs(colour,stdout);
}

void* tinsel_func(void *id)
{
	int x,y,i;
	char *tinsel_colour[] = {MAGENTA,CYAN,RED,YELLOW};
	
	while(1)
	{
		pthread_mutex_lock(&mutex1);
	
		i = rand() % 4;		// to access random colour for tinsel
	
		y = 3;
		x = rand() % COLS;
		plot_char(x,y,tinsel_colour[i]);	

		y = 22;
		x = rand() % COLS;
		plot_char(x,y,tinsel_colour[i]);	

		pthread_mutex_unlock(&mutex1);
	}

	return NULL;
}

void sleep_seconds(long seconds)
{
	clock_t limit,now  = clock();
	
	limit = now + seconds * CLOCKS_PER_SEC;
	
	while(limit > now)
	{
		now = clock();
	}
}


Post Reply