Page 1 of 1
How to make a cursor?
Posted: Fri Jun 12, 2015 11:20 am
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));
}
Re: How to make a cursor?
Posted: Fri Jun 12, 2015 5:30 pm
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
Re: How to make a cursor?
Posted: Sat Jun 13, 2015 1:21 am
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
Re: How to make a cursor?
Posted: Sat Jun 13, 2015 4:38 am
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.