FreeRDP
Loading...
Searching...
No Matches
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
33int 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 if (rdtk_surface_fill(surface, 0, 0, width, height, 0x3BB9FF) < 0)
94 goto fail;
95 if (rdtk_label_draw(surface, 16, 16, 128, 32, NULL, "label", 0, 0) < 0)
96 goto fail;
97 if (rdtk_button_draw(surface, 16, 64, 128, 32, NULL, "button") < 0)
98 goto fail;
99 if (rdtk_text_field_draw(surface, 16, 128, 128, 32, NULL, "text field") < 0)
100 goto fail;
101
102 window = XCreateSimpleWindow(display, root_window, x, y, width, height, 1, border, background);
103
104 XSelectInput(display, window, ExposureMask | KeyPressMask);
105 XMapWindow(display, window);
106
107 XSetFunction(display, gc, GXcopy);
108 XSetFillStyle(display, gc, FillSolid);
109
110 pixmap = XCreatePixmap(display, window, width, height, (unsigned)depth);
111
112 image = XCreateImage(display, visual, (unsigned)depth, ZPixmap, 0, (char*)buffer, width, height,
113 scanline_pad, 0);
114
115 while (1)
116 {
117 XNextEvent(display, &event);
118
119 if (event.type == Expose)
120 {
121 XPutImage(display, pixmap, gc, image, 0, 0, 0, 0, width, height);
122 XCopyArea(display, pixmap, window, gc, 0, 0, width, height, 0, 0);
123 }
124
125 if (event.type == KeyPress)
126 break;
127
128 if (event.type == ClientMessage)
129 break;
130 }
131
132 XFlush(display);
133
134 rc = 0;
135fail:
136 if (image)
137 XDestroyImage(image);
138 XCloseDisplay(display);
139
140 rdtk_surface_free(surface);
141 free(buffer);
142
143 rdtk_engine_free(engine);
144
145 return rc;
146}