diff --git a/.gitignore b/.gitignore index 71a7acb..779e471 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,4 @@ docs/reference/kmscon.????* docs/reference/*.stamp docs/reference/version.xml docs/reference/*/ -gtk-doc.make +wlterm diff --git a/Makefile.am b/Makefile.am index 22eff77..cbf839d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -26,14 +26,12 @@ pkgconfig_DATA = # genshader: Program used to convert shaders into C-source files # -bin_PROGRAMS = \ - kmscon +bin_PROGRAMS = check_PROGRAMS = noinst_PROGRAMS = \ genshader \ genunifont -noinst_LTLIBRARIES = \ - libkmscon-core.la +noinst_LTLIBRARIES = lib_LTLIBRARIES = # @@ -58,11 +56,11 @@ AM_CPPFLAGS = \ AM_LDFLAGS = \ -Wl,--as-needed -if DEBUG +if BUILD_ENABLE_DEBUG AM_CFLAGS += -g endif -if OPTIMIZATIONS +if BUILD_ENABLE_OPTIMIZATIONS AM_CFLAGS += -O2 else AM_CFLAGS += -O0 @@ -97,8 +95,11 @@ SHL_HOOK = \ # compiled into a separate object to allow using it in several other programs. # -lib_LTLIBRARIES += \ - libeloop.la +if BUILD_ENABLE_ELOOP +lib_LTLIBRARIES += libeloop.la +include_HEADERS += src/eloop.h +pkgconfig_DATA += libeloop.pc +endif libeloop_la_SOURCES = \ $(SHL_DLIST) \ @@ -107,7 +108,7 @@ libeloop_la_SOURCES = \ src/eloop.h \ src/eloop.c -if EV_HAVE_DBUS +if BUILD_ENABLE_DBUS libeloop_la_SOURCES += \ external/dbus-common.h \ external/dbus-loop.h \ @@ -122,11 +123,6 @@ libeloop_la_LIBADD = \ libeloop_la_LDFLAGS = \ -version-info 1:0:0 -include_HEADERS += \ - src/eloop.h - -pkgconfig_DATA += libeloop.pc - # # libtsm # The Terminal-emulator State Machine is a library that implements the whole VTE @@ -134,8 +130,14 @@ pkgconfig_DATA += libeloop.pc # be used to implement any kind of terminal emulator or debugger. # -lib_LTLIBRARIES += \ - libtsm.la +if BUILD_ENABLE_TSM +lib_LTLIBRARIES += libtsm.la +include_HEADERS += \ + src/tsm_screen.h \ + src/tsm_unicode.h \ + src/tsm_vte.h +pkgconfig_DATA += libtsm.pc +endif libtsm_la_SOURCES = \ $(SHL_LLOG) \ @@ -155,14 +157,6 @@ libtsm_la_CPPFLAGS = \ libtsm_la_LDFLAGS = \ -version-info 1:0:0 -include_HEADERS += \ - src/tsm_screen.h \ - src/tsm_unicode.h \ - src/tsm_vte.h - -pkgconfig_DATA += \ - libtsm.pc - # # libuterm # The uterm library provides helpers to create terminals in user-space. They @@ -171,8 +165,11 @@ pkgconfig_DATA += \ # abstractions and an input layer # -lib_LTLIBRARIES += \ - libuterm.la +if BUILD_ENABLE_UTERM +lib_LTLIBRARIES += libuterm.la +include_HEADERS += src/uterm.h +pkgconfig_DATA += libuterm.pc +endif libuterm_la_SOURCES = \ $(SHL_DLIST) \ @@ -210,12 +207,12 @@ libuterm_la_LIBADD = \ libuterm_la_LDFLAGS = \ -version-info 1:0:0 -if UTERM_HAVE_FBDEV +if BUILD_ENABLE_FBDEV libuterm_la_SOURCES += \ src/uterm_video_fbdev.c endif -if UTERM_HAVE_DRM +if BUILD_ENABLE_DRM libuterm_la_SOURCES += \ src/uterm_video_drm.c libuterm_la_SOURCES += \ @@ -226,21 +223,16 @@ nodist_libuterm_la_SOURCES += \ src/static_shaders.c endif -if UTERM_HAVE_DUMB +if BUILD_ENABLE_DUMB libuterm_la_SOURCES += \ src/uterm_video_dumb.c endif -if UTERM_HAVE_XKBCOMMON +if BUILD_ENABLE_XKBCOMMON libuterm_la_SOURCES += \ src/uterm_input_uxkb.c endif -include_HEADERS += \ - src/uterm.h - -pkgconfig_DATA += libuterm.pc - # # Shaders # As there is no need to modify shaders at run-time, we statically compile them @@ -297,6 +289,10 @@ src/text_font_unifont_data.c: $(UNIFONT) genunifont$(EXEEXT) # Only "main.c" is not included here as it contains the main() function. # +if BUILD_ENABLE_KMSCON +noinst_LTLIBRARIES += libkmscon-core.la +endif + libkmscon_core_la_SOURCES = \ $(SHL_DLIST) \ $(SHL_ARRAY) \ @@ -315,39 +311,39 @@ libkmscon_core_la_SOURCES = \ src/text_font.c nodist_libkmscon_core_la_SOURCES = -if KMSCON_HAVE_UNIFONT +if BUILD_ENABLE_UNIFONT libkmscon_core_la_SOURCES += \ src/text_font_unifont.c nodist_libkmscon_core_la_SOURCES += \ src/text_font_unifont_data.c endif -if KMSCON_HAVE_8X16 +if BUILD_ENABLE_8X16 libkmscon_core_la_SOURCES += \ src/text_font_8x16.c endif -if KMSCON_HAVE_FREETYPE2 +if BUILD_ENABLE_FREETYPE2 libkmscon_core_la_SOURCES += \ src/text_font_freetype2.c endif -if KMSCON_HAVE_PANGO +if BUILD_ENABLE_PANGO libkmscon_core_la_SOURCES += \ src/text_font_pango.c endif -if KMSCON_HAVE_BBLIT +if BUILD_ENABLE_BBLIT libkmscon_core_la_SOURCES += \ src/text_bblit.c endif -if KMSCON_HAVE_BBULK +if BUILD_ENABLE_BBULK libkmscon_core_la_SOURCES += \ src/text_bbulk.c endif -if KMSCON_HAVE_GLES2 +if BUILD_ENABLE_GLES2 libkmscon_core_la_SOURCES += \ src/text_gltex.c \ src/static_gl.h \ @@ -378,6 +374,15 @@ libkmscon_core_la_LIBADD = \ # built as part of kmscon. # +if BUILD_ENABLE_KMSCON +bin_PROGRAMS += kmscon +check_PROGRAMS += \ + test_output \ + test_vt \ + test_input \ + test_key +endif + kmscon_SOURCES = \ $(SHL_DLIST) \ src/main.c @@ -386,17 +391,43 @@ kmscon_LDADD = \ libeloop.la \ libkmscon-core.la -check_PROGRAMS += test_output test_output_SOURCES = tests/test_output.c tests/test_include.h test_output_LDADD = libkmscon-core.la -check_PROGRAMS += test_vt test_vt_SOURCES = tests/test_vt.c test_vt_LDADD = libkmscon-core.la -check_PROGRAMS += test_input test_input_SOURCES = tests/test_input.c test_input_LDADD = libkmscon-core.la -check_PROGRAMS += test_key test_key_SOURCES = tests/test_key.c + +# +# Wayland Terminal +# + +if BUILD_ENABLE_WLTERM +bin_PROGRAMS += wlterm +endif + +wlterm_SOURCES = \ + src/wlt_main.h \ + src/wlt_main.c \ + src/wlt_toolkit.h \ + src/wlt_toolkit.c \ + src/wlt_theme.h \ + src/wlt_theme.c \ + src/wlt_terminal.h \ + src/wlt_terminal.c \ + src/log.h \ + src/log.c \ + src/conf.h \ + src/conf.c +wlterm_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + $(WAYLAND_CFLAGS) +wlterm_LDADD = \ + libeloop.la \ + libtsm.la \ + -lpthread \ + $(WAYLAND_LIBS) diff --git a/configure.ac b/configure.ac index 301ee90..3fd6b73 100644 --- a/configure.ac +++ b/configure.ac @@ -73,10 +73,49 @@ PKG_CHECK_MODULES([FREETYPE2], [freetype2 fontconfig], PKG_CHECK_MODULES([PANGO], [pango pangoft2], [have_pango=yes], [have_pango=no]) +PKG_CHECK_MODULES([WAYLAND], [wayland-client wayland-server wayland-cursor], + [have_wayland=yes], [have_wayland=no]) + # -# Parse arguments -# Parse all command line arguments and save the values so we can check them -# later when configuring kmscon. +# Parse arguments for applications +# Parse all command-line arguments that enable or disable build of applications. +# This allows us to compute which dependencies we need. +# + +AC_MSG_CHECKING([whether to compile eloop]) +AC_ARG_ENABLE([eloop], + [AS_HELP_STRING([--enable-eloop], + [build eloop library])]) +AC_MSG_RESULT([ok]) + +AC_MSG_CHECKING([whether to compile TSM]) +AC_ARG_ENABLE([tsm], + [AS_HELP_STRING([--enable-tsm], + [build tsm library])]) +AC_MSG_RESULT([ok]) + +AC_MSG_CHECKING([whether to compile uterm]) +AC_ARG_ENABLE([uterm], + [AS_HELP_STRING([--enable-uterm], + [build uterm library])]) +AC_MSG_RESULT([ok]) + +AC_MSG_CHECKING([whether to compile kmscon]) +AC_ARG_ENABLE([kmscon], + [AS_HELP_STRING([--disable-kmscon], + [do not build kmscon])]) +AC_MSG_RESULT([ok]) + +AC_MSG_CHECKING([whether to build wlterm]) +AC_ARG_ENABLE([wlterm], + [AS_HELP_STRING([--enable-wlterm], + [build wlterm])]) +AC_MSG_RESULT([ok]) + +# +# Parse depedency arguments +# This parses all command-line arguments to check for which dependencies should +# be used and how the applications should be built. # AC_MSG_CHECKING([whether to use systemd for multi-seat support]) @@ -157,6 +196,13 @@ AC_ARG_ENABLE([bbulk], [disable bbulk rendering backend])]) AC_MSG_RESULT([ok]) +# +# Parse miscellaneous arguments +# These don't belong into the categories above for several reason so they are +# handled specially. +# They configure compiler options and similar. +# + AC_MSG_CHECKING([whether to build with debugging on]) AC_ARG_ENABLE([debug], [AS_HELP_STRING([--enable-debug], @@ -186,25 +232,83 @@ if test ! x$enable_optimizations = xno ; then fi if test x$debug_enabled = xyes ; then - AC_DEFINE([KMSCON_ENABLE_DEBUG], [1], - [Enable debug for kmscon]) - AC_DEFINE([LOG_ENABLE_DEBUG], [1], - [Enable debug for log subsystem]) - AC_DEFINE([LLOG_ENABLE_DEBUG], [1], - [Enable debug for llog subsystem]) + AC_DEFINE([BUILD_ENABLE_DEBUG], [1], + [Enable debug mode]) else AC_DEFINE([NDEBUG], [1], [No Debug]) fi -AM_CONDITIONAL([DEBUG], [test x$debug_enabled = xyes]) -AM_CONDITIONAL([OPTIMIZATIONS], [test x$optimizations_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_DEBUG], + [test x$debug_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_OPTIMIZATIONS], + [test x$optimizations_enabled = xyes]) # -# Main dependencies -# This checks for all dependencies that are not optional. +# Main applications +# This checks which applications to build so we can optionally disable unused +# dependencies below. +# +# Dependency Calculations +# The first thing we do is checking which applications to build. Then we +# forcibly enable all libraries that are needed by these and then we forcibly +# enable all other source-code dependecies. +# +# The "enable_XY" flag is user input that is set to "yes" if they enabled it +# explicitly, "no" if they disabled it explicitly or "" (empty) if they did +# not specify it explicitly. In the latter case we enable it if all dependencies +# are met, otherwise, we disable it. This does not work for applications, +# though. If an application is not specified explicitly by the user, then we +# force the default value. +# An application also forces all _hard_ dependencies to "yes" and so fails if +# they cannot be met. # -# We currently have no mandatory dependencies! +kmscon_enabled=no +if test ! x$enable_kmscon = xno ; then + kmscon_enabled=yes + enable_eloop=yes + enable_tsm=yes + enable_uterm=yes +fi + +AM_CONDITIONAL([BUILD_ENABLE_KMSCON], + [test x$kmscon_enabled = xyes]) + +wlterm_enabled=no +if test x$enable_wlterm = xyes ; then + wlterm_enabled=yes + enable_eloop=yes + enable_tsm=yes + enable_wayland=yes +fi + +AM_CONDITIONAL([BUILD_ENABLE_WLTERM], + [test x$wlterm_enabled = xyes]) + +uterm_enabled=no +if test x$enable_uterm = xyes ; then + uterm_enabled=yes + eloop_enabled=yes +fi + +AM_CONDITIONAL([BUILD_ENABLE_UTERM], + [test x$uterm_enabled = xyes]) + +eloop_enabled=no +if test x$enable_eloop = xyes ; then + eloop_enabled=yes +fi + +AM_CONDITIONAL([BUILD_ENABLE_ELOOP], + [test x$eloop_enabled = xyes]) + +tsm_enabled=no +if test x$enable_tsm = xyes ; then + tsm_enabled=yes +fi + +AM_CONDITIONAL([BUILD_ENABLE_TSM], + [test x$tsm_enabled = xyes]) # # Systemd dependency @@ -223,7 +327,7 @@ if test ! x$enable_systemd = xno ; then fi if test x$systemd_enabled = xyes ; then - AC_DEFINE([UTERM_HAVE_SYSTEMD], [1], + AC_DEFINE([BUILD_ENABLE_SYSTEMD], [1], [Use systemd for multi-seat support]) else SYSTEMD_CFLAGS="" @@ -247,7 +351,7 @@ if test ! x$enable_udev = xno ; then fi if test x$udev_enabled = xyes ; then - AC_DEFINE([UTERM_HAVE_UDEV], [1], + AC_DEFINE([BUILD_ENABLE_UDEV], [1], [Use udev for device hotplug support]) else UDEV_CFLAGS="" @@ -261,7 +365,7 @@ fi # dbus_enabled=no -if test ! x$enable_dbus = xno ; then +if test x$enable_dbus = xyes ; then if test x$have_dbus = xyes ; then dbus_enabled=yes elif test x$enable_dbus = xyes; then @@ -270,14 +374,14 @@ if test ! x$enable_dbus = xno ; then fi if test x$dbus_enabled = xyes; then - AC_DEFINE([EV_HAVE_DBUS], [1], + AC_DEFINE([BUILD_ENABLE_DBUS], [1], [Use dbus for IPC]) else DBUS_CFLAGS="" DBUS_LIBS="" fi -AM_CONDITIONAL([EV_HAVE_DBUS], [test x$dbus_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_DBUS], [test x$dbus_enabled = xyes]) # # Uterm fbdev backend @@ -292,11 +396,11 @@ if test ! x$enable_fbdev = xno ; then fi if test x$fbdev_enabled = xyes ; then - AC_DEFINE([UTERM_HAVE_FBDEV], [1], + AC_DEFINE([BUILD_ENABLE_FBDEV], [1], [Use uterm fbdev video backend]) fi -AM_CONDITIONAL([UTERM_HAVE_FBDEV], [test x$fbdev_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_FBDEV], [test x$fbdev_enabled = xyes]) # # Uterm drm backend @@ -332,11 +436,11 @@ if test ! x$enable_drm = xno ; then fi if test x$dumb_enabled = xyes ; then - AC_DEFINE([UTERM_HAVE_DUMB], [1], + AC_DEFINE([BUILD_ENABLE_DUMB], [1], [Use uterm dumb drm video backend]) if test x$drm_enabled = xyes ; then - AC_DEFINE([UTERM_HAVE_DRM], [1], + AC_DEFINE([BUILD_ENABLE_DRM], [1], [Use uterm DRM video backend]) else GBM_CFLAGS="" @@ -354,17 +458,18 @@ else fi if test x$gles2_enabled = xyes ; then - AC_DEFINE([KMSCON_HAVE_GLES2], [1], + AC_DEFINE([BUILD_ENABLE_GLES2], [1], [Use OpenGLESv2 as drawing backend]) else GLES2_CFLAGS="" GLES2_LIBS="" fi -AM_CONDITIONAL([UTERM_HAVE_DUMB], [test x$dumb_enabled = xyes]) -AM_CONDITIONAL([UTERM_HAVE_DRM], [test x$drm_enabled = xyes]) -AM_CONDITIONAL([KMSCON_HAVE_GLES2], [test x$gles2_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_DUMB], [test x$dumb_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_DRM], [test x$drm_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_GLES2], [test x$gles2_enabled = xyes]) +# check for gbm_bo_get_stride() function, otherwise gbm_bo_get_pitch() is used if test x$have_gbm = xyes ; then save_CFLAGS="$CFLAGS" save_LIBS="$LIBS" @@ -372,7 +477,7 @@ if test x$have_gbm = xyes ; then LIBS="$LIBS $GBM_LIBS $DRM_LIBS" AC_CHECK_LIB([gbm], [gbm_bo_get_stride], - [AC_DEFINE([HAVE_GBM_BO_GET_STRIDE], + [AC_DEFINE([BUILD_HAVE_GBM_BO_GET_STRIDE], [1], [Define to 1 if your libgbm provides gbm_bo_get_stride])]) CFLAGS="$save_CFLAGS" @@ -395,14 +500,14 @@ if test ! x$enable_xkbcommon = xno ; then fi if test x$xkbcommon_enabled = xyes ; then - AC_DEFINE([UTERM_HAVE_XKBCOMMON], [1], + AC_DEFINE([BUILD_ENABLE_XKBCOMMON], [1], [Use xkbcommon as input keyboard handling backend]) else XKBCOMMON_CFLAGS="" XKBCOMMON_LIBS="" fi -AM_CONDITIONAL([UTERM_HAVE_XKBCOMMON], [test x$xkbcommon_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_XKBCOMMON], [test x$xkbcommon_enabled = xyes]) # # Font backends @@ -421,11 +526,11 @@ if test x$enable_unifont = xyes ; then fi if test x$unifont_enabled = xyes ; then - AC_DEFINE([KMSCON_HAVE_UNIFONT], [1], + AC_DEFINE([BUILD_ENABLE_UNIFONT], [1], [Use static unifont backend]) fi -AM_CONDITIONAL([KMSCON_HAVE_UNIFONT], [test x$unifont_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_UNIFONT], [test x$unifont_enabled = xyes]) f8x16_enabled=no if test ! x$enable_f8x16 = xno ; then @@ -433,11 +538,11 @@ if test ! x$enable_f8x16 = xno ; then fi if test x$f8x16_enabled = xyes ; then - AC_DEFINE([KMSCON_HAVE_8X16], [1], + AC_DEFINE([BUILD_ENABLE_8X16], [1], [Use static 8x16 font backend]) fi -AM_CONDITIONAL([KMSCON_HAVE_8X16], [test x$f8x16_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_8X16], [test x$f8x16_enabled = xyes]) freetype2_enabled=no if test ! x$enable_freetype2 = xno ; then @@ -449,14 +554,14 @@ if test ! x$enable_freetype2 = xno ; then fi if test x$freetype2_enabled = xyes ; then - AC_DEFINE([KMSCON_HAVE_FREETYPE2], [1], + AC_DEFINE([BUILD_ENABLE_FREETYPE2], [1], [Use freetype2 as font backend]) else FREETYPE2_CFLAGS="" FREETYPE2_LIBS="" fi -AM_CONDITIONAL([KMSCON_HAVE_FREETYPE2], [test x$freetype2_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_FREETYPE2], [test x$freetype2_enabled = xyes]) pango_enabled=no if test ! x$enable_pango = xno ; then @@ -468,14 +573,14 @@ if test ! x$enable_pango = xno ; then fi if test x$pango_enabled = xyes ; then - AC_DEFINE([KMSCON_HAVE_PANGO], [1], + AC_DEFINE([BUILD_ENABLE_PANGO], [1], [Use pango as font backend]) else PANGO_CFLAGS="" PANGO_LIBS="" fi -AM_CONDITIONAL([KMSCON_HAVE_PANGO], [test x$pango_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_PANGO], [test x$pango_enabled = xyes]) # # BBlit Rendering backend @@ -487,11 +592,11 @@ if test ! x$enable_bblit = xno ; then fi if test x$bblit_enabled = xyes ; then - AC_DEFINE([KMSCON_HAVE_BBLIT], [1], + AC_DEFINE([BUILD_ENABLE_BBLIT], [1], [Use bblit rendering backend]) fi -AM_CONDITIONAL([KMSCON_HAVE_BBLIT], [test x$bblit_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_BBLIT], [test x$bblit_enabled = xyes]) # # BBulk Rendering backend @@ -503,11 +608,11 @@ if test ! x$enable_bbulk = xno ; then fi if test x$bbulk_enabled = xyes ; then - AC_DEFINE([KMSCON_HAVE_BBULK], [1], + AC_DEFINE([BUILD_ENABLE_BBULK], [1], [Use bbulk rendering backend]) fi -AM_CONDITIONAL([KMSCON_HAVE_BBULK], [test x$bbulk_enabled = xyes]) +AM_CONDITIONAL([BUILD_ENABLE_BBULK], [test x$bbulk_enabled = xyes]) # # OpenGL Texture rendering backend @@ -520,6 +625,28 @@ if test x$gles2_enabled = xyes ; then gltex_enabled=yes fi +# +# Wayland +# This checks whether the wayland libraries are needed and available. +# + +wayland_enabled=no +if test ! x$enable_wayland = xno ; then + if test x$have_wayland = xyes ; then + wayland_enabled=yes + elif test x$enable_wayland = xyes ; then + AC_ERROR([wayland libraries not found]) + fi +fi + +if test x$wayland_enabled = xyes ; then + AC_DEFINE([BUILD_ENABLE_WAYLAND], [1], + [Enable wayland backends]) +else + WAYLAND_CFLAGS="" + WAYLAND_LIBS="" +fi + # # Makefile vars # After everything is configured, we correctly substitute the values for the @@ -546,6 +673,8 @@ AC_SUBST(FREETYPE2_CFLAGS) AC_SUBST(FREETYPE2_LIBS) AC_SUBST(PANGO_CFLAGS) AC_SUBST(PANGO_LIBS) +AC_SUBST(WAYLAND_CFLAGS) +AC_SUBST(WAYLAND_LIBS) AC_CONFIG_FILES([Makefile libeloop.pc libtsm.pc libuterm.pc]) AC_OUTPUT @@ -558,9 +687,18 @@ AC_OUTPUT AC_MSG_NOTICE([Build configuration: + Applications and libraries: + kmscon: $kmscon_enabled + wlterm: $wlterm_enabled + uterm: $uterm_enabled + eloop: $eloop_enabled + tsm: $tsm_enabled + + Miscellaneous options: debug: $debug_enabled optimizations: $optimizations_enabled + Optional dependencies: systemd: $systemd_enabled udev: $udev_enabled dbus: $dbus_enabled diff --git a/src/log.c b/src/log.c index 4b6871d..1fcd774 100644 --- a/src/log.c +++ b/src/log.c @@ -73,6 +73,7 @@ static void log__time(long long *sec, long long *usec) * this so we default to 1 if no external build-number is specified. */ +/* TODO: define LOG_BUILD as incremental build number */ #ifdef LOG_BUILD static unsigned long log_build = LOG_BUILD; #else diff --git a/src/log.h b/src/log.h index b073ae3..52b07f6 100644 --- a/src/log.h +++ b/src/log.h @@ -18,8 +18,8 @@ * debugging-only because it may slow-down your application if every message is * filtered. * - * Define LOG_ENABLE_DEBUG before including this header to enable debug-messages - * for this file. + * Define BUILD_ENABLE_DEBUG before including this header to enable + * debug-messages for this file. */ #ifndef LOG_H_INCLUDED @@ -258,11 +258,11 @@ extern const char *LOG_SUBSYSTEM; * Helpers * The pick-up all the default values and submit the message to the * log-subsystem. The log_debug() function produces zero-code if - * LOG_ENABLE_DEBUG is not defined. Therefore, it can be heavily used for + * BUILD_ENABLE_DEBUG is not defined. Therefore, it can be heavily used for * debugging and will not have any side-effects. */ -#ifdef LOG_ENABLE_DEBUG +#ifdef BUILD_ENABLE_DEBUG #define log_debug(format, ...) \ log_printf(LOG_DEBUG, (format), ##__VA_ARGS__) #else diff --git a/src/shl_llog.h b/src/shl_llog.h index 636fa80..0325957 100644 --- a/src/shl_llog.h +++ b/src/shl_llog.h @@ -101,22 +101,29 @@ static const char *LLOG_SUBSYSTEM __attribute__((__unused__)); #define llog_dprintf(obj, sev, format, ...) \ llog_format((obj), LLOG_DEFAULT, (sev), (format), ##__VA_ARGS__) +static inline void llog_dummyf(llog_submit_t llog, unsigned int sev, + const char *format, ...) +{ +} + /* * Helpers * They pick-up all the default values and submit the message to the * llog-subsystem. The llog_debug() function produces zero-code if - * LLOG_ENABLE_DEBUG is not defined. Therefore, it can be heavily used for + * BUILD_ENABLE_DEBUG is not defined. Therefore, it can be heavily used for * debugging and will not have any side-effects. */ -#ifdef LLOG_ENABLE_DEBUG +#ifdef BUILD_ENABLE_DEBUG #define llog_ddebug(obj, format, ...) \ llog_dprintf((obj), LLOG_DEBUG, (format), ##__VA_ARGS__) #define llog_debug(obj, format, ...) \ llog_ddebug((obj)->llog, (format), ##__VA_ARGS__) #else - #define llog_ddebug(obj, format, ...) ((void)0) - #define llog_debug(obj, format, ...) ((void)0) + #define llog_ddebug(obj, format, ...) \ + llog_dummyf((obj), LLOG_DEBUG, (format), ##__VA_ARGS__) + #define llog_debug(obj, format, ...) \ + llog_ddebug((obj)->llog, (format), ##__VA_ARGS__) #endif #define llog_info(obj, format, ...) \ diff --git a/src/text.h b/src/text.h index 15201a9..d7e91a4 100644 --- a/src/text.h +++ b/src/text.h @@ -175,7 +175,7 @@ int kmscon_text_render_cb(struct tsm_screen *con, void *data); /* modularized backends */ -#ifdef KMSCON_HAVE_UNIFONT +#ifdef BUILD_ENABLE_UNIFONT int kmscon_font_unifont_load(void); void kmscon_font_unifont_unload(void); @@ -194,7 +194,7 @@ static inline void kmscon_font_unifont_unload(void) #endif -#ifdef KMSCON_HAVE_8X16 +#ifdef BUILD_ENABLE_8X16 int kmscon_font_8x16_load(void); void kmscon_font_8x16_unload(void); @@ -212,7 +212,7 @@ static inline void kmscon_font_8x16_unload(void) #endif -#ifdef KMSCON_HAVE_FREETYPE2 +#ifdef BUILD_ENABLE_FREETYPE2 int kmscon_font_freetype2_load(void); void kmscon_font_freetype2_unload(void); @@ -230,7 +230,7 @@ static inline void kmscon_font_freetype2_unload(void) #endif -#ifdef KMSCON_HAVE_PANGO +#ifdef BUILD_ENABLE_PANGO int kmscon_font_pango_load(void); void kmscon_font_pango_unload(void); @@ -248,7 +248,7 @@ static inline void kmscon_font_pango_unload(void) #endif -#ifdef KMSCON_HAVE_BBLIT +#ifdef BUILD_ENABLE_BBLIT int kmscon_text_bblit_load(void); void kmscon_text_bblit_unload(void); @@ -266,7 +266,7 @@ static inline void kmscon_text_bblit_unload(void) #endif -#ifdef KMSCON_HAVE_BBULK +#ifdef BUILD_ENABLE_BBULK int kmscon_text_bbulk_load(void); void kmscon_text_bbulk_unload(void); @@ -284,7 +284,7 @@ static inline void kmscon_text_bbulk_unload(void) #endif -#ifdef KMSCON_HAVE_GLES2 +#ifdef BUILD_ENABLE_GLES2 int kmscon_text_gltex_load(void); void kmscon_text_gltex_unload(void); diff --git a/src/tsm_vte.c b/src/tsm_vte.c index 9e812e9..aed3039 100644 --- a/src/tsm_vte.c +++ b/src/tsm_vte.c @@ -485,7 +485,7 @@ int tsm_vte_set_palette(struct tsm_vte *vte, const char *palette) static void vte_write_debug(struct tsm_vte *vte, const char *u8, size_t len, bool raw, const char *file, int line) { -#ifdef KMSCON_ENABLE_DEBUG +#ifdef BUILD_ENABLE_DEBUG /* in debug mode we check that escape sequences are always <0x7f so they * are correctly parsed by non-unicode and non-8bit-mode clients. */ size_t i; diff --git a/src/uterm_input.c b/src/uterm_input.c index 9f1499c..405ffc0 100644 --- a/src/uterm_input.c +++ b/src/uterm_input.c @@ -499,7 +499,7 @@ int uterm_input_string_to_keysym(struct uterm_input *input, const char *n, if (input) return kbd_desc_string_to_keysym(input->desc, n, out); -#ifdef UTERM_HAVE_XKBCOMMON +#ifdef BUILD_ENABLE_XKBCOMMON return uxkb_string_to_keysym(n, out); #endif diff --git a/src/uterm_input.h b/src/uterm_input.h index 322e5cb..c78a662 100644 --- a/src/uterm_input.h +++ b/src/uterm_input.h @@ -71,7 +71,7 @@ extern const struct kbd_dev_ops plain_dev_ops; extern int plain_string_to_keysym(const char *n, uint32_t *out); -#ifdef UTERM_HAVE_XKBCOMMON +#ifdef BUILD_ENABLE_XKBCOMMON struct uxkb_desc { struct xkb_context *ctx; @@ -88,7 +88,7 @@ extern const struct kbd_dev_ops uxkb_dev_ops; extern int uxkb_string_to_keysym(const char *n, uint32_t *out); -#else /* !UTERM_HAVE_XKBCOMMON */ +#else /* !BUILD_ENABLE_XKBCOMMON */ struct uxkb_desc { int unused; @@ -102,7 +102,7 @@ static const bool uxkb_available = false; static const struct kbd_desc_ops uxkb_desc_ops; static const struct kbd_dev_ops uxkb_dev_ops; -#endif /* UTERM_HAVE_XKBCOMMON */ +#endif /* BUILD_ENABLE_XKBCOMMON */ struct kbd_desc { unsigned long ref; diff --git a/src/uterm_monitor.c b/src/uterm_monitor.c index b1131a9..0ff620e 100644 --- a/src/uterm_monitor.c +++ b/src/uterm_monitor.c @@ -44,7 +44,7 @@ #include "shl_dlist.h" #include "uterm.h" -#ifdef UTERM_HAVE_SYSTEMD +#ifdef BUILD_ENABLE_SYSTEMD #include #endif @@ -72,7 +72,7 @@ struct uterm_monitor { uterm_monitor_cb cb; void *data; -#ifdef UTERM_HAVE_SYSTEMD +#ifdef BUILD_ENABLE_SYSTEMD sd_login_monitor *sd_mon; struct ev_fd *sd_mon_fd; #endif @@ -87,7 +87,7 @@ struct uterm_monitor { static void monitor_new_seat(struct uterm_monitor *mon, const char *name); static void monitor_free_seat(struct uterm_monitor_seat *seat); -#ifdef UTERM_HAVE_SYSTEMD +#ifdef BUILD_ENABLE_SYSTEMD static void monitor_refresh_seats(struct uterm_monitor *mon) { @@ -184,7 +184,7 @@ static void monitor_sd_deinit(struct uterm_monitor *mon) sd_login_monitor_unref(mon->sd_mon); } -#else /* !UTERM_HAVE_SYSTEMD */ +#else /* !BUILD_ENABLE_SYSTEMD */ static void monitor_refresh_seats(struct uterm_monitor *mon) { @@ -205,7 +205,7 @@ static void monitor_sd_deinit(struct uterm_monitor *mon) { } -#endif /* UTERM_HAVE_SYSTEMD */ +#endif /* BUILD_ENABLE_SYSTEMD */ static void seat_new_dev(struct uterm_monitor_seat *seat, unsigned int type, @@ -474,7 +474,7 @@ static void monitor_udev_add(struct uterm_monitor *mon, } if (!strcmp(subs, "drm")) { -#ifdef UTERM_HAVE_SYSTEMD +#ifdef BUILD_ENABLE_SYSTEMD if (udev_device_has_tag(dev, "seat") != 1) { log_debug("adding non-seat'ed device %s", name); return; @@ -488,7 +488,7 @@ static void monitor_udev_add(struct uterm_monitor *mon, sname = udev_device_get_property_value(dev, "ID_SEAT"); type = UTERM_MONITOR_DRM; } else if (!strcmp(subs, "graphics")) { -#ifdef UTERM_HAVE_SYSTEMD +#ifdef BUILD_ENABLE_SYSTEMD if (udev_device_has_tag(dev, "seat") != 1) { log_debug("adding non-seat'ed device %s", name); return; @@ -516,7 +516,7 @@ static void monitor_udev_add(struct uterm_monitor *mon, log_debug("adding device without parent %s", name); return; } -#ifdef UTERM_HAVE_SYSTEMD +#ifdef BUILD_ENABLE_SYSTEMD if (udev_device_has_tag(p, "seat") != 1) { log_debug("adding non-seat'ed device %s", name); return; diff --git a/src/uterm_video.h b/src/uterm_video.h index cadfcbb..b4b7655 100644 --- a/src/uterm_video.h +++ b/src/uterm_video.h @@ -86,7 +86,7 @@ struct video_ops { /* drm */ -#ifdef UTERM_HAVE_DRM +#ifdef BUILD_ENABLE_DRM #include #include @@ -148,7 +148,7 @@ extern const struct mode_ops drm_mode_ops; extern const struct display_ops drm_display_ops; extern const struct video_ops drm_video_ops; -#else /* !UTERM_HAVE_DRM */ +#else /* !BUILD_ENABLE_DRM */ struct drm_mode { int unused; @@ -167,11 +167,11 @@ static const struct mode_ops drm_mode_ops; static const struct display_ops drm_display_ops; static const struct video_ops drm_video_ops; -#endif /* UTERM_HAVE_DRM */ +#endif /* BUILD_ENABLE_DRM */ /* dumb drm */ -#ifdef UTERM_HAVE_DUMB +#ifdef BUILD_ENABLE_DUMB #include #include @@ -208,7 +208,7 @@ extern const struct mode_ops dumb_mode_ops; extern const struct display_ops dumb_display_ops; extern const struct video_ops dumb_video_ops; -#else /* !UTERM_HAVE_DUMB */ +#else /* !BUILD_ENABLE_DUMB */ struct dumb_mode { int unused; @@ -227,11 +227,11 @@ static const struct mode_ops dumb_mode_ops; static const struct display_ops dumb_display_ops; static const struct video_ops dumb_video_ops; -#endif /* UTERM_HAVE_DUMB */ +#endif /* BUILD_ENABLE_DUMB */ /* fbdev */ -#ifdef UTERM_HAVE_FBDEV +#ifdef BUILD_ENABLE_FBDEV #include @@ -277,7 +277,7 @@ extern const struct mode_ops fbdev_mode_ops; extern const struct display_ops fbdev_display_ops; extern const struct video_ops fbdev_video_ops; -#else /* !UTERM_HAVE_FBDEV */ +#else /* !BUILD_ENABLE_FBDEV */ struct fbdev_mode { int unused; @@ -296,7 +296,7 @@ static const struct mode_ops fbdev_mode_ops; static const struct display_ops fbdev_display_ops; static const struct video_ops fbdev_video_ops; -#endif /* UTERM_HAVE_FBDEV */ +#endif /* BUILD_ENABLE_FBDEV */ /* uterm_screen */ diff --git a/src/uterm_video_drm.c b/src/uterm_video_drm.c index 6ea5355..dbf7f84 100644 --- a/src/uterm_video_drm.c +++ b/src/uterm_video_drm.c @@ -82,7 +82,7 @@ static int init_rb(struct uterm_display *disp, struct drm_rb *rb) return -EFAULT; } -#ifdef HAVE_GBM_BO_GET_STRIDE +#ifdef BUILD_HAVE_GBM_BO_GET_STRIDE stride = gbm_bo_get_stride(rb->bo); #else stride = gbm_bo_get_pitch(rb->bo); diff --git a/src/wlt_main.c b/src/wlt_main.c new file mode 100644 index 0000000..ea5c8e4 --- /dev/null +++ b/src/wlt_main.c @@ -0,0 +1,271 @@ +/* + * wlt - Wayland Terminal + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal main application + */ + +#include +#include +#include +#include +#include +#include +#include +#include "conf.h" +#include "eloop.h" +#include "log.h" +#include "wlt_main.h" +#include "wlt_terminal.h" +#include "wlt_theme.h" +#include "wlt_toolkit.h" + +#define LOG_SUBSYSTEM "wlt" + +struct wlt_app { + struct ev_eloop *eloop; + struct wlt_display *disp; + struct wlt_window *wnd; +}; + +static void sig_generic(struct ev_eloop *eloop, struct signalfd_siginfo *info, + void *data) +{ + struct wlt_app *app = data; + + ev_eloop_exit(app->eloop); + log_info("terminating due to caught signal %d", info->ssi_signo); +} + +static void window_close(struct wlt_window *wnd, void *data) +{ + struct wlt_app *app = data; + + log_info("closing window"); + ev_eloop_exit(app->eloop); +} + +static int window_init(struct wlt_app *app) +{ + int ret; + struct wlt_theme *theme; + struct wlt_terminal *term; + + ret = wlt_display_create_window(app->disp, &app->wnd, + 200, 200, app); + if (ret) { + log_error("cannot create wayland window"); + return ret; + } + wlt_window_set_close_cb(app->wnd, window_close); + + ret = wlt_theme_new(&theme, app->wnd); + if (ret) { + log_error("cannot create theme"); + return ret; + } + + ret = wlt_terminal_new(&term, app->wnd); + if (ret) { + log_error("cannot create terminal"); + return ret; + } + + return 0; +} + +static void display_event(struct wlt_display *disp, unsigned int event, + void *data) +{ + struct wlt_app *app = data; + int ret; + + switch (event) { + case WLT_DISPLAY_READY: + log_info("wayland display initialized"); + ret = window_init(app); + if (ret) + ev_eloop_exit(app->eloop); + break; + case WLT_DISPLAY_HUP: + log_info("wayland display connection lost"); + ev_eloop_exit(app->eloop); + break; + } +} + +static void destroy_app(struct wlt_app *app) +{ + wlt_window_unref(app->wnd); + wlt_display_unregister_cb(app->disp, display_event, app); + wlt_display_unref(app->disp); + ev_eloop_unregister_signal_cb(app->eloop, SIGINT, sig_generic, app); + ev_eloop_unregister_signal_cb(app->eloop, SIGTERM, sig_generic, app); + ev_eloop_unref(app->eloop); +} + +static int setup_app(struct wlt_app *app) +{ + int ret; + + ret = ev_eloop_new(&app->eloop, log_llog); + if (ret) + goto err_app; + + ret = ev_eloop_register_signal_cb(app->eloop, SIGTERM, + sig_generic, app); + if (ret) + goto err_app; + + ret = ev_eloop_register_signal_cb(app->eloop, SIGINT, + sig_generic, app); + if (ret) + goto err_app; + + ret = wlt_display_new(&app->disp, app->eloop); + if (ret) + goto err_app; + + ret = wlt_display_register_cb(app->disp, display_event, app); + if (ret) + goto err_app; + + return 0; + +err_app: + destroy_app(app); + return ret; +} + +struct wlt_conf_t wlt_conf; + +static void print_help() +{ + /* + * Usage/Help information + * This should be scaled to a maximum of 80 characters per line: + * + * 80 char line: + * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | + * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n" + * 80 char line starting with tab: + * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 | + * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n" + */ + fprintf(stderr, + "Usage:\n" + "\t%1$s [options]\n" + "\t%1$s -h [options]\n" + "\n" + "You can prefix boolean options with \"no-\" to negate it. If an argument is\n" + "given multiple times, only the last argument matters if not otherwise stated.\n" + "\n" + "General Options:\n" + "\t-h, --help [off] Print this help and exit\n" + "\t-v, --verbose [off] Print verbose messages\n" + "\t --debug [off] Enable debug mode\n" + "\t --silent [off] Suppress notices and warnings\n", + "wlterm"); + /* + * 80 char line: + * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | + * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n" + * 80 char line starting with tab: + * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 | + * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n" + */ +} + +static int aftercheck_debug(struct conf_option *opt, int argc, char **argv, + int idx) +{ + /* --debug implies --verbose */ + if (wlt_conf.debug) + wlt_conf.verbose = 1; + + return 0; +} + +static int aftercheck_help(struct conf_option *opt, int argc, char **argv, + int idx) +{ + /* exit after printing --help information */ + if (wlt_conf.help) { + print_help(); + wlt_conf.exit = true; + } + + return 0; +} + +struct conf_option options[] = { + CONF_OPTION_BOOL('h', "help", aftercheck_help, &wlt_conf.help, false), + CONF_OPTION_BOOL('v', "verbose", NULL, &wlt_conf.verbose, false), + CONF_OPTION_BOOL(0, "debug", aftercheck_debug, &wlt_conf.debug, false), + CONF_OPTION_BOOL(0, "silent", NULL, &wlt_conf.silent, false), +}; + +int main(int argc, char **argv) +{ + int ret; + struct wlt_app app; + size_t onum; + + onum = sizeof(options) / sizeof(*options); + ret = conf_parse_argv(options, onum, argc, argv); + if (ret) + goto err_out; + + if (wlt_conf.exit) { + conf_free(options, onum); + return EXIT_SUCCESS; + } + + if (!wlt_conf.debug && !wlt_conf.verbose && wlt_conf.silent) + log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0)); + else + log_set_config(&LOG_CONFIG_INFO(wlt_conf.debug, + wlt_conf.verbose)); + + log_print_init("wlterm"); + + memset(&app, 0, sizeof(app)); + ret = setup_app(&app); + if (ret) + goto err_out; + + ev_eloop_run(app.eloop, -1); + + destroy_app(&app); + conf_free(options, onum); + log_info("exiting"); + + return EXIT_SUCCESS; + +err_out: + conf_free(options, onum); + log_err("cannot initialize wlterm, errno %d: %s", ret, strerror(-ret)); + return -ret; +} diff --git a/src/wlt_main.h b/src/wlt_main.h new file mode 100644 index 0000000..cf624a2 --- /dev/null +++ b/src/wlt_main.h @@ -0,0 +1,51 @@ +/* + * wlt - Main Header + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal global information and state + */ + +#ifndef WLT_MAIN_H +#define WLT_MAIN_H + +#include +#include + +struct wlt_conf_t { + /* show help/usage information */ + bool help; + /* exit application after parsing options */ + bool exit; + /* enable debug messages */ + bool debug; + /* enable verbose info messages */ + bool verbose; + /* disable notices and warnings */ + bool silent; +}; + +extern struct wlt_conf_t wlt_conf; + +#endif /* WLT_MAIN_H */ diff --git a/src/wlt_terminal.c b/src/wlt_terminal.c new file mode 100644 index 0000000..a23bba8 --- /dev/null +++ b/src/wlt_terminal.c @@ -0,0 +1,119 @@ +/* + * wlt - Terminals + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal console helpers + */ + +#include +#include +#include +#include "log.h" +#include "tsm_unicode.h" +#include "tsm_screen.h" +#include "tsm_vte.h" +#include "wlt_toolkit.h" + +#define LOG_SUBSYSTEM "wlt_terminal" + +struct wlt_terminal { + struct wlt_window *wnd; + struct wlt_widget *widget; + struct wlt_shm_buffer buffer; +}; + +static void widget_redraw(struct wlt_widget *widget, void *data) +{ + struct wlt_terminal *term = data; + unsigned int i, j; + uint8_t *dst; + uint32_t *line; + + dst = term->buffer.data; + for (i = 0; i < term->buffer.height; ++i) { + line = (uint32_t*)dst; + for (j = 0; j < term->buffer.width; ++j) + line[j] = 0xffffffff; + dst += term->buffer.stride; + } +} + +static void widget_resize(struct wlt_widget *widget, struct wlt_rect *alloc, + void *data) +{ + struct wlt_terminal *term = data; + + wlt_window_get_buffer(term->wnd, alloc, &term->buffer); + + /* don't allow children */ + alloc->width = 0; + alloc->height = 0; +} + +static void widget_destroy(struct wlt_widget *widget, void *data) +{ + struct wlt_terminal *term = data; + + free(term); +} + +int wlt_terminal_new(struct wlt_terminal **out, struct wlt_window *wnd) +{ + struct wlt_terminal *term; + int ret; + + if (!out || !wnd) + return -EINVAL; + + term = malloc(sizeof(*term)); + if (!term) + return -ENOMEM; + memset(term, 0, sizeof(*term)); + term->wnd = wnd; + + ret = wlt_window_create_widget(wnd, &term->widget, term); + if (ret) { + log_error("cannot create terminal widget"); + goto err_free; + } + + wlt_widget_set_destroy_cb(term->widget, widget_destroy); + wlt_widget_set_redraw_cb(term->widget, widget_redraw); + wlt_widget_set_resize_cb(term->widget, NULL, widget_resize); + *out = term; + return 0; + +err_free: + free(term); + return ret; +} + +void wlt_terminal_destroy(struct wlt_terminal *term) +{ + if (!term) + return; + + wlt_widget_destroy(term->widget); +} diff --git a/src/wlt_terminal.h b/src/wlt_terminal.h new file mode 100644 index 0000000..d60dab9 --- /dev/null +++ b/src/wlt_terminal.h @@ -0,0 +1,41 @@ +/* + * wlt - Terminals + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal console helpers + */ + +#ifndef WLT_TERMINAL_H +#define WLT_TERMINAL_H + +#include +#include "wlt_toolkit.h" + +struct wlt_terminal; + +int wlt_terminal_new(struct wlt_terminal **out, struct wlt_window *wnd); +void wlt_terminal_destroy(struct wlt_terminal *term); + +#endif /* WLT_TERMINAL_H */ diff --git a/src/wlt_theme.c b/src/wlt_theme.c new file mode 100644 index 0000000..96c1caa --- /dev/null +++ b/src/wlt_theme.c @@ -0,0 +1,563 @@ +/* + * wlt - Theme Helper + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal theme/decoration drawing helper + */ + +#include +#include +#include +#include +#include +#include "log.h" +#include "wlt_theme.h" +#include "wlt_toolkit.h" + +#define LOG_SUBSYSTEM "wlt_theme" + +enum theme_location { + LOC_NOWHERE, + LOC_SOMEWHERE, + LOC_RESIZE_TOP, + LOC_RESIZE_BOTTOM, + LOC_RESIZE_LEFT, + LOC_RESIZE_RIGHT, + LOC_RESIZE_TOP_LEFT, + LOC_RESIZE_TOP_RIGHT, + LOC_RESIZE_BOTTOM_LEFT, + LOC_RESIZE_BOTTOM_RIGHT, + LOC_CONTROL, + LOC_MINIMIZE, + LOC_MAXIMIZE, + LOC_CLOSE, +}; + +struct wlt_theme { + struct wlt_window *wnd; + struct wlt_widget *widget; + struct wlt_shm_buffer buffer; + struct wlt_rect alloc; + unsigned int control_height; + unsigned int frame_width; + unsigned int resize_margin; + unsigned int button_size; + unsigned int button_padding; + unsigned int button_margin; + + unsigned int pointer_x; + unsigned int pointer_y; + unsigned int pointer_loc; + bool pointer_pressed; + unsigned int pointer_grabbed; +}; + +static void draw_control(struct wlt_theme *theme) +{ + uint8_t *dst; + uint32_t *line, backcol; + unsigned int i, j; + unsigned int b1off, b2off, b3off; + + /* background */ + if (theme->pointer_loc == LOC_NOWHERE) + backcol = (0x60 << 24) | (0xaa << 16) | + (0xaa << 8) | 0xaa; + else + backcol = (0x20 << 24) | (0xee << 16) | + (0xee << 8) | 0xee; + dst = theme->buffer.data; + for (i = 0; i < theme->control_height; ++i) { + line = (uint32_t*)dst; + for (j = 0; j < theme->buffer.width; ++j) { + if (i == 0 || i == theme->control_height - 1 || + j == 0 || j == theme->buffer.width - 1) + line[j] = 0xff << 24; + else + line[j] = backcol; + } + dst += theme->buffer.stride; + } + + /* buttons */ + b1off = theme->buffer.width - theme->button_margin - theme->button_size; + b2off = b1off - theme->button_padding - theme->button_size; + b3off = b2off - theme->button_padding - theme->button_size; + dst = theme->buffer.data + theme->buffer.stride * theme->button_margin; + for (i = 0; i < theme->button_size; ++i) { + line = (uint32_t*)dst; + for (j = 0; j < theme->buffer.width; ++j) { + if (j >= b1off && + j < b1off + theme->button_size) { + if (i == 0 || + i == theme->button_size - 1 || + j == b1off || + j == b1off + theme->button_size - 1) + line[j] = 0xff << 24; + else if (theme->pointer_loc == LOC_CLOSE && + theme->pointer_pressed && + theme->pointer_grabbed == LOC_CLOSE) + line[j] = (0xff << 24) | 0x1f1f1f1f; + else if (theme->pointer_loc == LOC_CLOSE && + !theme->pointer_pressed) + line[j] = 0xffffffff; + else + line[j] = (0xff << 24) | 0x33333333; + } else if (j >= b2off && + j < b2off + theme->button_size) { + if (i == 0 || + i == theme->button_size - 1 || + j == b2off || + j == b2off + theme->button_size - 1) + line[j] = 0xff << 24; + else if (theme->pointer_loc == LOC_MAXIMIZE && + theme->pointer_pressed && + theme->pointer_grabbed == LOC_MAXIMIZE) + line[j] = (0xff << 24) | 0x1f1f1f1f; + else if (theme->pointer_loc == LOC_MAXIMIZE && + !theme->pointer_pressed) + line[j] = 0xffffffff; + else + line[j] = (0xff << 24) | 0x66666666; + } else if (j >= b3off && + j < b3off + theme->button_size) { + if (i == 0 || + i == theme->button_size - 1 || + j == b3off || + j == b3off + theme->button_size - 1) + line[j] = 0xff << 24; + else if (theme->pointer_loc == LOC_MINIMIZE && + theme->pointer_pressed && + theme->pointer_grabbed == LOC_MINIMIZE) + line[j] = (0xff << 24) | 0x1f1f1f1f; + else if (theme->pointer_loc == LOC_MINIMIZE && + !theme->pointer_pressed) + line[j] = 0xffffffff; + else + line[j] = (0xff << 24) | 0xaaaaaaaa; + } + } + dst += theme->buffer.stride; + } +} + +static void draw_frame(struct wlt_theme *theme) +{ + uint8_t *dst; + uint32_t *line; + unsigned int i, j, height; + + /* top frame */ + dst = theme->buffer.data + theme->buffer.stride * + theme->control_height; + for (i = 0; i < theme->frame_width; ++i) { + line = (uint32_t*)dst; + for (j = 0; j < theme->buffer.width; ++j) + line[j] = 0xa0 << 24; + dst += theme->buffer.stride; + } + + /* bottom frame */ + dst = theme->buffer.data + theme->buffer.stride * + (theme->buffer.height - theme->frame_width); + for (i = 0; i < theme->frame_width; ++i) { + line = (uint32_t*)dst; + for (j = 0; j < theme->buffer.width; ++j) + line[j] = 0xa0 << 24; + dst += theme->buffer.stride; + } + + /* left frame */ + dst = theme->buffer.data + theme->buffer.stride * + (theme->control_height + theme->frame_width); + height = theme->buffer.height - theme->control_height - + theme->frame_width * 2; + for (i = 0; i < height; ++i) { + line = (uint32_t*)dst; + for (j = 0; j < theme->frame_width; ++j) + line[j] = 0xa0 << 24; + dst += theme->buffer.stride; + } + + /* right frame */ + dst = theme->buffer.data + theme->buffer.stride * + (theme->control_height + theme->frame_width); + height = theme->buffer.height - theme->control_height - + theme->frame_width * 2; + for (i = 0; i < height; ++i) { + line = (uint32_t*)dst; + line += theme->buffer.width - theme->frame_width; + for (j = 0; j < theme->frame_width; ++j) + line[j] = 0xa0 << 24; + dst += theme->buffer.stride; + } +} + +static void widget_draw_fallback(struct wlt_theme *theme) +{ + uint8_t *dst; + uint32_t *line; + unsigned int i, j; + + dst = theme->buffer.data; + for (i = 0; i < theme->buffer.height; ++i) { + line = (uint32_t*)dst; + for (j = 0; j < theme->buffer.width; ++j) { + line[j] = 0xff << 24; + } + dst += theme->buffer.stride; + } +} + +static void widget_redraw(struct wlt_widget *widget, void *data) +{ + struct wlt_theme *theme = data; + unsigned int width, height; + + width = theme->buffer.width; + height = theme->buffer.height; + if (width < 2 || + width < 2 * theme->frame_width) { + widget_draw_fallback(theme); + } else if (height < theme->control_height + 2 * theme->frame_width) { + widget_draw_fallback(theme); + } else { + draw_frame(theme); + draw_control(theme); + } +} + +static void widget_prepare_resize(struct wlt_widget *widget, + unsigned int *width, unsigned int *height, + void *data) +{ + struct wlt_theme *theme = data; + unsigned int minw, minh; + + minw = theme->frame_width * 2; + minh = theme->control_height + theme->frame_width * 2; + if (*width < minw) + *width = minw; + if (*height < minh) + *height = minh; +} + +static void widget_resize(struct wlt_widget *widget, struct wlt_rect *alloc, + void *data) +{ + struct wlt_theme *theme = data; + unsigned int nwidth, nheight; + + wlt_window_get_buffer(theme->wnd, alloc, &theme->buffer); + memcpy(&theme->alloc, alloc, sizeof(*alloc)); + + alloc->x = theme->frame_width; + alloc->y = theme->control_height + theme->frame_width; + nwidth = alloc->width - 2 * theme->frame_width; + nheight = alloc->height; + nheight -= (theme->control_height + 2 * theme->frame_width); + + if (nwidth > alloc->width || nheight > alloc->height) { + alloc->width = 0; + alloc->height = 0; + } else { + alloc->width = nwidth; + alloc->height = nheight; + } +} + +static unsigned int get_pointer_location(struct wlt_theme *theme) +{ + unsigned int m = theme->resize_margin; + unsigned int b1off, b2off, b3off; + + if (theme->pointer_y < m) { + if (theme->pointer_x < m) + return LOC_RESIZE_TOP_LEFT; + else if (theme->pointer_x >= theme->buffer.width - m) + return LOC_RESIZE_TOP_RIGHT; + else + return LOC_RESIZE_TOP; + } + + if (theme->pointer_y >= theme->buffer.height - m) { + if (theme->pointer_x < m) + return LOC_RESIZE_BOTTOM_LEFT; + else if (theme->pointer_x >= theme->buffer.width - m) + return LOC_RESIZE_BOTTOM_RIGHT; + else + return LOC_RESIZE_BOTTOM; + } + + if (theme->pointer_x < m) + return LOC_RESIZE_LEFT; + + if (theme->pointer_x >= theme->buffer.width - m) + return LOC_RESIZE_RIGHT; + + if (theme->pointer_y < theme->control_height) { + b1off = theme->buffer.width - theme->button_margin - + theme->button_size; + b2off = b1off - theme->button_padding - theme->button_size; + b3off = b2off - theme->button_padding - theme->button_size; + + if (theme->pointer_y >= theme->button_margin && + theme->pointer_y < theme->control_height - + theme->button_margin) { + if (theme->pointer_x >= b1off && + theme->pointer_x < b1off + theme->button_size) + return LOC_CLOSE; + if (theme->pointer_x >= b2off && + theme->pointer_x < b2off + theme->button_size) + return LOC_MAXIMIZE; + if (theme->pointer_x >= b3off && + theme->pointer_x < b3off + theme->button_size) + return LOC_MINIMIZE; + } + + return LOC_CONTROL; + } + + return LOC_SOMEWHERE; +} + +static void set_pointer_location(struct wlt_theme *theme, unsigned int loc) +{ + if (theme->pointer_loc == loc) + return; + + theme->pointer_loc = loc; + if (loc == LOC_NOWHERE) { + theme->pointer_x = -1; + theme->pointer_y = -1; + } + + wlt_window_schedule_redraw(theme->wnd); +} + +static void widget_pointer_motion(struct wlt_widget *widget, + unsigned int x, unsigned int y, void *data) +{ + struct wlt_theme *theme = data; + + if (!wlt_rect_contains(&theme->alloc, x, y)) { + set_pointer_location(theme, LOC_NOWHERE); + return; + } else { + theme->pointer_x = x + theme->alloc.x; + theme->pointer_y = y + theme->alloc.y; + set_pointer_location(theme, get_pointer_location(theme)); + } + + switch (theme->pointer_loc) { + case LOC_RESIZE_LEFT: + wlt_window_set_cursor(theme->wnd, WLT_CURSOR_LEFT); + break; + case LOC_RESIZE_RIGHT: + wlt_window_set_cursor(theme->wnd, WLT_CURSOR_RIGHT); + break; + case LOC_RESIZE_TOP: + wlt_window_set_cursor(theme->wnd, WLT_CURSOR_TOP); + break; + case LOC_RESIZE_BOTTOM: + wlt_window_set_cursor(theme->wnd, WLT_CURSOR_BOTTOM); + break; + case LOC_RESIZE_TOP_LEFT: + wlt_window_set_cursor(theme->wnd, WLT_CURSOR_TOP_LEFT); + break; + case LOC_RESIZE_TOP_RIGHT: + wlt_window_set_cursor(theme->wnd, WLT_CURSOR_TOP_RIGHT); + break; + case LOC_RESIZE_BOTTOM_LEFT: + wlt_window_set_cursor(theme->wnd, + WLT_CURSOR_BOTTOM_LEFT); + break; + case LOC_RESIZE_BOTTOM_RIGHT: + wlt_window_set_cursor(theme->wnd, + WLT_CURSOR_BOTTOM_RIGHT); + break; + default: + wlt_window_set_cursor(theme->wnd, WLT_CURSOR_LEFT_PTR); + } +} + +static void widget_pointer_enter(struct wlt_widget *widget, + unsigned int x, unsigned int y, void *data) +{ + struct wlt_theme *theme = data; + + widget_pointer_motion(widget, x, y, theme); +} + +static void widget_pointer_leave(struct wlt_widget *widget, void *data) +{ + struct wlt_theme *theme = data; + + if (theme->pointer_pressed) { + theme->pointer_pressed = false; + wlt_window_schedule_redraw(theme->wnd); + } + + set_pointer_location(theme, LOC_NOWHERE); +} + +static void button_action(struct wlt_theme *theme) +{ + if (theme->pointer_grabbed != theme->pointer_loc) + return; + + switch (theme->pointer_loc) { + case LOC_CLOSE: + wlt_window_close(theme->wnd); + break; + case LOC_MAXIMIZE: + break; + case LOC_MINIMIZE: + break; + } +} + +static void widget_pointer_button(struct wlt_widget *widget, + uint32_t button, uint32_t state, void *data) +{ + struct wlt_theme *theme = data; + + if (button != BTN_LEFT) + return; + + if (state != WL_POINTER_BUTTON_STATE_PRESSED) { + if (theme->pointer_pressed) { + button_action(theme); + theme->pointer_pressed = false; + theme->pointer_grabbed = LOC_NOWHERE; + wlt_window_schedule_redraw(theme->wnd); + } + return; + } + + if (!theme->pointer_pressed) { + theme->pointer_pressed = true; + theme->pointer_grabbed = theme->pointer_loc; + wlt_window_schedule_redraw(theme->wnd); + } + + switch (theme->pointer_loc) { + case LOC_RESIZE_LEFT: + wlt_window_resize(theme->wnd, WL_SHELL_SURFACE_RESIZE_LEFT); + break; + case LOC_RESIZE_RIGHT: + wlt_window_resize(theme->wnd, WL_SHELL_SURFACE_RESIZE_RIGHT); + break; + case LOC_RESIZE_TOP: + wlt_window_resize(theme->wnd, + WL_SHELL_SURFACE_RESIZE_TOP); + break; + case LOC_RESIZE_BOTTOM: + wlt_window_resize(theme->wnd, + WL_SHELL_SURFACE_RESIZE_BOTTOM); + break; + case LOC_RESIZE_TOP_LEFT: + wlt_window_resize(theme->wnd, + WL_SHELL_SURFACE_RESIZE_TOP_LEFT); + break; + case LOC_RESIZE_TOP_RIGHT: + wlt_window_resize(theme->wnd, + WL_SHELL_SURFACE_RESIZE_TOP_RIGHT); + break; + case LOC_RESIZE_BOTTOM_LEFT: + wlt_window_resize(theme->wnd, + WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT); + break; + case LOC_RESIZE_BOTTOM_RIGHT: + wlt_window_resize(theme->wnd, + WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT); + break; + case LOC_CONTROL: + wlt_window_move(theme->wnd); + break; + } +} + +static void widget_destroy(struct wlt_widget *widget, void *data) +{ + struct wlt_theme *theme = data; + + log_debug("destroy theme"); + + free(theme); +} + +int wlt_theme_new(struct wlt_theme **out, struct wlt_window *wnd) +{ + struct wlt_theme *theme; + int ret; + + if (!out || !wnd) + return -EINVAL; + + log_debug("create new theme"); + + theme = malloc(sizeof(*theme)); + if (!theme) + return -ENOMEM; + memset(theme, 0, sizeof(*theme)); + theme->wnd = wnd; + theme->control_height = 25; + theme->frame_width = 5; + theme->resize_margin = 5; + theme->button_size = 15; + theme->button_padding = 3; + theme->button_margin = 5; + theme->pointer_grabbed = LOC_NOWHERE; + set_pointer_location(theme, LOC_NOWHERE); + + ret = wlt_window_create_widget(wnd, &theme->widget, theme); + if (ret) { + log_error("cannot create widget"); + goto err_free; + } + + wlt_widget_set_destroy_cb(theme->widget, widget_destroy); + wlt_widget_set_redraw_cb(theme->widget, widget_redraw); + wlt_widget_set_resize_cb(theme->widget, widget_prepare_resize, + widget_resize); + wlt_widget_set_pointer_cb(theme->widget, widget_pointer_enter, + widget_pointer_leave, widget_pointer_motion, + widget_pointer_button); + *out = theme; + return 0; + +err_free: + free(theme); + return ret; +} + +void wlt_theme_destroy(struct wlt_theme *theme) +{ + if (!theme) + return; + + wlt_widget_destroy(theme->widget); +} diff --git a/src/wlt_theme.h b/src/wlt_theme.h new file mode 100644 index 0000000..bcca4c1 --- /dev/null +++ b/src/wlt_theme.h @@ -0,0 +1,41 @@ +/* + * wlt - Theme Helper + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal theme/decoration drawing helper + */ + +#ifndef WLT_THEME_H +#define WLT_THEME_H + +#include +#include "wlt_toolkit.h" + +struct wlt_theme; + +int wlt_theme_new(struct wlt_theme **out, struct wlt_window *wnd); +void wlt_theme_destroy(struct wlt_theme *theme); + +#endif /* WLT_THEME_H */ diff --git a/src/wlt_toolkit.c b/src/wlt_toolkit.c new file mode 100644 index 0000000..04c0b0c --- /dev/null +++ b/src/wlt_toolkit.c @@ -0,0 +1,1283 @@ +/* + * wlt - Toolkit Helper + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal toolkit helpers + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "eloop.h" +#include "log.h" +#include "shl_dlist.h" +#include "shl_hook.h" +#include "wlt_toolkit.h" + +#define LOG_SUBSYSTEM "wlt_toolkit" + +struct wlt_pool { + struct wl_shm_pool *w_pool; + size_t size; + uint8_t *data; +}; + +enum { + STATE_INIT = 0, + STATE_RUNNING, + STATE_HUP, +}; + +struct wlt_display { + unsigned long ref; + struct ev_eloop *eloop; + struct wl_display *dp; + struct ev_fd *dp_fd; + struct wl_global_listener *dp_listener; + struct shl_hook *listeners; + unsigned int state; + + struct shl_dlist window_list; + + struct wl_compositor *w_comp; + struct wl_seat *w_seat; + struct wl_shell *w_shell; + struct wl_shm *w_shm; + + uint32_t last_serial; + uint32_t pointer_enter_serial; + struct wl_pointer *w_pointer; + struct wlt_window *pointer_focus; + + uint32_t cursor_serial; + unsigned int current_cursor; + struct wl_surface *w_cursor_surface; + struct wl_cursor_theme *cursor_theme; + struct wl_cursor *cursors[WLT_CURSOR_NUM]; +}; + +struct wlt_window { + unsigned long ref; + struct shl_dlist list; + void *data; + wlt_window_close_cb close_cb; + bool close_pending; + struct wlt_display *disp; + struct wlt_pool *pool; + + struct wl_surface *w_surface; + struct wl_shell_surface *w_shell_surface; + struct wl_buffer *w_buffer; + + bool buffer_attached; + bool skip_damage; + bool need_resize; + bool need_redraw; + bool need_frame; + bool idle_pending; + unsigned int new_width; + unsigned int new_height; + unsigned int resize_edges; + struct wlt_shm_buffer buffer; + struct wl_callback *w_frame; + + struct shl_dlist widget_list; +}; + +struct wlt_widget { + struct shl_dlist list; + struct wlt_window *wnd; + void *data; + wlt_widget_redraw_cb redraw_cb; + wlt_widget_destroy_cb destroy_cb; + wlt_widget_prepare_resize_cb prepare_resize_cb; + wlt_widget_resize_cb resize_cb; + wlt_widget_pointer_enter_cb pointer_enter_cb; + wlt_widget_pointer_leave_cb pointer_leave_cb; + wlt_widget_pointer_motion_cb pointer_motion_cb; + wlt_widget_pointer_button_cb pointer_button_cb; +}; + +static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp, + size_t size) +{ + static const char template[] = "/wlterm-shared-XXXXXX"; + struct wlt_pool *pool; + int fd, ret; + const char *path; + char *name; + + path = getenv("XDG_RUNTIME_DIR"); + if (!path) { + log_error("XDG_RUNTIME_DIR not set"); + return -EFAULT; + } + + pool = malloc(sizeof(*pool)); + if (!pool) + return -ENOMEM; + memset(pool, 0, sizeof(*pool)); + pool->size = size; + + name = malloc(strlen(path) + sizeof(template)); + if (!name) { + ret = -ENOMEM; + goto err_free; + } + strcpy(name, path); + strcat(name, template); + + fd = mkostemp(name, O_CLOEXEC); + if (fd < 0) { + log_error("cannot create temporary file %s via mkostemp() (%d): %m", + name, errno); + ret = -EFAULT; + free(name); + goto err_free; + } + + ret = unlink(name); + if (ret) + log_warning("cannot unlink temporary file %s (%d): %m", + name, errno); + free(name); + + ret = ftruncate(fd, size); + if (ret) { + log_error("cannot truncate temporary file to length %lu (%d): %m", + size, errno); + ret = -EFAULT; + goto err_close; + } + + pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, + fd, 0); + if (pool->data == MAP_FAILED) { + log_error("cannot mmap temporary file (%d): %m", errno); + ret = -EFAULT; + goto err_close; + } + + pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size); + if (!pool->w_pool) { + log_error("cannot create wayland shm pool object"); + ret = -EFAULT; + goto err_map; + } + + close(fd); + *out = pool; + return 0; + +err_map: + munmap(pool->data, size); +err_close: + close(fd); +err_free: + free(pool); + return ret; +} + +static void wlt_pool_free(struct wlt_pool *pool) +{ + munmap(pool->data, pool->size); + wl_shm_pool_destroy(pool->w_pool); + free(pool); +} + +/* + * These cursor-names are based on: + * https://bugs.kde.org/attachment.cgi?id=67313 + */ + +static char *(*cursors[])[] = { + [WLT_CURSOR_NONE] = &(char*[]) { + NULL, + }, + [WLT_CURSOR_TOP] = &(char*[]) { + "top_side", + "n-resize", + NULL, + }, + [WLT_CURSOR_BOTTOM] = &(char*[]) { + "bottom_side", + "s-resize", + NULL, + }, + [WLT_CURSOR_LEFT] = &(char*[]) { + "left_side", + "w-resize", + NULL, + }, + [WLT_CURSOR_RIGHT] = &(char*[]) { + "right_side", + "e-resize", + NULL, + }, + [WLT_CURSOR_TOP_LEFT] = &(char*[]) { + "top_left_corner", + "nw-resize", + NULL, + }, + [WLT_CURSOR_TOP_RIGHT] = &(char*[]) { + "top_right_corner", + "ne-resize", + NULL, + }, + [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) { + "bottom_left_corner", + "sw-resize", + NULL, + }, + [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) { + "bottom_right_corner", + "se-resize", + NULL, + }, + [WLT_CURSOR_DRAGGING] = &(char*[]) { + "grabbing", + "closedhand", + "208530c400c041818281048008011002", + NULL, + }, + [WLT_CURSOR_LEFT_PTR] = &(char*[]) { + "left_ptr", + "default", + "top_left_arrow", + "left-arrow", + NULL, + }, + [WLT_CURSOR_IBEAM] = &(char*[]) { + "xterm", + "ibeam", + "text", + NULL, + }, +}; + +static int load_cursors(struct wlt_display *disp) +{ + unsigned int i, j; + size_t size = sizeof(cursors) / sizeof(*cursors); + struct wl_cursor *cursor; + + disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp); + + disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm); + if (!disp->cursor_theme) { + log_warning("cannot load curdor theme"); + return -EFAULT; + } + + for (i = 0; i < WLT_CURSOR_NUM; ++i) { + cursor = NULL; + if (i < size && cursors[i]) { + for (j = 0; (*cursors[i])[j]; ++j) { + cursor = wl_cursor_theme_get_cursor( + disp->cursor_theme, + (*cursors[i])[j]); + if (cursor) + break; + } + } + + if (!cursor && i != WLT_CURSOR_NONE) + log_warning("cannot load cursor for ID %d", i); + + disp->cursors[i] = cursor; + } + + return 0; +} + +static void unload_cursors(struct wlt_display *disp) +{ + if (disp->cursor_theme) + wl_cursor_theme_destroy(disp->cursor_theme); + if (disp->w_cursor_surface) + wl_surface_destroy(disp->w_cursor_surface); +} + +static void set_cursor(struct wlt_display *disp, unsigned int cursor) +{ + bool force = false; + struct wl_cursor *cur; + struct wl_cursor_image *image; + struct wl_buffer *buffer; + + if (cursor >= WLT_CURSOR_NUM) + cursor = WLT_CURSOR_LEFT_PTR; + + if (disp->pointer_enter_serial > disp->cursor_serial) + force = true; + + if (!force && cursor == disp->current_cursor) + return; + + disp->current_cursor = cursor; + disp->cursor_serial = disp->pointer_enter_serial; + + cur = disp->cursors[cursor]; + if (!cur) { + wl_pointer_set_cursor(disp->w_pointer, + disp->pointer_enter_serial, + NULL, 0, 0); + return; + } + + image = cur->images[0]; + buffer = wl_cursor_image_get_buffer(image); + if (!buffer) { + log_error("cannot load buffer for cursor image"); + return; + } + + wl_pointer_set_cursor(disp->w_pointer, + disp->pointer_enter_serial, + disp->w_cursor_surface, + image->hotspot_x, image->hotspot_y); + wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0); + wl_surface_damage(disp->w_cursor_surface, 0, 0, + image->width, image->height); +} + +static int dp_mask_update(uint32_t mask, void *data) +{ + struct wlt_display *disp = data; + int ret; + int mode; + + if (!disp->dp_fd) + return 0; + + mode = 0; + if (mask & WL_DISPLAY_READABLE) + mode |= EV_READABLE; + if (mask & WL_DISPLAY_WRITABLE) + mode |= EV_WRITEABLE; + + ret = ev_fd_update(disp->dp_fd, mode); + if (ret) + log_warning("cannot update wayland-fd event-polling modes (%d)", + ret); + + return 0; +} + +static void dp_event(struct ev_fd *fd, int mask, void *data) +{ + struct wlt_display *disp = data; + int mode; + + if (mask & (EV_HUP | EV_ERR)) { + log_warning("HUP/ERR on wayland socket"); + disp->state = STATE_HUP; + shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP); + ev_eloop_rm_fd(disp->dp_fd); + disp->dp_fd = NULL; + return; + } + + mode = 0; + if (mask & EV_READABLE) + mode |= WL_DISPLAY_READABLE; + if (mask & EV_WRITEABLE) + mode |= WL_DISPLAY_WRITABLE; + + if (mode) + wl_display_iterate(disp->dp, mode); +} + +static void pointer_enter(void *data, struct wl_pointer *w_pointer, + uint32_t serial, struct wl_surface *w_surface, + wl_fixed_t x, wl_fixed_t y) +{ + struct wlt_display *disp = data; + struct shl_dlist *iter; + struct wlt_window *wnd; + struct wlt_widget *widget; + + if (!w_surface) + return; + + shl_dlist_for_each(iter, &disp->window_list) { + wnd = shl_dlist_entry(iter, struct wlt_window, list); + if (wnd->w_surface == w_surface) + break; + } + + if (iter == &disp->window_list) { + log_debug("unknown surface"); + return; + } + + disp->pointer_enter_serial = serial; + disp->last_serial = serial; + disp->pointer_focus = wnd; + shl_dlist_for_each(iter, &wnd->widget_list) { + widget = shl_dlist_entry(iter, struct wlt_widget, list); + if (widget->pointer_enter_cb) + widget->pointer_enter_cb(widget, x >> 8, y >> 8, + widget->data); + } +} + +static void pointer_leave(void *data, struct wl_pointer *w_pointer, + uint32_t serial, struct wl_surface *w_surface) +{ + struct wlt_display *disp = data; + struct shl_dlist *iter; + struct wlt_window *wnd; + struct wlt_widget *widget; + + wnd = disp->pointer_focus; + disp->pointer_focus = NULL; + disp->last_serial = serial; + + if (!wnd) + return; + + shl_dlist_for_each(iter, &wnd->widget_list) { + widget = shl_dlist_entry(iter, struct wlt_widget, list); + if (widget->pointer_leave_cb) + widget->pointer_leave_cb(widget, widget->data); + } +} + +static void pointer_motion(void *data, struct wl_pointer *w_pointer, + uint32_t time, wl_fixed_t x, wl_fixed_t y) +{ + struct wlt_display *disp = data; + struct shl_dlist *iter; + struct wlt_window *wnd; + struct wlt_widget *widget; + + wnd = disp->pointer_focus; + if (!wnd) + return; + + shl_dlist_for_each(iter, &wnd->widget_list) { + widget = shl_dlist_entry(iter, struct wlt_widget, list); + if (widget->pointer_motion_cb) + widget->pointer_motion_cb(widget, x >> 8, y >> 8, + widget->data); + } +} + +static void pointer_button(void *data, struct wl_pointer *w_pointer, + uint32_t serial, uint32_t time, uint32_t button, + uint32_t state) +{ + struct wlt_display *disp = data; + struct shl_dlist *iter; + struct wlt_window *wnd; + struct wlt_widget *widget; + + wnd = disp->pointer_focus; + disp->last_serial = serial; + + if (!wnd) + return; + + shl_dlist_for_each(iter, &wnd->widget_list) { + widget = shl_dlist_entry(iter, struct wlt_widget, list); + if (widget->pointer_button_cb) + widget->pointer_button_cb(widget, button, state, + widget->data); + } +} + +static void pointer_axis(void *data, struct wl_pointer *w_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) +{ +} + +static const struct wl_pointer_listener pointer_listener = { + .enter = pointer_enter, + .leave = pointer_leave, + .motion = pointer_motion, + .button = pointer_button, + .axis = pointer_axis, +}; + +static void check_ready(struct wlt_display *disp) +{ + if (disp->state > STATE_INIT) + return; + + if (disp->w_comp && + disp->w_seat && + disp->w_shell && + disp->w_shm && + disp->w_pointer) { + log_debug("wayland display initialized"); + load_cursors(disp); + + disp->state = STATE_RUNNING; + shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY); + } +} + +static void seat_capabilities(void *data, struct wl_seat *seat, + enum wl_seat_capability caps) +{ + struct wlt_display *disp = data; + + if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) { + disp->w_pointer = wl_seat_get_pointer(seat); + wl_pointer_add_listener(disp->w_pointer, &pointer_listener, + disp); + } + + check_ready(disp); +} + +static const struct wl_seat_listener seat_listener = { + .capabilities = seat_capabilities, +}; + +static void dp_global(struct wl_display *dp, uint32_t id, const char *iface, + uint32_t version, void *data) +{ + struct wlt_display *disp = data; + + if (!strcmp(iface, "wl_display")) { + log_debug("new wl_display global"); + return; + } else if (!strcmp(iface, "wl_compositor")) { + if (disp->w_comp) { + log_error("global wl_compositor advertised twice"); + return; + } + disp->w_comp = wl_display_bind(disp->dp, + id, + &wl_compositor_interface); + if (!disp->w_comp) { + log_error("cannot bind wl_compositor object"); + return; + } + } else if (!strcmp(iface, "wl_seat")) { + if (disp->w_seat) { + log_error("global wl_seat advertised twice"); + return; + } + disp->w_seat = wl_display_bind(disp->dp, + id, + &wl_seat_interface); + if (!disp->w_seat) { + log_error("cannot bind wl_seat object"); + return; + } + + wl_seat_add_listener(disp->w_seat, &seat_listener, disp); + } else if (!strcmp(iface, "wl_shell")) { + if (disp->w_shell) { + log_error("global wl_shell advertised twice"); + return; + } + disp->w_shell = wl_display_bind(disp->dp, + id, + &wl_shell_interface); + if (!disp->w_shell) { + log_error("cannot bind wl_shell object"); + return; + } + } else if (!strcmp(iface, "wl_shm")) { + if (disp->w_shm) { + log_error("global wl_shm advertised twice"); + return; + } + disp->w_shm = wl_display_bind(disp->dp, + id, + &wl_shm_interface); + if (!disp->w_shm) { + log_error("cannot bind wl_shm object"); + return; + } + } else { + log_debug("ignoring new unknown global %s", iface); + return; + } + + log_debug("new global %s", iface); + + check_ready(disp); +} + +int wlt_display_new(struct wlt_display **out, + struct ev_eloop *eloop) +{ + struct wlt_display *disp; + int ret; + + if (!out || !eloop) + return -EINVAL; + + log_debug("creating new wlt-display"); + + disp = malloc(sizeof(*disp)); + if (!disp) + return -ENOMEM; + memset(disp, 0, sizeof(*disp)); + disp->ref = 1; + disp->eloop = eloop; + shl_dlist_init(&disp->window_list); + + ret = shl_hook_new(&disp->listeners); + if (ret) { + log_error("cannot create listeners hook"); + goto err_free; + } + + disp->dp = wl_display_connect(NULL); + if (!disp->dp) { + log_error("cannot connect to wayland socket (%d): %m", errno); + ret = -EFAULT; + goto err_listener; + } + + ret = ev_eloop_new_fd(eloop, + &disp->dp_fd, + wl_display_get_fd(disp->dp, + dp_mask_update, + disp), + EV_READABLE, + dp_event, + disp); + if (ret) { + log_error("cannot create event-fd for wayland display (%d)", + ret); + goto err_dp; + } + + disp->dp_listener = wl_display_add_global_listener(disp->dp, + dp_global, + disp); + if (!disp->dp_listener) { + log_error("cannot add wayland global listener (%d)"); + goto err_dp_fd; + } + + log_debug("wlt-display waiting for globals..."); + + ev_eloop_ref(disp->eloop); + *out = disp; + return 0; + +err_dp_fd: + ev_eloop_rm_fd(disp->dp_fd); +err_dp: + wl_display_disconnect(disp->dp); +err_listener: + shl_hook_free(disp->listeners); +err_free: + free(disp); + return ret; +} + +void wlt_display_ref(struct wlt_display *disp) +{ + if (!disp || !disp->ref) + return; + + ++disp->ref; +} + +void wlt_display_unref(struct wlt_display *disp) +{ + if (!disp || !disp->ref || --disp->ref) + return; + + unload_cursors(disp); + wl_display_remove_global_listener(disp->dp, disp->dp_listener); + ev_eloop_rm_fd(disp->dp_fd); + wl_display_flush(disp->dp); + wl_display_disconnect(disp->dp); + shl_hook_free(disp->listeners); + ev_eloop_unref(disp->eloop); + free(disp); +} + +int wlt_display_register_cb(struct wlt_display *disp, + wlt_display_cb cb, void *data) +{ + if (!disp) + return -EINVAL; + + return shl_hook_add_cast(disp->listeners, cb, data); +} + +void wlt_display_unregister_cb(struct wlt_display *disp, + wlt_display_cb cb, void *data) +{ + if (!disp) + return; + + shl_hook_rm_cast(disp->listeners, cb, data); +} + +static void shell_surface_ping(void *data, struct wl_shell_surface *s, + uint32_t serial) +{ + wl_shell_surface_pong(s, serial); +} + +static void wlt_window_do_redraw(struct wlt_window *wnd, + unsigned int oldw, + unsigned int oldh) +{ + struct shl_dlist *iter; + struct wlt_widget *widget; + unsigned int x, y; + struct wlt_rect alloc; + + alloc.x = 0; + alloc.y = 0; + alloc.width = wnd->buffer.width; + alloc.height = wnd->buffer.height; + shl_dlist_for_each(iter, &wnd->widget_list) { + widget = shl_dlist_entry(iter, struct wlt_widget, list); + if (widget->resize_cb) + widget->resize_cb(widget, &alloc, widget->data); + } + + memset(wnd->buffer.data, 0, + wnd->buffer.stride * wnd->buffer.height); + + wnd->skip_damage = true; + shl_dlist_for_each(iter, &wnd->widget_list) { + widget = shl_dlist_entry(iter, struct wlt_widget, list); + if (widget->redraw_cb) + widget->redraw_cb(widget, widget->data); + } + wnd->skip_damage = false; + + if (!wnd->buffer_attached) { + wnd->buffer_attached = true; + if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT) + x = (int)oldw - wnd->buffer.width; + else + x = 0; + if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP) + y = (int)oldh - wnd->buffer.height; + else + y = 0; + wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y); + wnd->resize_edges = 0; + } + + wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width, + wnd->buffer.height); +} + +static int resize_window(struct wlt_window *wnd, unsigned int width, + unsigned int height) +{ + struct shl_dlist *iter; + struct wlt_widget *widget; + struct wl_buffer *old_buffer = NULL; + struct wlt_pool *old_pool = NULL; + size_t nsize; + int ret; + unsigned int oldw, oldh; + + if (!wnd || !width || !height) + return -EINVAL; + + shl_dlist_for_each(iter, &wnd->widget_list) { + widget = shl_dlist_entry(iter, struct wlt_widget, list); + if (widget->prepare_resize_cb) + widget->prepare_resize_cb(widget, &width, &height, + widget->data); + } + + if (width == wnd->buffer.width && + height == wnd->buffer.height) + return 0; + + oldw = wnd->buffer.width; + oldh = wnd->buffer.height; + + nsize = width * height * 4; + if (wnd->pool && wnd->pool->size >= nsize) { + old_buffer = wnd->w_buffer; + wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool, + 0, width, height, + width * 4, + WL_SHM_FORMAT_ARGB8888); + if (!wnd->w_buffer) { + log_error("cannot create wayland shm buffer"); + wnd->w_buffer = old_buffer; + return -ENOMEM; + } + } else { + old_pool = wnd->pool; + ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize); + if (ret) { + log_error("cannot create memory pool"); + wnd->pool = old_pool; + return ret; + } + + old_buffer = wnd->w_buffer; + wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool, + 0, width, height, + width * 4, + WL_SHM_FORMAT_ARGB8888); + if (!wnd->w_buffer) { + log_error("cannot create wayland shm buffer"); + wnd->w_buffer = old_buffer; + wlt_pool_free(wnd->pool); + wnd->pool = old_pool; + return -ENOMEM; + } + } + + wnd->buffer.data = wnd->pool->data; + wnd->buffer.width = width; + wnd->buffer.height = height; + wnd->buffer.stride = width * 4; + wnd->buffer_attached = false; + + wlt_window_do_redraw(wnd, oldw, oldh); + + if (old_buffer) + wl_buffer_destroy(old_buffer); + if (old_pool) + wlt_pool_free(old_pool); + + return 0; +} + +static void idle_frame(struct ev_eloop *eloop, void *unused, void *data); + +static void do_frame(struct wlt_window *wnd) +{ + wnd->idle_pending = false; + ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd); + + if (wnd->need_resize) { + wnd->need_resize = false; + wnd->need_redraw = false; + resize_window(wnd, wnd->new_width, wnd->new_height); + } + + if (wnd->need_redraw) { + wnd->need_redraw = false; + wlt_window_do_redraw(wnd, wnd->buffer.width, + wnd->buffer.height); + } +} + +static void frame_callback(void *data, struct wl_callback *w_callback, + uint32_t time) +{ + struct wlt_window *wnd = data; + + wl_callback_destroy(w_callback); + wnd->w_frame = NULL; + wnd->need_frame = false; + + do_frame(wnd); +} + +static void idle_frame(struct ev_eloop *eloop, void *unused, void *data) +{ + struct wlt_window *wnd = data; + + wnd->need_frame = true; + do_frame(wnd); +} + +static const struct wl_callback_listener frame_callback_listener = { + .done = frame_callback, +}; + +static void schedule_frame(struct wlt_window *wnd) +{ + if (!wnd || wnd->w_frame) + return; + + if (!wnd->need_frame && !wnd->idle_pending) { + wnd->idle_pending = true; + ev_eloop_register_idle_cb(wnd->disp->eloop, + idle_frame, wnd); + wnd->w_frame = wl_surface_frame(wnd->w_surface); + wl_callback_add_listener(wnd->w_frame, + &frame_callback_listener, wnd); + } +} + +static void shell_surface_configure(void *data, struct wl_shell_surface *s, + uint32_t edges, int32_t width, + int32_t height) +{ + struct wlt_window *wnd = data; + + if (width <= 0) + width = 1; + if (height <= 0) + height = 1; + + wnd->resize_edges = edges; + wlt_window_set_size(wnd, width, height); +} + +static void shell_surface_popup_done(void *data, struct wl_shell_surface *s) +{ +} + +static const struct wl_shell_surface_listener shell_surface_listener = { + .ping = shell_surface_ping, + .configure = shell_surface_configure, + .popup_done = shell_surface_popup_done, +}; + +static void close_window(struct ev_eloop *eloop, void *unused, void *data) +{ + struct wlt_window *wnd = data; + + ev_eloop_unregister_idle_cb(eloop, close_window, wnd); + wnd->close_pending = false; + + if (wnd->close_cb) + wnd->close_cb(wnd, wnd->data); +} + +int wlt_display_create_window(struct wlt_display *disp, + struct wlt_window **out, + unsigned int width, + unsigned int height, + void *data) +{ + struct wlt_window *wnd; + int ret; + + if (!disp || !out || !width || !height) + return -EINVAL; + + if (disp->state != STATE_RUNNING) { + log_error("cannot create window, display is not running but in state %u", + disp->state); + return -EBUSY; + } + + wnd = malloc(sizeof(*wnd)); + if (!wnd) + return -ENOMEM; + memset(wnd, 0, sizeof(*wnd)); + wnd->ref = 1; + wnd->disp = disp; + wnd->data = data; + shl_dlist_init(&wnd->widget_list); + + wnd->w_surface = wl_compositor_create_surface(disp->w_comp); + if (!wnd->w_surface) { + log_error("cannot create wayland surface"); + ret = -EFAULT; + goto err_free; + } + + wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell, + wnd->w_surface); + if (!wnd->w_shell_surface) { + log_error("cannot retrieve shell-surface for wayland surface"); + ret = -EFAULT; + goto err_surface; + } + + wl_shell_surface_add_listener(wnd->w_shell_surface, + &shell_surface_listener, wnd); + wl_shell_surface_set_toplevel(wnd->w_shell_surface); + + ret = resize_window(wnd, width, height); + if (ret) + goto err_shell_surface; + + wlt_display_ref(disp); + shl_dlist_link(&disp->window_list, &wnd->list); + *out = wnd; + return 0; + +err_shell_surface: + wl_shell_surface_destroy(wnd->w_shell_surface); +err_surface: + wl_surface_destroy(wnd->w_surface); +err_free: + free(wnd); + return ret; +} + +void wlt_window_ref(struct wlt_window *wnd) +{ + if (!wnd || !wnd->ref) + return; + + ++wnd->ref; +} + +void wlt_window_unref(struct wlt_window *wnd) +{ + struct wlt_widget *widget; + + if (!wnd || !wnd->ref || --wnd->ref) + return; + + while (!shl_dlist_empty(&wnd->widget_list)) { + widget = shl_dlist_entry(wnd->widget_list.next, + struct wlt_widget, list); + wlt_widget_destroy(widget); + } + + if (wnd->close_pending) + ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window, + wnd); + if (wnd->idle_pending) + ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd); + shl_dlist_unlink(&wnd->list); + if (wnd->w_frame) + wl_callback_destroy(wnd->w_frame); + if (wnd->w_buffer) + wl_buffer_destroy(wnd->w_buffer); + if (wnd->pool) + wlt_pool_free(wnd->pool); + wl_shell_surface_destroy(wnd->w_shell_surface); + wl_surface_destroy(wnd->w_surface); + wlt_display_unref(wnd->disp); + free(wnd); +} + +int wlt_window_create_widget(struct wlt_window *wnd, + struct wlt_widget **out, + void *data) +{ + struct wlt_widget *widget; + + if (!wnd || !out) + return -EINVAL; + + widget = malloc(sizeof(*widget)); + if (!widget) + return -ENOMEM; + memset(widget, 0, sizeof(*widget)); + widget->wnd = wnd; + widget->data = data; + + wlt_window_schedule_redraw(wnd); + shl_dlist_link_tail(&wnd->widget_list, &widget->list); + *out = widget; + return 0; +} + +void wlt_window_schedule_redraw(struct wlt_window *wnd) +{ + if (!wnd) + return; + + wnd->need_redraw = true; + schedule_frame(wnd); +} + +void wlt_window_damage(struct wlt_window *wnd, + struct wlt_rect *damage) +{ + if (!wnd || !damage || wnd->skip_damage) + return; + + wl_surface_damage(wnd->w_surface, damage->x, damage->y, + damage->width, damage->height); +} + +void wlt_window_get_buffer(struct wlt_window *wnd, + const struct wlt_rect *alloc, + struct wlt_shm_buffer *buf) +{ + struct wlt_shm_buffer *rbuf; + + if (!wnd || !buf) + return; + + rbuf = &wnd->buffer; + + if (!alloc) { + memcpy(buf, rbuf, sizeof(*buf)); + return; + } + + if (alloc->x >= rbuf->width || + alloc->y >= rbuf->height) { + memset(buf, 0, sizeof(*buf)); + return; + } + + /* set width */ + if (alloc->x + alloc->width > rbuf->width) + buf->width = rbuf->width - alloc->x; + else + buf->width = alloc->width; + + /* set height */ + if (alloc->y + alloc->height > rbuf->height) + buf->height = rbuf->height - alloc->y; + else + buf->height = alloc->height; + + /* set stride */ + buf->stride = rbuf->stride; + + /* set data */ + buf->data = rbuf->data; + buf->data += alloc->y * rbuf->stride; + buf->data += alloc->x * 4; +} + +void wlt_window_move(struct wlt_window *wnd) +{ + if (!wnd) + return; + + wl_shell_surface_move(wnd->w_shell_surface, + wnd->disp->w_seat, + wnd->disp->last_serial); +} + +void wlt_window_resize(struct wlt_window *wnd, uint32_t edges) +{ + if (!wnd) + return; + + wl_shell_surface_resize(wnd->w_shell_surface, + wnd->disp->w_seat, + wnd->disp->last_serial, + edges); +} + +int wlt_window_set_size(struct wlt_window *wnd, + unsigned int width, unsigned int height) +{ + if (!wnd || !width || !height) + return -EINVAL; + + wnd->new_width = width; + wnd->new_height = height; + wnd->need_resize = true; + schedule_frame(wnd); + + return 0; +} + +void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor) +{ + if (!wnd) + return; + + set_cursor(wnd->disp, cursor); +} + +void wlt_window_set_close_cb(struct wlt_window *wnd, + wlt_window_close_cb cb) +{ + if (!wnd) + return; + + wnd->close_cb = cb; +} + +void wlt_window_close(struct wlt_window *wnd) +{ + if (!wnd) + return; + + wnd->close_pending = true; + ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd); +} + +void wlt_widget_destroy(struct wlt_widget *widget) +{ + if (!widget) + return; + + if (widget->destroy_cb) + widget->destroy_cb(widget, widget->data); + shl_dlist_unlink(&widget->list); + free(widget); +} + +struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget) +{ + if (!widget) + return NULL; + + return widget->wnd; +} + +void wlt_widget_set_redraw_cb(struct wlt_widget *widget, + wlt_widget_redraw_cb cb) +{ + if (!widget) + return; + + widget->redraw_cb = cb; +} + +void wlt_widget_set_destroy_cb(struct wlt_widget *widget, + wlt_widget_destroy_cb cb) +{ + if (!widget) + return; + + widget->destroy_cb = cb; +} + +void wlt_widget_set_resize_cb(struct wlt_widget *widget, + wlt_widget_prepare_resize_cb prepare_cb, + wlt_widget_resize_cb cb) +{ + if (!widget) + return; + + widget->prepare_resize_cb = prepare_cb; + widget->resize_cb = cb; +} + +void wlt_widget_set_pointer_cb(struct wlt_widget *widget, + wlt_widget_pointer_enter_cb enter_cb, + wlt_widget_pointer_leave_cb leave_cb, + wlt_widget_pointer_motion_cb motion_cb, + wlt_widget_pointer_button_cb button_cb) +{ + if (!widget) + return; + + widget->pointer_enter_cb = enter_cb; + widget->pointer_leave_cb = leave_cb; + widget->pointer_motion_cb = motion_cb; + widget->pointer_button_cb = button_cb; +} diff --git a/src/wlt_toolkit.h b/src/wlt_toolkit.h new file mode 100644 index 0000000..ddaa8b0 --- /dev/null +++ b/src/wlt_toolkit.h @@ -0,0 +1,168 @@ +/* + * wlt - Toolkit Helper + * + * Copyright (c) 2012 David Herrmann + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Wayland Terminal toolkit helpers + */ + +#ifndef WLT_TOOLKIT_H +#define WLT_TOOLKIT_H + +#include +#include +#include "eloop.h" + +struct wlt_display; +struct wlt_window; +struct wlt_widget; + +enum wlt_display_event { + WLT_DISPLAY_HUP, + WLT_DISPLAY_READY, +}; + +struct wlt_rect { + unsigned int x; + unsigned int y; + unsigned int width; + unsigned int height; +}; + +struct wlt_shm_buffer { + uint8_t *data; + unsigned int width; + unsigned int height; + unsigned int stride; +}; + +enum cursor_type { + WLT_CURSOR_NONE, + WLT_CURSOR_TOP, + WLT_CURSOR_BOTTOM, + WLT_CURSOR_LEFT, + WLT_CURSOR_RIGHT, + WLT_CURSOR_TOP_LEFT, + WLT_CURSOR_TOP_RIGHT, + WLT_CURSOR_BOTTOM_LEFT, + WLT_CURSOR_BOTTOM_RIGHT, + WLT_CURSOR_DRAGGING, + WLT_CURSOR_LEFT_PTR, + WLT_CURSOR_IBEAM, + WLT_CURSOR_NUM, +}; + +typedef void (*wlt_display_cb) (struct wlt_display *disp, + unsigned int event, + void *data); +typedef void (*wlt_window_close_cb) (struct wlt_window *wnd, void *data); +typedef void (*wlt_widget_redraw_cb) (struct wlt_widget *widget, + void *data); +typedef void (*wlt_widget_destroy_cb) (struct wlt_widget *widget, + void *data); +typedef void (*wlt_widget_prepare_resize_cb) (struct wlt_widget *widget, + unsigned int *width, + unsigned int *height, + void *data); +typedef void (*wlt_widget_resize_cb) (struct wlt_widget *widget, + struct wlt_rect *allocation, + void *data); +typedef void (*wlt_widget_pointer_enter_cb) (struct wlt_widget *widget, + unsigned int x, + unsigned int y, + void *data); +typedef void (*wlt_widget_pointer_leave_cb) (struct wlt_widget *widget, + void *data); +typedef void (*wlt_widget_pointer_motion_cb) (struct wlt_widget *widget, + unsigned int x, + unsigned int y, + void *data); +typedef void (*wlt_widget_pointer_button_cb) (struct wlt_widget *widget, + uint32_t button, + uint32_t state, + void *data); + +int wlt_display_new(struct wlt_display **out, + struct ev_eloop *eloop); +void wlt_display_ref(struct wlt_display *disp); +void wlt_display_unref(struct wlt_display *disp); + +int wlt_display_create_window(struct wlt_display *disp, + struct wlt_window **out, + unsigned int width, + unsigned int height, + void *data); +int wlt_display_register_cb(struct wlt_display *disp, + wlt_display_cb cb, void *data); +void wlt_display_unregister_cb(struct wlt_display *disp, + wlt_display_cb cb, void *data); + +void wlt_window_ref(struct wlt_window *wnd); +void wlt_window_unref(struct wlt_window *wnd); + +int wlt_window_create_widget(struct wlt_window *wnd, + struct wlt_widget **out, + void *data); +void wlt_window_schedule_redraw(struct wlt_window *wnd); +void wlt_window_damage(struct wlt_window *wnd, + struct wlt_rect *damage); +void wlt_window_get_buffer(struct wlt_window *wnd, + const struct wlt_rect *alloc, + struct wlt_shm_buffer *buf); +void wlt_window_move(struct wlt_window *wnd); +void wlt_window_resize(struct wlt_window *wnd, uint32_t edges); +int wlt_window_set_size(struct wlt_window *wnd, + unsigned int width, unsigned int height); +void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor); +void wlt_window_set_close_cb(struct wlt_window *wnd, + wlt_window_close_cb cb); +void wlt_window_close(struct wlt_window *wnd); + +void wlt_widget_destroy(struct wlt_widget *widget); +struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget); +void wlt_widget_set_redraw_cb(struct wlt_widget *widget, + wlt_widget_redraw_cb cb); +void wlt_widget_set_destroy_cb(struct wlt_widget *widget, + wlt_widget_destroy_cb cb); +void wlt_widget_set_resize_cb(struct wlt_widget *widget, + wlt_widget_prepare_resize_cb prepare_cb, + wlt_widget_resize_cb cb); +void wlt_widget_set_pointer_cb(struct wlt_widget *widget, + wlt_widget_pointer_enter_cb enter_cb, + wlt_widget_pointer_leave_cb leave_cb, + wlt_widget_pointer_motion_cb motion_cb, + wlt_widget_pointer_button_cb button_cb); + +static inline bool wlt_rect_contains(struct wlt_rect *rect, + unsigned int x, + unsigned int y) +{ + if (x >= rect->x + rect->width) + return false; + if (y >= rect->y + rect->height) + return false; + return true; +} + +#endif /* WLT_TOOLKIT_H */