LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-19-2020, 08:23 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
how to get position of cursur in C++


hi to all, I've centos 7.5 in VM, and i am using C::B, now i want to know position of cursur in terminal. how to do that. i googled and find below soution but i am not getting its point. code is:-

Code:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main (void)

{
  int ttyfd = open ("/dev/tty", O_RDWR);
  if (ttyfd < 0)
    {
      printf ("Cannot open /devv/tty: errno = %d, %s\r\n",
        errno, strerror (errno));
      exit (EXIT_FAILURE);
    }

  write (ttyfd, "\x1B[6n\n", 5);

  unsigned char answer[16];
  size_t answerlen = 0;
  while (answerlen < sizeof (answer) - 1 &&
         read (ttyfd, answer + answerlen, 1) == 1)
    if (answer [answerlen ++] == 'R') break;
  answer [answerlen] = '\0';

  printf ("Answerback = \"");
  for (size_t i = 0; i < answerlen; ++ i)
    if (answer [i] < ' ' || '~' < answer [i])
      printf ("\\x%02X", (unsigned char) answer [i]);
    else
      printf ("%c", answer [i]);
  printf ("\"\r\n");

  return EXIT_SUCCESS;
}
output is :-
Code:
[rahul@centos7client Release]$ ./poscurr 

^[[28;1R
         Answerback = "\x1B[28;1R"
[rahul@centos7client Release]$
what it does mean. please any one can explain this.

Last edited by rahulvishwakarma; 02-19-2020 at 08:25 AM.
 
Old 02-19-2020, 10:16 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,751

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by rahulvishwakarma View Post
hi to all, I've centos 7.5 in VM, and i am using C::B, now i want to know position of cursur in terminal. how to do that. i googled and find below soution but i am not getting its point. code is:-
Code:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main (void)

{
  int ttyfd = open ("/dev/tty", O_RDWR);
  if (ttyfd < 0)
    {
      printf ("Cannot open /devv/tty: errno = %d, %s\r\n",
        errno, strerror (errno));
      exit (EXIT_FAILURE);
    }

  write (ttyfd, "\x1B[6n\n", 5);

  unsigned char answer[16];
  size_t answerlen = 0;
  while (answerlen < sizeof (answer) - 1 &&
         read (ttyfd, answer + answerlen, 1) == 1)
    if (answer [answerlen ++] == 'R') break;
  answer [answerlen] = '\0';

  printf ("Answerback = \"");
  for (size_t i = 0; i < answerlen; ++ i)
    if (answer [i] < ' ' || '~' < answer [i])
      printf ("\\x%02X", (unsigned char) answer [i]);
    else
      printf ("%c", answer [i]);
  printf ("\"\r\n");

  return EXIT_SUCCESS;
}
output is :-
Code:
[rahul@centos7client Release]$ ./poscurr 

^[[28;1R
         Answerback = "\x1B[28;1R"
[rahul@centos7client Release]$
what it does mean. please any one can explain this.
Please see your other threads regarding very similar topics:
https://www.linuxquestions.org/quest...-a-4175669527/
https://www.linuxquestions.org/quest...ux-4175669529/
 
Old 02-19-2020, 01:24 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
which part is unclear?
 
Old 02-19-2020, 03:54 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I suggest you look up ANSI terminal escape sequences.

Code:
^[[28;1R
Row 28, column 1.

Edit: I don't know what's C::B. You program looks like regular C.

Last edited by berndbausch; 02-19-2020 at 03:57 PM.
 
1 members found this post helpful.
Old 03-15-2020, 09:58 AM   #5
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Original Poster
Rep: Reputation: 2
sorry for late, when i ran that program and it always shows "^[[Y;1R" where Y is row and x coordinate is 1 always. How to get rid of this.
 
Old 03-15-2020, 10:02 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,751

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by rahulvishwakarma View Post
sorry for late, when i ran that program and it always shows "^[[Y;1R" where Y is row and x coordinate is 1 always. How to get rid of this.
Sorry, but this sounds very much like homework. You have a task that has no real purpose, and you downloaded code that does what you want...and it now seems like you want us to rewrite/explain it all to you.

What have YOU figured out/thought about in regards to the code?? And chances are that code is returning exactly what it should...want a different number? Put the cursor somewhere else. And you do realize that the MOUSE cursor is far different than the TERMINAL cursor, right???
 
Old 03-15-2020, 10:53 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by rahulvishwakarma View Post
sorry for late, when i ran that program and it always shows "^[[Y;1R" where Y is row and x coordinate is 1 always. How to get rid of this.
This is the terminal's answer to the escape sequence "\x1B[6n\n". Do you not want to see this output? If so, perhaps there is a way to put the terminal in a mode that suppresses output on the screen.

EDIT: There is. Good old stty, how could I forget it. These days everything is graphical, but in the golden days, we had serial terminals when talking to UNIX (which was still better than punched cards, if you ask me):
Code:
stty -echo; ./yourprogram; stty echo
You do your own googling to find a suitable escape sequence or other programmatic way.

Last edited by berndbausch; 03-15-2020 at 07:20 PM. Reason: added stty and cool comment about the old times
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to get the cursor position line at the mid of the windows in vim? cola Linux - Newbie 5 03-28-2010 10:18 AM
Change default position of the X cursor or get rid of it!!! thebover Linux - Software 2 04-30-2009 12:47 AM
mouse works, but cursur is gone gwyndion Linux - Desktop 3 02-19-2007 09:24 PM
How to get movie position from mplayer? capthookb Programming 1 08-09-2006 01:54 AM
Anyone knows how to get the pointer position on the desktop using GTK2.0? norman68 Linux - Software 0 06-24-2003 04:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration