LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-28-2010, 11:15 AM   #1
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Rep: Reputation: 15
Question Get width/height of a terminal window in c++?


How do I find out how many characters fit into a terminal window horizontally and how many lines the terminal window is high (i.e. how many rows and columns are visible - the width and height)?

I am using c++ and Linux Mint 8 - Gnome.

Thanx in advance for any responses...
 
Old 05-29-2010, 06:05 AM   #2
antegallya
Member
 
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
Hello,

a quick and dirty way :
Code:
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>

int main (void)
{
    int cols = 80;
    int lines = 24;

#ifdef TIOCGSIZE
    struct ttysize ts;
    ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
    cols = ts.ts_cols;
    lines = ts.ts_lines;
#elif defined(TIOCGWINSZ)
    struct winsize ts;
    ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
    cols = ts.ws_col;
    lines = ts.ws_row;
#endif /* TIOCGSIZE */

    printf("Terminal is %dx%d\n", cols, lines);
}
 
3 members found this post helpful.
Old 05-30-2010, 02:38 PM   #3
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Original Poster
Rep: Reputation: 15
Smile Thx :D

Thank you for that code antegallya.
For now i don't need it anymore but I may find some use for it later.
Thanx, bye!
 
Old 05-30-2010, 06:56 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 808

Rep: Reputation: 178Reputation: 178
The only other thing you may wish to consider: what should happen if the user resizes the terminal window while your program is running? If you're interested in this situation, your program should expose itself to the signal SIGWINCH. If your program receives that signal, then the terminal window has probably been resized. Your program should then rediscover what the window size is. It may wish to clear and re-draw the window in this case.
 
1 members found this post helpful.
Old 06-01-2010, 01:34 AM   #5
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Original Poster
Rep: Reputation: 15
Lightbulb Got it :D

This code works for me
Code:
#include <ncurses.h>
namespace dim
{
	int x()
	{
		int xdim;
		int ydim;
		getmaxyx(stdscr, ydim, xdim);
		return xdim;
	}
	int y()
	{
		int xdim;
		int ydim;
		getmaxyx(stdscr, ydim, xdim);
		return ydim;
	}
}
cout << "The size of your terminal is x: " << dim::x << " y: " << dim::y << endl;
__________________


Quote:
Code:
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>

__________________


int main (void)
{
    int cols = 80;
    int lines = 24;

#ifdef TIOCGSIZE
    struct ttysize ts;
    ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
    cols = ts.ts_cols;
    lines = ts.ts_lines;
#elif defined(TIOCGWINSZ)
    struct winsize ts;
    ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
    cols = ts.ws_col;
    lines = ts.ws_row;
#endif /* TIOCGSIZE */

    printf("Terminal is %dx%d\n", cols, lines);
}
Im no expert by any means but im prety sure stdio.h is for c not c++.
Please correct me if im wrong tho

__________________


Quote:
your program should expose itself to the signal SIGWINCH.
Sorry, don't know what that is
If somebody could explain how to use the "signal SIGWINCH" and/or what it is I would be very grateful Failing this does anyone know of a good tutorial which might explain it?

Thanx!
 
Old 06-01-2010, 07:04 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by rohedin View Post
Im no expert by any means but im prety sure stdio.h is for c not c++.
Please correct me if im wrong tho
In C++, just use cstdio instead of sdtio.h.
 
0 members found this post helpful.
Old 06-01-2010, 04:38 PM   #7
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by MTK358 View Post
In C++, just use cstdio instead of sdtio.h.
lol I know. Just wanted to point that out for the sake of correctness.

BTW... anyone know what a "signal SIGWINCH" is?
I'll looks it up on Google

Last edited by rohedin; 06-01-2010 at 04:40 PM. Reason: Awesomeness
 
Old 06-01-2010, 07:33 PM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 808

Rep: Reputation: 178Reputation: 178
I think your first step toward using SIGWINCH is to find out how to use signals in the first place. Then simply set up your program so that it reacts to SIGWINCH by retrieving the window size again (and redrawing the data if necessary).

For using signals, I haven't read this intro, but wikipedia points to it. See if it helps you.
 
1 members found this post helpful.
Old 06-02-2010, 05:00 PM   #9
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wje_lq View Post
For using signals, I haven't read this intro, but wikipedia points to it. See if it helps you.
I may well decide to read through that - It certainly sounds very interesting and usefull - after I have completed my current project.
So thanx for the link wje_lq!
 
Old 10-24-2012, 07:00 PM   #10
slacktroll
Member
 
Registered: May 2011
Distribution: Slackware 14.0 on Thinkpad X60
Posts: 57

Rep: Reputation: 3
removed useless code.
ktx bai.

Last edited by slacktroll; 11-03-2012 at 01:27 AM.
 
  


Reply

Tags
c++, terminal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
ffmpeg error: invalid width x height (0x320) MythN00b Linux - Software 1 12-27-2007 07:39 AM
Reduce width and height of gdmgreeter and Xfce desktop lothario Linux - Software 5 06-02-2006 02:27 PM
How can a bash script findout the width of the terminal window alex.e.c Linux - Software 2 10-14-2004 11:20 AM
Scripting question: the width of the terminal window Bebo Programming 13 11-06-2003 04:41 PM
Laptop Screen Height and Width blueStatic Linux - Hardware 1 09-03-2003 12:06 AM


All times are GMT -5. The time now is 01:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration