]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/AtmoTools.cpp
Remove unnedeeded msg_Error.
[vlc] / modules / video_filter / atmo / AtmoTools.cpp
1 /*
2  * AtmoTools.cpp: Collection of tool and helperfunction
3  *
4  * See the README.txt file for copyright information and how to reach the author(s).
5  *
6  * $Id$
7  */
8
9 #include "AtmoTools.h"
10 #include "AtmoLiveView.h"
11 #include "AtmoSerialConnection.h"
12
13 #if !defined(_ATMO_VLC_PLUGIN_)
14 #   include "AtmoColorChanger.h"
15 #   include "AtmoLeftRightColorChanger.h"
16 #   include "AtmoDummyConnection.h"
17 #   include "AtmoDmxSerialConnection.h"
18 #endif
19
20
21 CAtmoTools::CAtmoTools(void)
22 {
23 }
24
25 CAtmoTools::~CAtmoTools(void)
26 {
27 }
28
29 void CAtmoTools::ShowShutdownColor(CAtmoDynData *pDynData)
30 {
31     pDynData->LockCriticalSection();
32
33     CAtmoConnection *atmoConnection = pDynData->getAtmoConnection();
34     CAtmoConfig *atmoConfig = pDynData->getAtmoConfig();
35     if((atmoConnection != NULL) && (atmoConfig!=NULL)) {
36        int r[ATMO_NUM_CHANNELS],g[ATMO_NUM_CHANNELS],b[ATMO_NUM_CHANNELS],i;
37        // set a special color? on shutdown of the software? mostly may use black or so ...
38        // if this function ist disabled ... atmo will continuing to show the last color...
39        if(atmoConnection->isOpen() == ATMO_TRUE) {
40                 if(atmoConfig->isSetShutdownColor() == 1) {
41                         for(i=0;i<ATMO_NUM_CHANNELS;i++) {
42                                 r[i] = atmoConfig->getShutdownColor_Red();
43                                 g[i] = atmoConfig->getShutdownColor_Green();
44                                 b[i] = atmoConfig->getShutdownColor_Blue();
45                         }
46                 atmoConnection->SendData(ATMO_NUM_CHANNELS,r,g,b);
47                 }
48        }
49         }
50
51     pDynData->UnLockCriticalSection();
52 }
53
54 EffectMode CAtmoTools::SwitchEffect(CAtmoDynData *pDynData, EffectMode newEffectMode)
55 {
56     // may need a critical section??
57     if(pDynData == NULL) {
58        return emUndefined;
59     }
60     pDynData->LockCriticalSection();
61
62     CAtmoConfig *atmoConfig = pDynData->getAtmoConfig();
63     if(atmoConfig == NULL) {
64        pDynData->UnLockCriticalSection();
65        return emUndefined;
66     }
67     CAtmoConnection *atmoConnection = pDynData->getAtmoConnection();
68
69     EffectMode oldEffectMode = atmoConfig->getEffectMode();
70     CThread *currentEffect = pDynData->getEffectThread();
71
72     // stop and delete/cleanup current Effect Thread...
73     pDynData->setEffectThread(NULL);
74     if(currentEffect!=NULL) {
75        currentEffect->Terminate();
76        delete currentEffect;
77        currentEffect = NULL;
78     }
79
80     if((atmoConnection!=NULL) && (atmoConnection->isOpen()==ATMO_TRUE)) {
81         // neuen EffectThread nur mit aktiver Connection starten...
82
83         switch(newEffectMode) {
84             case emDisabled:
85                 break;
86
87             case emStaticColor:
88                     // get values from config - and put them to all channels?
89                     int r[ATMO_NUM_CHANNELS],g[ATMO_NUM_CHANNELS],b[ATMO_NUM_CHANNELS];
90                     for(int i=0;i<ATMO_NUM_CHANNELS;i++) {
91                         r[i] = (atmoConfig->getStaticColor_Red()   * atmoConfig->getWhiteAdjustment_Red())/255;
92                         g[i] = (atmoConfig->getStaticColor_Green() * atmoConfig->getWhiteAdjustment_Green())/255;
93                         b[i] = (atmoConfig->getStaticColor_Blue()  * atmoConfig->getWhiteAdjustment_Blue())/255;
94                     }
95                     atmoConnection->SendData(ATMO_NUM_CHANNELS,r,g,b);
96                 break;
97
98             case emLivePicture:
99                 currentEffect = new CAtmoLiveView(pDynData);
100                 break;
101
102 #if !defined(_ATMO_VLC_PLUGIN_)
103             case emColorChange:
104                 currentEffect = new CAtmoColorChanger(atmoConnection, atmoConfig);
105                 break;
106 #endif
107
108 #if !defined(_ATMO_VLC_PLUGIN_)
109             case emLrColorChange:
110                 currentEffect = new CAtmoLeftRightColorChanger(atmoConnection, atmoConfig);
111                 break;
112 #endif
113         }
114
115     }
116
117     atmoConfig->setEffectMode(newEffectMode);
118
119     pDynData->setEffectThread(currentEffect);
120
121     if(currentEffect!=NULL)
122        currentEffect->Run();
123
124     pDynData->UnLockCriticalSection();
125     return oldEffectMode;
126 }
127
128 ATMO_BOOL CAtmoTools::RecreateConnection(CAtmoDynData *pDynData)
129 {
130     pDynData->LockCriticalSection();
131
132     CAtmoConnection *current = pDynData->getAtmoConnection();
133     AtmoConnectionType act = pDynData->getAtmoConfig()->getConnectionType();
134     pDynData->setAtmoConnection(NULL);
135     if(current != NULL) {
136        current->CloseConnection();
137        delete current;
138     }
139
140     switch(act) {
141            case actSerialPort: {
142                CAtmoSerialConnection *tempConnection = new CAtmoSerialConnection(pDynData->getAtmoConfig());
143                if(tempConnection->OpenConnection() == ATMO_FALSE) {
144 #if !defined(_ATMO_VLC_PLUGIN_)
145                   char errorMsgBuf[200];
146                   sprintf(errorMsgBuf,"Failed to open serial port com%d with errorcode: %d (0x%x)",
147                             pDynData->getAtmoConfig()->getComport(),
148                             tempConnection->getLastError(),
149                             tempConnection->getLastError()
150                         );
151                   MessageBox(0,errorMsgBuf,"Error",MB_ICONERROR | MB_OK);
152 #endif
153                   delete tempConnection;
154
155                   pDynData->UnLockCriticalSection();
156                   return ATMO_FALSE;
157                }
158                pDynData->setAtmoConnection(tempConnection);
159
160                CAtmoTools::SetChannelAssignment(pDynData,
161                            pDynData->getAtmoConfig()->getCurrentChannelAssignment());
162
163                pDynData->UnLockCriticalSection();
164                return ATMO_TRUE;
165            }
166
167 #if !defined(_ATMO_VLC_PLUGIN_)
168            case actDummy: {
169                CAtmoDummyConnection *tempConnection = new CAtmoDummyConnection(pDynData->getHinstance(),
170                                                                                pDynData->getAtmoConfig());
171                if(tempConnection->OpenConnection() == ATMO_FALSE) {
172                   delete tempConnection;
173
174                   pDynData->UnLockCriticalSection();
175                   return ATMO_FALSE;
176                }
177                pDynData->setAtmoConnection(tempConnection);
178
179                CAtmoTools::SetChannelAssignment(pDynData, pDynData->getAtmoConfig()->getCurrentChannelAssignment());
180
181                pDynData->UnLockCriticalSection();
182                return ATMO_TRUE;
183            }
184
185            case actDMX: {
186                // create here your DMX connections... instead of the dummy....
187                CAtmoDmxSerialConnection *tempConnection = new CAtmoDmxSerialConnection(pDynData->getAtmoConfig());
188                if(tempConnection->OpenConnection() == ATMO_FALSE) {
189                   delete tempConnection;
190
191                   pDynData->UnLockCriticalSection();
192                   return ATMO_FALSE;
193                }
194                pDynData->setAtmoConnection(tempConnection);
195
196                CAtmoTools::SetChannelAssignment(pDynData, pDynData->getAtmoConfig()->getCurrentChannelAssignment());
197
198                pDynData->UnLockCriticalSection();
199                return ATMO_TRUE;
200            }
201 #endif
202
203            default: {
204                pDynData->UnLockCriticalSection();
205                return ATMO_FALSE;
206            }
207     }
208 }
209
210 tColorPacket CAtmoTools::WhiteCalibration(CAtmoConfig *pAtmoConfig, tColorPacket ColorPacket)
211 {
212     int w_adj_red   = pAtmoConfig->getWhiteAdjustment_Red();
213     int w_adj_green = pAtmoConfig->getWhiteAdjustment_Green();
214     int w_adj_blue  = pAtmoConfig->getWhiteAdjustment_Blue();
215
216     for (int i = 0; i < ATMO_NUM_CHANNELS; i++)  {
217         ColorPacket.channel[i].r = (unsigned char)(((int)w_adj_red   * (int)ColorPacket.channel[i].r)   / 255);
218         ColorPacket.channel[i].g = (unsigned char)(((int)w_adj_green * (int)ColorPacket.channel[i].g) / 255);
219         ColorPacket.channel[i].b = (unsigned char)(((int)w_adj_blue  * (int)ColorPacket.channel[i].b)  / 255);
220     }
221     return ColorPacket;
222 }
223
224 tColorPacket CAtmoTools::ApplyGamma(CAtmoConfig *pAtmoConfig, tColorPacket ColorPacket)
225 {
226   return ColorPacket;
227 }
228
229 int CAtmoTools::SetChannelAssignment(CAtmoDynData *pDynData, int index)
230 {
231     CAtmoConfig *pAtmoConfig = pDynData->getAtmoConfig();
232     CAtmoConnection *pAtmoConnection = pDynData->getAtmoConnection();
233     int oldIndex = pAtmoConfig->getCurrentChannelAssignment();
234
235     tChannelAssignment *ca = pAtmoConfig->getChannelAssignment(index);
236     if((ca!=NULL) && (pAtmoConnection!=NULL)) {
237         pAtmoConnection->SetChannelAssignment(ca);
238         pAtmoConfig->setCurrentChannelAssignment(index);
239     }
240     return oldIndex;
241 }
242
243
244 #if !defined(_ATMO_VLC_PLUGIN_)
245
246 void CAtmoTools::SaveBitmap(HDC hdc,HBITMAP hBmp,char *fileName) {
247      BITMAPINFO bmpInfo;
248      BITMAPFILEHEADER  bmpFileHeader;
249      ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
250      bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
251
252      GetDIBits(hdc,hBmp,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
253      if(bmpInfo.bmiHeader.biSizeImage<=0)
254         bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth * abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
255      void *pBuf = malloc(bmpInfo.bmiHeader.biSizeImage);
256      bmpInfo.bmiHeader.biCompression=BI_RGB;
257
258      GetDIBits(hdc,hBmp,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);
259
260
261      bmpFileHeader.bfReserved1=0;
262      bmpFileHeader.bfReserved2=0;
263      bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
264 #ifdef _ATMO_VLC_PLUGIN_
265      bmpFileHeader.bfType = VLC_TWOCC('M','B');
266 #else
267      bmpFileHeader.bfType = MakeWord('M','B');
268 #endif
269      bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
270
271
272      FILE *fp = NULL;
273      fp = fopen(fileName,"wb");
274      fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
275      fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
276      fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
277      fclose(fp);
278 }
279
280 #endif
281