/video.c

https://gitlab.com/amiloradovsky/synth · C · 92 lines · 77 code · 13 blank · 2 comment · 14 complexity · 6d17902ac95369f9dab1ddf6198043df MD5 · raw file

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <strings.h>
  4. #include <err.h>
  5. #include <X11/Xutil.h>
  6. #include "video.h"
  7. #define COLORS_COUNT 3
  8. #define DPY_W(dpy) DisplayWidth(dpy, DefaultScreen(dpy))
  9. #define DPY_H(dpy) DisplayHeight(dpy, DefaultScreen(dpy))
  10. Display *dpy;
  11. Window win;
  12. GC gc ;
  13. float * buffers [ COLORS_COUNT ];
  14. u_long colors [ COLORS_COUNT ] = { 0x000000, 0xBBBBBB, 0xFFFFFF };
  15. int f_carrage;
  16. u_int width, height;
  17. int
  18. window_init ( w, h )
  19. u_int w, h ;
  20. {
  21. int i ;
  22. XSetWindowAttributes a;
  23. if (!(dpy = XOpenDisplay(NULL)))
  24. return 1 ;
  25. if ( w || h ) {
  26. width = w ? ( w < DPY_W ( dpy ) ? w : DPY_W ( dpy ) ) : h ;
  27. height = h ? ( h < DPY_H ( dpy ) ? h : DPY_H ( dpy ) ) : w ;
  28. } else
  29. return 2 ;
  30. for ( i = 0 ; i < COLORS_COUNT ; i ++ ) {
  31. *( buffers + i ) = (float *) calloc ( width, sizeof ( float ) );
  32. bzero ( *( buffers + i ), width * sizeof ( float ) );
  33. }
  34. f_carrage = 0;
  35. bzero ( &a, sizeof ( a ) );
  36. a.event_mask = KeyPressMask|KeyReleaseMask|ExposureMask;
  37. if (!(win = XCreateWindow(dpy, DefaultRootWindow(dpy), 0,0 ,
  38. width, height, 1, CopyFromParent, InputOutput,
  39. CopyFromParent, CWEventMask, &a)))
  40. return 3 ;
  41. if ( ! XMapWindow (dpy, win) )
  42. return 4 ;
  43. gc = DefaultGC ( dpy, DefaultScreen ( dpy ) );
  44. return 0 ;
  45. }
  46. void
  47. points_draw()
  48. {
  49. int i, j ;
  50. /* drawing of point on the window */
  51. /* origin of diagram: (0, height / 2) */
  52. for ( i = 1 ; i < width ; i++ )
  53. for ( j = 0 ; j < COLORS_COUNT ; j ++ ) {
  54. XSetForeground ( dpy, gc, *( colors + j ) );
  55. XDrawLine ( dpy, win, gc,
  56. i - 1, (1.0 + *(*(buffers + j) + i - 1)) * height / 2,
  57. i , (1.0 + *(*(buffers + j) + i )) * height / 2
  58. );
  59. }
  60. for ( i = 0; i < width; i++ )
  61. for ( j = 1 ; j < COLORS_COUNT ; j ++ )
  62. *( *( buffers + j - 1 ) + i ) = *( *( buffers + j ) + i );
  63. }
  64. void
  65. point_push ( float x )
  66. {
  67. XEvent ev;
  68. while ( XCheckWindowEvent ( dpy, win, ExposureMask, &ev ) == True ) {
  69. XSetForeground ( dpy, gc, * colors );
  70. XFillRectangle ( dpy, win, gc,
  71. ev.xexpose.x, ev.xexpose.y,
  72. ev.xexpose.width, ev.xexpose.height );
  73. }
  74. *( *( buffers + COLORS_COUNT - 1 ) + f_carrage++ ) = x;
  75. if ( f_carrage == width ) {
  76. f_carrage = 0 ;
  77. points_draw ();
  78. }
  79. }