Search code examples
cncursescurses

How does getmaxyx work? (from curses)


I'm confused on how the "function" getmaxyx works... because I know it's actually a macro. But how does this macro work?

Code example:

#include <ncurses.h>

int main() {
  int col, row;
  initscr();
  getmaxyx(stdscr,row,col);
  endwin();
  return 0;
}

Solution

  • Reading the ncurses.h header file reveals that getmaxyx is indeed a macro:

    #define getmaxyx(win,y,x)       (y = getmaxy(win), x = getmaxx(win))
    

    Further investigation shows that getmaxy and getmaxx are also macros:

    #define getmaxx(win)            ((win) ? ((win)->_maxx + 1) : ERR)
    #define getmaxy(win)            ((win) ? ((win)->_maxy + 1) : ERR)
    

    Now, the argument win is a pointer to WINDOW. In fact, WINDOW is a typedef of struct _win_st. Perusal of this struct reveals the fields:

    NCURSES_SIZE_T _maxy, _maxx; /* maximums of x and y, NOT window size */
    

    So, when a new window is created, it is associated with a WINDOW structure that contains information about the window. In particular, the maximum x and y coordinates of the window are stored in the fields _maxx and _maxy. These fields are accessed by the macro getmaxyx() to provide the window dimensions.