Skip to content

Instantly share code, notes, and snippets.

@berk76
Last active December 30, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berk76/c94161263b7d3b06dfcf85811d18d575 to your computer and use it in GitHub Desktop.
Save berk76/c94161263b7d3b06dfcf85811d18d575 to your computer and use it in GitHub Desktop.
DOS: Cursor mangling in C

Cursor mangling in C

/*********************************************************************/
/*                                                                   */
/*  All the following functions were written by Paul Edwards and     */
/*  placed in the Public Domain except for cursor() which was        */
/*  written by Gary Chambers and I am waiting for him to tell me     */
/*  whether his routine can be placed in the Public Domain.  Oh      */
/*  yeah, I modified Gary's routines *slightly*.                     */
/*                                                                   */
/*  These routines are open for comment and improvement.             */
/*  They have been tested under Turbo C++ only.                      */
/*                                                                   */
/*********************************************************************/

/* hidecur - hide the cursor */
void hidecur(void);
/* showcur - show the cursor */
void showcur(void);
/* curpush - save state then hide/unhide(0/1) cursor */
void curpush(int state);
/* curpop - return saved state of cursor */
void curpop(void);
/* cursor - internal routine */
static void cursor(int state);

/* cursor:
written by Gary Chambers, Intl C Conference (Fidonet 1:275/37)
note: the OLD, unreliable method of hiding the cursor was to set
illegal scan lines.  Setting bit 5 in CH is a documented method of
hiding the cursor.  This program uses the latter method */

#include <dos.h>

static void cursor(int state)
{
  union REGS regs;
  static int oldstart=0, oldstop=0, hidden=0;

  if (state && hidden) {         /* Restores cursor */
    regs.h.ah = 0x01;            /* BIOS Set Cursor Type */
    regs.h.ch = oldstart;        /* Retrieve starting scan line */
    regs.h.cl = oldstop;         /* Retrieve ending scan line */
    int86(0x10, &regs, &regs);   /* Call BIOS */
    hidden = 0;                  /* not hidden */
  }
  else if (!hidden) {            /* Removes cursor */
    regs.h.ah = 0x03;            /* BIOS Get Cursor Position */
    int86(0x10, &regs, &regs);   /* Call BIOS */
    oldstart = regs.h.ch;        /* Save starting scan line */
    oldstop = regs.h.cl;         /* Save ending scan line */
    regs.h.ah = 0x01;            /* BIOS Set Cursor Type */
    regs.h.ch = 0x20;            /* Set bit 5 in CH */
    int86(0x10, &regs, &regs);   /* Call BIOS */
    hidden = 1;                  /* cursor is hidden now */
  }
}

void showcur(void)
{
  cursor(1);
  return;
}

void hidecur(void)
{
  cursor(0);
  return;
}

#define CURSLIM 20
static int curcnt = 0;
static char curs_stck[CURSLIM+1];

void curpush(int state)
{
  if (curcnt != CURSLIM) /* ignore request on overflow */
  {
    if (curcnt == 0) curs_stck[curcnt] = 1;
    curcnt++;
    curs_stck[curcnt] = state;
    cursor(state);
  }
  return;
}

void curpop(void)
{
  if (curcnt > 0) /* ignore request on underflow */
  {
    curcnt--;
    cursor(curs_stck[curcnt]);
  }
  return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment