display-x11.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:8k
源码类别:

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * x264: h264 encoder
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 Tuukka Toivonen <tuukkat@ee.oulu.fi>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  19.  *****************************************************************************/
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "display.h"
  25. static long event_mask = ConfigureNotify|ExposureMask|KeyPressMask|ButtonPressMask|StructureNotifyMask|ResizeRedirectMask;
  26. static Display *disp_display = NULL;
  27. static struct disp_window {
  28. int init;
  29. Window window;
  30. } disp_window[10];
  31. static inline void disp_chkerror(int cond, char *e) {
  32. if (!cond) return;
  33. fprintf(stderr, "error: %sn", e ? e : "?");
  34. abort();
  35. }
  36. static void disp_init_display(void) {
  37. Visual *visual;
  38. int dpy_class;
  39. int screen;
  40. int dpy_depth;
  41. if (disp_display != NULL) return;
  42. memset(&disp_window, 0, sizeof(disp_window));
  43. disp_display = XOpenDisplay("");
  44. disp_chkerror(!disp_display, "no display");
  45. screen = DefaultScreen(disp_display);
  46. visual = DefaultVisual(disp_display, screen);
  47. dpy_class = visual->class;
  48. dpy_depth = DefaultDepth(disp_display, screen);
  49. disp_chkerror(!((dpy_class == TrueColor && dpy_depth == 32)
  50. || (dpy_class == TrueColor && dpy_depth == 24)
  51. || (dpy_class == TrueColor && dpy_depth == 16)
  52. || (dpy_class == PseudoColor && dpy_depth == 8)),
  53. "requires 8 bit PseudoColor or 16/24/32 bit TrueColor display");
  54. }
  55. static void disp_init_window(int num, int width, int height, const unsigned char *tit) {
  56. XSizeHints *shint;
  57. XSetWindowAttributes xswa;
  58. XEvent xev;
  59. int screen = DefaultScreen(disp_display);
  60. Visual *visual = DefaultVisual (disp_display, screen);
  61. unsigned int fg, bg;
  62. unsigned int mask;
  63. char title[200];
  64. Window window;
  65. int dpy_depth;
  66. if (tit) {
  67. snprintf(title, 200, "%s: %i/disp", tit, num);
  68. } else {
  69. snprintf(title, 200, "%i/disp", num);
  70. }
  71. shint = XAllocSizeHints();
  72. disp_chkerror(!shint, "memerror");
  73. shint->min_width = shint->max_width = shint->width = width;
  74. shint->min_height = shint->max_height = shint->height = height;
  75. shint->flags = PSize | PMinSize | PMaxSize;
  76. disp_chkerror(num<0 || num>=10, "bad win num");
  77. if (!disp_window[num].init) {
  78. disp_window[num].init = 1;
  79. bg = WhitePixel(disp_display, screen);
  80. fg = BlackPixel(disp_display, screen);
  81. dpy_depth = DefaultDepth(disp_display, screen);
  82. if (dpy_depth==32 || dpy_depth==24 || dpy_depth==16) {
  83. mask |= CWColormap;
  84. xswa.colormap = XCreateColormap(disp_display, DefaultRootWindow(disp_display), visual, AllocNone);
  85. }
  86. xswa.background_pixel = bg;
  87. xswa.border_pixel = fg;
  88. xswa.backing_store = Always;
  89. xswa.backing_planes = -1;
  90. xswa.bit_gravity = NorthWestGravity;
  91. mask = CWBackPixel | CWBorderPixel | CWBackingStore | CWBackingPlanes | CWBitGravity;
  92. window = XCreateWindow(disp_display, DefaultRootWindow(disp_display),
  93. shint->x, shint->y, shint->width, shint->height,
  94. 1, dpy_depth, InputOutput, visual, mask, &xswa);
  95. disp_window[num].window = window;
  96. XSelectInput(disp_display, window, event_mask);
  97. XSetStandardProperties(disp_display, window, title, title, None, NULL, 0, shint); /* Tell other applications about this window */
  98. XMapWindow(disp_display, window); /* Map window. */
  99. do { /* Wait for map. */
  100. XNextEvent(disp_display, &xev);
  101. } while (xev.type!=MapNotify || xev.xmap.event!=window);
  102. //XSelectInput(disp_display, window, KeyPressMask); /*  XSelectInput(display, window, NoEventMask);*/
  103. }
  104. window = disp_window[num].window;
  105. XSetStandardProperties(disp_display, window, title, title, None, NULL, 0, shint); /* Tell other applications about this window */
  106. XResizeWindow(disp_display, window, width, height);
  107. XSync(disp_display, 1);
  108. XFree(shint);
  109. }
  110. void disp_sync(void) {
  111. XSync(disp_display, 1);
  112. }
  113. void disp_setcolor(unsigned char *name) {
  114. int screen;
  115. GC gc;
  116. XColor c_exact, c_nearest;
  117. Colormap cm;
  118. Status st;
  119. screen = DefaultScreen(disp_display);
  120. gc = DefaultGC(disp_display, screen); /* allocate colors */
  121. cm = DefaultColormap(disp_display, screen);
  122. st = XAllocNamedColor(disp_display, cm, name, &c_nearest, &c_exact);
  123. disp_chkerror(st!=1, "XAllocNamedColor error");
  124. XSetForeground(disp_display, gc, c_nearest.pixel);
  125. }
  126. void disp_gray(int num, char *data, int width, int height, int stride, const unsigned char *tit) {
  127. Visual *visual;
  128. XImage *ximage;
  129. unsigned char *image;
  130. int y,x,pixelsize;
  131. char dummy;
  132. int t = 1;
  133. int dpy_depth;
  134. int screen;
  135. GC gc;
  136. //XEvent xev;
  137. disp_init_display();
  138. disp_init_window(num, width, height, tit);
  139. screen = DefaultScreen(disp_display);
  140. visual = DefaultVisual(disp_display, screen);
  141. dpy_depth = DefaultDepth(disp_display, screen);
  142. ximage = XCreateImage(disp_display, visual, dpy_depth, ZPixmap, 0, &dummy, width, height, 8, 0);
  143. disp_chkerror(!ximage, "no ximage");
  144. if (*(char *)&t == 1) {
  145. ximage->byte_order = LSBFirst;
  146. ximage->bitmap_bit_order = LSBFirst;
  147. } else {
  148. ximage->byte_order = MSBFirst;
  149. ximage->bitmap_bit_order = MSBFirst;
  150. }
  151. pixelsize = dpy_depth>8 ? sizeof(int) : sizeof(unsigned char);
  152. image = malloc(width * height * pixelsize);
  153. disp_chkerror(!image, "malloc failed");
  154. for (y=0; y<height; y++) for (x=0; x<width; x++) {
  155. memset(&image[(width*y + x)*pixelsize], data[y*stride+x], pixelsize);
  156. }
  157. ximage->data = image;
  158. gc = DefaultGC(disp_display, screen); /* allocate colors */
  159. // XUnmapWindow(disp_display, disp_window[num].window); /* Map window. */
  160. // XMapWindow(disp_display, disp_window[num].window); /* Map window. */
  161. XPutImage(disp_display, disp_window[num].window, gc, ximage, 0, 0, 0, 0, width, height);
  162. // do { /* Wait for map. */
  163. // XNextEvent(disp_display, &xev);
  164. // } while (xev.type!=MapNotify || xev.xmap.event!=disp_window[num].window);
  165. XPutImage(disp_display, disp_window[num].window, gc, ximage, 0, 0, 0, 0, width, height);
  166. XDestroyImage(ximage);
  167. XSync(disp_display, 1);
  168. }
  169. void disp_gray_zoom(int num, char *data, int width, int height, int stride, const unsigned char *tit, int zoom) {
  170. unsigned char *dataz;
  171. int y,x,y0,x0;
  172. dataz = malloc(width*zoom * height*zoom);
  173. disp_chkerror(!dataz, "malloc");
  174. for (y=0; y<height; y++) for (x=0; x<width; x++) {
  175. for (y0=0; y0<zoom; y0++) for (x0=0; x0<zoom; x0++) {
  176. dataz[(y*zoom + y0)*width*zoom + x*zoom + x0] = data[y*stride+x];
  177. }
  178. }
  179. disp_gray(num, dataz, width*zoom, height*zoom, width*zoom, tit);
  180. free(dataz);
  181. }
  182. void disp_point(int num, int x1, int y1) {
  183. int screen;
  184. GC gc;
  185. screen = DefaultScreen(disp_display);
  186. gc = DefaultGC(disp_display, screen); /* allocate colors */
  187. XDrawPoint(disp_display, disp_window[num].window, gc, x1, y1);
  188. // XSync(disp_display, 1);
  189. }
  190. void disp_line(int num, int x1, int y1, int x2, int y2) {
  191. int screen;
  192. GC gc;
  193. screen = DefaultScreen(disp_display);
  194. gc = DefaultGC(disp_display, screen); /* allocate colors */
  195. XDrawLine(disp_display, disp_window[num].window, gc, x1, y1, x2, y2);
  196. // XSync(disp_display, 1);
  197. }
  198. void disp_rect(int num, int x1, int y1, int x2, int y2) {
  199. int screen;
  200. GC gc;
  201. screen = DefaultScreen(disp_display);
  202. gc = DefaultGC(disp_display, screen); /* allocate colors */
  203. XDrawRectangle(disp_display, disp_window[num].window, gc, x1, y1, x2-x1, y2-y1);
  204. // XSync(disp_display, 1);
  205. }