version 2.8
gdriver.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 #include "../../gfx.h"
9 
10 #if GFX_USE_GDRIVER
11 
12 #include "gdriver.h"
13 
14 #include <string.h> // For memset
15 
16 // Define the tables to hold the driver instances.
17 static GDriver *dhead;
18 static GDriver *dtail;
19 
20 // The system initialization.
21 void _gdriverInit(void) {
22 }
23 
24 // The system de-initialization.
25 void _gdriverDeinit(void) {
26  while(dhead)
27  gdriverUnRegister(dhead);
28 }
29 
30 
31 GDriver *gdriverRegister(const GDriverVMT *vmt, void *param) {
32  GDriver * pd;
33  unsigned dinstance, sinstance;
34 
35  // Loop to find the driver instance and the system instance numbers
36  dinstance = sinstance = 0;
37  for(pd = dhead; pd; pd = pd->driverchain) {
38  if (pd->vmt == vmt)
39  dinstance++;
40  if (pd->vmt->type == vmt->type)
41  sinstance++;
42  }
43 
44  // Get a new driver instance of the correct size and initialize it
45  pd = gfxAlloc(vmt->objsize);
46  if (!pd)
47  return 0;
48  memset(pd, 0, vmt->objsize);
49  pd->vmt = vmt;
50  if (vmt->init && !vmt->init(pd, param, dinstance, sinstance)) {
51  gfxFree(pd);
52  return 0;
53  }
54 
55  // Add it to the driver chain (at the end)
56  if (dhead)
57  dtail->driverchain = pd;
58  else
59  dhead = pd;
60  dtail = pd;
61 
62  // Do the post init
63  if (vmt->postinit)
64  vmt->postinit(pd);
65 
66  return pd;
67 }
68 
69 void gdriverUnRegister(GDriver *driver) {
70  GDriver *pd;
71 
72  // Safety
73  if (!driver)
74  return;
75 
76  // Remove it from the list of drivers
77  if (dhead == driver)
78  dhead = driver->driverchain;
79  else {
80  for(pd = dhead; pd->driverchain; pd = pd->driverchain) {
81  if (pd->driverchain == driver) {
82  pd->driverchain = driver->driverchain;
83  if (!pd->driverchain)
84  dtail = pd;
85  break;
86  }
87  }
88  }
89 
90  // Call the deinit()
91  if (driver->vmt->deinit)
92  driver->vmt->deinit(driver);
93 
94  // Cleanup
95  gfxFree(driver);
96 }
97 
98 GDriver *gdriverGetInstance(uint16_t type, unsigned instance) {
99  GDriver *pd;
100  unsigned sinstance;
101 
102  // Loop to find the system instance
103  sinstance = 0;
104  for(pd = dhead; pd; pd = pd->driverchain) {
105  if (pd->vmt->type == type) {
106  if (sinstance == instance)
107  return pd;
108  sinstance++;
109  }
110  }
111  return 0;
112 }
113 
114 unsigned gdriverInstanceCount(uint16_t type) {
115  GDriver *pd;
116  unsigned sinstance;
117 
118  // Loop to count the system instances
119  sinstance = 0;
120  for(pd = dhead; pd; pd = pd->driverchain) {
121  if (pd->vmt->type == type)
122  sinstance++;
123  }
124  return sinstance;
125 }
126 
127 GDriver *gdriverGetNext(uint16_t type, GDriver *driver) {
128  driver = driver ? driver->driverchain : dhead;
129 
130  while(driver && driver->vmt->type != type)
131  driver = driver->driverchain;
132 
133  return driver;
134 }
135 
137  GDriver *pd;
138  unsigned instance;
139 
140  // Loop to find the system instance
141  instance = 0;
142  for(pd = dhead; pd; pd = pd->driverchain) {
143  if (pd == driver)
144  return instance;
145  if (pd->vmt->type == driver->vmt->type)
146  instance++;
147  }
148  return (unsigned)-1;
149 }
150 
151 #endif /* GFX_USE_GDRIVER */
void gdriverUnRegister(GDriver *driver)
UnRegister a driver instance.
Definition: gdriver.c:69
GDriver * gdriverRegister(const GDriverVMT *vmt, void *param)
Register a new driver instance.
Definition: gdriver.c:31
All runtime driver structures start with this structure.
Definition: gdriver.h:58
void * gfxAlloc(size_t sz)
Allocate memory.
unsigned gdriverGetDriverInstanceNumber(GDriver *driver)
Get the instance number for a device.
Definition: gdriver.c:136
unsigned gdriverInstanceCount(uint16_t type)
Get the count of instances of a type of device.
Definition: gdriver.c:114
void gfxFree(void *ptr)
Free memory.
GDriver * gdriverGetNext(uint16_t type, GDriver *driver)
Get the next driver for a type of device.
Definition: gdriver.c:127
All driver VMT&#39;s start with this structure.
Definition: gdriver.h:66
GDriver * gdriverGetInstance(uint16_t type, unsigned instance)
Get the driver for a particular instance of a type of device.
Definition: gdriver.c:98