From 37bf3835b0b3ce7f4c80924f40735698f057ef6f Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Mon, 7 Nov 2022 06:49:40 +0100 Subject: Create class Fl_Unix_Screen_Driver used by X11 and Wayland platforms --- src/drivers/Unix/Fl_Unix_Screen_Driver.H | 61 ++++++++++ src/drivers/Unix/Fl_Unix_Screen_Driver.cxx | 96 ++++++++++++++++ src/drivers/Unix/Fl_Unix_System_Driver.H | 4 - src/drivers/Unix/Fl_Unix_System_Driver.cxx | 172 ++++++----------------------- 4 files changed, 189 insertions(+), 144 deletions(-) create mode 100644 src/drivers/Unix/Fl_Unix_Screen_Driver.H create mode 100644 src/drivers/Unix/Fl_Unix_Screen_Driver.cxx (limited to 'src/drivers/Unix') diff --git a/src/drivers/Unix/Fl_Unix_Screen_Driver.H b/src/drivers/Unix/Fl_Unix_Screen_Driver.H new file mode 100644 index 000000000..ebd0ed706 --- /dev/null +++ b/src/drivers/Unix/Fl_Unix_Screen_Driver.H @@ -0,0 +1,61 @@ +// +// Definition of the part of the screen driver shared by X11 and Wayland platforms +// for the Fast Light Tool Kit (FLTK). +// +// Copyright 2021-2022 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#ifndef FL_UNIX_SCREEN_DRIVER_H +#define FL_UNIX_SCREEN_DRIVER_H + +#include "../../Fl_Screen_Driver.H" + +# if USE_POLL + +# include +static pollfd *pollfds = 0; + +# else +# if HAVE_SYS_SELECT_H +# include +# endif /* HAVE_SYS_SELECT_H */ + +// The following #define is only needed for HP-UX 9.x and earlier: +//#define select(a,b,c,d,e) select((a),(int *)(b),(int *)(c),(int *)(d),(e)) + +# define POLLIN 1 +# define POLLOUT 4 +# define POLLERR 8 + +# endif /* USE_POLL */ + + +class Fl_Unix_Screen_Driver : public Fl_Screen_Driver { +public: + static fd_set fdsets[3]; + static int maxfd; + static int nfds; + static struct FD { + # if !USE_POLL + int fd; + short events; + # endif + void (*cb)(int, void*); + void* arg; + } *fd; + virtual int poll_or_select_with_delay(double time_to_wait); + virtual int poll_or_select(); + virtual void *control_maximize_button(void *) { return NULL; } +}; + +#endif /* FL_UNIX_SCREEN_DRIVER_H */ diff --git a/src/drivers/Unix/Fl_Unix_Screen_Driver.cxx b/src/drivers/Unix/Fl_Unix_Screen_Driver.cxx new file mode 100644 index 000000000..0b33b0a20 --- /dev/null +++ b/src/drivers/Unix/Fl_Unix_Screen_Driver.cxx @@ -0,0 +1,96 @@ +// +// Definition of the part of the Screen interface shared by X11/Wayland +// +// Copyright 2022 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#include +#include "Fl_Unix_Screen_Driver.H" + +fd_set Fl_Unix_Screen_Driver::fdsets[3]; +int Fl_Unix_Screen_Driver::maxfd = 0; +int Fl_Unix_Screen_Driver::nfds = 0; +Fl_Unix_Screen_Driver::FD *Fl_Unix_Screen_Driver::fd = NULL; + +// these pointers are set by the Fl::lock() function: +static void nothing() {} +void (*fl_lock_function)() = nothing; +void (*fl_unlock_function)() = nothing; + + +// This is never called with time_to_wait < 0.0: +// It should return negative on error, 0 if nothing happens before +// timeout, and >0 if any callbacks were done. +int Fl_Unix_Screen_Driver::poll_or_select_with_delay(double time_to_wait) { +# if !USE_POLL + fd_set fdt[3]; + fdt[0] = fdsets[0]; + fdt[1] = fdsets[1]; + fdt[2] = fdsets[2]; +# endif + int n; + + fl_unlock_function(); + + if (time_to_wait < 2147483.648) { +# if USE_POLL + n = ::poll(pollfds, nfds, int(time_to_wait*1000 + .5)); +# else + timeval t; + t.tv_sec = int(time_to_wait); + t.tv_usec = int(1000000 * (time_to_wait-t.tv_sec)); + n = ::select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],&t); +# endif + } else { +# if USE_POLL + n = ::poll(pollfds, nfds, -1); +# else + n = ::select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],0); +# endif + } + + fl_lock_function(); + + if (n > 0) { + for (int i=0; i #include // fl_strdup #include @@ -641,55 +642,22 @@ const char *Fl_Unix_System_Driver::filename_name(const char *name) { } -//////////////////////////////////////////////////////////////// -// interface to poll/select call: - -# if USE_POLL - -# include -static pollfd *pollfds = 0; - -# else -# if HAVE_SYS_SELECT_H -# include -# endif /* HAVE_SYS_SELECT_H */ - -// The following #define is only needed for HP-UX 9.x and earlier: -//#define select(a,b,c,d,e) select((a),(int *)(b),(int *)(c),(int *)(d),(e)) - -static fd_set fdsets[3]; -static int maxfd; -# define POLLIN 1 -# define POLLOUT 4 -# define POLLERR 8 - -# endif /* USE_POLL */ - -static int nfds = 0; static int fd_array_size = 0; -struct FD { -# if !USE_POLL - int fd; - short events; -# endif - void (*cb)(int, void*); - void* arg; -}; - -static FD *fd = 0; void Fl_Unix_System_Driver::add_fd(int n, int events, void (*cb)(int, void*), void *v) { remove_fd(n,events); - int i = nfds++; + int i = Fl_Unix_Screen_Driver::nfds++; if (i >= fd_array_size) { - FD *temp; + Fl_Unix_Screen_Driver::FD *temp; fd_array_size = 2*fd_array_size+1; - if (!fd) temp = (FD*)malloc(fd_array_size*sizeof(FD)); - else temp = (FD*)realloc(fd, fd_array_size*sizeof(FD)); + if (!Fl_Unix_Screen_Driver::fd) temp = + (Fl_Unix_Screen_Driver::FD*)malloc(fd_array_size*sizeof(Fl_Unix_Screen_Driver::FD)); + else temp = (Fl_Unix_Screen_Driver::FD*)realloc(Fl_Unix_Screen_Driver::fd, + fd_array_size*sizeof(Fl_Unix_Screen_Driver::FD)); if (!temp) return; - fd = temp; + Fl_Unix_Screen_Driver::fd = temp; # if USE_POLL pollfd *tpoll; @@ -701,18 +669,18 @@ void Fl_Unix_System_Driver::add_fd(int n, int events, void (*cb)(int, void*), vo pollfds = tpoll; # endif } - fd[i].cb = cb; - fd[i].arg = v; + Fl_Unix_Screen_Driver::fd[i].cb = cb; + Fl_Unix_Screen_Driver::fd[i].arg = v; # if USE_POLL pollfds[i].fd = n; pollfds[i].events = events; # else - fd[i].fd = n; - fd[i].events = events; - if (events & POLLIN) FD_SET(n, &fdsets[0]); - if (events & POLLOUT) FD_SET(n, &fdsets[1]); - if (events & POLLERR) FD_SET(n, &fdsets[2]); - if (n > maxfd) maxfd = n; + Fl_Unix_Screen_Driver::fd[i].fd = n; + Fl_Unix_Screen_Driver::fd[i].events = events; + if (events & POLLIN) FD_SET(n, &Fl_Unix_Screen_Driver::fdsets[0]); + if (events & POLLOUT) FD_SET(n, &Fl_Unix_Screen_Driver::fdsets[1]); + if (events & POLLERR) FD_SET(n, &Fl_Unix_Screen_Driver::fdsets[2]); + if (n > Fl_Unix_Screen_Driver::maxfd) Fl_Unix_Screen_Driver::maxfd = n; # endif } @@ -723,9 +691,9 @@ void Fl_Unix_System_Driver::add_fd(int n, void (*cb)(int, void*), void* v) { void Fl_Unix_System_Driver::remove_fd(int n, int events) { int i,j; # if !USE_POLL - maxfd = -1; // recalculate maxfd on the fly + Fl_Unix_Screen_Driver::maxfd = -1; // recalculate maxfd on the fly # endif - for (i=j=0; i maxfd) maxfd = fd[i].fd; + if (Fl_Unix_Screen_Driver::fd[i].fd > Fl_Unix_Screen_Driver::maxfd) Fl_Unix_Screen_Driver::maxfd = Fl_Unix_Screen_Driver::fd[i].fd; # endif // move it down in the array if necessary: if (j0 if any callbacks were done. -int Fl_Unix_System_Driver::poll_or_select_with_delay(double time_to_wait) { -# if !USE_POLL - fd_set fdt[3]; - fdt[0] = fdsets[0]; - fdt[1] = fdsets[1]; - fdt[2] = fdsets[2]; -# endif - int n; - - fl_unlock_function(); - - if (time_to_wait < 2147483.648) { -# if USE_POLL - n = ::poll(pollfds, nfds, int(time_to_wait*1000 + .5)); -# else - timeval t; - t.tv_sec = int(time_to_wait); - t.tv_usec = int(1000000 * (time_to_wait-t.tv_sec)); - n = ::select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],&t); -# endif - } else { -# if USE_POLL - n = ::poll(pollfds, nfds, -1); -# else - n = ::select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],0); -# endif - } - - fl_lock_function(); - - if (n > 0) { - for (int i=0; ipoll_or_select_with_delay(0.0); + int ret = scr_dr->poll_or_select_with_delay(0.0); Fl::flush(); return ret; } else { @@ -852,15 +748,16 @@ double Fl_Unix_System_Driver::wait(double time_to_wait) Fl_Timeout::elapse_timeouts(); time_to_wait = Fl_Timeout::time_to_wait(time_to_wait); } - return this->poll_or_select_with_delay(time_to_wait); + return scr_dr->poll_or_select_with_delay(time_to_wait); } } int Fl_Unix_System_Driver::ready() { + Fl_Unix_Screen_Driver *scr_dr = (Fl_Unix_Screen_Driver*)Fl::screen_driver(); Fl_Timeout::elapse_timeouts(); if (Fl_Timeout::time_to_wait(1.0) <= 0.0) return 1; - return this->poll_or_select(); + return scr_dr->poll_or_select(); } @@ -954,8 +851,3 @@ Fl_RGB_Image *Fl_Unix_System_Driver::own_bmp_to_RGB(char *bmp) { img->alloc_array = 1; return img; } - - -void *Fl_Unix_System_Driver::control_maximize_button(void *data) { - return NULL; -} -- cgit v1.2.3