LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Get width/height of a terminal window in c++? (http://www.linuxquestions.org/questions/programming-9/get-width-height-of-a-terminal-window-in-c-810739/)

rohedin 05-28-2010 11:15 AM

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...

antegallya 05-29-2010 06:05 AM

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);
}


rohedin 05-30-2010 02:38 PM

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! :D

wje_lq 05-30-2010 06:56 PM

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.

rohedin 06-01-2010 01:34 AM

Got it :D
 
This code works for me :D
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 :confused:
If somebody could explain how to use the "signal SIGWINCH" and/or what it is I would be very grateful :hattip: Failing this does anyone know of a good tutorial which might explain it? :study:

Thanx! :D

MTK358 06-01-2010 07:04 AM

Quote:

Originally Posted by rohedin (Post 3988211)
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.

rohedin 06-01-2010 04:38 PM

Quote:

Originally Posted by MTK358 (Post 3988531)
In C++, just use cstdio instead of sdtio.h.

lol :D 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 ;)

wje_lq 06-01-2010 07:33 PM

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.

rohedin 06-02-2010 05:00 PM

Quote:

Originally Posted by wje_lq (Post 3989394)
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!
:D

slacktroll 10-24-2012 07:00 PM

removed useless code.
ktx bai.


All times are GMT -5. The time now is 01:15 PM.