private void AddFrame(Window child) { const int frame_width = 3; const int title_height = 20; const int inner_border = 1; if (this.WindowIndexByClient.ContainsKey(child)) { return; // Window has already been framed. } var Name = String.Empty; Xlib.XFetchName(this.display, child, ref Name); Log.Debug($"Framing {Name}"); Xlib.XGetWindowAttributes(this.display, child, out var attr); var title = Xlib.XCreateSimpleWindow(this.display, this.root, attr.x, attr.y, attr.width - (2 * inner_border), (title_height - 2 * inner_border), inner_border, this.Colours.InactiveTitleColor, this.Colours.InactiveTitleBorder); // Try to keep the child window in the same place, unless this would push the window decorations off screen. var adjusted_x_loc = (attr.x - frame_width < 0) ? 0 : attr.x - frame_width; var adjusted_y_loc = (attr.y - (title_height + frame_width) < 0) ? 0 : (attr.y - (title_height + frame_width)); var frame = Xlib.XCreateSimpleWindow(this.display, this.root, adjusted_x_loc, adjusted_y_loc, attr.width, attr.height + title_height, 3, this.Colours.InactiveFrameColor, this.Colours.WindowBackground); Xlib.XSelectInput(this.display, title, EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.Button1MotionMask | EventMask.ExposureMask); Xlib.XSelectInput(this.display, frame, EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.Button1MotionMask | EventMask.FocusChangeMask | EventMask.SubstructureRedirectMask | EventMask.SubstructureNotifyMask); Xlib.XDefineCursor(this.display, title, this.Cursors.TitleCursor); Xlib.XDefineCursor(this.display, frame, this.Cursors.FrameCursor); Xlib.XReparentWindow(this.display, title, frame, 0, 0); Xlib.XReparentWindow(this.display, child, frame, 0, title_height); Xlib.XMapWindow(this.display, title); Xlib.XMapWindow(this.display, frame); // Ensure the child window survives the untimely death of the window manager. Xlib.XAddToSaveSet(this.display, child); // Grab left click events from the client, so we can focus & raise on click SetFocusTrap(child); var wg = new WindowGroup { child = child, frame = frame, title = title }; this.WindowIndexByClient[child] = wg; this.WindowIndexByTitle[title] = wg; this.WindowIndexByFrame[frame] = wg; }
public RCode CreateWindowPane(VectorI2d vWindowPos, VectorI2d vWindowSize, bool bFullScreen) { display = Xlib.XOpenDisplay(null); screen = Xlib.XDefaultScreen(display); root = Xlib.XRootWindow(display, screen); mainWindow = Xlib.XCreateSimpleWindow( display, root, vWindowPos.x, vWindowPos.y, (uint)vWindowSize.x, (uint)vWindowSize.y, 1, Xlib.XBlackPixel(display, screen), Xlib.XWhitePixel(display, screen)); EventMask eventMask = // Keyboard EventMask.KeyPressMask | EventMask.KeyReleaseMask | // Mouse EventMask.PointerMotionMask | EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | // Window EventMask.FocusChangeMask | EventMask.EnterWindowMask | EventMask.LeaveWindowMask; Xlib.XSelectInput(display, mainWindow, eventMask); Xlib.XMapWindow(display, mainWindow); // Disable windows re-sizing XSizeHints sizeHints = new XSizeHints() { flags = XSizeHintsFlags.PMaxSize | XSizeHintsFlags.PMinSize, min_width = vWindowSize.x, max_width = vWindowSize.x, min_height = vWindowSize.y, max_height = vWindowSize.y }; XlibExt.XSetWMNormalHints(display, mainWindow, ref sizeHints); // Setup delete message when the user closes the window deleteWindowMessage = XlibExt.XInternAtom(display, "WM_DELETE_WINDOW", false); XlibExt.XSetWMProtocols(display, mainWindow, new Atom[] { deleteWindowMessage }, 1); return(RCode.OK); }
public Program(SimpleLogger.LogLevel level) { Log = new SimpleLogger(level); Log.Info("Started class"); /* areas to update */ IntPtr updates, current_update; /* our virtual framebuffer image we draw into */ IntPtr buffer; /* a font */ IntPtr font; /* our color range */ IntPtr range; /* our mouse x, y coordinates */ int mouse_x = 0, mouse_y = 0; /* connect to X */ disp = Xlib.XOpenDisplay(null); /* get default visual , colormap etc. you could ask imlib2 for what it */ /* thinks is the best, but this example is intended to be simple */ vis = Xlib.XDefaultVisual(disp, Xlib.XDefaultScreen(disp)); depth = Xlib.XDefaultDepth(disp, Xlib.XDefaultScreen(disp)); cm = Xlib.XDefaultColormap(disp, Xlib.XDefaultScreen(disp)); /* create a window 640x480 */ win = Xlib.XCreateSimpleWindow(disp, Xlib.XDefaultRootWindow(disp), 0, 0, 640, 480, 0, 0, 0); OnError = ErrorHandler; Xlib.XSetErrorHandler(OnError); /* tell X what events we are interested in */ Xlib.XSelectInput(disp, win, EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.KeyPressMask | EventMask.PointerMotionMask | EventMask.ExposureMask); /* show the window */ Xlib.XMapWindow(disp, win); /* set our cache to 2 Mb so it doesn't have to go hit the disk as long as */ /* the images we use use less than 2Mb of RAM (that is uncompressed) */ Imlib.imlib_set_cache_size(2048 * 1024); /* set the font cache to 512Kb - again to avoid re-loading */ Imlib.imlib_set_font_cache_size(512 * 1024); /* add the ./ttfonts dir to our font path - you'll want a notepad.ttf */ /* in that dir for the text to display */ Imlib.imlib_add_path_to_font_path("/usr/share/fonts/truetype/ubuntuz"); /* set the maximum number of colors to allocate for 8bpp and less to 128 */ Imlib.imlib_set_color_usage(128); /* dither for depths < 24bpp */ Imlib.imlib_context_set_dither(1); /* set the display , visual, colormap and drawable we are using */ Imlib.imlib_context_set_display(disp); Imlib.imlib_context_set_visual(vis); Imlib.imlib_context_set_colormap(cm); Imlib.imlib_context_set_drawable(win); /* infinite event loop */ Atom WM_DELETE_WINDOW = Xlib.XInternAtom(disp, "WM_DELETE_WINDOW", false); XSetWMProtocols(disp, win, out WM_DELETE_WINDOW, 1); IntPtr ev = Marshal.AllocHGlobal(24 * sizeof(long)); var home = Environment.GetEnvironmentVariable("HOME"); var done = false; while (!done) { /* image variable */ IntPtr image; /* width and height values */ int w, h, text_w, text_h; /* init our updates to empty */ updates = Imlib.imlib_updates_init(); /* while there are events form X - handle them */ do { Xlib.XNextEvent(disp, ev); var xevent = Marshal.PtrToStructure <X11.XAnyEvent>(ev); switch (xevent.type) { case (int)Event.ClientMessage: // TODO Should check here for other client message types - // however as the only protocol registered above is WM_DELETE_WINDOW // it is safe for this small example. done = true; Console.WriteLine("Client Message"); break; case (int)Event.Expose: /* window rectangle was exposed - add it to the list of */ /* rectangles we need to re-render */ var expose_event = Marshal.PtrToStructure <X11.XExposeEvent>(ev); updates = Imlib.imlib_update_append_rect(updates, expose_event.x, expose_event.y, expose_event.width, expose_event.height); break; case (int)Event.KeyPress: /* if we click anywhere in the window, exit */ var key_event = Marshal.PtrToStructure <X11.XKeyEvent>(ev); X11.KeySym key = Xlib.XKeycodeToKeysym(disp, (KeyCode)key_event.keycode, 0); if (key == KeySym.XK_Escape) { Console.WriteLine($"{key} / {KeySym.XK_Escape}"); done = true; } break; case (int)Event.MotionNotify: /* if the mouse moves - note it */ /* add a rectangle update for the new mouse position */ var motion_event = Marshal.PtrToStructure <X11.XMotionEvent>(ev); image = Imlib.imlib_load_image($"{home}/.local/share/data/images/Arrow.png"); Imlib.imlib_context_set_image(image); w = Imlib.imlib_image_get_width(); h = Imlib.imlib_image_get_height(); Imlib.imlib_context_set_image(image); Imlib.imlib_free_image(); /* the old position - so we wipe over where it used to be */ updates = Imlib.imlib_update_append_rect(updates, mouse_x - (w / 2), mouse_y - (h / 2), w, h); font = Imlib.imlib_load_font("Ubuntu-M/30"); if (font != IntPtr.Zero) { Imlib.imlib_context_set_font(font); string text = String.Format($"Mouse is at {mouse_x}, {mouse_y}"); Imlib.imlib_get_text_size(text, out text_w, out text_h); Imlib.imlib_free_font(); updates = Imlib.imlib_update_append_rect(updates, 320 - (text_w / 2), 240 - (text_h / 2), text_w, text_h); } mouse_x = motion_event.x; mouse_y = motion_event.y; /* the new one */ updates = Imlib.imlib_update_append_rect(updates, mouse_x - (w / 2), mouse_y - (h / 2), w, h); font = Imlib.imlib_load_font("Ubuntu-M/30"); if (font != IntPtr.Zero) { Imlib.imlib_context_set_font(font); string text = String.Format($"Mouse is at {mouse_x}, {mouse_y}"); Imlib.imlib_get_text_size(text, out text_w, out text_h); Imlib.imlib_free_font(); updates = Imlib.imlib_update_append_rect(updates, 320 - (text_w / 2), 240 - (text_h / 2), text_w, text_h); } break; default: /* any other events - do nothing */ break; } }while (Xlib.XPending(disp) != 0); /* no more events for now ? ok - idle time so lets draw stuff */ /* take all the little rectangles to redraw and merge them into */ /* something sane for rendering */ updates = Imlib.imlib_updates_merge_for_rendering(updates, 640, 480); for (current_update = updates; current_update != IntPtr.Zero; current_update = Imlib.imlib_updates_get_next(current_update)) { int up_x, up_y, up_w, up_h; /* find out where the first update is */ Imlib.imlib_updates_get_coordinates(current_update, out up_x, out up_y, out up_w, out up_h); /* create our buffer image for rendering this update */ buffer = Imlib.imlib_create_image(up_w, up_h); /* we can blend stuff now */ Imlib.imlib_context_set_blend(1); /* fill the window background */ /* load the background image - you'll need to have some images */ /* in ./images lying around for this to actually work */ // image = Imlib.imlib_load_image("./data/images/bg.png"); image = Imlib.imlib_load_image($"{home}/.local/share/catlock/themes/badabing.locked.jpg"); /* we're working with this image now */ Imlib.imlib_context_set_image(image); /* get its size */ w = Imlib.imlib_image_get_width(); h = Imlib.imlib_image_get_height(); /* now we want to work with the buffer */ Imlib.imlib_context_set_image(buffer); /* if the iimage loaded */ if (image != IntPtr.Zero) { /* blend image onto the buffer and scale it to 640x480 */ Imlib.imlib_blend_image_onto_image(image, 0, 0, 0, w, h, -up_x, -up_y, 640, 480); /* working with the loaded image */ Imlib.imlib_context_set_image(image); /* free it */ Imlib.imlib_free_image(); } /* draw an icon centered around the mouse position */ image = Imlib.imlib_load_image($"{home}/.local/share/data/images/Arrow.png"); Imlib.imlib_context_set_image(image); w = Imlib.imlib_image_get_width(); h = Imlib.imlib_image_get_height(); Imlib.imlib_context_set_image(buffer); if (image != IntPtr.Zero) { Imlib.imlib_blend_image_onto_image(image, 0, 0, 0, w, h, mouse_x - (w / 2) - up_x, mouse_y - (h / 2) - up_y, w, h); Imlib.imlib_context_set_image(image); Imlib.imlib_free_image(); } /* draw a gradient on top of things at the top left of the window */ /* create a range */ range = Imlib.imlib_create_color_range(); Imlib.imlib_context_set_color_range(range); /* add white opaque as the first color */ Imlib.imlib_context_set_color(255, 255, 255, 255); Imlib.imlib_add_color_to_color_range(0); /* add an orange color, semi-transparent 10 units from the first */ Imlib.imlib_context_set_color(255, 200, 10, 100); Imlib.imlib_add_color_to_color_range(10); /* add black, fully transparent at the end 20 units away */ Imlib.imlib_context_set_color(0, 0, 0, 0); Imlib.imlib_add_color_to_color_range(20); /* draw the range */ Imlib.imlib_context_set_image(buffer); Imlib.imlib_image_fill_color_range_rectangle(-up_x, -up_y, 128, 128, -45.0); /* free it */ Imlib.imlib_free_color_range(); /* draw text - centered with the current mouse x, y */ font = Imlib.imlib_load_font("Ubuntu-M/30"); if (font != IntPtr.Zero) { /* set the current font */ Imlib.imlib_context_set_font(font); /* set the image */ Imlib.imlib_context_set_image(buffer); /* set the color (black) */ Imlib.imlib_context_set_color(0, 0, 0, 255); /* print text to display in the buffer */ string text = String.Format($"Mouse is at {mouse_x}, {mouse_y}"); /* query the size it will be */ Imlib.imlib_get_text_size(text, out text_w, out text_h); /* draw it */ Imlib.imlib_text_draw(320 - (text_w / 2) - up_x, 240 - (text_h / 2) - up_y, text); /* free the font */ Imlib.imlib_free_font(); } /* don't blend the image onto the drawable - slower */ Imlib.imlib_context_set_blend(0); /* set the buffer image as our current image */ Imlib.imlib_context_set_image(buffer); /* render the image at 0, 0 */ Imlib.imlib_render_image_on_drawable(up_x, up_y); /* don't need that temporary buffer image anymore */ Imlib.imlib_free_image(); } /* if we had updates - free them */ if (updates != IntPtr.Zero) { Imlib.imlib_updates_free(updates); } /* loop again waiting for events */ } Marshal.FreeHGlobal(ev); // Xlib.XFreeGC(disp, gc); Xlib.XDestroyWindow(disp, win); Xlib.XCloseDisplay(disp); }