From 085496b60f23cc6f3ee86e443ec4791e4a96feff Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc-Andr=C3=A9=20Moreau?= Date: Tue, 21 Oct 2014 12:38:15 -0400 Subject: [PATCH] rdtk: add sample X11 window program --- rdtk/CMakeLists.txt | 4 ++ rdtk/sample/.gitignore | 2 + rdtk/sample/CMakeLists.txt | 35 +++++++++++ rdtk/sample/rdtk_x11.c | 147 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 rdtk/sample/.gitignore create mode 100644 rdtk/sample/CMakeLists.txt create mode 100644 rdtk/sample/rdtk_x11.c diff --git a/rdtk/CMakeLists.txt b/rdtk/CMakeLists.txt index 41f1ebf..35b0052 100644 --- a/rdtk/CMakeLists.txt +++ b/rdtk/CMakeLists.txt @@ -66,6 +66,10 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) add_subdirectory(include) add_subdirectory(librdtk) +if(WITH_X11) + add_subdirectory(sample) +endif() + # Exporting if(${CMAKE_VERSION} VERSION_GREATER "2.8.10") diff --git a/rdtk/sample/.gitignore b/rdtk/sample/.gitignore new file mode 100644 index 0000000..3bcce3e --- /dev/null +++ b/rdtk/sample/.gitignore @@ -0,0 +1,2 @@ +rdtk-sample + diff --git a/rdtk/sample/CMakeLists.txt b/rdtk/sample/CMakeLists.txt new file mode 100644 index 0000000..3e7a1c5 --- /dev/null +++ b/rdtk/sample/CMakeLists.txt @@ -0,0 +1,35 @@ +# RdTk: Remote Desktop Toolkit +# rdtk cmake build script +# +# Copyright 2014 Marc-Andre Moreau +# +# 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. + +set(MODULE_NAME "rdtk-sample") +set(MODULE_PREFIX "RDTK_SAMPLE") + +include_directories(${X11_INCLUDE_DIRS}) + +set(${MODULE_PREFIX}_SRCS + rdtk_x11.c) + +add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS}) + +set(${MODULE_PREFIX}_LIBS rdtk) + +list(APPEND ${MODULE_PREFIX}_LIBS ${X11_LIBRARIES}) + +target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS}) + +set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "RdTk") + diff --git a/rdtk/sample/rdtk_x11.c b/rdtk/sample/rdtk_x11.c new file mode 100644 index 0000000..27c81fe --- /dev/null +++ b/rdtk/sample/rdtk_x11.c @@ -0,0 +1,147 @@ +/** + * RdTk: Remote Desktop Toolkit + * + * Copyright 2014 Marc-Andre Moreau + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include + +int main(int argc, char** argv) +{ + GC gc; + int index; + int depth; + int x, y; + int width; + int height; + BYTE* buffer; + int scanline; + int pf_count; + XEvent event; + XImage* image; + Pixmap pixmap; + Screen* screen; + Visual* visual; + int scanline_pad; + int screen_number; + Display* display; + Window window; + Window root_window; + rdtkEngine* engine; + rdtkSurface* surface; + unsigned long border; + unsigned long background; + XPixmapFormatValues* pf; + XPixmapFormatValues* pfs; + + display = XOpenDisplay(NULL); + + if (!display) + { + fprintf(stderr, "Cannot open display\n"); + exit(1); + } + + 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 (index = 0; index < pf_count; index++) + { + pf = &pfs[index]; + + if (pf->depth == depth) + { + scanline_pad = pf->scanline_pad; + break; + } + } + + XFree(pfs); + + engine = rdtk_engine_new(); + + scanline = width * 4; + buffer = (BYTE*) malloc(scanline * height); + + 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"); + + window = XCreateSimpleWindow(display, root_window, + x, y, width, height, 1, border, background); + + XSelectInput(display, window, ExposureMask | KeyPressMask); + XMapWindow(display, window); + + XSetFunction(display, gc, GXcopy); + XSetFillStyle(display, gc, FillSolid); + + pixmap = XCreatePixmap(display, window, width, height, depth); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, + (char*) buffer, width, height, scanline_pad, 0); + + 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); + + XCloseDisplay(display); + + rdtk_surface_free(surface); + free(buffer); + + rdtk_engine_free(engine); + + return 0; +} -- 2.7.4