FreeRDP/rdtk/sample/rdtk_x11.c

163 lines
3.6 KiB
C
Raw Normal View History

2014-10-21 20:38:15 +04:00
/**
* RdTk: Remote Desktop Toolkit
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-02-16 13:10:11 +03:00
#include <rdtk/config.h>
2014-10-21 20:38:15 +04:00
2021-06-18 11:01:52 +03:00
#include <stdint.h>
#include <winpr/wlog.h>
2014-10-21 20:38:15 +04:00
#include <rdtk/rdtk.h>
#include <X11/Xlib.h>
2018-02-12 13:14:54 +03:00
#include <X11/Xutil.h>
2014-10-21 20:38:15 +04:00
#define TAG "rdtk.sample"
2014-10-21 20:38:15 +04:00
int main(int argc, char** argv)
{
2024-08-07 10:35:51 +03:00
int rc = 1;
GC gc = NULL;
int depth = 0;
int x = 0;
int y = 0;
int width = 0;
int height = 0;
uint8_t* buffer = NULL;
int scanline = 0;
int pf_count = 0;
2014-10-21 20:38:15 +04:00
XEvent event;
XImage* image = NULL;
Pixmap pixmap = 0;
Screen* screen = NULL;
Visual* visual = NULL;
int scanline_pad = 0;
int screen_number = 0;
Display* display = NULL;
Window window = 0;
Window root_window = 0;
rdtkEngine* engine = NULL;
rdtkSurface* surface = NULL;
unsigned long border = 0;
unsigned long background = 0;
XPixmapFormatValues* pf = NULL;
XPixmapFormatValues* pfs = NULL;
2019-11-06 17:24:51 +03:00
2021-07-29 11:18:52 +03:00
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
2014-10-21 20:38:15 +04:00
display = XOpenDisplay(NULL);
if (!display)
{
WLog_ERR(TAG, "Cannot open display");
return 1;
2014-10-21 20:38:15 +04:00
}
x = 10;
y = 10;
width = 640;
height = 480;
screen_number = DefaultScreen(display);
screen = ScreenOfDisplay(display, screen_number);
visual = DefaultVisual(display, screen_number);
gc = DefaultGC(display, screen_number);
depth = DefaultDepthOfScreen(screen);
root_window = RootWindow(display, screen_number);
border = BlackPixel(display, screen_number);
background = WhitePixel(display, screen_number);
scanline_pad = 0;
pfs = XListPixmapFormats(display, &pf_count);
for (int index = 0; index < pf_count; index++)
2014-10-21 20:38:15 +04:00
{
pf = &pfs[index];
if (pf->depth == depth)
{
scanline_pad = pf->scanline_pad;
break;
}
}
XFree(pfs);
engine = rdtk_engine_new();
if (!engine)
2024-08-07 10:35:51 +03:00
goto fail;
2014-10-21 20:38:15 +04:00
scanline = width * 4;
2021-06-18 11:01:52 +03:00
buffer = (uint8_t*)calloc(height, scanline);
if (!buffer)
2024-08-07 10:35:51 +03:00
goto fail;
2014-10-21 20:38:15 +04:00
surface = rdtk_surface_new(engine, buffer, width, height, scanline);
rdtk_surface_fill(surface, 0, 0, width, height, 0x3BB9FF);
rdtk_label_draw(surface, 16, 16, 128, 32, NULL, "label", 0, 0);
rdtk_button_draw(surface, 16, 64, 128, 32, NULL, "button");
rdtk_text_field_draw(surface, 16, 128, 128, 32, NULL, "text field");
2019-11-06 17:24:51 +03:00
window = XCreateSimpleWindow(display, root_window, x, y, width, height, 1, border, background);
2014-10-21 20:38:15 +04:00
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
XSetFunction(display, gc, GXcopy);
XSetFillStyle(display, gc, FillSolid);
pixmap = XCreatePixmap(display, window, width, height, depth);
2019-11-06 17:24:51 +03:00
image = XCreateImage(display, visual, depth, ZPixmap, 0, (char*)buffer, width, height,
scanline_pad, 0);
2014-10-21 20:38:15 +04:00
while (1)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
XPutImage(display, pixmap, gc, image, 0, 0, 0, 0, width, height);
XCopyArea(display, pixmap, window, gc, 0, 0, width, height, 0, 0);
}
if (event.type == KeyPress)
break;
if (event.type == ClientMessage)
break;
}
XFlush(display);
2024-08-07 10:35:51 +03:00
rc = 0;
fail:
if (image)
XDestroyImage(image);
2014-10-21 20:38:15 +04:00
XCloseDisplay(display);
rdtk_surface_free(surface);
free(buffer);
rdtk_engine_free(engine);
2024-08-07 10:35:51 +03:00
return rc;
2014-10-21 20:38:15 +04:00
}