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 "Ps2Mouse.h"
21 #include "modules/Module.h"
22 #include "pedigree/kernel/Log.h"
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/machine/Device.h"
25 #include "pedigree/kernel/utilities/String.h"
26 #include "pedigree/kernel/utilities/Vector.h"
27 #include "pedigree/kernel/utilities/utility.h"
28 #include "system/kernel/machine/mach_pc/Ps2Controller.h"
29 
30 // Global static object for the PS/2 mouse we'll be working with
31 EXPORTED_PUBLIC Ps2Mouse *g_Ps2Mouse = 0;
32 
33 static bool entry()
34 {
35  auto f = [](Device *p) {
36  if (g_Ps2Mouse)
37  {
38  return p;
39  }
40 
41  if (p->addresses().count() > 0)
42  {
43  if (p->addresses()[0]->m_Name == "ps2-base")
44  {
45  Ps2Controller *controller = static_cast<Ps2Controller *>(p);
46 
47  Ps2Mouse *pNewChild = new Ps2Mouse(p);
48  if (pNewChild->initialise(controller))
49  {
50  g_Ps2Mouse = pNewChild;
51  }
52  else
53  {
54  ERROR("PS/2 Mouse initialisation failed!");
55  delete pNewChild;
56  }
57  }
58  }
59 
60  return p;
61  };
62 
63  auto c = pedigree_std::make_callable(f);
64  Device::foreach (c, 0);
65 
66  // Cannot replace the child, as we need to have it present for keyboards.
67  if (g_Ps2Mouse)
68  {
69  Device::addToRoot(g_Ps2Mouse);
70  return true;
71  }
72 
73  return false;
74 }
75 
76 static void unload()
77 {
78  if (g_Ps2Mouse)
79  delete g_Ps2Mouse;
80 }
81 
82 MODULE_NAME("ps2mouse");
83 MODULE_ENTRY(&entry);
84 MODULE_EXIT(&unload);
85 MODULE_DEPENDS(0);
virtual bool initialise(Ps2Controller *pController)
Definition: Ps2Mouse.cc:47
static void addToRoot(Device *device)
Definition: Device.cc:102
Definition: Device.h:43
static void foreach(Callback callback, Device *root=0)
Definition: Device.cc:94
#define ERROR(text)
Definition: Log.h:82