Thread: Setting terminal size through c?

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    Setting terminal size through c?

    Hello friends,

    Please tell me the way to change the terminal size with a small C program.

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    16
    There's no portable way to do this.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    9
    You can use something like that - in windows:
    As far as i know, you don't need to include a library.
    Code:
    void main()
    {
        SMALL_RECT windowSize = {0 , 0 , 77 , 47} //change the values
        SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &windowSize)
    }

  4. #4
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Quote Originally Posted by trep View Post
    You can use something like that - in windows:
    As far as i know, you don't need to include a library.
    Code:
    void main()
    {
        SMALL_RECT windowSize = {0 , 0 , 77 , 47} //change the values
        SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &windowSize)
    }
    Please help in coding through linux.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Do you realize that a 'terminal' should have no 'window size' as it can run in an windowing less environment, which is quite common for Linux systems?

    If you mean, change xterm window size, use the ncurses library (By googling : resizeterm(3): change curses terminal size - Linux man page ). You'll have to modify quite a lot of your IO structure for that, afaik. Even after that, there seems to be no guarantee that it would work.

    If you really want windows use a gui toolkit for that. (You seem to know gtk, from your earlier posts)

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Conceptually, you can change the size of the terminal window from within a program.

    Practicably, you aren't going to do that unless you only hope to reduce the window size.

    If you are running a system that isn't running under a "X Server" instance:
    The size of the terminal is usually fixed by the loader. Any attempt you make to circumvent this may fail without error, fail with an error, or crash the virtual terminal instance. It depends on how virtual terminals are configured, what fonts are available, and what driver is responsible for the drawing.

    If you are running a system that is running under a "X Server" instance:
    You still have no guarantees. The size of a "X" terminal emulator can usually be adjusted, but different terminal emulators handle it differently. It also happens that the shell will occasionally stand in your way. It is theoretically possible to use a combination of terminal emulator specific calls with terminal interface library (You may wish to try "ncurses".) signal handlers to force a size adjustment within a program while leaving the terminal useable. This just isn't likely to work. Ever.

    The `resizeterm' extension will often not work to enlarge the window for all the reasons listed above plus a few extras related to security and environment variables. It depends on the implementations, but it is usually coded in such a way that the size can never be greater than the natural size of the original terminal being emulated unless outside influence has already increased the size beyond that limit. As an added "bonus" that will prevent people from using your program more than once: the `resizeterm' extension can affect every terminal emulator instance running under the relevant user.

    Here is what you can do, and it will work. Almost always.

    Variation 1: (Effort: Barely Any)
    Use a terminal interface library to request the current size of the window at the top of `main' and fail gracefully with an error if the size is not sufficient for your purposes with a note about what sizes are supported. Note: Don't think you can get away with calling `resize' from the `system' function; the processes forked by `system' lives in a different universe so it just will not work.

    Variation 2: (Effort: Mission Impossible)
    Query permissions and optionally attempt to use "Xlib" directly to do what `resizeterm' usually doesn't do by calculating everything yourself, issuing the size adjustment commands, and optionally calling the relevant terminal interface library API to refresh the current terminal information.

    Soma

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by phantomotap View Post
    Variation 2: (Effort: Mission Impossible)
    Query permissions and optionally attempt to use "Xlib" directly to do what `resizeterm' usually doesn't do by calculating everything yourself, issuing the size adjustment commands, and optionally calling the relevant terminal interface library API to refresh the current terminal information.
    Is that technically possible, unless the program is written using Xlib to be a gui program embedding a terminal emulator?
    Unless that is the case, it should, I think, spout errors like "X sever not found" or "Display not specified".

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Edit]
    You'll also find unreliable utility in the form of "\033[8;h;wt".

    The point to all of this by the way isn't to say that it is truly impossible so much as it is to say that it probably isn't worth your time.
    [/Edit]

    Soma

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Redacted]

    Soma
    Last edited by phantomotap; 05-22-2012 at 04:30 AM. Reason: joke

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    9
    Do you realize that a 'terminal' should have no 'window size' as it can run in an windowing less environment, which is quite common for Linux systems?
    I didn't exactly now what he meant. Therefore I've written "in windows".
    Because I know a lot of people who call the "windows cmd" - terminal!

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You should be able to set and get window size with tty ioctls, operating on struct winsize with TIOCSWINSZ and TIOCGWINSZ. This will not affect the size of the window, but the area within the window that is used however.

  12. #12
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Quote Originally Posted by trep View Post
    I didn't exactly now what he meant. Therefore I've written "in windows".
    Because I know a lot of people who call the "windows cmd" - terminal!
    I know you are confused a little bit.

    Actually, I was talking about gnome-terminal.

  13. #13
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Quote Originally Posted by Subsonics View Post
    You should be able to set and get window size with tty ioctls, operating on struct winsize with TIOCSWINSZ and TIOCGWINSZ. This will not affect the size of the window, but the area within the window that is used however.
    I used these constants, but still no serious effect.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Ravi Raj View Post
    I used these constants, but still no serious effect.
    No serious effect? What kind of effect did you see then?

    Trying this for example will give you the current size (in characters) of your window. Setting up the struct with values for ws_col and ws_row and using the *S* constant sets the window size. But not the actual window, the area within that is actually used.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    
    int main()
    {
        struct winsize ws = {0,0,0,0};
        int fp = 0;
    
        fp = open("/dev/ttys001", O_RDONLY);
        if(fp == -1) return 1;
    
        if( ioctl(fp, TIOCGWINSZ, &ws) == -1 ) {
            perror("");
            return 1;
        }
    
        printf("x: %d y: %d\n", ws.ws_col, ws.ws_row);
    
        close(fp);
        return 0;
    }
    The actual device file will of course differ.

  15. #15
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Quote Originally Posted by Subsonics View Post
    No serious effect? What kind of effect did you see then?

    Trying this for example will give you the current size (in characters) of your window. Setting up the struct with values for ws_col and ws_row and using the *S* constant sets the window size. But not the actual window, the area within that is actually used.

    ...
    I am using this code:

    Code:
    void
    term_size(int *rows, int *cols)
    {
        struct winsize ws;
        int fd;
    
        /* Open the controlling terminal. */
        fd = open("/dev/tty", O_RDWR);
        if (fd < 0)
            exit(1);
    
        /* Get window size of terminal. */ 
        if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
            exit(1);
    
        *rows = ws.ws_row;
        *cols = ws.ws_col;
    
        close(fd);    
    }
    Yeah, i know it just shows the terminal size and width, my question is how can I programatically maximize, minimize or resize this gnome-terminal window.
    There must be some way, may be tough but there must be !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to tell ncurses program terminal size
    By apawamajawa in forum Linux Programming
    Replies: 1
    Last Post: 07-20-2011, 02:04 AM
  2. Getting screen size - unix/linux terminal
    By enok in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2009, 04:56 AM
  3. terminal window size
    By eight8ball in forum C Programming
    Replies: 5
    Last Post: 01-08-2009, 08:48 AM
  4. Setting Console Size
    By twomers in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2006, 03:24 PM
  5. Setting the font size
    By Exile in forum Linux Programming
    Replies: 3
    Last Post: 02-21-2005, 06:38 AM

Tags for this Thread