tape-kernel 1.0
a modular modern independent kernel
Loading...
Searching...
No Matches
cm.c
Go to the documentation of this file.
1//cm.c - cursor and mouse stuff
2#include "../lib/types.h"
3#include "cm.h"
4#include "vga.h"
5#include "io.h"
6#include "../lib/err.h"
7
8/**
9 * @brief cnb syncs current cursor vals too 2 ptrs of vars for them
10 *
11 * cnb is a function that gets the current cursors x and y position
12 *
13 * a common way to use it is
14 * @code{.c}
15 * #include "../io/cm.h" //or just cm.h
16 * int cx, cy; //cursor x and cursor y
17 * cnb(&cx, &cy);
18 * @endcode
19 *
20 * @see cob()
21*/
22void __cnb(int* x, int* y) {
23 outb(0x3D4, 0x0E); //select high byte cpu reg
24 uint8_t hgh = inb(0x3D5); //read high byte
25 outb(0x3D4, 0x0F); //select low byte now
26 uint8_t low = inb(0x3D5);
27
28 uint16_t pos = (hgh << 8) | low; //calculate position
29 *x = pos % 80; //calculate x and y based of position
30 *y = pos / 80;
31}
32
33/**
34 * @brief cob exports cursor positions
35 *
36 * cob is a function that sets the cursor to a x and y position
37 *
38 * use cob with
39 * @code{.c}
40 * #include "../io/cm.h" //or just cm.h
41 * cob(0, 0); //or your values
42 * @endcode
43 *
44 * @warning when coding after running cob you must re-sync your cx and cy, DO NOT DO
45 * @code{.c}
46 * cnb(&cx, &cy);
47 * cob(cx, cy + 1);
48 * (rest of code)
49 * @endcode
50 *
51 * the rest of the code will still have the old cursor values and not be syncd, instead do
52 * @code{.c}
53 * cnb(&cx, &cy);
54 * cob(cx, cy + 1);
55 * cnb(&cx, &cy);
56 * (rest of code)
57 * @endcode
58 *
59 * this is good because the rest of your code will have up to date cursor positions
60 *
61 * @see cnb()
62*/
63void __cob(int x, int y) {
64 if (y >= 25) {
65 scrl(); //scroll if we're past bottom
66 y = 24;
67 }
68 if (y < 0) y = 0; //clamps
69 if (x >= 80) x = 79;
70 if (x < 0) x = 0;
71
72 uint16_t pos = (uint16_t)(y * 80 + x); //calculate pos based off given position
73
74 outb(0x3D4, 0x0F); //request low byte
75 outb(0x3D5, (uint8_t)(pos & 0xFF)); //write to data registry pos for low
76
77 outb(0x3D4, 0x0E); //request high byte now
78 outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); //write data reg for high byte
79}
80
81/**
82 * @brief scur shows the hardware text cursor
83 *
84 * scur is a function that enables the vga text cursor by writing
85 * cursor start and end scanlines to the crtc registers
86 *
87 * using scur is done with
88 * @code{.c}
89 * #include "../io/cm.h" //or just cm.h
90 * scur();
91 * @endcode
92 *
93 * @see hcur(), cob()
94 */
95void __scur(void) {
96 outb(0x3D4, 0x0A); //cursor start register
97 outb(0x3D5, 0x06); //normal cursor (scanline 6 to 7)
98 outb(0x3D4, 0x0B); //cursor end register
99 outb(0x3D5, 0x07); //bottom scanline
100}
101
102/**
103 * @brief hcur hides the hardware text cursor
104 *
105 * hcur is a function that disables the vga text cursor by setting
106 * bit 5 of the cursor start register, making the cursor invisible
107 *
108 * using hcur is done with
109 * @code{.c}
110 * #include "../io/cm.h" //or just cm.h
111 * hcur();
112 * @endcode
113 *
114 * @see scur(), cob()
115 */
116void __hcur(void) {
117 outb(0x3D4, 0x0A); //cursor start register
118 outb(0x3D5, 0x20); //bit 5 = 1 disables cursor
119}
void __scur(void)
scur shows the hardware text cursor
Definition cm.c:95
void __hcur(void)
hcur hides the hardware text cursor
Definition cm.c:116
void __cnb(int *x, int *y)
cnb syncs current cursor vals too 2 ptrs of vars for them
Definition cm.c:22
void __cob(int x, int y)
cob exports cursor positions
Definition cm.c:63
#define inb
Definition io.h:6
#define outb
Definition io.h:7
unsigned short uint16_t
Definition types.h:29
unsigned char uint8_t
Definition types.h:28
#define scrl
Definition vga.h:8