PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/components/audio/vumeter/XVumeter.cxx

https://github.com/ejgarcia/isabel
C++ | 403 lines | 202 code | 49 blank | 152 comment | 17 complexity | b10457a5be25007b7a7fe1547ab09043 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////
  2. //
  3. // ISABEL: A group collaboration tool for the Internet
  4. // Copyright (C) 2009 Agora System S.A.
  5. //
  6. // This file is part of Isabel.
  7. //
  8. // Isabel is free software: you can redistribute it and/or modify
  9. // it under the terms of the Affero GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // Isabel is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // Affero GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the Affero GNU General Public License
  19. // along with Isabel. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. /////////////////////////////////////////////////////////////////////////
  22. #include "XVumeter.h"
  23. #include "noise.xbm"
  24. #include "silence.xbm"
  25. XVumeter::XVumeter(Display *dpy, Window w, int vuID) {
  26. this -> vuID = vuID;
  27. XWindowAttributes winAttr;
  28. int parentWidth= 0;
  29. int parentHeight= 0;
  30. // int width= 0;
  31. // int height= 0;
  32. int depth= CopyFromParent;
  33. int visual= CopyFromParent;
  34. int winAttrMask= 0;
  35. XSetWindowAttributes setWinAttr;
  36. Colormap colormap;
  37. XColor greenColor, greenDB;
  38. XColor yellowColor, yellowDB;
  39. XColor redColor, redDB;
  40. XColor greyColor, greyDB;
  41. this -> display = dpy;
  42. this -> parent = w;
  43. XSetErrorHandler((int (*)(Display *, XErrorEvent *))XVumeter::Handler);
  44. if(!XGetWindowAttributes(
  45. display,
  46. parent,
  47. &winAttr
  48. )) {
  49. perror("XVumeter::XVumeter : XGetWindowAttributes");
  50. exit(1);
  51. }
  52. parentWidth= winAttr.width;
  53. parentHeight= winAttr.height;
  54. XSetErrorHandler((int (*)(Display *, XErrorEvent *))XVumeter::Handler);
  55. width= parentWidth;
  56. height= parentHeight;
  57. /*
  58. winAttrMask= CWBackPixmap | CWWinGravity;
  59. setWinAttr.background_pixmap= ParentRelative;
  60. setWinAttr.win_gravity=SouthGravity;
  61. frame= XCreateWindow(
  62. display,
  63. parent,
  64. 0, parentHeight-height,
  65. width, height,
  66. 0,
  67. depth,
  68. InputOutput,
  69. (Visual *)visual,
  70. winAttrMask,
  71. &setWinAttr
  72. );
  73. {
  74. XWindowAttributes xwa;
  75. XGetWindowAttributes(
  76. display,
  77. frame,
  78. &xwa
  79. );
  80. depth= xwa.depth;
  81. }
  82. */
  83. {
  84. XWindowAttributes xwa;
  85. XGetWindowAttributes(
  86. display,
  87. parent,
  88. &xwa
  89. );
  90. depth= xwa.depth;
  91. }
  92. colormap= XDefaultColormap(display, 0);
  93. black= XBlackPixel(display, 0);
  94. white= XWhitePixel(display, 0);
  95. XAllocNamedColor(
  96. display,
  97. colormap,
  98. "darkgrey",
  99. &greyColor,
  100. &greyDB
  101. );
  102. grey= greyColor.pixel;
  103. XAllocNamedColor(
  104. display,
  105. colormap,
  106. "green",
  107. &greenColor,
  108. &greenDB
  109. );
  110. green= greenColor.pixel;
  111. XAllocNamedColor(
  112. display,
  113. colormap,
  114. "yellow",
  115. &yellowColor,
  116. &yellowDB
  117. );
  118. yellow= yellowColor.pixel;
  119. XAllocNamedColor(
  120. display,
  121. colormap,
  122. "red",
  123. &redColor,
  124. &redDB
  125. );
  126. red= redColor.pixel;
  127. /*
  128. black_noise= XCreatePixmapFromBitmapData(
  129. display,
  130. frame,
  131. (char *)noise_bits,
  132. noise_width,
  133. noise_height,
  134. black,
  135. white,
  136. depth
  137. );
  138. green_noise= XCreatePixmapFromBitmapData(
  139. display,
  140. frame,
  141. (char *)noise_bits,
  142. noise_width,
  143. noise_height,
  144. green,
  145. white,
  146. depth
  147. );
  148. yellow_noise= XCreatePixmapFromBitmapData(
  149. display,
  150. frame,
  151. (char *)noise_bits,
  152. noise_width,
  153. noise_height,
  154. yellow,
  155. white,
  156. depth
  157. );
  158. red_noise= XCreatePixmapFromBitmapData(
  159. display,
  160. frame,
  161. (char *)noise_bits,
  162. noise_width,
  163. noise_height,
  164. red,
  165. white,
  166. depth
  167. );
  168. if(width> noise_width)
  169. noise_pos= (width-noise_width)/2;
  170. else
  171. noise_pos= 0;
  172. */
  173. winAttrMask= CWBackPixmap | CWWinGravity;
  174. setWinAttr.background_pixmap= None;
  175. setWinAttr.win_gravity=SouthGravity;
  176. /*
  177. for(i= 0; i< NUM_LEDS; i++) {
  178. leds[i]= XCreateWindow(
  179. display,
  180. frame,
  181. noise_pos, height-(i+1)*noise_height,
  182. noise_width, noise_height,
  183. 0,
  184. depth,
  185. InputOutput,
  186. (Visual *)visual,
  187. winAttrMask,
  188. &setWinAttr
  189. );
  190. }
  191. XMapWindow(display, frame);
  192. for(i= 0; i< NUM_LEDS; i++)
  193. XMapWindow(display, leds[i]);
  194. */
  195. myframe = XCreateWindow(display, parent, 0, 0, width, height, 0, depth, InputOutput, (Visual *)visual, winAttrMask, &setWinAttr);
  196. XMapWindow(display, myframe);
  197. gc = XCreateGC(display, myframe, 0, NULL);
  198. power= 0;
  199. maxPower= 0;
  200. maxPowerLife= 0;
  201. XSync(display, False);
  202. }
  203. XVumeter::~XVumeter() {
  204. XDestroySubwindows(display, frame);
  205. XDestroyWindow(display, frame);
  206. XSync(display, False);
  207. }
  208. void XVumeter::SetPower(int power) {
  209. int h, i;
  210. power = (int)((power/100.0)*(power/100.0)*100.0);
  211. h = power*height/100;
  212. if (h < 0) h = 0;
  213. int step = ((height)/10);
  214. h = power/10;
  215. XSetForeground(display, gc, grey);
  216. XFillRectangle(display, myframe, gc, 0, 0, width, height);
  217. // Esto es para diodo
  218. if ( (width <= 32) && (height <= 32 ) ) {
  219. XSetForeground(display, gc, black);
  220. XFillRectangle(display, myframe, gc, 0, 0, width, height);
  221. XSetForeground(display, gc, power> 80?red:power>60?yellow:power>0?green:black);
  222. XFillRectangle(display, myframe, gc, (width/2)-(((width-2)*power/100)/2), (height/2)-(((height-2)*power/100)/2), (width-2)*power/100, (height-2)*power/100);
  223. XFlush(display);
  224. this -> power = power;
  225. return;
  226. } else {
  227. for ( i = 0 ; i < 10 ; i++ ) {
  228. if ( i >= h) {
  229. XSetForeground(display, gc, grey);
  230. XFillRectangle(display, myframe, gc, 0, height-((i+1)*step-1), width, step);
  231. XSetForeground(display, gc, black);
  232. XFillRectangle(display, myframe, gc, 2, height-((i+1)*step-1), width-4, step-2);
  233. } else {
  234. XSetForeground(display, gc, grey);
  235. XFillRectangle(display, myframe, gc, 0, height-((i+1)*step-1), width, step);
  236. XSetForeground(display, gc, i > 7 ?red: i > 4?yellow: i >= 0 ?green:black);
  237. XFillRectangle(display, myframe, gc, 2, height-((i+1)*step-1), width-4, step-2);
  238. }
  239. }
  240. XFlush(display);
  241. this -> power = power;
  242. return;
  243. } /*
  244. for ( i = 0 ; i <= height ; i++ ) {
  245. int percent;
  246. if ( i > h ) {
  247. if (i%2) {
  248. XSetForeground(display, gc, grey);
  249. XDrawLine(display, myframe, gc, 0, height-i, width, height-i);
  250. } else {
  251. XSetForeground(display, gc, black);
  252. XDrawLine(display, myframe, gc, 0, height-i, width, height-i);
  253. }
  254. // XSetForeground(display, gc, grey);
  255. // XDrawLine(display, myframe, gc, 0, height-i, width, height-i);
  256. } else {
  257. if (i%2) {
  258. percent = i*100/height;
  259. XSetForeground(display, gc, percent > 80?red: percent > 60?yellow: percent > 0 ?green:black);
  260. XDrawLine(display, myframe, gc, 0, height-i, width, height-i);
  261. } else {
  262. XSetForeground(display, gc, black);
  263. XDrawLine(display, myframe, gc, 0, height-i, width, height-i);
  264. }
  265. }
  266. }
  267. XFlush(display);
  268. this -> power = power;
  269. return;
  270. */
  271. // Esto es para diodo
  272. XSetForeground(display, gc, black);
  273. XFillRectangle(display, myframe, gc, 0, 0, width, height);
  274. XSetForeground(display, gc, power> 80?red:power>60?yellow:power>0?green:black);
  275. XFillRectangle(display, myframe, gc, (width/2)-(((width-2)*power/100)/2), (height/2)-(((height-2)*power/100)/2), (width-2)*power/100, (height-2)*power/100);
  276. XFlush(display);
  277. this -> power = power;
  278. return;
  279. h = power*NUM_LEDS/100;
  280. for ( i = 0 ; i < NUM_LEDS ; i++ ) {
  281. if ( i < h ) {
  282. XSetWindowBackgroundPixmap(
  283. display,
  284. leds[i],
  285. i> RED_LED?red_noise:i>YELLOW_LED?yellow_noise:green_noise
  286. );
  287. XClearWindow(display, leds[i]);
  288. } else {
  289. XSetWindowBackgroundPixmap(
  290. display,
  291. leds[i],
  292. black_noise
  293. );
  294. XClearWindow(display, leds[i]);
  295. }
  296. }
  297. if ( power > maxPower ) {
  298. maxPower = power;
  299. maxPowerLife = 10;
  300. } else {
  301. maxPowerLife--;
  302. if (maxPowerLife <= 0) {
  303. maxPowerLife = 10;
  304. maxPower = 0;
  305. if (maxPower <= 0 ) maxPower = 0;
  306. }
  307. }
  308. h = (maxPower*NUM_LEDS/100) - 1;
  309. if (h >= 0) {
  310. XSetWindowBackgroundPixmap(
  311. display,
  312. leds[h],
  313. h > RED_LED?red_noise: h >YELLOW_LED?yellow_noise:green_noise
  314. );
  315. XClearWindow(display, leds[h]);
  316. }
  317. this -> power = power;
  318. XSync(display, False);
  319. return;
  320. }
  321. void XVumeter::Decay() {
  322. SetPower((int)(power-1));
  323. }
  324. int XVumeter::Handler(Display *dpy, XErrorEvent *err) {
  325. return 0;
  326. }
  327. XVumeter *XVumeter::Create(Display *dpy, Window w, int vuID) {
  328. XWindowAttributes winAttr;
  329. XSetErrorHandler((int (*)(Display *, XErrorEvent *))XVumeter::Handler);
  330. if(!XGetWindowAttributes(
  331. dpy,
  332. w,
  333. &winAttr
  334. )) {
  335. return NULL;
  336. }
  337. return new XVumeter(dpy, w, vuID);
  338. }
  339. int XVumeter::GetID() {
  340. return vuID;
  341. }
  342. void XVumeter::SetSize(int w, int h) {
  343. height = h;
  344. width = w;
  345. }