FreeRDP
rdtk_x11.c
1 
19 #include <rdtk/config.h>
20 
21 #include <stdint.h>
22 
23 #include <winpr/wlog.h>
24 #include <rdtk/rdtk.h>
25 
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 
29 #define TAG "rdtk.sample"
30 
31 int main(int argc, char** argv)
32 {
33  int rc = 1;
34  GC gc = NULL;
35  int depth = 0;
36  int x = 0;
37  int y = 0;
38  int width = 0;
39  int height = 0;
40  uint8_t* buffer = NULL;
41  int scanline = 0;
42  int pf_count = 0;
43  XEvent event;
44  XImage* image = NULL;
45  Pixmap pixmap = 0;
46  Screen* screen = NULL;
47  Visual* visual = NULL;
48  int scanline_pad = 0;
49  int screen_number = 0;
50  Display* display = NULL;
51  Window window = 0;
52  Window root_window = 0;
53  rdtkEngine* engine = NULL;
54  rdtkSurface* surface = NULL;
55  unsigned long border = 0;
56  unsigned long background = 0;
57  XPixmapFormatValues* pf = NULL;
58  XPixmapFormatValues* pfs = NULL;
59 
60  WINPR_UNUSED(argc);
61  WINPR_UNUSED(argv);
62 
63  display = XOpenDisplay(NULL);
64 
65  if (!display)
66  {
67  WLog_ERR(TAG, "Cannot open display");
68  return 1;
69  }
70 
71  x = 10;
72  y = 10;
73  width = 640;
74  height = 480;
75 
76  screen_number = DefaultScreen(display);
77  screen = ScreenOfDisplay(display, screen_number);
78  visual = DefaultVisual(display, screen_number);
79  gc = DefaultGC(display, screen_number);
80  depth = DefaultDepthOfScreen(screen);
81  root_window = RootWindow(display, screen_number);
82  border = BlackPixel(display, screen_number);
83  background = WhitePixel(display, screen_number);
84 
85  scanline_pad = 0;
86 
87  pfs = XListPixmapFormats(display, &pf_count);
88 
89  for (int index = 0; index < pf_count; index++)
90  {
91  pf = &pfs[index];
92 
93  if (pf->depth == depth)
94  {
95  scanline_pad = pf->scanline_pad;
96  break;
97  }
98  }
99 
100  XFree(pfs);
101 
102  engine = rdtk_engine_new();
103  if (!engine)
104  goto fail;
105 
106  scanline = width * 4;
107  buffer = (uint8_t*)calloc(height, scanline);
108  if (!buffer)
109  goto fail;
110 
111  surface = rdtk_surface_new(engine, buffer, width, height, scanline);
112 
113  rdtk_surface_fill(surface, 0, 0, width, height, 0x3BB9FF);
114  rdtk_label_draw(surface, 16, 16, 128, 32, NULL, "label", 0, 0);
115  rdtk_button_draw(surface, 16, 64, 128, 32, NULL, "button");
116  rdtk_text_field_draw(surface, 16, 128, 128, 32, NULL, "text field");
117 
118  window = XCreateSimpleWindow(display, root_window, x, y, width, height, 1, border, background);
119 
120  XSelectInput(display, window, ExposureMask | KeyPressMask);
121  XMapWindow(display, window);
122 
123  XSetFunction(display, gc, GXcopy);
124  XSetFillStyle(display, gc, FillSolid);
125 
126  pixmap = XCreatePixmap(display, window, width, height, depth);
127 
128  image = XCreateImage(display, visual, depth, ZPixmap, 0, (char*)buffer, width, height,
129  scanline_pad, 0);
130 
131  while (1)
132  {
133  XNextEvent(display, &event);
134 
135  if (event.type == Expose)
136  {
137  XPutImage(display, pixmap, gc, image, 0, 0, 0, 0, width, height);
138  XCopyArea(display, pixmap, window, gc, 0, 0, width, height, 0, 0);
139  }
140 
141  if (event.type == KeyPress)
142  break;
143 
144  if (event.type == ClientMessage)
145  break;
146  }
147 
148  XFlush(display);
149 
150  rc = 0;
151 fail:
152  if (image)
153  XDestroyImage(image);
154  XCloseDisplay(display);
155 
156  rdtk_surface_free(surface);
157  free(buffer);
158 
159  rdtk_engine_free(engine);
160 
161  return rc;
162 }