BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / accelerants / 3dfx / accelerant.cpp
blob813494dababd62c28e74a974e0f6bd88410e539a
1 /*
2 * Copyright 2007-2010 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Gerald Zajac
7 */
9 #include "accelerant.h"
11 #include <errno.h>
12 #include <string.h>
13 #include <unistd.h>
17 AccelerantInfo gInfo; // global data used by various source files of
18 // accelerant.
22 static status_t
23 InitCommon(int fileDesc)
25 // Initialization function used by primary and cloned accelerants.
27 gInfo.deviceFileDesc = fileDesc;
29 // Get area ID of shared data from driver.
31 area_id sharedArea;
32 status_t result = ioctl(gInfo.deviceFileDesc, TDFX_GET_SHARED_DATA,
33 &sharedArea, sizeof(sharedArea));
34 if (result != B_OK)
35 return result;
37 gInfo.sharedInfoArea = clone_area("3DFX shared info",
38 (void**)&(gInfo.sharedInfo), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA,
39 sharedArea);
40 if (gInfo.sharedInfoArea < 0)
41 return gInfo.sharedInfoArea; // sharedInfoArea has error code
43 gInfo.regsArea = clone_area("3DFX regs area", (void**)&(gInfo.regs),
44 B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, gInfo.sharedInfo->regsArea);
45 if (gInfo.regsArea < 0) {
46 delete_area(gInfo.sharedInfoArea);
47 return gInfo.regsArea; // regsArea has error code
50 return B_OK;
54 static void
55 UninitCommon(void)
57 // This function is used by both primary and cloned accelerants.
59 delete_area(gInfo.regsArea);
60 gInfo.regs = 0;
62 delete_area(gInfo.sharedInfoArea);
63 gInfo.sharedInfo = 0;
67 status_t
68 InitAccelerant(int fileDesc)
70 // Initialize the accelerant. fileDesc is the file handle of the device
71 // (in /dev/graphics) that has been opened by the app_server.
73 TRACE("Enter InitAccelerant()\n");
75 gInfo.bAccelerantIsClone = false; // indicate this is primary accelerant
77 status_t result = InitCommon(fileDesc);
78 if (result == B_OK) {
79 SharedInfo& si = *gInfo.sharedInfo;
81 TRACE("Vendor ID: 0x%X, Device ID: 0x%X\n", si.vendorID, si.deviceID);
83 // Ensure that InitAccelerant is executed just once (copies should be clones)
85 if (si.bAccelerantInUse) {
86 result = B_NOT_ALLOWED;
87 } else {
88 result = TDFX_Init(); // perform init related to current chip
89 if (result == B_OK) {
90 result = si.engineLock.Init("3DFX engine lock");
91 if (result == B_OK)
92 result = si.overlayLock.Init("3DFX overlay lock");
93 if (result == B_OK) {
94 TDFX_ShowCursor(false);
96 // ensure that this function won't be executed again
97 // (copies should be clones)
98 si.bAccelerantInUse = true;
103 if (result != B_OK)
104 UninitCommon();
107 TRACE("Leave InitAccelerant(), result: 0x%X\n", result);
108 return result;
112 ssize_t
113 AccelerantCloneInfoSize(void)
115 // Return the number of bytes required to hold the information required
116 // to clone the device. The information is merely the name of the device;
117 // thus, return the size of the name buffer.
119 return B_OS_NAME_LENGTH;
123 void
124 GetAccelerantCloneInfo(void* data)
126 // Return the info required to clone the device. Argument data points to
127 // a buffer which is the size returned by AccelerantCloneInfoSize().
129 ioctl(gInfo.deviceFileDesc, TDFX_DEVICE_NAME, data, B_OS_NAME_LENGTH);
133 status_t
134 CloneAccelerant(void* data)
136 // Initialize a copy of the accelerant as a clone. Argument data points to
137 // a copy of the data which was returned by GetAccelerantCloneInfo().
139 TRACE("Enter CloneAccelerant()\n");
141 char path[MAXPATHLEN] = "/dev/";
142 strcat(path, (const char*)data);
144 gInfo.deviceFileDesc = open(path, B_READ_WRITE); // open the device
145 if (gInfo.deviceFileDesc < 0)
146 return errno;
148 gInfo.bAccelerantIsClone = true;
150 status_t result = InitCommon(gInfo.deviceFileDesc);
151 if (result != B_OK) {
152 close(gInfo.deviceFileDesc);
153 return result;
156 result = gInfo.modeListArea = clone_area("3DFX cloned display_modes",
157 (void**) &gInfo.modeList, B_ANY_ADDRESS, B_READ_AREA,
158 gInfo.sharedInfo->modeArea);
159 if (result < 0) {
160 UninitCommon();
161 close(gInfo.deviceFileDesc);
162 return result;
165 TRACE("Leave CloneAccelerant()\n");
166 return B_OK;
170 void
171 UninitAccelerant(void)
173 delete_area(gInfo.modeListArea);
174 gInfo.modeList = NULL;
176 UninitCommon();
178 if (gInfo.bAccelerantIsClone)
179 close(gInfo.deviceFileDesc);
183 status_t
184 GetAccelerantDeviceInfo(accelerant_device_info* adi)
186 // Get info about the device.
188 SharedInfo& si = *gInfo.sharedInfo;
190 adi->version = 1;
191 strcpy(adi->name, "3dfx chipset");
192 strcpy(adi->chipset, si.chipName);
193 strcpy(adi->serial_no, "unknown");
194 adi->memory = si.maxFrameBufferSize;
195 adi->dac_speed = 270;
197 return B_OK;