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