summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Herrmann <dh.herrmann@gmail.com>2013-10-22 13:40:00 +0200
committerDavid Herrmann <dh.herrmann@gmail.com>2013-10-22 13:40:00 +0200
commit2503645dc5fdfe1ee724e3110ca4af489f8da6ae (patch)
treec58afffb231c40b1199c2c21880027f30338a69d
Initial import
Import the wlterm gtk-rewrite. It's meant to replace the old wlterm by using gtk+ instead of hardcoding wl-calls. There's still stuff missing, but the main terminal is already working quite well. Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
-rw-r--r--.gitignore1
-rw-r--r--COPYING40
-rw-r--r--LICENSE_htable510
-rw-r--r--Makefile6
-rw-r--r--src/shl_htable.c428
-rw-r--r--src/shl_htable.h222
-rw-r--r--src/shl_pty.c674
-rw-r--r--src/shl_pty.h58
-rw-r--r--src/wlt_font.c423
-rw-r--r--src/wlt_render.c95
-rw-r--r--src/wlterm.c527
-rw-r--r--src/wlterm.h93
12 files changed, 3077 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3e9203a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+wlterm
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..0543b5f
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,40 @@
+= Authors =
+
+This software was written by:
+ David Herrmann <dh.herrmann@googlemail.com>
+
+= Copyright Notice =
+
+This software is licensed under the terms of the MIT license. Please see each
+source file for the related copyright notice and license.
+
+If the file does not contain a copright notice, the following license shall
+apply:
+
+ Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
+
+ 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.
+
+== Third-Party Source ==
+
+The hash-table implementation in src/shl_htable.c is licensed as LGPL-2.1, see
+LICENSE_htable for more. Copyright is:
+
+ Author: Rusty Russel <rusty@rustcorp.com.au>
diff --git a/LICENSE_htable b/LICENSE_htable
new file mode 100644
index 0000000..2d2d780
--- /dev/null
+++ b/LICENSE_htable
@@ -0,0 +1,510 @@
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL. It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it. You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations
+below.
+
+ When we speak of free software, we are referring to freedom of use,
+not price. Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+ To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights. These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ To protect each distributor, we want to make it very clear that
+there is no warranty for the free library. Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+ Finally, software patents pose a constant threat to the existence of
+any free program. We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder. Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+ Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License. This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License. We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+ When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library. The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom. The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+ We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License. It also provides other free software developers Less
+of an advantage over competing non-free programs. These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries. However, the Lesser license provides advantages in certain
+special circumstances.
+
+ For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it
+becomes a de-facto standard. To achieve this, non-free programs must
+be allowed to use the library. A more frequent case is that a free
+library does the same job as widely used non-free libraries. In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+ In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software. For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+ Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control
+compilation and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (1) uses at run time a
+ copy of the library already present on the user's computer system,
+ rather than copying library functions into the executable, and (2)
+ will operate properly with a modified version of the library, if
+ the user installs one, as long as the modified version is
+ interface-compatible with the version that the work was made with.
+
+ c) Accompany the work with a written offer, valid for at least
+ three years, to give the same user the materials specified in
+ Subsection 6a, above, for a charge no more than the cost of
+ performing this distribution.
+
+ d) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ e) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply, and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License
+may add an explicit geographical distribution limitation excluding those
+countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms
+of the ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library.
+It is safest to attach them to the start of each source file to most
+effectively convey the exclusion of warranty; and each file should
+have at least the "copyright" line and a pointer to where the full
+notice is found.
+
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or
+your school, if any, to sign a "copyright disclaimer" for the library,
+if necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James
+ Random Hacker.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
+
+
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8909afd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,6 @@
+CFLAGS=-g -O0 -Wall -ltsm -D_GNU_SOURCE
+GTK=`pkg-config --cflags --libs gtk+-3.0 cairo pango pangocairo xkbcommon`
+FILES=src/wlterm.c src/wlt_font.c src/wlt_render.c src/shl_htable.c src/shl_pty.c
+
+all:
+ gcc -o wlterm $(FILES) $(CFLAGS) $(GTK)
diff --git a/src/shl_htable.c b/src/shl_htable.c
new file mode 100644
index 0000000..e79a7c0
--- /dev/null
+++ b/src/shl_htable.c
@@ -0,0 +1,428 @@
+/*
+ * SHL - Dynamic hash-table
+ *
+ * Written-by: Rusty Russell <rusty@rustcorp.com.au>
+ * Adjusted-by: David Herrmann <dh.herrmann@gmail.com>
+ * Licensed under LGPLv2+ - see LICENSE_htable file for details
+ */
+
+/*
+ * Please see ccan/htable/_info at:
+ * https://github.com/rustyrussell/ccan/tree/master/ccan/htable
+ * for information on the hashtable algorithm. This file copies the code inline
+ * and is released under the same conditions.
+ *
+ * At the end of the file you can find some helpers to use this htable to store
+ * objects with "unsigned long" or "char*" keys.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "shl_htable.h"
+
+#define COLD __attribute__((cold))
+
+struct htable {
+ /* KEEP IN SYNC WITH "struct shl_htable_int" */
+ size_t (*rehash)(const void *elem, void *priv);
+ void *priv;
+ unsigned int bits;
+ size_t elems, deleted, max, max_with_deleted;
+ /* These are the bits which are the same in all pointers. */
+ uintptr_t common_mask, common_bits;
+ uintptr_t perfect_bit;
+ uintptr_t *table;
+};
+
+#define HTABLE_INITIALIZER(name, rehash, priv) \
+ { rehash, priv, 0, 0, 0, 0, 0, -1, 0, 0, &name.perfect_bit }
+
+struct htable_iter {
+ size_t off;
+};
+
+/*
+ * INLINE COPY OF ccan/htable.c
+ */
+
+/* We use 0x1 as deleted marker. */
+#define HTABLE_DELETED (0x1)
+
+/* We clear out the bits which are always the same, and put metadata there. */
+static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
+ uintptr_t e)
+{
+ return e & ht->common_mask;
+}
+
+static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
+{
+ return (void *)((e & ~ht->common_mask) | ht->common_bits);
+}
+
+static inline uintptr_t make_hval(const struct htable *ht,
+ const void *p, uintptr_t bits)
+{
+ return ((uintptr_t)p & ~ht->common_mask) | bits;
+}
+
+static inline bool entry_is_valid(uintptr_t e)
+{
+ return e > HTABLE_DELETED;
+}
+
+static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
+ size_t hash)
+{
+ /* Shuffling the extra bits (as specified in mask) down the
+ * end is quite expensive. But the lower bits are redundant, so
+ * we fold the value first. */
+ return (hash ^ (hash >> ht->bits))
+ & ht->common_mask & ~ht->perfect_bit;
+}
+
+static void htable_init(struct htable *ht,
+ size_t (*rehash)(const void *elem, void *priv),
+ void *priv)
+{
+ struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
+ *ht = empty;
+ ht->rehash = rehash;
+ ht->priv = priv;
+ ht->table = &ht->perfect_bit;
+}
+
+static void htable_clear(struct htable *ht,
+ void (*free_cb) (void *entry, void *ctx),
+ void *ctx)
+{
+ size_t i;
+
+ if (ht->table != &ht->perfect_bit) {
+ if (free_cb) {
+ for (i = 0; i < (size_t)1 << ht->bits; ++i) {
+ if (entry_is_valid(ht->table[i]))
+ free_cb(get_raw_ptr(ht, ht->table[i]),
+ ctx);
+ }
+ }
+
+ free((void *)ht->table);
+ }
+
+ htable_init(ht, ht->rehash, ht->priv);
+}
+
+static void htable_visit(struct htable *ht,
+ void (*visit_cb) (void *elem, void *ctx),
+ void *ctx)
+{
+ size_t i;
+
+ if (visit_cb && ht->table != &ht->perfect_bit) {
+ for (i = 0; i < (size_t)1 << ht->bits; ++i) {
+ if (entry_is_valid(ht->table[i]))
+ visit_cb(get_raw_ptr(ht, ht->table[i]), ctx);
+ }
+ }
+}
+
+static size_t hash_bucket(const struct htable *ht, size_t h)
+{
+ return h & ((1 << ht->bits)-1);
+}
+
+static void *htable_val(const struct htable *ht,
+ struct htable_iter *i, size_t hash, uintptr_t perfect)
+{
+ uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
+
+ while (ht->table[i->off]) {
+ if (ht->table[i->off] != HTABLE_DELETED) {
+ if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
+ return get_raw_ptr(ht, ht->table[i->off]);
+ }
+ i->off = (i->off + 1) & ((1 << ht->bits)-1);
+ h2 &= ~perfect;
+ }
+ return NULL;
+}
+
+static void *htable_firstval(const struct htable *ht,
+ struct htable_iter *i, size_t hash)
+{
+ i->off = hash_bucket(ht, hash);
+ return htable_val(ht, i, hash, ht->perfect_bit);
+}
+
+static void *htable_nextval(const struct htable *ht,
+ struct htable_iter *i, size_t hash)
+{
+ i->off = (i->off + 1) & ((1 << ht->bits)-1);
+ return htable_val(ht, i, hash, 0);
+}
+
+/* This does not expand the hash table, that's up to caller. */
+static void ht_add(struct htable *ht, const void *new, size_t h)
+{
+ size_t i;
+ uintptr_t perfect = ht->perfect_bit;
+
+ i = hash_bucket(ht, h);
+
+ while (entry_is_valid(ht->table[i])) {
+ perfect = 0;
+ i = (i + 1) & ((1 << ht->bits)-1);
+ }
+ ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
+}
+
+static COLD bool double_table(struct htable *ht)
+{
+ unsigned int i;
+ size_t oldnum = (size_t)1 << ht->bits;
+ uintptr_t *oldtable, e;
+
+ oldtable = ht->table;
+ ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
+ if (!ht->table) {
+ ht->table = oldtable;
+ return false;
+ }
+ ht->bits++;
+ ht->max = ((size_t)3 << ht->bits) / 4;
+ ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
+
+ /* If we lost our "perfect bit", get it back now. */
+ if (!ht->perfect_bit && ht->common_mask) {
+ for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
+ if (ht->common_mask & ((size_t)1 << i)) {
+ ht->perfect_bit = (size_t)1 << i;
+ break;
+ }
+ }
+ }
+
+ if (oldtable != &ht->perfect_bit) {
+ for (i = 0; i < oldnum; i++) {
+ if (entry_is_valid(e = oldtable[i])) {
+ void *p = get_raw_ptr(ht, e);
+ ht_add(ht, p, ht->rehash(p, ht->priv));
+ }
+ }
+ free(oldtable);
+ }
+ ht->deleted = 0;
+ return true;
+}
+
+static COLD void rehash_table(struct htable *ht)
+{
+ size_t start, i;
+ uintptr_t e;
+
+ /* Beware wrap cases: we need to start from first empty bucket. */
+ for (start = 0; ht->table[start]; start++);
+
+ for (i = 0; i < (size_t)1 << ht->bits; i++) {
+ size_t h = (i + start) & ((1 << ht->bits)-1);
+ e = ht->table[h];
+ if (!e)
+ continue;
+ if (e == HTABLE_DELETED)
+ ht->table[h] = 0;
+ else if (!(e & ht->perfect_bit)) {
+ void *p = get_raw_ptr(ht, e);
+ ht->table[h] = 0;
+ ht_add(ht, p, ht->rehash(p, ht->priv));
+ }
+ }
+ ht->deleted = 0;
+}
+
+/* We stole some bits, now we need to put them back... */
+static COLD void update_common(struct htable *ht, const void *p)
+{
+ unsigned int i;
+ uintptr_t maskdiff, bitsdiff;
+
+ if (ht->elems == 0) {
+ /* Always reveal one bit of the pointer in the bucket,
+ * so it's not zero or HTABLE_DELETED (1), even if
+ * hash happens to be 0. Assumes (void *)1 is not a
+ * valid pointer. */
+ for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
+ if ((uintptr_t)p & ((uintptr_t)1 << i))
+ break;
+ }
+
+ ht->common_mask = ~((uintptr_t)1 << i);
+ ht->common_bits = ((uintptr_t)p & ht->common_mask);
+ ht->perfect_bit = 1;
+ return;
+ }
+
+ /* Find bits which are unequal to old common set. */
+ maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
+
+ /* These are the bits which go there in existing entries. */
+ bitsdiff = ht->common_bits & maskdiff;
+
+ for (i = 0; i < (size_t)1 << ht->bits; i++) {
+ if (!entry_is_valid(ht->table[i]))
+ continue;
+ /* Clear the bits no longer in the mask, set them as
+ * expected. */
+ ht->table[i] &= ~maskdiff;
+ ht->table[i] |= bitsdiff;
+ }
+
+ /* Take away those bits from our mask, bits and perfect bit. */
+ ht->common_mask &= ~maskdiff;
+ ht->common_bits &= ~maskdiff;
+ ht->perfect_bit &= ~maskdiff;
+}
+
+static bool htable_add(struct htable *ht, size_t hash, const void *p)
+{
+ if (ht->elems+1 > ht->max && !double_table(ht))
+ return false;
+ if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
+ rehash_table(ht);
+ assert(p);
+ if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
+ update_common(ht, p);
+
+ ht_add(ht, p, hash);
+ ht->elems++;
+ return true;
+}
+
+static void htable_delval(struct htable *ht, struct htable_iter *i)
+{
+ assert(i->off < (size_t)1 << ht->bits);
+ assert(entry_is_valid(ht->table[i->off]));
+
+ ht->elems--;
+ ht->table[i->off] = HTABLE_DELETED;
+ ht->deleted++;
+}
+
+/*
+ * Wrapper code to make it easier to use this hash-table as map.
+ */
+
+void shl_htable_init(struct shl_htable *htable,
+ bool (*compare) (const void *a, const void *b),
+ size_t (*rehash)(const void *elem, void *priv),
+ void *priv)
+{
+ struct htable *ht = (void*)&htable->htable;
+
+ htable->compare = compare;
+ htable_init(ht, rehash, priv);
+}
+
+void shl_htable_clear(struct shl_htable *htable,
+ void (*free_cb) (void *elem, void *ctx),
+ void *ctx)
+{
+ struct htable *ht = (void*)&htable->htable;
+
+ htable_clear(ht, free_cb, ctx);
+}
+
+void shl_htable_visit(struct shl_htable *htable,
+ void (*visit_cb) (void *elem, void *ctx),
+ void *ctx)
+{
+ struct htable *ht = (void*)&htable->htable;
+
+ htable_visit(ht, visit_cb, ctx);
+}
+
+bool shl_htable_lookup(struct shl_htable *htable, const void *obj, size_t hash,
+ void **out)
+{
+ struct htable *ht = (void*)&htable->htable;
+ struct htable_iter i;
+ void *c;
+
+ for (c = htable_firstval(ht, &i, hash);
+ c;
+ c = htable_nextval(ht, &i, hash)) {
+ if (htable->compare(obj, c)) {
+ if (out)
+ *out = c;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+int shl_htable_insert(struct shl_htable *htable, const void *obj, size_t hash)
+{
+ struct htable *ht = (void*)&htable->htable;
+ bool b;
+
+ b = htable_add(ht, hash, (void*)obj);
+ return b ? 0 : -ENOMEM;
+}
+
+bool shl_htable_remove(struct shl_htable *htable, const void *obj, size_t hash,
+ void **out)
+{
+ struct htable *ht = (void*)&htable->htable;
+ struct htable_iter i;
+ void *c;
+
+ for (c = htable_firstval(ht, &i, hash);
+ c;
+ c = htable_nextval(ht, &i, hash)) {
+ if (htable->compare(obj, c)) {
+ if (out)
+ *out = c;
+ htable_delval(ht, &i);
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/*
+ * Helpers
+ */
+
+bool shl_htable_compare_ulong(const void *a, const void *b)
+{
+ return *(const unsigned long*)a == *(const unsigned long*)b;
+}
+
+size_t shl_htable_rehash_ulong(const void *elem, void *priv)
+{
+ return (size_t)*(const unsigned long*)elem;
+}
+
+bool shl_htable_compare_str(const void *a, const void *b)
+{
+ return !strcmp(*(char**)a, *(char**)b);
+}
+
+/* DJB's hash function */
+size_t shl_htable_rehash_str(const void *elem, void *priv)
+{
+ const char *str = *(char**)elem;
+ size_t hash = 5381;
+
+ for ( ; *str; ++str)
+ hash = (hash << 5) + hash + (size_t)*str;
+
+ return hash;
+}
diff --git a/src/shl_htable.h b/src/shl_htable.h
new file mode 100644
index 0000000..9591180
--- /dev/null
+++ b/src/shl_htable.h
@@ -0,0 +1,222 @@
+/*
+ * SHL - Dynamic hash-table
+ *
+ * Copyright (c) 2010-2013 David Herrmann <dh.herrmann@gmail.com>
+ * Licensed under LGPLv2+ - see LICENSE_htable file for details
+ */
+
+/*
+ * Dynamic hash-table
+ * Implementation of a self-resizing hashtable to store arbitrary objects.
+ * Entries are not allocated by the table itself but are user-allocated. A
+ * single entry can be stored multiple times in the hashtable. No
+ * maintenance-members need to be embedded in user-allocated objects. However,
+ * the key (and optionally the hash) must be stored in the objects.
+ *
+ * Uses internally the htable from CCAN. See LICENSE_htable.
+ */
+
+#ifndef SHL_HTABLE_H
+#define SHL_HTABLE_H
+
+#include <limits.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+/* miscellaneous */
+
+#define shl_htable_offsetof(pointer, type, member) ({ \
+ const typeof(((type*)0)->member) *__ptr = (pointer); \
+ (type*)(((char*)__ptr) - offsetof(type, member)); \
+ })
+
+/* htable */
+
+struct shl_htable_int {
+ size_t (*rehash)(const void *elem, void *priv);
+ void *priv;
+ unsigned int bits;
+ size_t elems, deleted, max, max_with_deleted;
+ /* These are the bits which are the same in all pointers. */
+ uintptr_t common_mask, common_bits;
+ uintptr_t perfect_bit;
+ uintptr_t *table;
+};
+
+struct shl_htable {
+ bool (*compare) (const void *a, const void *b);
+ struct shl_htable_int htable;
+};
+
+#define SHL_HTABLE_INIT(_obj, _compare, _rehash, _priv) \
+ { \
+ .compare = (_compare), \
+ .htable = { \
+ .rehash = (_rehash), \
+ .priv = (_priv), \
+ .bits = 0, \
+ .elems = 0, \
+ .deleted = 0, \
+ .max = 0, \
+ .max_with_deleted = 0, \
+ .common_mask = -1, \
+ .common_bits = 0, \
+ .perfect_bit = 0, \
+ .table = &(_obj).htable.perfect_bit \
+ } \
+ }
+
+void shl_htable_init(struct shl_htable *htable,
+ bool (*compare) (const void *a, const void *b),
+ size_t (*rehash)(const void *elem, void *priv),
+ void *priv);
+void shl_htable_clear(struct shl_htable *htable,
+ void (*free_cb) (void *elem, void *ctx),
+ void *ctx);
+void shl_htable_visit(struct shl_htable *htable,
+ void (*visit_cb) (void *elem, void *ctx),
+ void *ctx);
+bool shl_htable_lookup(struct shl_htable *htable, const void *obj, size_t hash,
+ void **out);
+int shl_htable_insert(struct shl_htable *htable, const void *obj, size_t hash);
+bool shl_htable_remove(struct shl_htable *htable, const void *obj, size_t hash,
+ void **out);
+
+/* ulong htables */
+
+#if SIZE_MAX < ULONG_MAX
+# error "'size_t' is smaller than 'unsigned long'"
+#endif
+
+bool shl_htable_compare_ulong(const void *a, const void *b);
+size_t shl_htable_rehash_ulong(const void *elem, void *priv);
+
+#define SHL_HTABLE_INIT_ULONG(_obj) \
+ SHL_HTABLE_INIT((_obj), shl_htable_compare_ulong, \
+ shl_htable_rehash_ulong, \
+ NULL)
+
+static inline void shl_htable_init_ulong(struct shl_htable *htable)
+{
+ shl_htable_init(htable, shl_htable_compare_ulong,
+ shl_htable_rehash_ulong, NULL);
+}
+
+static inline void shl_htable_clear_ulong(struct shl_htable *htable,
+ void (*cb) (unsigned long *elem,
+ void *ctx),
+ void *ctx)
+{
+ shl_htable_clear(htable, (void (*) (void*, void*))cb, ctx);
+}
+
+static inline void shl_htable_visit_ulong(struct shl_htable *htable,
+ void (*cb) (unsigned long *elem,
+ void *ctx),
+ void *ctx)
+{
+ shl_htable_visit(htable, (void (*) (void*, void*))cb, ctx);
+}
+
+static inline bool shl_htable_lookup_ulong(struct shl_htable *htable,
+ unsigned long key,
+ unsigned long **out)
+{
+ return shl_htable_lookup(htable, (const void*)&key, (size_t)key,
+ (void**)out);
+}
+
+static inline int shl_htable_insert_ulong(struct shl_htable *htable,
+ const unsigned long *key)
+{
+ return shl_htable_insert(htable, (const void*)key, (size_t)*key);
+}
+
+static inline bool shl_htable_remove_ulong(struct shl_htable *htable,
+ unsigned long key,
+ unsigned long **out)
+{
+ return shl_htable_remove(htable, (const void*)&key, (size_t)key,
+ (void**)out);
+}
+
+/* string htables */
+
+bool shl_htable_compare_str(const void *a, const void *b);
+size_t shl_htable_rehash_str(const void *elem, void *priv);
+
+#define SHL_HTABLE_INIT_STR(_obj) \
+ SHL_HTABLE_INIT((_obj), shl_htable_compare_str, \
+ shl_htable_rehash_str, \
+ NULL)
+
+static inline void shl_htable_init_str(struct shl_htable *htable)
+{
+ shl_htable_init(htable, shl_htable_compare_str,
+ shl_htable_rehash_str, NULL);
+}
+
+static inline void shl_htable_clear_str(struct shl_htable *htable,
+ void (*cb) (char **elem,
+ void *ctx),
+ void *ctx)
+{
+ shl_htable_clear(htable, (void (*) (void*, void*))cb, ctx);
+}
+
+static inline void shl_htable_visit_str(struct shl_htable *htable,
+ void (*cb) (char **elem,
+ void *ctx),
+ void *ctx)
+{
+ shl_htable_visit(htable, (void (*) (void*, void*))cb, ctx);
+}
+
+static inline size_t shl_htable_hash_str(struct shl_htable *htable,
+ const char *str, size_t *hash)
+{
+ size_t h;
+
+ if (hash && *hash) {
+ h = *hash;
+ } else {
+ h = htable->htable.rehash((const void*)&str, NULL);
+ if (hash)
+ *hash = h;
+ }
+
+ return h;
+}
+
+static inline bool shl_htable_lookup_str(struct shl_htable *htable,
+ const char *str, size_t *hash,
+ char ***out)
+{
+ size_t h;
+
+ h = shl_htable_hash_str(htable, str, hash);
+ return shl_htable_lookup(htable, (const void*)&str, h, (void**)out);
+}
+
+static inline int shl_htable_insert_str(struct shl_htable *htable,
+ char **str, size_t *hash)
+{
+ size_t h;
+
+ h = shl_htable_hash_str(htable, *str, hash);
+ return shl_htable_insert(htable, (const void*)str, h);
+}
+
+static inline bool shl_htable_remove_str(struct shl_htable *htable,
+ const char *str, size_t *hash,
+ char ***out)
+{
+ size_t h;
+
+ h = shl_htable_hash_str(htable, str, hash);
+ return shl_htable_remove(htable, (const void*)&str, h, (void **)out);
+}
+
+#endif /* SHL_HTABLE_H */
diff --git a/src/shl_pty.c b/src/shl_pty.c
new file mode 100644
index 0000000..7348281
--- /dev/null
+++ b/src/shl_pty.c
@@ -0,0 +1,674 @@
+/*
+ * SHL - PTY Helpers
+ *
+ * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
+ * Dedicated to the Public Domain
+ */
+
+/*
+ * PTY Helpers
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <pty.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/epoll.h>
+#include <sys/ioctl.h>
+#include <sys/uio.h>
+#include <termios.h>
+#include <unistd.h>
+#include "shl_pty.h"
+
+#define SHL_PTY_BUFSIZE 16384
+
+/*
+ * Ring Buffer
+ * Our PTY helper buffers outgoing data so the caller can rely on write
+ * operations to always succeed (except for OOM). To buffer data in a PTY we
+ * use a fast ring buffer to avoid heavy re-allocations on every write.
+ *
+ * Note that this allows users to use pty-writes for small data without
+ * causing heavy allocations in the PTY layer. This is quite important for
+ * keyboard-handling or other DEC-VT emulations.
+ */
+
+struct ring {
+ char *buf;
+ size_t size;
+ size_t start;
+ size_t end;
+};
+
+#define RING_MASK(_r, _v) ((_v) & ((_r)->size - 1))
+
+/*
+ * Resize ring-buffer to size @nsize. @nsize must be a power-of-2, otherwise
+ * ring operations will behave incorrectly.
+ */
+static int ring_resize(struct ring *r, size_t nsize)
+{
+ char *buf;
+
+ buf = malloc(nsize);
+ if (!buf)
+ return -ENOMEM;
+
+ if (r->end == r->start) {
+ r->end = 0;
+ r->start = 0;
+ } else if (r->end > r->start) {
+ memcpy(buf, &r->buf[r->start], r->end - r->start);
+
+ r->end -= r->start;
+ r->start = 0;
+ } else {
+ memcpy(buf, &r->buf[r->start], r->size - r->start);
+ memcpy(&buf[r->size - r->start], r->buf, r->end);
+
+ r->end += r->size - r->start;
+ r->start = 0;
+ }
+
+ free(r->buf);
+ r->buf = buf;
+ r->size = nsize;
+
+ return 0;
+}
+
+/* Compute next higher power-of-2 of @v. Returns 4096 in case v is 0. */
+static size_t ring_pow2(size_t v)
+{
+ size_t i;
+
+ if (!v)
+ return 4096;
+
+ --v;
+
+ for (i = 1; i < 8 * sizeof(size_t); i *= 2)
+ v |= v >> i;
+
+ return ++v;
+}
+
+/*
+ * Resize ring-buffer to provide enough room for @add bytes of new data. This
+ * resizes the buffer if it is too small. It returns -ENOMEM on OOM and 0 on
+ * success.
+ */
+static int ring_grow(struct ring *r, size_t add)
+{
+ size_t len;
+
+ /*
+ * Note that "end == start" means "empty buffer". Hence, we can never
+ * fill the last byte of a buffer. That means, we must account for an
+ * additional byte here ("end == start"-byte).
+ */
+
+ if (r->end < r->start)
+ len = r->start - r->end;
+ else
+ len = r->start + r->size - r->end;
+
+ /* don't use ">=" as "end == start" would be ambigious */
+ if (len > add)
+ return 0;
+
+ /* +1 for additional "end == start" byte */
+ len = r->size + add - len + 1;
+ len = ring_pow2(len);
+
+ if (len <= r->size)
+ return -ENOMEM;
+
+ return ring_resize(r, len);
+}
+
+/*
+ * Push @len bytes from @u8 into the ring buffer. The buffer is resized if it
+ * is too small. -ENOMEM is returned on OOM, 0 on success.
+ */
+static int ring_push(struct ring *r, const char *u8, size_t len)
+{
+ int err;
+ size_t l;
+
+ err = ring_grow(r, len);
+ if (err < 0)
+ return err;
+
+ if (r->start <= r->end) {
+ l = r->size - r->end;
+ if (l > len)
+ l = len;
+
+ memcpy(&r->buf[r->end], u8, l);
+ r->end = RING_MASK(r, r->end + l);
+
+ len -= l;
+ u8 += l;
+ }
+
+ if (!len)
+ return 0;
+
+ memcpy(&r->buf[r->end], u8, len);
+ r->end = RING_MASK(r, r->end + len);
+
+ return 0;
+}
+
+/*
+ * Get data pointers for current ring-buffer data. @vec must be an array of 2
+ * iovec objects. They are filled according to the data available in the
+ * ring-buffer. 0, 1 or 2 is returned according to the number of iovec objects
+ * that were filled (0 meaning buffer is empty).
+ *
+ * Hint: "struct iovec" is defined in <sys/uio.h> and looks like this:
+ * struct iovec {
+ * void *iov_base;
+ * size_t iov_len;
+ * };
+ */
+static size_t ring_peek(struct ring *r, struct iovec *vec)
+{
+ if (r->end > r->start) {
+ vec[0].iov_base = &r->buf[r->start];
+ vec[0].iov_len = r->end - r->start;
+ return 1;
+ } else if (r->end < r->start) {
+ vec[0].iov_base = &r->buf[r->start];
+ vec[0].iov_len = r->size - r->start;
+ vec[1].iov_base = r->buf;
+ vec[1].iov_len = r->end;
+ return 2;
+ } else {
+ return 0;
+ }
+}
+
+/*
+ * Remove @len bytes from the start of the ring-buffer. Note that we protect
+ * against overflows so removing more bytes than available is safe.
+ */
+static void ring_pop(struct ring *r, size_t len)
+{
+ size_t l;
+
+ if (r->start > r->end) {
+ l = r->size - r->start;
+ if (l > len)
+ l = len;
+
+ r->start = RING_MASK(r, r->start + l);
+ len -= l;
+ }
+
+ if (!len)
+ return;
+
+ l = r->end - r->start;
+ if (l > len)
+ l = len;
+
+ r->start = RING_MASK(r, r->start + l);
+}
+
+/*
+ * PTY
+ * A PTY object represents a single PTY connection between a master and a
+ * child. The child process is fork()ed so the caller controls what program
+ * will be run.
+ *
+ * Programs like /bin/login tend to perform a vhangup() on their TTY
+ * before running the login procedure. This also causes the pty master
+ * to get a EPOLLHUP event as long as no client has the TTY opened.
+ * This means, we cannot use the TTY connection as reliable way to track
+ * the client. Instead, we _must_ rely on the PID of the client to track
+ * them.
+ * However, this has the side effect that if the client forks and the
+ * parent exits, we loose them and restart the client. But this seems to
+ * be the expected behavior so we implement it here.
+ *
+ * Unfortunately, epoll always polls for EPOLLHUP so as long as the
+ * vhangup() is ongoing, we will _always_ get EPOLLHUP and cannot sleep.
+ * This gets worse if the client closes the TTY but doesn't exit.
+ * Therefore, we the fd must be edge-triggered in the epoll-set so we
+ * only get the events once they change. This has to be taken into by the
+ * user of shl_pty. As many event-loops don't support edge-triggered
+ * behavior, you can use the shl_pty_bridge interface.
+ *
+ * Note that shl_pty does not track SIGHUP, you need to do that yourself
+ * and call shl_pty_close() once the client exited.
+ */
+
+struct shl_pty {
+ unsigned long ref;
+ int fd;
+ pid_t child;
+ char in_buf[SHL_PTY_BUFSIZE];
+ struct ring out_buf;
+
+ shl_pty_input_cb cb;
+ void *data;
+};
+
+enum shl_pty_msg {
+ SHL_PTY_FAILED,
+ SHL_PTY_SETUP,
+};
+
+static char pty_recv(int fd)
+{
+ int r;
+ char d;
+
+ do {
+ r = read(fd, &d, 1);
+ } while (r < 0 && (errno == EINTR || errno == EAGAIN));
+
+ return (r <= 0) ? SHL_PTY_FAILED : d;
+}
+
+static int pty_send(int fd, char d)
+{
+ int r;
+
+ do {
+ r = write(fd, &d, 1);
+ } while (r < 0 && (errno == EINTR || errno == EAGAIN));
+
+ return (r == 1) ? 0 : -EINVAL;
+}
+
+static int pty_setup_child(int slave,
+ unsigned short term_width,
+ unsigned short term_height)
+{
+ struct termios attr;
+ struct winsize ws;
+
+ /* get terminal attributes */
+ if (tcgetattr(slave, &attr) < 0)
+ return -errno;
+
+ /* erase character should be normal backspace, PLEASEEE! */
+ attr.c_cc[VERASE] = 010;
+
+ /* set changed terminal attributes */
+ if (tcsetattr(slave, TCSANOW, &attr) < 0)
+ return -errno;
+
+ memset(&ws, 0, sizeof(ws));
+ ws.ws_col = term_width;
+ ws.ws_row = term_height;
+
+ if (ioctl(slave, TIOCSWINSZ, &ws) < 0)
+ return -errno;
+
+ if (dup2(slave, STDIN_FILENO) != STDIN_FILENO ||
+ dup2(slave, STDOUT_FILENO) != STDOUT_FILENO ||
+ dup2(slave, STDERR_FILENO) != STDERR_FILENO)
+ return -errno;
+
+ return 0;
+}
+
+static int pty_init_child(int fd)
+{
+ int r;
+ sigset_t sigset;
+ char *slave_name;
+ int slave, i;
+ pid_t pid;
+
+ /* unlockpt() requires unset signal-handlers */
+ sigemptyset(&sigset);
+ r = sigprocmask(SIG_SETMASK, &sigset, NULL);
+ if (r < 0)
+ return -errno;
+
+ for (i = 1; i < SIGUNUSED; ++i)
+ signal(i, SIG_DFL);
+
+ r = grantpt(fd);
+ if (r < 0)
+ return -errno;
+
+ r = unlockpt(fd);
+ if (r < 0)
+ return -errno;
+
+ slave_name = ptsname(fd);
+ if (!slave_name)
+ return -errno;
+
+ /* open slave-TTY */
+ slave = open(slave_name, O_RDWR | O_CLOEXEC | O_NOCTTY);
+ if (slave < 0)
+ return -errno;
+
+ /* open session so we loose our controlling TTY */
+ pid = setsid();
+ if (pid < 0) {
+ close(slave);
+ return -errno;
+ }
+
+ /* set controlling TTY */
+ r = ioctl(slave, TIOCSCTTY, 0);
+ if (r < 0) {
+ close(slave);
+ return -errno;
+ }
+
+ return slave;
+}
+
+pid_t shl_pty_open(struct shl_pty **out,
+ shl_pty_input_cb cb,
+ void *data,
+ unsigned short term_width,
+ unsigned short term_height)
+{
+ struct shl_pty *pty;
+ pid_t pid;
+ int fd, comm[2], slave, r;
+ char d;
+
+ pty = calloc(1, sizeof(*pty));
+ if (!pty)
+ return -ENOMEM;
+
+ fd = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC | O_NONBLOCK);
+ if (fd < 0) {
+ free(pty);
+ return -errno;
+ }
+
+ r = pipe2(comm, O_CLOEXEC);
+ if (r < 0) {
+ r = -errno;
+ close(fd);
+ free(pty);
+ return r;
+ }
+
+ pid = fork();
+ if (pid < 0) {
+ /* error */
+ pid = -errno;
+ close(comm[0]);
+ close(comm[1]);
+ close(fd);
+ free(pty);
+ return pid;
+ } else if (!pid) {
+ /* child */
+ close(comm[0]);
+ free(pty);
+
+ slave = pty_init_child(fd);
+ close(fd);
+
+ if (slave < 0)
+ exit(1);
+
+ r = pty_setup_child(slave, term_width, term_height);
+ if (r < 0)
+ exit(1);
+
+ /* close slave if it's not one of the std-fds */
+ if (slave > 2)
+ close(slave);
+
+ /* wake parent */
+ pty_send(comm[1], SHL_PTY_SETUP);
+ close(comm[1]);
+
+ *out = NULL;
+ return pid;
+ }
+
+ /* parent */
+ close(comm[1]);
+
+ pty->ref = 1;
+ pty->fd = fd;
+ pty->child = pid;
+ pty->cb = cb;
+ pty->data = data;
+
+ /* wait for child setup */
+ d = pty_recv(comm[0]);
+ if (d != SHL_PTY_SETUP) {
+ close(comm[0]);
+ close(fd);
+ free(pty);
+ return -EINVAL;
+ }
+
+ close(comm[0]);
+ *out = pty;
+ return pid;
+}
+
+void shl_pty_ref(struct shl_pty *pty)
+{
+ if (!pty || !pty->ref)
+ return;
+
+ ++pty->ref;
+}
+
+void shl_pty_unref(struct shl_pty *pty)
+{
+ if (!pty || !pty->ref || --pty->ref)
+ return;
+
+ shl_pty_close(pty);
+ free(pty->out_buf.buf);
+ free(pty);
+}
+
+void shl_pty_close(struct shl_pty *pty)
+{
+ if (pty->fd < 0)
+ return;
+
+ close(pty->fd);
+ pty->fd = -1;
+}
+
+bool shl_pty_is_open(struct shl_pty *pty)
+{
+ return pty->fd >= 0;
+}
+
+int shl_pty_get_fd(struct shl_pty *pty)
+{
+ return pty->fd;
+}
+
+pid_t shl_pty_get_child(struct shl_pty *pty)
+{
+ return pty->child;
+}
+
+static void pty_write(struct shl_pty *pty)
+{
+ struct iovec vec[2];
+ size_t num;
+ ssize_t r;
+
+ num = ring_peek(&pty->out_buf, vec);
+ if (!num)
+ return;
+
+ /* ignore errors in favor of SIGCHLD; (we're edge-triggered, anyway) */
+ r = writev(pty->fd, vec, (int)num);
+ if (r >= 0)
+ ring_pop(&pty->out_buf, (size_t)r);
+}
+
+static int pty_read(struct shl_pty *pty)
+{
+ ssize_t len, num;
+
+ /* We're edge-triggered, means we need to read the whole queue. This,
+ * however, might cause us to stall if the writer is faster than we
+ * are. Therefore, we have some rather arbitrary limit on how fast
+ * we read. If we reach it, we simply return EAGAIN to the caller and
+ * let them deal with it. */
+ num = 50;
+ do {
+ len = read(pty->fd, pty->in_buf, sizeof(pty->in_buf));
+ if (len > 0)
+ pty->cb(pty, pty->in_buf, len, pty->data);
+ } while (len > 0 && --num);
+
+ return !num ? -EAGAIN : 0;
+}
+
+int shl_pty_dispatch(struct shl_pty *pty)
+{
+ int r;
+
+ r = pty_read(pty);
+ pty_write(pty);
+ return r;
+}
+
+int shl_pty_write(struct shl_pty *pty, const char *u8, size_t len)
+{
+ if (!shl_pty_is_open(pty))
+ return -ENODEV;
+
+ return ring_push(&pty->out_buf, u8, len);
+}
+
+int shl_pty_signal(struct shl_pty *pty, int sig)
+{
+ int r;
+
+ if (!shl_pty_is_open(pty))
+ return -ENODEV;
+
+ r = ioctl(pty->fd, TIOCSIG, sig);
+ return (r < 0) ? -errno : 0;
+}
+
+int shl_pty_resize(struct shl_pty *pty,
+ unsigned short term_width,
+ unsigned short term_height)
+{
+ struct winsize ws;
+ int r;
+
+ if (!shl_pty_is_open(pty))
+ return -ENODEV;
+
+ memset(&ws, 0, sizeof(ws));
+ ws.ws_col = term_width;
+ ws.ws_row = term_height;
+
+ /*
+ * This will send SIGWINCH to the pty slave foreground process group.
+ * We will also get one, but we don't need it.
+ */
+ r = ioctl(pty->fd, TIOCSWINSZ, &ws);
+ return (r < 0) ? -errno : 0;
+}
+
+/*
+ * PTY Bridge
+ * The PTY bridge wraps multiple ptys in a single file-descriptor. It is
+ * enough for the caller to listen for read-events on the fd.
+ *
+ * This interface is provided to allow integration of PTYs into event-loops
+ * that do not support edge-triggered interfaces. There is no other reason
+ * to use this bridge.
+ */
+
+int shl_pty_bridge_new(void)
+{
+ int fd;
+
+ fd = epoll_create1(EPOLL_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+
+ return fd;
+}
+
+void shl_pty_bridge_free(int bridge)
+{
+ close(bridge);
+}
+
+int shl_pty_bridge_dispatch(int bridge, int timeout)
+{
+ struct epoll_event up, ev;
+ struct shl_pty *pty;
+ int fd, r;
+
+ r = epoll_wait(bridge, &ev, 1, timeout);
+ if (r < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ return 0;
+
+ return -errno;
+ }
+
+ if (!r)
+ return 0;
+
+ pty = ev.data.ptr;
+ r = shl_pty_dispatch(pty);
+ if (r == -EAGAIN) {
+ /* EAGAIN means we couldn't dispatch data fast enough. Modify
+ * the fd in the epoll-set so we get edge-triggered events
+ * next round. */
+ memset(&up, 0, sizeof(up));
+ up.events = EPOLLIN | EPOLLOUT | EPOLLET;
+ up.data.ptr = pty;
+ fd = shl_pty_get_fd(pty);
+ epoll_ctl(bridge, EPOLL_CTL_ADD, fd, &up);
+ }
+
+ return 0;
+}
+
+int shl_pty_bridge_add(int bridge, struct shl_pty *pty)
+{
+ struct epoll_event ev;
+ int r, fd;
+
+ memset(&ev, 0, sizeof(ev));
+ ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
+ ev.data.ptr = pty;
+ fd = shl_pty_get_fd(pty);
+
+ r = epoll_ctl(bridge, EPOLL_CTL_ADD, fd, &ev);
+ if (r < 0)
+ return -errno;
+
+ return 0;
+}
+
+void shl_pty_bridge_remove(int bridge, struct shl_pty *pty)
+{
+ int fd;
+
+ fd = shl_pty_get_fd(pty);
+ epoll_ctl(bridge, EPOLL_CTL_DEL, fd, NULL);
+}
diff --git a/src/shl_pty.h b/src/shl_pty.h
new file mode 100644
index 0000000..1b67616
--- /dev/null
+++ b/src/shl_pty.h
@@ -0,0 +1,58 @@
+/*
+ * SHL - PTY Helpers
+ *
+ * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
+ * Dedicated to the Public Domain
+ */
+
+/*
+ * PTY Helpers
+ */
+
+#ifndef SHL_PTY_H
+#define SHL_PTY_H
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* pty */
+
+struct shl_pty;
+
+typedef void (*shl_pty_input_cb) (struct shl_pty *pty, char *u8,
+ size_t len, void *data);
+
+pid_t shl_pty_open(struct shl_pty **out,
+ shl_pty_input_cb cb,
+ void *data,
+ unsigned short term_width,
+ unsigned short term_height);
+void shl_pty_ref(struct shl_pty *pty);
+void shl_pty_unref(struct shl_pty *pty);
+void shl_pty_close(struct shl_pty *pty);
+
+bool shl_pty_is_open(struct shl_pty *pty);
+int shl_pty_get_fd(struct shl_pty *pty);
+pid_t shl_pty_get_child(struct shl_pty *pty);
+
+int shl_pty_dispatch(struct shl_pty *pty);
+int shl_pty_write(struct shl_pty *pty, const char *u8, size_t len);
+int shl_pty_signal(struct shl_pty *pty, int sig);
+int shl_pty_resize(struct shl_pty *pty,
+ unsigned short term_width,
+ unsigned short term_height);
+
+/* pty bridge */
+
+int shl_pty_bridge_new(void);
+void shl_pty_bridge_free(int bridge);
+
+int shl_pty_bridge_dispatch(int bridge, int timeout);
+int shl_pty_bridge_add(int bridge, struct shl_pty *pty);
+void shl_pty_bridge_remove(int bridge, struct shl_pty *pty);
+
+#endif /* SHL_PTY_H */
diff --git a/src/wlt_font.c b/src/wlt_font.c
new file mode 100644
index 0000000..edfe370
--- /dev/null
+++ b/src/wlt_font.c
@@ -0,0 +1,423 @@
+/*
+ * wlterm - pango fonts
+ *
+ * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
+ *
+ * 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.
+ */
+
+/*
+ * Pango Fonts
+ * This helper uses pango to provide font support. Terminals have special
+ * requirements for fonts. We need fixed cell sizes, multi-width characters
+ * and more. This helper measures fonts and provides fixed glyphs to the
+ * caller. No sophisticated font-handling is required by the caller.
+ */
+
+#include <cairo.h>
+#include <errno.h>
+#include <glib.h>
+#include <pango/pango.h>
+#include <pango/pangocairo.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "wlterm.h"
+#include "shl_htable.h"
+
+struct wlt_font {
+ unsigned long ref;
+ PangoFontMap *map;
+};
+
+struct wlt_face {
+ unsigned long ref;
+ struct wlt_font *font;
+ PangoContext *ctx;
+
+ struct shl_htable glyphs;
+ unsigned int width;
+ unsigned int height;
+ unsigned int baseline;
+};
+
+#define wlt_to_glyph(_id) \
+ shl_htable_offsetof((_id), struct wlt_glyph, id)
+
+static void wlt_glyph_free(struct wlt_glyph *glyph);
+
+int wlt_font_new(struct wlt_font **out)
+{
+ struct wlt_font *font;
+ int r;
+
+ font = calloc(1, sizeof(*font));
+ if (!font)
+ return -ENOMEM;
+ font->ref = 1;
+
+ font->map = pango_cairo_font_map_get_default();
+ if (font->map) {
+ g_object_ref(font->map);
+ } else {
+ font->map = pango_cairo_font_map_new();
+ if (!font->map) {
+ r = -ENOMEM;
+ goto err_free;
+ }
+ }
+
+ *out = font;
+ return 0;
+
+err_free:
+ free(font);
+ return r;
+}
+
+void wlt_font_ref(struct wlt_font *font)
+{
+ if (!font || !font->ref)
+ return;
+
+ ++font->ref;
+}
+
+void wlt_font_unref(struct wlt_font *font)
+{
+ if (!font || !font->ref || --font->ref)
+ return;
+
+ g_object_unref(font->map);
+ free(font);
+}
+
+static void init_pango_desc(PangoFontDescription *desc, int desc_size,
+ int desc_bold, int desc_italic)
+{
+ PangoFontMask mask;
+ int v;
+
+ if (desc_size != WLT_FACE_DONT_CARE) {
+ v = desc_size * PANGO_SCALE;
+ if (desc_size > 0 && v > 0)
+ pango_font_description_set_absolute_size(desc, v);
+ }
+
+ if (desc_bold != WLT_FACE_DONT_CARE) {
+ v = desc_bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL;
+ pango_font_description_set_weight(desc, v);
+ }
+
+ if (desc_italic != WLT_FACE_DONT_CARE) {
+ v = desc_italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
+ pango_font_description_set_style(desc, v);
+ }
+
+ pango_font_description_set_variant(desc, PANGO_VARIANT_NORMAL);
+ pango_font_description_set_stretch(desc, PANGO_STRETCH_NORMAL);
+ pango_font_description_set_gravity(desc, PANGO_GRAVITY_SOUTH);
+
+ mask = pango_font_description_get_set_fields(desc);
+
+ if (!(mask & PANGO_FONT_MASK_FAMILY))
+ pango_font_description_set_family(desc, "monospace");
+ if (!(mask & PANGO_FONT_MASK_WEIGHT))
+ pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
+ if (!(mask & PANGO_FONT_MASK_STYLE))
+ pango_font_description_set_style(desc, PANGO_STYLE_NORMAL);
+ if (!(mask & PANGO_FONT_MASK_SIZE))
+ pango_font_description_set_size(desc, 10 * PANGO_SCALE);
+}
+
+/*
+ * There is no way to check whether a font is a monospace font. Moreover, there
+ * is no "monospace extents" field of fonts that we can use to calculate a
+ * suitable cell size. Any bounding-boxes provided by the fonts are mostly
+ * useless for cell-size computations. Therefore, we simply render a bunch of
+ * ASCII characters and compute the cell-size from these. If you passed a
+ * monospace font, it will work out greatly. If you passed some other font, you
+ * will get a suitable tradeoff (well, don't do that..).
+ */
+static void measure_pango(struct wlt_face *face)
+{
+ static const char str[] = "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "@!\"$%&/()=?\\}][{°^~+*#'<>|-_.:,;`´";
+ static const size_t str_len = sizeof(str) - 1;
+ PangoLayout *layout;
+ PangoRectangle rec;
+
+ layout = pango_layout_new(face->ctx);
+
+ pango_layout_set_height(layout, 0);
+ pango_layout_set_spacing(layout, 0);
+ pango_layout_set_text(layout, str, str_len);
+ pango_layout_get_pixel_extents(layout, NULL, &rec);
+
+ /* We use an example layout to render a bunch of ASCII characters in a
+ * single line. The height and baseline of the resulting extents can be
+ * copied unchanged into the face. For the width we calculate the
+ * average (rounding up). */
+ face->width = (rec.width + (str_len - 1)) / str_len;
+ face->height = rec.height;
+ face->baseline = PANGO_PIXELS_CEIL(pango_layout_get_baseline(layout));
+
+ g_object_unref(layout);
+}
+
+static int init_pango(struct wlt_face *face, const char *desc_str,
+ int desc_size, int desc_bold, int desc_italic)
+{
+ PangoFontDescription *desc;
+
+ /* set context options */
+ pango_context_set_base_dir(face->ctx, PANGO_DIRECTION_LTR);
+ pango_context_set_language(face->ctx, pango_language_get_default());
+
+ /* set font description */
+ desc = pango_font_description_from_string(desc_str);
+ init_pango_desc(desc, desc_size, desc_bold, desc_italic);
+ pango_context_set_font_description(face->ctx, desc);
+ pango_font_description_free(desc);
+
+ /* measure font */
+ measure_pango(face);
+
+ if (!face->width || !face->height)
+ return -EINVAL;
+
+ return 0;
+}
+
+int wlt_face_new(struct wlt_face **out, struct wlt_font *font,
+ const char *desc_str, int desc_size, int desc_bold,
+ int desc_italic)
+{
+ struct wlt_face *face;
+ int r;
+
+ face = calloc(1, sizeof(*face));
+ if (!face)
+ return -ENOMEM;
+ face->ref = 1;
+ face->font = font;
+
+ shl_htable_init_ulong(&face->glyphs);
+ face->ctx = pango_font_map_create_context(font->map);
+
+ r = init_pango(face, desc_str, desc_size, desc_bold, desc_italic);
+ if (r < 0)
+ goto err_ctx;
+
+ wlt_font_ref(face->font);
+ *out = face;
+ return 0;
+
+err_ctx:
+ g_object_unref(face->ctx);
+ free(face);
+ return r;
+}
+
+void wlt_face_ref(struct wlt_face *face)
+{
+ if (!face || !face->ref)
+ return;
+
+ ++face->ref;
+}
+
+static void free_glyph(unsigned long *elem, void *ctx)
+{
+ wlt_glyph_free(wlt_to_glyph(elem));
+}
+
+void wlt_face_unref(struct wlt_face *face)
+{
+ if (!face || !face->ref || --face->ref)
+ return;
+
+ g_object_unref(face->ctx);
+ shl_htable_clear_ulong(&face->glyphs, free_glyph, NULL);
+ wlt_font_unref(face->font);
+ free(face);
+}
+
+unsigned int wlt_face_get_width(struct wlt_face *face)
+{
+ return face->width;
+}
+
+unsigned int wlt_face_get_height(struct wlt_face *face)
+{
+ return face->height;
+}
+
+static unsigned int c2f(cairo_format_t format)
+{
+ switch (format) {
+ case CAIRO_FORMAT_A1:
+ return WLT_GLYPH_A1;
+ case CAIRO_FORMAT_A8:
+ return WLT_GLYPH_A8;
+ case CAIRO_FORMAT_RGB24:
+ return WLT_GLYPH_RGB24;
+ default:
+ return WLT_GLYPH_INVALID;
+ }
+}
+
+static int create_glyph(struct wlt_face *face, struct wlt_glyph *glyph,
+ const uint32_t *ch, size_t len)
+{
+ PangoLayoutLine *line;
+ cairo_surface_t *surface;
+ PangoRectangle rec;
+ cairo_format_t format;
+ PangoLayout *layout;
+ cairo_t *cr;
+ size_t cnt;
+ glong ulen;
+ char *val;
+ int r;
+
+ format = CAIRO_FORMAT_A8;
+ glyph->format = c2f(format);
+ glyph->width = face->width * glyph->cwidth;
+ glyph->stride = cairo_format_stride_for_width(format, glyph->width);
+ glyph->height = face->height;
+
+ glyph->buffer = calloc(1, glyph->stride * glyph->height);
+ if (!glyph->buffer)
+ return -ENOMEM;
+
+ surface = cairo_image_surface_create_for_data(glyph->buffer,
+ format,
+ glyph->width,
+ glyph->height,
+ glyph->stride);
+ if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
+ r = -ENOMEM;
+ goto err_buffer;
+ }
+
+ cr = cairo_create(surface);
+ if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
+ r = -ENOMEM;
+ goto err_surface;
+ }
+
+ pango_cairo_update_context(cr, face->ctx);
+ layout = pango_layout_new(face->ctx);
+
+ val = g_ucs4_to_utf8(ch, len, NULL, &ulen, NULL);
+ if (!val) {
+ r = -ERANGE;
+ goto err_layout;
+ }
+
+ /* render one line only */
+ pango_layout_set_height(layout, 0);
+ /* no line spacing */
+ pango_layout_set_spacing(layout, 0);
+ /* set text to char [+combining-chars] */
+ pango_layout_set_text(layout, val, ulen);
+
+ g_free(val);
+
+ cnt = pango_layout_get_line_count(layout);
+ if (cnt == 0) {
+ r = -ERANGE;
+ goto err_layout;
+ }
+
+ line = pango_layout_get_line_readonly(layout, 0);
+ pango_layout_line_get_pixel_extents(line, NULL, &rec);
+
+ cairo_move_to(cr, -rec.x, face->baseline),
+ cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
+ pango_cairo_show_layout_line(cr, line);
+
+ g_object_unref(layout);
+ cairo_destroy(cr);
+ glyph->cr_surface = surface;
+ return 0;
+
+err_layout:
+ g_object_unref(layout);
+ cairo_destroy(cr);
+err_surface:
+ cairo_surface_destroy(surface);
+err_buffer:
+ free(glyph->buffer);
+ return r;
+}
+
+int wlt_face_render(struct wlt_face *face, struct wlt_glyph **out,
+ unsigned long id, const uint32_t *ch, size_t len,
+ size_t cwidth)
+{
+ struct wlt_glyph *glyph;
+ unsigned long *gid;
+ bool b;
+ int r;
+
+ b = shl_htable_lookup_ulong(&face->glyphs, id, &gid);
+ if (b) {
+ *out = wlt_to_glyph(gid);
+ return 0;
+ }
+
+ if (!len || !cwidth)
+ return -EINVAL;
+
+ glyph = calloc(1, sizeof(*glyph));
+ if (!glyph)
+ return -ENOMEM;
+ glyph->id = id;
+ glyph->cwidth = cwidth;
+
+ r = create_glyph(face, glyph, ch, len);
+ if (r < 0)
+ goto err_free;
+
+ r = shl_htable_insert_ulong(&face->glyphs, &glyph->id);
+ if (r < 0)
+ goto err_glyph;
+
+ *out = glyph;
+ return 0;
+
+err_glyph:
+ free(glyph->buffer);
+err_free:
+ free(glyph);
+ return r;
+}
+
+static void wlt_glyph_free(struct wlt_glyph *glyph)
+{
+ if (glyph->cr_surface)
+ cairo_surface_destroy(glyph->cr_surface);
+ free(glyph->buffer);
+ free(glyph);
+}
diff --git a/src/wlt_render.c b/src/wlt_render.c
new file mode 100644
index 0000000..3306c18
--- /dev/null
+++ b/src/wlt_render.c
@@ -0,0 +1,95 @@
+/*
+ * wlterm - Wayland Terminal
+ *
+ * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
+ *
+ * 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.
+ */
+
+/*
+ * Terminal Rendering
+ */
+
+#include <cairo.h>
+#include <errno.h>
+#include <libtsm.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "wlterm.h"
+
+static int wlt_render_cell(struct tsm_screen *screen, uint32_t id,
+ const uint32_t *ch, size_t len, unsigned int cwidth,
+ unsigned int posx, unsigned int posy,
+ const struct tsm_screen_attr *attr, void *data)
+{
+ struct wlt_draw_ctx *ctx = data;
+ cairo_t *cr = ctx->cr;
+ struct wlt_glyph *glyph;
+ int r;
+
+ cairo_rectangle(cr, posx * ctx->cell_width, posy * ctx->cell_height,
+ ctx->cell_width, ctx->cell_height);
+
+ /* draw bg */
+ cairo_set_source_rgb(cr,
+ attr->br / (double)256,
+ attr->bg / (double)256,
+ attr->bb / (double)256);
+ cairo_fill(cr);
+
+ /* draw fg */
+ if (len) {
+ r = wlt_face_render(ctx->face, &glyph, id, ch, len, cwidth);
+ if (r < 0)
+ return r;
+
+ cairo_set_source_rgb(cr,
+ attr->fr / (double)256,
+ attr->fg / (double)256,
+ attr->fb / (double)256);
+ cairo_mask_surface(cr, glyph->cr_surface,
+ posx * glyph->width,
+ posy * glyph->height);
+ }
+
+ return 0;
+}
+
+static void wlt_render_debug(struct wlt_draw_ctx *ctx)
+{
+ cairo_t *cr = ctx->cr;
+ double x1, x2, y1, y2;
+
+ if (1)
+ return;
+
+ cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
+ cairo_rectangle(cr, x1, y1, x2, y2);
+ cairo_set_source_rgb(cr, 1, 0, 0);
+ cairo_stroke(cr);
+}
+
+void wlt_render(struct wlt_draw_ctx *ctx)
+{
+ tsm_screen_draw(ctx->screen, NULL, wlt_render_cell, NULL, ctx);
+ wlt_render_debug(ctx);
+}
diff --git a/src/wlterm.c b/src/wlterm.c
new file mode 100644
index 0000000..42d2482
--- /dev/null
+++ b/src/wlterm.c
@@ -0,0 +1,527 @@
+/*
+ * wlterm - Wayland Terminal
+ *
+ * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
+ *
+ * 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.
+ */
+
+/*
+ * wlterm - Wayland Terminal
+ * This is a rewrite of wlterm using GTK. It's meant to be simple and small and
+ * serve as example how to use GTK+tsm+pango to write terminal emulators.
+ */
+
+#include <cairo.h>
+#include <errno.h>
+#include <gtk/gtk.h>
+#include <libtsm.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <xkbcommon/xkbcommon.h>
+#include "shl_pty.h"
+#include "wlterm.h"
+
+struct term {
+ GtkWidget *window;
+ GtkWidget *tarea;
+ struct wlt_font *font;
+ struct tsm_screen *screen;
+ struct tsm_vte *vte;
+
+ struct shl_pty *pty;
+ int pty_bridge;
+ GIOChannel *bridge_chan;
+ guint bridge_src;
+ GSource *pty_idle;
+ guint pty_idle_src;
+ guint child_src;
+
+ struct wlt_face *face;
+ unsigned int cell_width;
+ unsigned int cell_height;
+ unsigned int width;
+ unsigned int height;
+ unsigned int columns;
+ unsigned int rows;
+
+ unsigned int adjust_size : 1;
+ unsigned int initialized : 1;
+ unsigned int exited : 1;
+};
+
+static void err(const char *format, ...)
+{
+ va_list list;
+
+ fprintf(stderr, "ERROR: ");
+
+ va_start(list, format);
+ vfprintf(stderr, format, list);
+ va_end(list);
+
+ fprintf(stderr, "\n");
+}
+
+static void info(const char *format, ...)
+{
+ va_list list;
+
+ fprintf(stderr, "INFO: ");
+
+ va_start(list, format);
+ vfprintf(stderr, format, list);
+ va_end(list);
+
+ fprintf(stderr, "\n");
+}
+
+static const char *sev2str_table[] = {
+ "FATAL",
+ "ALERT",
+ "CRITICAL",
+ "ERROR",
+ "WARNING",
+ "NOTICE",
+ "INFO",
+ "DEBUG"
+};
+
+static const char *sev2str(unsigned int sev)
+{
+ if (sev > 7)
+ return "DEBUG";
+
+ return sev2str_table[sev];
+}
+
+static void log_tsm(void *data, const char *file, int line, const char *fn,
+ const char *subs, unsigned int sev, const char *format,
+ va_list args)
+{
+ fprintf(stderr, "%s: %s: ", sev2str(sev), subs);
+ vfprintf(stderr, format, args);
+ fprintf(stderr, "\n");
+}
+
+static void __attribute__((noreturn)) term_run_child(struct term *term)
+{
+ char **argv = (char*[]){ "/bin/sh", NULL };
+
+ execve(argv[0], argv, environ);
+ exit(1);
+}
+
+static void term_set_geometry(struct term *term)
+{
+ GdkWindowHints hints;
+ GdkGeometry geometry;
+
+ geometry.width_inc = term->cell_width;
+ geometry.height_inc = term->cell_height;
+ geometry.base_width = geometry.width_inc;
+ geometry.base_height = geometry.height_inc;
+ geometry.min_width = geometry.base_width;
+ geometry.min_height = geometry.base_height;
+
+ hints = GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE;
+ if (term->adjust_size)
+ hints |= GDK_HINT_RESIZE_INC;
+
+ gtk_window_set_geometry_hints(GTK_WINDOW(term->window), term->tarea,
+ &geometry, hints);
+ gtk_widget_queue_resize(term->tarea);
+}
+
+static void term_recalc_cells(struct term *term)
+{
+ term->columns = term->width / term->cell_width;
+ term->rows = term->height / term->cell_height;
+
+ if (!term->columns)
+ term->columns = 1;
+ if (!term->rows)
+ term->rows = 1;
+}
+
+static int term_change_font(struct term *term)
+{
+ struct wlt_face *old;
+ int r;
+
+ old = term->face;
+ r = wlt_face_new(&term->face, term->font, "monospace",
+ WLT_FACE_DONT_CARE, 0, 0);
+ if (r < 0)
+ return r;
+
+ wlt_face_unref(old);
+
+ term->cell_width = wlt_face_get_width(term->face);
+ term->cell_height = wlt_face_get_height(term->face);
+
+ return 0;
+}
+
+static void term_notify_resize(struct term *term)
+{
+ int r;
+
+ r = tsm_screen_resize(term->screen, term->columns, term->rows);
+ if (r < 0)
+ err("cannot resize TSM screen (%d)", r);
+
+ r = shl_pty_resize(term->pty, term->columns, term->rows);
+ if (r < 0)
+ err("cannot resize pty (%d)", r);
+}
+
+static void term_read_cb(struct shl_pty *pty, char *u8, size_t len, void *data)
+{
+ struct term *term = data;
+
+ tsm_vte_input(term->vte, u8, len);
+ gtk_widget_queue_draw(term->tarea);
+}
+
+static void term_child_cb(GPid pid, gint status, gpointer data)
+{
+ struct term *term = data;
+
+ g_spawn_close_pid(pid);
+ term->child_src = 0;
+ gtk_main_quit();
+}
+
+static gboolean term_configure_cb(GtkWidget *widget, GdkEvent *ev,
+ gpointer data)
+{
+ struct term *term = data;
+ GdkEventConfigure *cev = (void*)ev;
+ GdkWindow *wnd;
+ GdkWindowState st;
+ GdkEventMask mask;
+ bool new_adjust_size = term->adjust_size;
+ int r, pid;
+
+ /* Initial configure-event, setup fonts, pty, etc. */
+ if (!term->initialized) {
+ r = term_change_font(term);
+ if (r < 0) {
+ err("cannot load font (%d)", r);
+ gtk_main_quit();
+ return TRUE;
+ }
+
+ /* compute cell size */
+ term->width = cev->width;
+ term->height = cev->height;
+ term_recalc_cells(term);
+
+ term_set_geometry(term);
+ gtk_widget_queue_draw(term->tarea);
+
+ r = shl_pty_open(&term->pty, term_read_cb, term,
+ term->columns, term->rows);
+ if (r < 0) {
+ err("cannot spawn pty (%d)", r);
+ gtk_main_quit();
+ return TRUE;
+ } else if (!r) {
+ /* child */
+ term_run_child(term);
+ exit(1);
+ }
+
+ r = shl_pty_bridge_add(term->pty_bridge, term->pty);
+ if (r < 0) {
+ err("cannot add pty to bridge (%d)", r);
+ shl_pty_close(term->pty);
+ gtk_main_quit();
+ return TRUE;
+ }
+
+ pid = shl_pty_get_child(term->pty);
+ term->child_src = g_child_watch_add(pid, term_child_cb, term);
+
+ wnd = gtk_widget_get_window(term->window);
+ mask = gdk_window_get_events(wnd);
+ gdk_window_set_events(wnd, mask | GDK_KEY_PRESS_MASK);
+
+ term->initialized = 1;
+ term_notify_resize(term);
+ } else {
+ /* compute cell size */
+ term->width = cev->width;
+ term->height = cev->height;
+ term_recalc_cells(term);
+ term_notify_resize(term);
+ }
+
+ /* adjust geometry */
+ wnd = gtk_widget_get_window(term->window);
+ st = gdk_window_get_state(wnd);
+
+ if (st & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN)) {
+ if (term->adjust_size)
+ new_adjust_size = false;
+ } else if (!term->adjust_size) {
+ new_adjust_size = true;
+ }
+
+ if (new_adjust_size != term->adjust_size) {
+ term->adjust_size = new_adjust_size;
+ term_set_geometry(term);
+ }
+
+ gtk_widget_queue_draw(term->tarea);
+
+ return TRUE;
+}
+
+static gboolean term_redraw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
+{
+ struct term *term = data;
+ struct wlt_draw_ctx ctx;
+ int64_t start, end;
+
+ if (!term->initialized)
+ return FALSE;
+
+ start = g_get_monotonic_time();
+
+ ctx.cr = cr;
+ ctx.face = term->face;
+ ctx.cell_width = term->cell_width;
+ ctx.cell_height = term->cell_height;
+ ctx.screen = term->screen;
+ wlt_render(&ctx);
+
+ end = g_get_monotonic_time();
+ if (0)
+ info("draw: %lldms", (end - start) / 1000);
+
+ return FALSE;
+}
+
+static void term_destroy_cb(GtkWidget *widget, gpointer data)
+{
+ struct term *term = data;
+
+ term->window = NULL;
+ term->tarea = NULL;
+
+ if (!term->exited)
+ gtk_main_quit();
+}
+
+static gboolean term_key_cb(GtkWidget *widget, GdkEvent *ev, gpointer data)
+{
+ struct term *term = data;
+ GdkEventKey *e = (void*)ev;
+ unsigned int mods = 0;
+
+ if (e->type != GDK_KEY_PRESS)
+ return FALSE;
+
+ if (e->state & GDK_SHIFT_MASK)
+ mods |= TSM_SHIFT_MASK;
+ if (e->state & GDK_LOCK_MASK)
+ mods |= TSM_LOCK_MASK;
+ if (e->state & GDK_CONTROL_MASK)
+ mods |= TSM_CONTROL_MASK;
+ if (e->state & GDK_MOD1_MASK)
+ mods |= TSM_ALT_MASK;
+ if (e->state & GDK_MOD4_MASK)
+ mods |= TSM_LOGO_MASK;
+
+ return tsm_vte_handle_keyboard(term->vte, e->keyval, 0,
+ mods, xkb_keysym_to_utf32(e->keyval));
+}
+
+static gboolean term_pty_idle_cb(gpointer data)
+{
+ struct term *term = data;
+
+ shl_pty_dispatch(term->pty);
+ term->pty_idle_src = 0;
+
+ return FALSE;
+}
+
+static void term_write_cb(struct tsm_vte *vte, const char *u8, size_t len,
+ void *data)
+{
+ struct term *term = data;
+ int r;
+
+ if (!term->initialized)
+ return;
+
+ r = shl_pty_write(term->pty, u8, len);
+ if (r < 0)
+ err("OOM in pty-write (%d)", r);
+
+ if (!term->pty_idle_src)
+ term->pty_idle_src = g_idle_add(term_pty_idle_cb, term);
+}
+
+static gboolean term_bridge_cb(GIOChannel *chan, GIOCondition cond,
+ gpointer data)
+{
+ struct term *term = data;
+ int r;
+
+ r = shl_pty_bridge_dispatch(term->pty_bridge, 0);
+ if (r < 0)
+ err("bridge dispatch failed (%d)", r);
+
+ return TRUE;
+}
+
+static void term_free(struct term *term)
+{
+ if (term->pty) {
+ shl_pty_bridge_remove(term->pty_bridge, term->pty);
+ shl_pty_close(term->pty);
+ shl_pty_unref(term->pty);
+ }
+ if (term->child_src)
+ g_source_remove(term->child_src);
+ if (term->pty_idle_src)
+ g_source_remove(term->pty_idle_src);
+ g_source_unref(term->pty_idle);
+ g_source_remove(term->bridge_src);
+ g_io_channel_unref(term->bridge_chan);
+ shl_pty_bridge_free(term->pty_bridge);
+ tsm_vte_unref(term->vte);
+ tsm_screen_unref(term->screen);
+ wlt_face_unref(term->face);
+ wlt_font_unref(term->font);
+ if (term->window)
+ gtk_widget_destroy(term->window);
+ free(term);
+}
+
+static int term_new(struct term **out)
+{
+ struct term *term;
+ int r;
+
+ term = calloc(1, sizeof(*term));
+ if (!term)
+ return -ENOMEM;
+ term->adjust_size = 1;
+
+ r = wlt_font_new(&term->font);
+ if (r < 0)
+ goto err_free;
+
+ r = tsm_screen_new(&term->screen, log_tsm, term);
+ if (r < 0)
+ goto err_font;
+
+ r = tsm_vte_new(&term->vte, term->screen, term_write_cb, term,
+ log_tsm, term);
+ if (r < 0)
+ goto err_screen;
+
+ term->pty_bridge = shl_pty_bridge_new();
+ if (term->pty_bridge < 0) {
+ r = term->pty_bridge;
+ goto err_vte;
+ }
+
+ term->bridge_chan = g_io_channel_unix_new(term->pty_bridge);
+ term->bridge_src = g_io_add_watch(term->bridge_chan,
+ G_IO_IN,
+ term_bridge_cb, term);
+
+ term->pty_idle = g_idle_source_new();
+
+ term->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_title(GTK_WINDOW(term->window), "Terminal");
+ gtk_window_set_has_resize_grip(GTK_WINDOW(term->window), FALSE);
+ gtk_window_set_decorated(GTK_WINDOW(term->window), FALSE);
+ g_signal_connect(term->window, "destroy", G_CALLBACK(term_destroy_cb),
+ term);
+ g_signal_connect(term->window, "key-press-event",
+ G_CALLBACK(term_key_cb), term);
+
+ term->tarea = gtk_drawing_area_new();
+ g_signal_connect(term->tarea, "configure-event",
+ G_CALLBACK(term_configure_cb), term);
+ g_signal_connect(term->tarea, "draw",
+ G_CALLBACK(term_redraw_cb), term);
+ gtk_container_add(GTK_CONTAINER(term->window), term->tarea);
+
+ *out = term;
+ return 0;
+
+err_vte:
+ tsm_vte_unref(term->vte);
+err_screen:
+ tsm_screen_unref(term->screen);
+err_font:
+ wlt_font_unref(term->font);
+err_free:
+ free(term);
+ return r;
+}
+
+static void term_show(struct term *term)
+{
+ if (term->window)
+ gtk_widget_show_all(term->window);
+}
+
+static void term_hide(struct term *term)
+{
+ if (term->window)
+ gtk_widget_hide(term->window);
+}
+
+int main(int argc, char **argv)
+{
+ struct term *term;
+ int r;
+
+ gtk_init(&argc, &argv);
+
+ r = term_new(&term);
+ if (r < 0)
+ goto error;
+
+ term_show(term);
+ gtk_main();
+ term->exited = 1;
+ term_hide(term);
+ term_free(term);
+
+ return 0;
+
+error:
+ errno = -r;
+ err("cannot initialize terminal: %m");
+ return -r;
+}
diff --git a/src/wlterm.h b/src/wlterm.h
new file mode 100644
index 0000000..f4124e7
--- /dev/null
+++ b/src/wlterm.h
@@ -0,0 +1,93 @@
+/*
+ * wlterm - Wayland Terminal
+ *
+ * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
+ *
+ * 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.
+ */
+
+/*
+ * TODO
+ */
+
+#ifndef WLT_WLTERM_H
+#define WLT_WLTERM_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+/* types */
+
+struct wlt_font;
+struct wlt_face;
+
+/* fonts */
+
+enum wlt_glyph_format {
+ WLT_GLYPH_INVALID,
+ WLT_GLYPH_A1,
+ WLT_GLYPH_A8,
+ WLT_GLYPH_RGB24,
+};
+
+struct wlt_glyph {
+ unsigned long id;
+
+ unsigned int cwidth;
+ unsigned int format;
+ unsigned int width;
+ int stride;
+ unsigned int height;
+ uint8_t *buffer;
+ void *cr_surface;
+};
+
+#define WLT_FACE_DONT_CARE (-1)
+
+int wlt_font_new(struct wlt_font **out);
+void wlt_font_ref(struct wlt_font *font);
+void wlt_font_unref(struct wlt_font *font);
+
+int wlt_face_new(struct wlt_face **out, struct wlt_font *font,
+ const char *desc_str, int desc_size, int desc_bold,
+ int desc_italic);
+void wlt_face_ref(struct wlt_face *face);
+void wlt_face_unref(struct wlt_face *face);
+unsigned int wlt_face_get_width(struct wlt_face *face);
+unsigned int wlt_face_get_height(struct wlt_face *face);
+
+int wlt_face_render(struct wlt_face *face, struct wlt_glyph **out,
+ unsigned long id, const uint32_t *ch, size_t len,
+ size_t cwidth);
+
+/* rendering */
+
+struct wlt_draw_ctx {
+ cairo_t *cr;
+ struct wlt_face *face;
+ unsigned int cell_width;
+ unsigned int cell_height;
+ struct tsm_screen *screen;
+};
+
+void wlt_render(struct wlt_draw_ctx *ctx);
+
+#endif /* WLT_WLTERM_H */