How do I get the cursor position?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kinghajj

How do I get the cursor position?

Post by kinghajj »

I know how to set it using the hardware ports, but I don't know how to get it's position. Can anyone give me a link to someplace that'll explain it, or have code for it?
XStream

Re:How do I get the cursor position?

Post by XStream »

Read the Cursor Location High/Low Indexes (0xE/0xF) of the CRTC Register which when combined to form a word will give you the location in characters of the cursor.

For example, setting the cursor to row 17 and column 2 you would get the word 0x0552, which is row * 80 + column, so to determine the location just do this:

Code: Select all

assume ax equals the location

xor dx, dx
div ax, 80

ax = row
dx = column
Now, seeing as though I am just starting to learn how to program the VGA registers, if you could show me how to set the cursor position that would be good. :)

Cheers.
AR

Re:How do I get the cursor position?

Post by AR »

Changing cursor position is in the Wiki >
http://www.osdev.org/osfaq2/index.php/H ... 20print%3F

A general, and poorly explained, run down of the VGA Ports/Registers is here at bonafide:
http://www.osdever.net/documents/vga_po ... ?the_id=26

I found this page with a quick google:
http://www.osdever.net/tutorials/brunma ... ?the_id=65
Relevant part being:

Code: Select all

   unsigned short offset;
   out(0x3D4, 14);
   offset = in(0x3D5) << 8;
   out(0x3D4, 15);
   offset |= in(0x3D5);
offset now contains the position of the cursor (X * Y). You'll need to divide by 80 to get Y and get the modulus of 80 for X.
As a side note, it is also recommended that you keep track of the position internally and don't continually re-read it as it is supposedly quite slow.
XStream

Re:How do I get the cursor position?

Post by XStream »

Thanks for that link to the Wiki, I really need to remember about that resource.

I did actually assume that writing the same indexes would set the location of the cursor but it never changed, oh well I must have done something wrong.

Cheers.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:How do I get the cursor position?

Post by bubach »

Code: Select all

; Get the cursor pos. OUT: DH = X   DL = Y
;------------------------------------------
getcursorxy:
      push   eax
      push   ebx

      call   getcursor
      mov   ax, bx
      mov   dl, 80
      div   dl
      mov   dl, al
      mov   dh, ah

      pop   ebx
      pop   eax
      ret



; Get the cursor pos. OUT: BX = Offset
;--------------------------------------
getcursor:
      push   eax
      push   edx

      mov   dx, 0x3D4
      mov   al, 0x0E
      out   dx, al
      inc   dx
      in   al, dx
      mov   bh, al
      mov   al, 0x0F
      dec   dx
      out   dx, al
      inc   dx
      in   al, dx
      mov   bl, al

      pop   edx
      pop   eax
      ret
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How do I get the cursor position?

Post by Pype.Clicker »

there are little chance you actually *need* to get the cursor position! Only your kernel code may have set the position so you just have to remember that last location in some variable that you read back, and voil?.
dh

Re:How do I get the cursor position?

Post by dh »

What I do is i have three variables in the screen.c module:

Code: Select all

int _X;
int _Y;
int _COLOR;  // should be colour
What happens is when a kPrint, _Print or other /"non-positioning"/ function wants to put a character, it updates _X and _Y then calls kPutChar(chara, _Y, _X, _COLOR).

(Just as a small snippit, I got minimal boundary checking (can be turned on + off) which wraps the cursor to wherever it should be. So if the next character is the beginning of the line and a backspace character is printed, the _Print function (currently the only print avilable), sets the _X to the limit and _Y--.)

Cheers, DH
Post Reply