How to make a cursor?

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
Blagoje
Posts: 1
Joined: Fri Jun 12, 2015 11:11 am

How to make a cursor?

Post by Blagoje »

I'm new in OS development, and i don't know how to add cursor, i'm try with this code, but dose't work.
If you know code for cursor in C / C++ that will be great :)

Code: Select all

void update_cursor(int x, int y){
    unsigned short position=(x*80) + y;
 
    // cursor LOW port to vga INDEX register
    outb(0x3D4, 0x0F);
    outb(0x3D5, (unsigned char)(position&0xFF));
    // cursor HIGH port to vga INDEX register
    outb(0x3D4, 0x0E);
    outb(0x3D5, (unsigned char )((position>>8)&0xFF));
 }
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How to make a cursor?

Post by Brendan »

Hi,
Blagoje wrote:I'm new in OS development, and i don't know how to add cursor, i'm try with this code, but dose't work.
The cursor that code is trying to use assumes the video card is in a legacy "VGA compatible" mode (e.g. and not less obsolete SVGA mode or its own "native PCI" mode), requires an obsolete/ugly text mode, can only provide an ugly/blocky cursor, has poor "one character" precision (e.g. you can't use it for a mouse pointer where pixel precision is standard), there's only one (can't use it for both keyboard cursor and mouse cursor) and the IO port writes are much slower than accessing display memory. Basically it's deprecated, unreliable/not portable, ugly, limited and slow.

In general I'd recommend forgetting that text mode exists, setting a higher resolution graphical mode (e.g. with VBE), using a framebuffer, and drawing your own cursor/s however you want wherever you want in software (until you've got native drivers capable of using hardware acceleration).

Note that (depending on OS/boot loader) there's a brief period of time (during very early boot) where you want to display things like error messages before a usable video mode (and/or fonts) are setup. This is the only case where text mode is acceptable; but for this case you're never waiting for user input and therefore a cursor should not be shown to begin with.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

Re: How to make a cursor?

Post by freecrac »

I like to use a blinking software cursor within a higher resolution graphical mode (e.g. with VBE), using a framebuffer.

Code: Select all

	  cli
	  xor      ax, ax
 	  mov      es, ax
	  mov      ebx, DWORD PTR es:[8 * 4]
	  mov      DWORD PTR[ALTVEC], ebx
	  mov      cs:DWORD PTR[OLDVEC], ebx
	  mov      es:[8 * 4], OFFSET NEUVEC
	  mov      es:[(8 * 4)+2], cs
	  mov      al, 36h           ; 18,2 Hertz (Standard)
	  out      43h, al
	  xor      al, al
	  out      40h, al           ; low
	  out      40h, al           ; high
	  sti
;---------------------------------------------------

	       cmp     BYTE PTR[CURFLAG], 0
	       jz  short SWAP
	       call SKIPCHA                 ; draw inverse char to the screen
	       jmp  short SKIP
;---------------------------
SWAP:     call GETCHAR                 ; draw char to the screen
;---------------------------
SKIP:



;---------------------------------------------------
NEUVEC:    inc     BYTE PTR[CURFLAG+1]
	        cmp     BYTE PTR[CURFLAG+1], 7 ; delay
	        jb  short NOHIT
	        mov     BYTE PTR[CURFLAG+1], 0
	        xor     BYTE PTR[CURFLAG], 1    ; reverse Aktiv-Flag

NOHIT:    DB 0EAh                         ; jmp far
OLDVEC:   DD 0
;---------------------------------------------------

.DATA
ALTVEC  DD 0
CURFLAG DB 0, 0

embryo2
Member
Member
Posts: 397
Joined: Wed Jun 03, 2015 5:03 am

Re: How to make a cursor?

Post by embryo2 »

Blagoje wrote:i don't know how to add cursor
You are trying to use VGA hardware in text mode, so you should understand how it works. Here yo can start. In particular, some extracted information about text mode cursor is available here.

But cursor also can be emulated by assigning special attributes to the second byte in the VGA text buffer's byte pairs (e.g. blinking bit). In such a way you can forget about using VGA ports and emulate the cursor entirely in software. The cost here is the emulation support development time, but it allows you to implement any custom cursor logic you want without reading a word about VGA hardware.
My previous account (embryo) was accidentally deleted, so I have no chance but to use something new. But may be it was a good lesson about software reliability :)
Post Reply