The Pedigree Project 0.1
modules/drivers/x86/ps2mouse/main.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "pedigree/kernel/Log.h"
21#include "pedigree/kernel/compiler.h"
22#include "pedigree/kernel/machine/Device.h"
23#include "pedigree/kernel/utilities/String.h"
24#include "pedigree/kernel/utilities/Vector.h"
25#include "pedigree/kernel/utilities/utility.h"
26
27#include "Ps2Mouse.h"
28#include "modules/Module.h"
29#include "system/kernel/machine/mach_pc/Ps2Controller.h"
30
31// Global static object for the PS/2 mouse we'll be working with
32EXPORTED_PUBLIC Ps2Mouse* g_Ps2Mouse = 0;
33
34static bool entry() {
35 auto f = [](Device* p) {
36 if (g_Ps2Mouse) {
37 return p;
38 }
39
40 if (p->addresses().count() > 0) {
41 if (p->addresses()[0]->m_Name.compare("ps2-base", 8)) {
42 Ps2Controller* controller = static_cast<Ps2Controller*>(p);
43
44 Ps2Mouse* pNewChild = new Ps2Mouse(p);
45 if (pNewChild->initialise(controller)) {
46 g_Ps2Mouse = pNewChild;
47 } else {
48 ERROR("PS/2 Mouse initialisation failed!");
49 delete pNewChild;
50 }
51 }
52 }
53
54 return p;
55 };
56
57 auto c = pedigree_std::make_callable(f);
58 Device::foreach (c, 0);
59
60 // Cannot replace the child, as we need to have it present for keyboards.
61 if (g_Ps2Mouse) {
62 Device::addToRoot(g_Ps2Mouse);
63 }
64 // POSIX mouse files also use the callback-registration types when no
65 // physical mouse was found, so their exported code must remain available.
66 return true;
67}
68
69static void unload() {
70 if (g_Ps2Mouse) {
71 auto removeMouse = [](Device* device) { return device == g_Ps2Mouse ? nullptr : device; };
72 auto callback = pedigree_std::make_callable(removeMouse);
73 Device::foreach (callback, 0);
74 g_Ps2Mouse = nullptr;
75 }
76}
77
78MODULE_NAME("ps2mouse");
79MODULE_ENTRY(&entry);
80MODULE_EXIT(&unload);
81MODULE_DEPENDS(0);
static void addToRoot(Device *device)
Definition Device.cc:114
static void foreach(Callback callback, Device *root=0)
Definition Device.cc:107