Project

General

Profile

Statistics
| Revision:

root / proj / proj.c @ 150

History | View | Annotate | Download (3.82 KB)

1 144 up20180655
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4
5 147 up20180655
#include <stdbool.h>
6 144 up20180655
#include <stdint.h>
7
8 149 up20180655
#include "i8254.h"
9
#include "kbc_macros.h"
10
#include "graphics_macros.h"
11 150 up20180655
#include "mouse_macros.h"
12 149 up20180655
#include "proj_macros.h"
13
#include "errors.h"
14 147 up20180655
15 149 up20180655
#include "kbc.h"
16
#include "graphics.h"
17
#include "timer.h"
18
#include "keyboard.h"
19 150 up20180655
#include "mouse.h"
20 149 up20180655
#include "utils.h"
21
22 144 up20180655
int main(int argc, char* argv[]) {
23
24
    lcf_set_language("EN-US");
25
26
    //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
27
28
    //lcf_log_output("/home/lcom/labs/proj/output.txt");
29
30
    if (lcf_start(argc, argv)) return 1;
31
32
    lcf_cleanup();
33
34
    return 0;
35
}
36 147 up20180655
37 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
38 147 up20180655
39 149 up20180655
    /// loop stuff
40
    int ipc_status, r;
41
    message msg;
42
    /// Keyboard interrupt handling
43
    uint8_t kbc_irq_bit = KBC_IRQ;
44
    int kbc_id = 0;
45
    int kbc_irq = BIT(kbc_irq_bit);
46
    if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
47
        return 1;
48
    }
49 147 up20180655
50 149 up20180655
    /// Timer interrupt handling
51
    //const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
52
    uint8_t timer_irq_bit = TIMER0_IRQ;
53
    int timer_id = 0;
54
    int timer_irq = BIT(timer_irq_bit);
55
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) {
56
        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
57
        if (free_memory_map()) {
58
            printf("%s: lm_free failed\n", __func__);
59
        }
60
        return 1;
61
    }
62
63 150 up20180655
    /// Mouse interrupt handling
64
    uint8_t mouse_irq_bit = MOUSE_IRQ;
65
    int mouse_id = 0;
66
    int mouse_irq = BIT(mouse_irq_bit);
67
68
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
69
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
70
    if (mouse_set_data_report(true)) return 1; // enables mouse data reporting
71
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
72
73 147 up20180655
    /// cycle
74
    int good = 1;
75
    while (good) {
76
        /* Get a request message. */
77
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
78
            printf("driver_receive failed with %d", r);
79
            continue;
80
        }
81
        if (is_ipc_notify(ipc_status)) { /* received notification */
82
            switch (_ENDPOINT_P(msg.m_source)) {
83
                case HARDWARE: /* hardware interrupt notification */
84
                    if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */
85
                        kbc_ih();
86
                        if (scancode[0] == ESC_BREAK_CODE) good = 0;
87
                    }
88 149 up20180655
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
89
                        timer_int_handler();
90
                    }
91 150 up20180655
92
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
93
                        mouse_ih();
94
                    }
95 147 up20180655
                    break;
96
                default:
97
                    break; /* no other notifications expected: do nothing */
98
            }
99
        } else { /* received standart message, not a notification */
100
            /* no standart message expected: do nothing */
101
        }
102
    }
103 149 up20180655
104
    // Unsubscribe Keyboard interrupts
105
    if (unsubscribe_interrupt(&kbc_id)) {
106
        return 1;
107
    };
108
109
    // Unsubscribe Timer Interrupts
110
    if (unsubscribe_interrupt(&timer_id)) {
111
        return 1;
112
    }
113
114 150 up20180655
    // Unsubscribe Mouse Interrupts
115
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
116
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
117
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
118
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
119
120 149 up20180655
    return 0;
121 147 up20180655
}