Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4347 lines
93 KiB

Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
11 years ago
11 years ago
13 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
10 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
10 years ago
10 years ago
14 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
14 years ago
10 years ago
14 years ago
13 years ago
10 years ago
14 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
10 years ago
13 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
9 years ago
14 years ago
14 years ago
  1. /* See LICENSE for license details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <locale.h>
  7. #include <pwd.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <stdint.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/select.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <libgen.h>
  23. #include <X11/Xatom.h>
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/cursorfont.h>
  27. #include <X11/keysym.h>
  28. #include <X11/Xft/Xft.h>
  29. #include <X11/XKBlib.h>
  30. #include <fontconfig/fontconfig.h>
  31. #include <wchar.h>
  32. #include "arg.h"
  33. char *argv0;
  34. #define Glyph Glyph_
  35. #define Font Font_
  36. #if defined(__linux)
  37. #include <pty.h>
  38. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  39. #include <util.h>
  40. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  41. #include <libutil.h>
  42. #endif
  43. /* XEMBED messages */
  44. #define XEMBED_FOCUS_IN 4
  45. #define XEMBED_FOCUS_OUT 5
  46. /* Arbitrary sizes */
  47. #define UTF_INVALID 0xFFFD
  48. #define UTF_SIZ 4
  49. #define ESC_BUF_SIZ (128*UTF_SIZ)
  50. #define ESC_ARG_SIZ 16
  51. #define STR_BUF_SIZ ESC_BUF_SIZ
  52. #define STR_ARG_SIZ ESC_ARG_SIZ
  53. #define XK_ANY_MOD UINT_MAX
  54. #define XK_NO_MOD 0
  55. #define XK_SWITCH_MOD (1<<13)
  56. /* macros */
  57. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  58. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  59. #define LEN(a) (sizeof(a) / sizeof(a)[0])
  60. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  61. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  62. #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
  63. #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
  64. #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
  65. #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
  66. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  67. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
  68. (a).bg != (b).bg)
  69. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  70. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
  71. (t1.tv_nsec-t2.tv_nsec)/1E6)
  72. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  73. #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
  74. #define IS_TRUECOL(x) (1 << 24 & (x))
  75. #define TRUERED(x) (((x) & 0xff0000) >> 8)
  76. #define TRUEGREEN(x) (((x) & 0xff00))
  77. #define TRUEBLUE(x) (((x) & 0xff) << 8)
  78. enum glyph_attribute {
  79. ATTR_NULL = 0,
  80. ATTR_BOLD = 1 << 0,
  81. ATTR_FAINT = 1 << 1,
  82. ATTR_ITALIC = 1 << 2,
  83. ATTR_UNDERLINE = 1 << 3,
  84. ATTR_BLINK = 1 << 4,
  85. ATTR_REVERSE = 1 << 5,
  86. ATTR_INVISIBLE = 1 << 6,
  87. ATTR_STRUCK = 1 << 7,
  88. ATTR_WRAP = 1 << 8,
  89. ATTR_WIDE = 1 << 9,
  90. ATTR_WDUMMY = 1 << 10,
  91. ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
  92. };
  93. enum cursor_movement {
  94. CURSOR_SAVE,
  95. CURSOR_LOAD
  96. };
  97. enum cursor_state {
  98. CURSOR_DEFAULT = 0,
  99. CURSOR_WRAPNEXT = 1,
  100. CURSOR_ORIGIN = 2
  101. };
  102. enum term_mode {
  103. MODE_WRAP = 1 << 0,
  104. MODE_INSERT = 1 << 1,
  105. MODE_APPKEYPAD = 1 << 2,
  106. MODE_ALTSCREEN = 1 << 3,
  107. MODE_CRLF = 1 << 4,
  108. MODE_MOUSEBTN = 1 << 5,
  109. MODE_MOUSEMOTION = 1 << 6,
  110. MODE_REVERSE = 1 << 7,
  111. MODE_KBDLOCK = 1 << 8,
  112. MODE_HIDE = 1 << 9,
  113. MODE_ECHO = 1 << 10,
  114. MODE_APPCURSOR = 1 << 11,
  115. MODE_MOUSESGR = 1 << 12,
  116. MODE_8BIT = 1 << 13,
  117. MODE_BLINK = 1 << 14,
  118. MODE_FBLINK = 1 << 15,
  119. MODE_FOCUS = 1 << 16,
  120. MODE_MOUSEX10 = 1 << 17,
  121. MODE_MOUSEMANY = 1 << 18,
  122. MODE_BRCKTPASTE = 1 << 19,
  123. MODE_PRINT = 1 << 20,
  124. MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
  125. |MODE_MOUSEMANY,
  126. };
  127. enum charset {
  128. CS_GRAPHIC0,
  129. CS_GRAPHIC1,
  130. CS_UK,
  131. CS_USA,
  132. CS_MULTI,
  133. CS_GER,
  134. CS_FIN
  135. };
  136. enum escape_state {
  137. ESC_START = 1,
  138. ESC_CSI = 2,
  139. ESC_STR = 4, /* DCS, OSC, PM, APC */
  140. ESC_ALTCHARSET = 8,
  141. ESC_STR_END = 16, /* a final string was encountered */
  142. ESC_TEST = 32, /* Enter in test mode */
  143. };
  144. enum window_state {
  145. WIN_VISIBLE = 1,
  146. WIN_FOCUSED = 2
  147. };
  148. enum selection_mode {
  149. SEL_IDLE = 0,
  150. SEL_EMPTY = 1,
  151. SEL_READY = 2
  152. };
  153. enum selection_type {
  154. SEL_REGULAR = 1,
  155. SEL_RECTANGULAR = 2
  156. };
  157. enum selection_snap {
  158. SNAP_WORD = 1,
  159. SNAP_LINE = 2
  160. };
  161. typedef unsigned char uchar;
  162. typedef unsigned int uint;
  163. typedef unsigned long ulong;
  164. typedef unsigned short ushort;
  165. typedef uint_least32_t Rune;
  166. typedef XftDraw *Draw;
  167. typedef XftColor Color;
  168. typedef struct {
  169. Rune u; /* character code */
  170. ushort mode; /* attribute flags */
  171. uint32_t fg; /* foreground */
  172. uint32_t bg; /* background */
  173. } Glyph;
  174. typedef Glyph *Line;
  175. typedef struct {
  176. Glyph attr; /* current char attributes */
  177. int x;
  178. int y;
  179. char state;
  180. } TCursor;
  181. /* CSI Escape sequence structs */
  182. /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
  183. typedef struct {
  184. char buf[ESC_BUF_SIZ]; /* raw string */
  185. int len; /* raw string length */
  186. char priv;
  187. int arg[ESC_ARG_SIZ];
  188. int narg; /* nb of args */
  189. char mode[2];
  190. } CSIEscape;
  191. /* STR Escape sequence structs */
  192. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  193. typedef struct {
  194. char type; /* ESC type ... */
  195. char buf[STR_BUF_SIZ]; /* raw string */
  196. int len; /* raw string length */
  197. char *args[STR_ARG_SIZ];
  198. int narg; /* nb of args */
  199. } STREscape;
  200. /* Internal representation of the screen */
  201. typedef struct {
  202. int row; /* nb row */
  203. int col; /* nb col */
  204. Line *line; /* screen */
  205. Line *alt; /* alternate screen */
  206. int *dirty; /* dirtyness of lines */
  207. XftGlyphFontSpec *specbuf; /* font spec buffer used for rendering */
  208. TCursor c; /* cursor */
  209. int top; /* top scroll limit */
  210. int bot; /* bottom scroll limit */
  211. int mode; /* terminal mode flags */
  212. int esc; /* escape state flags */
  213. char trantbl[4]; /* charset table translation */
  214. int charset; /* current charset */
  215. int icharset; /* selected charset for sequence */
  216. int numlock; /* lock numbers in keyboard */
  217. int *tabs;
  218. } Term;
  219. /* Purely graphic info */
  220. typedef struct {
  221. Display *dpy;
  222. Colormap cmap;
  223. Window win;
  224. Drawable buf;
  225. Atom xembed, wmdeletewin, netwmname, netwmpid;
  226. XIM xim;
  227. XIC xic;
  228. Draw draw;
  229. Visual *vis;
  230. XSetWindowAttributes attrs;
  231. int scr;
  232. int isfixed; /* is fixed geometry? */
  233. int l, t; /* left and top offset */
  234. int gm; /* geometry mask */
  235. int tw, th; /* tty width and height */
  236. int w, h; /* window width and height */
  237. int ch; /* char height */
  238. int cw; /* char width */
  239. char state; /* focus, redraw, visible */
  240. int cursor; /* cursor style */
  241. } XWindow;
  242. typedef struct {
  243. uint b;
  244. uint mask;
  245. char *s;
  246. } Mousekey;
  247. typedef struct {
  248. KeySym k;
  249. uint mask;
  250. char *s;
  251. /* three valued logic variables: 0 indifferent, 1 on, -1 off */
  252. signed char appkey; /* application keypad */
  253. signed char appcursor; /* application cursor */
  254. signed char crlf; /* crlf mode */
  255. } Key;
  256. typedef struct {
  257. int mode;
  258. int type;
  259. int snap;
  260. /*
  261. * Selection variables:
  262. * nb normalized coordinates of the beginning of the selection
  263. * ne normalized coordinates of the end of the selection
  264. * ob original coordinates of the beginning of the selection
  265. * oe original coordinates of the end of the selection
  266. */
  267. struct {
  268. int x, y;
  269. } nb, ne, ob, oe;
  270. char *primary, *clipboard;
  271. Atom xtarget;
  272. int alt;
  273. struct timespec tclick1;
  274. struct timespec tclick2;
  275. } Selection;
  276. typedef union {
  277. int i;
  278. uint ui;
  279. float f;
  280. const void *v;
  281. } Arg;
  282. typedef struct {
  283. uint mod;
  284. KeySym keysym;
  285. void (*func)(const Arg *);
  286. const Arg arg;
  287. } Shortcut;
  288. /* function definitions used in config.h */
  289. static void clipcopy(const Arg *);
  290. static void clippaste(const Arg *);
  291. static void numlock(const Arg *);
  292. static void selpaste(const Arg *);
  293. static void xzoom(const Arg *);
  294. static void xzoomabs(const Arg *);
  295. static void xzoomreset(const Arg *);
  296. static void printsel(const Arg *);
  297. static void printscreen(const Arg *) ;
  298. static void toggleprinter(const Arg *);
  299. /* Config.h for applying patches and the configuration. */
  300. #include "config.h"
  301. /* Font structure */
  302. typedef struct {
  303. int height;
  304. int width;
  305. int ascent;
  306. int descent;
  307. short lbearing;
  308. short rbearing;
  309. XftFont *match;
  310. FcFontSet *set;
  311. FcPattern *pattern;
  312. } Font;
  313. /* Drawing Context */
  314. typedef struct {
  315. Color col[MAX(LEN(colorname), 256)];
  316. Font font, bfont, ifont, ibfont;
  317. GC gc;
  318. } DC;
  319. static void die(const char *, ...);
  320. static void draw(void);
  321. static void redraw(void);
  322. static void drawregion(int, int, int, int);
  323. static void execsh(void);
  324. static void stty(void);
  325. static void sigchld(int);
  326. static void run(void);
  327. static void csidump(void);
  328. static void csihandle(void);
  329. static void csiparse(void);
  330. static void csireset(void);
  331. static int eschandle(uchar);
  332. static void strdump(void);
  333. static void strhandle(void);
  334. static void strparse(void);
  335. static void strreset(void);
  336. static int tattrset(int);
  337. static void tprinter(char *, size_t);
  338. static void tdumpsel(void);
  339. static void tdumpline(int);
  340. static void tdump(void);
  341. static void tclearregion(int, int, int, int);
  342. static void tcursor(int);
  343. static void tdeletechar(int);
  344. static void tdeleteline(int);
  345. static void tinsertblank(int);
  346. static void tinsertblankline(int);
  347. static int tlinelen(int);
  348. static void tmoveto(int, int);
  349. static void tmoveato(int, int);
  350. static void tnew(int, int);
  351. static void tnewline(int);
  352. static void tputtab(int);
  353. static void tputc(Rune);
  354. static void treset(void);
  355. static void tresize(int, int);
  356. static void tscrollup(int, int);
  357. static void tscrolldown(int, int);
  358. static void tsetattr(int *, int);
  359. static void tsetchar(Rune, Glyph *, int, int);
  360. static void tsetscroll(int, int);
  361. static void tswapscreen(void);
  362. static void tsetdirt(int, int);
  363. static void tsetdirtattr(int);
  364. static void tsetmode(int, int, int *, int);
  365. static void tfulldirt(void);
  366. static void techo(Rune);
  367. static void tcontrolcode(uchar );
  368. static void tdectest(char );
  369. static int32_t tdefcolor(int *, int *, int);
  370. static void tdeftran(char);
  371. static inline int match(uint, uint);
  372. static void ttynew(void);
  373. static void ttyread(void);
  374. static void ttyresize(void);
  375. static void ttysend(char *, size_t);
  376. static void ttywrite(const char *, size_t);
  377. static void tstrsequence(uchar);
  378. static inline ushort sixd_to_16bit(int);
  379. static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
  380. static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
  381. static void xdrawglyph(Glyph, int, int);
  382. static void xhints(void);
  383. static void xclear(int, int, int, int);
  384. static void xdrawcursor(void);
  385. static void xinit(void);
  386. static void xloadcols(void);
  387. static int xsetcolorname(int, const char *);
  388. static int xgeommasktogravity(int);
  389. static int xloadfont(Font *, FcPattern *);
  390. static void xloadfonts(char *, double);
  391. static void xsettitle(char *);
  392. static void xresettitle(void);
  393. static void xsetpointermotion(int);
  394. static void xseturgency(int);
  395. static void xsetsel(char *, Time);
  396. static void xtermclear(int, int, int, int);
  397. static void xunloadfont(Font *);
  398. static void xunloadfonts(void);
  399. static void xresize(int, int);
  400. static void expose(XEvent *);
  401. static void visibility(XEvent *);
  402. static void unmap(XEvent *);
  403. static char *kmap(KeySym, uint);
  404. static void kpress(XEvent *);
  405. static void cmessage(XEvent *);
  406. static void cresize(int, int);
  407. static void resize(XEvent *);
  408. static void focus(XEvent *);
  409. static void brelease(XEvent *);
  410. static void bpress(XEvent *);
  411. static void bmotion(XEvent *);
  412. static void propnotify(XEvent *);
  413. static void selnotify(XEvent *);
  414. static void selclear(XEvent *);
  415. static void selrequest(XEvent *);
  416. static void selinit(void);
  417. static void selnormalize(void);
  418. static inline int selected(int, int);
  419. static char *getsel(void);
  420. static void selcopy(Time);
  421. static void selscroll(int, int);
  422. static void selsnap(int *, int *, int);
  423. static int x2col(int);
  424. static int y2row(int);
  425. static void getbuttoninfo(XEvent *);
  426. static void mousereport(XEvent *);
  427. static size_t utf8decode(char *, Rune *, size_t);
  428. static Rune utf8decodebyte(char, size_t *);
  429. static size_t utf8encode(Rune, char *);
  430. static char utf8encodebyte(Rune, size_t);
  431. static char *utf8strchr(char *s, Rune u);
  432. static size_t utf8validate(Rune *, size_t);
  433. static ssize_t xwrite(int, const char *, size_t);
  434. static void *xmalloc(size_t);
  435. static void *xrealloc(void *, size_t);
  436. static char *xstrdup(char *);
  437. static void usage(void);
  438. static void (*handler[LASTEvent])(XEvent *) = {
  439. [KeyPress] = kpress,
  440. [ClientMessage] = cmessage,
  441. [ConfigureNotify] = resize,
  442. [VisibilityNotify] = visibility,
  443. [UnmapNotify] = unmap,
  444. [Expose] = expose,
  445. [FocusIn] = focus,
  446. [FocusOut] = focus,
  447. [MotionNotify] = bmotion,
  448. [ButtonPress] = bpress,
  449. [ButtonRelease] = brelease,
  450. /*
  451. * Uncomment if you want the selection to disappear when you select something
  452. * different in another window.
  453. */
  454. /* [SelectionClear] = selclear, */
  455. [SelectionNotify] = selnotify,
  456. /*
  457. * PropertyNotify is only turned on when there is some INCR transfer happening
  458. * for the selection retrieval.
  459. */
  460. [PropertyNotify] = propnotify,
  461. [SelectionRequest] = selrequest,
  462. };
  463. /* Globals */
  464. static DC dc;
  465. static XWindow xw;
  466. static Term term;
  467. static CSIEscape csiescseq;
  468. static STREscape strescseq;
  469. static int cmdfd;
  470. static pid_t pid;
  471. static Selection sel;
  472. static int iofd = 1;
  473. static char **opt_cmd = NULL;
  474. static char *opt_io = NULL;
  475. static char *opt_title = NULL;
  476. static char *opt_embed = NULL;
  477. static char *opt_class = NULL;
  478. static char *opt_font = NULL;
  479. static char *opt_line = NULL;
  480. static int oldbutton = 3; /* button event on startup: 3 = release */
  481. static char *usedfont = NULL;
  482. static double usedfontsize = 0;
  483. static double defaultfontsize = 0;
  484. static uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  485. static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  486. static Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
  487. static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  488. /* Font Ring Cache */
  489. enum {
  490. FRC_NORMAL,
  491. FRC_ITALIC,
  492. FRC_BOLD,
  493. FRC_ITALICBOLD
  494. };
  495. typedef struct {
  496. XftFont *font;
  497. int flags;
  498. Rune unicodep;
  499. } Fontcache;
  500. /* Fontcache is an array now. A new font will be appended to the array. */
  501. static Fontcache frc[16];
  502. static int frclen = 0;
  503. ssize_t
  504. xwrite(int fd, const char *s, size_t len)
  505. {
  506. size_t aux = len;
  507. ssize_t r;
  508. while (len > 0) {
  509. r = write(fd, s, len);
  510. if (r < 0)
  511. return r;
  512. len -= r;
  513. s += r;
  514. }
  515. return aux;
  516. }
  517. void *
  518. xmalloc(size_t len)
  519. {
  520. void *p = malloc(len);
  521. if (!p)
  522. die("Out of memory\n");
  523. return p;
  524. }
  525. void *
  526. xrealloc(void *p, size_t len)
  527. {
  528. if ((p = realloc(p, len)) == NULL)
  529. die("Out of memory\n");
  530. return p;
  531. }
  532. char *
  533. xstrdup(char *s)
  534. {
  535. if ((s = strdup(s)) == NULL)
  536. die("Out of memory\n");
  537. return s;
  538. }
  539. size_t
  540. utf8decode(char *c, Rune *u, size_t clen)
  541. {
  542. size_t i, j, len, type;
  543. Rune udecoded;
  544. *u = UTF_INVALID;
  545. if (!clen)
  546. return 0;
  547. udecoded = utf8decodebyte(c[0], &len);
  548. if (!BETWEEN(len, 1, UTF_SIZ))
  549. return 1;
  550. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  551. udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
  552. if (type != 0)
  553. return j;
  554. }
  555. if (j < len)
  556. return 0;
  557. *u = udecoded;
  558. utf8validate(u, len);
  559. return len;
  560. }
  561. Rune
  562. utf8decodebyte(char c, size_t *i)
  563. {
  564. for (*i = 0; *i < LEN(utfmask); ++(*i))
  565. if (((uchar)c & utfmask[*i]) == utfbyte[*i])
  566. return (uchar)c & ~utfmask[*i];
  567. return 0;
  568. }
  569. size_t
  570. utf8encode(Rune u, char *c)
  571. {
  572. size_t len, i;
  573. len = utf8validate(&u, 0);
  574. if (len > UTF_SIZ)
  575. return 0;
  576. for (i = len - 1; i != 0; --i) {
  577. c[i] = utf8encodebyte(u, 0);
  578. u >>= 6;
  579. }
  580. c[0] = utf8encodebyte(u, len);
  581. return len;
  582. }
  583. char
  584. utf8encodebyte(Rune u, size_t i)
  585. {
  586. return utfbyte[i] | (u & ~utfmask[i]);
  587. }
  588. char *
  589. utf8strchr(char *s, Rune u)
  590. {
  591. Rune r;
  592. size_t i, j, len;
  593. len = strlen(s);
  594. for (i = 0, j = 0; i < len; i += j) {
  595. if (!(j = utf8decode(&s[i], &r, len - i)))
  596. break;
  597. if (r == u)
  598. return &(s[i]);
  599. }
  600. return NULL;
  601. }
  602. size_t
  603. utf8validate(Rune *u, size_t i)
  604. {
  605. if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
  606. *u = UTF_INVALID;
  607. for (i = 1; *u > utfmax[i]; ++i)
  608. ;
  609. return i;
  610. }
  611. void
  612. selinit(void)
  613. {
  614. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  615. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  616. sel.mode = SEL_IDLE;
  617. sel.ob.x = -1;
  618. sel.primary = NULL;
  619. sel.clipboard = NULL;
  620. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  621. if (sel.xtarget == None)
  622. sel.xtarget = XA_STRING;
  623. }
  624. int
  625. x2col(int x)
  626. {
  627. x -= borderpx;
  628. x /= xw.cw;
  629. return LIMIT(x, 0, term.col-1);
  630. }
  631. int
  632. y2row(int y)
  633. {
  634. y -= borderpx;
  635. y /= xw.ch;
  636. return LIMIT(y, 0, term.row-1);
  637. }
  638. int
  639. tlinelen(int y)
  640. {
  641. int i = term.col;
  642. if (term.line[y][i - 1].mode & ATTR_WRAP)
  643. return i;
  644. while (i > 0 && term.line[y][i - 1].u == ' ')
  645. --i;
  646. return i;
  647. }
  648. void
  649. selnormalize(void)
  650. {
  651. int i;
  652. if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
  653. sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
  654. sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
  655. } else {
  656. sel.nb.x = MIN(sel.ob.x, sel.oe.x);
  657. sel.ne.x = MAX(sel.ob.x, sel.oe.x);
  658. }
  659. sel.nb.y = MIN(sel.ob.y, sel.oe.y);
  660. sel.ne.y = MAX(sel.ob.y, sel.oe.y);
  661. selsnap(&sel.nb.x, &sel.nb.y, -1);
  662. selsnap(&sel.ne.x, &sel.ne.y, +1);
  663. /* expand selection over line breaks */
  664. if (sel.type == SEL_RECTANGULAR)
  665. return;
  666. i = tlinelen(sel.nb.y);
  667. if (i < sel.nb.x)
  668. sel.nb.x = i;
  669. if (tlinelen(sel.ne.y) <= sel.ne.x)
  670. sel.ne.x = term.col - 1;
  671. }
  672. int
  673. selected(int x, int y)
  674. {
  675. if (sel.mode == SEL_EMPTY)
  676. return 0;
  677. if (sel.type == SEL_RECTANGULAR)
  678. return BETWEEN(y, sel.nb.y, sel.ne.y)
  679. && BETWEEN(x, sel.nb.x, sel.ne.x);
  680. return BETWEEN(y, sel.nb.y, sel.ne.y)
  681. && (y != sel.nb.y || x >= sel.nb.x)
  682. && (y != sel.ne.y || x <= sel.ne.x);
  683. }
  684. void
  685. selsnap(int *x, int *y, int direction)
  686. {
  687. int newx, newy, xt, yt;
  688. int delim, prevdelim;
  689. Glyph *gp, *prevgp;
  690. switch (sel.snap) {
  691. case SNAP_WORD:
  692. /*
  693. * Snap around if the word wraps around at the end or
  694. * beginning of a line.
  695. */
  696. prevgp = &term.line[*y][*x];
  697. prevdelim = ISDELIM(prevgp->u);
  698. for (;;) {
  699. newx = *x + direction;
  700. newy = *y;
  701. if (!BETWEEN(newx, 0, term.col - 1)) {
  702. newy += direction;
  703. newx = (newx + term.col) % term.col;
  704. if (!BETWEEN(newy, 0, term.row - 1))
  705. break;
  706. if (direction > 0)
  707. yt = *y, xt = *x;
  708. else
  709. yt = newy, xt = newx;
  710. if (!(term.line[yt][xt].mode & ATTR_WRAP))
  711. break;
  712. }
  713. if (newx >= tlinelen(newy))
  714. break;
  715. gp = &term.line[newy][newx];
  716. delim = ISDELIM(gp->u);
  717. if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
  718. || (delim && gp->u != prevgp->u)))
  719. break;
  720. *x = newx;
  721. *y = newy;
  722. prevgp = gp;
  723. prevdelim = delim;
  724. }
  725. break;
  726. case SNAP_LINE:
  727. /*
  728. * Snap around if the the previous line or the current one
  729. * has set ATTR_WRAP at its end. Then the whole next or
  730. * previous line will be selected.
  731. */
  732. *x = (direction < 0) ? 0 : term.col - 1;
  733. if (direction < 0) {
  734. for (; *y > 0; *y += direction) {
  735. if (!(term.line[*y-1][term.col-1].mode
  736. & ATTR_WRAP)) {
  737. break;
  738. }
  739. }
  740. } else if (direction > 0) {
  741. for (; *y < term.row-1; *y += direction) {
  742. if (!(term.line[*y][term.col-1].mode
  743. & ATTR_WRAP)) {
  744. break;
  745. }
  746. }
  747. }
  748. break;
  749. }
  750. }
  751. void
  752. getbuttoninfo(XEvent *e)
  753. {
  754. int type;
  755. uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
  756. sel.alt = IS_SET(MODE_ALTSCREEN);
  757. sel.oe.x = x2col(e->xbutton.x);
  758. sel.oe.y = y2row(e->xbutton.y);
  759. selnormalize();
  760. sel.type = SEL_REGULAR;
  761. for (type = 1; type < LEN(selmasks); ++type) {
  762. if (match(selmasks[type], state)) {
  763. sel.type = type;
  764. break;
  765. }
  766. }
  767. }
  768. void
  769. mousereport(XEvent *e)
  770. {
  771. int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
  772. button = e->xbutton.button, state = e->xbutton.state,
  773. len;
  774. char buf[40];
  775. static int ox, oy;
  776. /* from urxvt */
  777. if (e->xbutton.type == MotionNotify) {
  778. if (x == ox && y == oy)
  779. return;
  780. if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
  781. return;
  782. /* MOUSE_MOTION: no reporting if no button is pressed */
  783. if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
  784. return;
  785. button = oldbutton + 32;
  786. ox = x;
  787. oy = y;
  788. } else {
  789. if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
  790. button = 3;
  791. } else {
  792. button -= Button1;
  793. if (button >= 3)
  794. button += 64 - 3;
  795. }
  796. if (e->xbutton.type == ButtonPress) {
  797. oldbutton = button;
  798. ox = x;
  799. oy = y;
  800. } else if (e->xbutton.type == ButtonRelease) {
  801. oldbutton = 3;
  802. /* MODE_MOUSEX10: no button release reporting */
  803. if (IS_SET(MODE_MOUSEX10))
  804. return;
  805. if (button == 64 || button == 65)
  806. return;
  807. }
  808. }
  809. if (!IS_SET(MODE_MOUSEX10)) {
  810. button += ((state & ShiftMask ) ? 4 : 0)
  811. + ((state & Mod4Mask ) ? 8 : 0)
  812. + ((state & ControlMask) ? 16 : 0);
  813. }
  814. if (IS_SET(MODE_MOUSESGR)) {
  815. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  816. button, x+1, y+1,
  817. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  818. } else if (x < 223 && y < 223) {
  819. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  820. 32+button, 32+x+1, 32+y+1);
  821. } else {
  822. return;
  823. }
  824. ttywrite(buf, len);
  825. }
  826. void
  827. bpress(XEvent *e)
  828. {
  829. struct timespec now;
  830. Mousekey *mk;
  831. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  832. mousereport(e);
  833. return;
  834. }
  835. for (mk = mshortcuts; mk < mshortcuts + LEN(mshortcuts); mk++) {
  836. if (e->xbutton.button == mk->b
  837. && match(mk->mask, e->xbutton.state)) {
  838. ttysend(mk->s, strlen(mk->s));
  839. return;
  840. }
  841. }
  842. if (e->xbutton.button == Button1) {
  843. clock_gettime(CLOCK_MONOTONIC, &now);
  844. /* Clear previous selection, logically and visually. */
  845. selclear(NULL);
  846. sel.mode = SEL_EMPTY;
  847. sel.type = SEL_REGULAR;
  848. sel.oe.x = sel.ob.x = x2col(e->xbutton.x);
  849. sel.oe.y = sel.ob.y = y2row(e->xbutton.y);
  850. /*
  851. * If the user clicks below predefined timeouts specific
  852. * snapping behaviour is exposed.
  853. */
  854. if (TIMEDIFF(now, sel.tclick2) <= tripleclicktimeout) {
  855. sel.snap = SNAP_LINE;
  856. } else if (TIMEDIFF(now, sel.tclick1) <= doubleclicktimeout) {
  857. sel.snap = SNAP_WORD;
  858. } else {
  859. sel.snap = 0;
  860. }
  861. selnormalize();
  862. if (sel.snap != 0)
  863. sel.mode = SEL_READY;
  864. tsetdirt(sel.nb.y, sel.ne.y);
  865. sel.tclick2 = sel.tclick1;
  866. sel.tclick1 = now;
  867. }
  868. }
  869. char *
  870. getsel(void)
  871. {
  872. char *str, *ptr;
  873. int y, bufsize, lastx, linelen;
  874. Glyph *gp, *last;
  875. if (sel.ob.x == -1)
  876. return NULL;
  877. bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
  878. ptr = str = xmalloc(bufsize);
  879. /* append every set & selected glyph to the selection */
  880. for (y = sel.nb.y; y <= sel.ne.y; y++) {
  881. linelen = tlinelen(y);
  882. if (sel.type == SEL_RECTANGULAR) {
  883. gp = &term.line[y][sel.nb.x];
  884. lastx = sel.ne.x;
  885. } else {
  886. gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
  887. lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
  888. }
  889. last = &term.line[y][MIN(lastx, linelen-1)];
  890. while (last >= gp && last->u == ' ')
  891. --last;
  892. for ( ; gp <= last; ++gp) {
  893. if (gp->mode & ATTR_WDUMMY)
  894. continue;
  895. ptr += utf8encode(gp->u, ptr);
  896. }
  897. /*
  898. * Copy and pasting of line endings is inconsistent
  899. * in the inconsistent terminal and GUI world.
  900. * The best solution seems like to produce '\n' when
  901. * something is copied from st and convert '\n' to
  902. * '\r', when something to be pasted is received by
  903. * st.
  904. * FIXME: Fix the computer world.
  905. */
  906. if ((y < sel.ne.y || lastx >= linelen) && !(last->mode & ATTR_WRAP))
  907. *ptr++ = '\n';
  908. }
  909. *ptr = 0;
  910. return str;
  911. }
  912. void
  913. selcopy(Time t)
  914. {
  915. xsetsel(getsel(), t);
  916. }
  917. void
  918. propnotify(XEvent *e)
  919. {
  920. XPropertyEvent *xpev;
  921. Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  922. xpev = &e->xproperty;
  923. if (xpev->state == PropertyNewValue &&
  924. (xpev->atom == XA_PRIMARY ||
  925. xpev->atom == clipboard)) {
  926. selnotify(e);
  927. }
  928. }
  929. void
  930. selnotify(XEvent *e)
  931. {
  932. ulong nitems, ofs, rem;
  933. int format;
  934. uchar *data, *last, *repl;
  935. Atom type, incratom, property;
  936. incratom = XInternAtom(xw.dpy, "INCR", 0);
  937. ofs = 0;
  938. if (e->type == SelectionNotify) {
  939. property = e->xselection.property;
  940. } else if(e->type == PropertyNotify) {
  941. property = e->xproperty.atom;
  942. } else {
  943. return;
  944. }
  945. if (property == None)
  946. return;
  947. do {
  948. if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
  949. BUFSIZ/4, False, AnyPropertyType,
  950. &type, &format, &nitems, &rem,
  951. &data)) {
  952. fprintf(stderr, "Clipboard allocation failed\n");
  953. return;
  954. }
  955. if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
  956. /*
  957. * If there is some PropertyNotify with no data, then
  958. * this is the signal of the selection owner that all
  959. * data has been transferred. We won't need to receive
  960. * PropertyNotify events anymore.
  961. */
  962. MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
  963. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  964. &xw.attrs);
  965. }
  966. if (type == incratom) {
  967. /*
  968. * Activate the PropertyNotify events so we receive
  969. * when the selection owner does send us the next
  970. * chunk of data.
  971. */
  972. MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
  973. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  974. &xw.attrs);
  975. /*
  976. * Deleting the property is the transfer start signal.
  977. */
  978. XDeleteProperty(xw.dpy, xw.win, (int)property);
  979. continue;
  980. }
  981. /*
  982. * As seen in getsel:
  983. * Line endings are inconsistent in the terminal and GUI world
  984. * copy and pasting. When receiving some selection data,
  985. * replace all '\n' with '\r'.
  986. * FIXME: Fix the computer world.
  987. */
  988. repl = data;
  989. last = data + nitems * format / 8;
  990. while ((repl = memchr(repl, '\n', last - repl))) {
  991. *repl++ = '\r';
  992. }
  993. if (IS_SET(MODE_BRCKTPASTE))
  994. ttywrite("\033[200~", 6);
  995. ttysend((char *)data, nitems * format / 8);
  996. if (IS_SET(MODE_BRCKTPASTE))
  997. ttywrite("\033[201~", 6);
  998. XFree(data);
  999. /* number of 32-bit chunks returned */
  1000. ofs += nitems * format / 32;
  1001. } while (rem > 0);
  1002. /*
  1003. * Deleting the property again tells the selection owner to send the
  1004. * next data chunk in the property.
  1005. */
  1006. if (e->type == PropertyNotify)
  1007. XDeleteProperty(xw.dpy, xw.win, (int)property);
  1008. }
  1009. void
  1010. selpaste(const Arg *dummy)
  1011. {
  1012. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
  1013. xw.win, CurrentTime);
  1014. }
  1015. void
  1016. clipcopy(const Arg *dummy)
  1017. {
  1018. Atom clipboard;
  1019. if (sel.clipboard != NULL)
  1020. free(sel.clipboard);
  1021. if (sel.primary != NULL) {
  1022. sel.clipboard = xstrdup(sel.primary);
  1023. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  1024. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  1025. }
  1026. }
  1027. void
  1028. clippaste(const Arg *dummy)
  1029. {
  1030. Atom clipboard;
  1031. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  1032. XConvertSelection(xw.dpy, clipboard, sel.xtarget, clipboard,
  1033. xw.win, CurrentTime);
  1034. }
  1035. void
  1036. selclear(XEvent *e)
  1037. {
  1038. if (sel.ob.x == -1)
  1039. return;
  1040. sel.mode = SEL_IDLE;
  1041. sel.ob.x = -1;
  1042. tsetdirt(sel.nb.y, sel.ne.y);
  1043. }
  1044. void
  1045. selrequest(XEvent *e)
  1046. {
  1047. XSelectionRequestEvent *xsre;
  1048. XSelectionEvent xev;
  1049. Atom xa_targets, string, clipboard;
  1050. char *seltext;
  1051. xsre = (XSelectionRequestEvent *) e;
  1052. xev.type = SelectionNotify;
  1053. xev.requestor = xsre->requestor;
  1054. xev.selection = xsre->selection;
  1055. xev.target = xsre->target;
  1056. xev.time = xsre->time;
  1057. if (xsre->property == None)
  1058. xsre->property = xsre->target;
  1059. /* reject */
  1060. xev.property = None;
  1061. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  1062. if (xsre->target == xa_targets) {
  1063. /* respond with the supported type */
  1064. string = sel.xtarget;
  1065. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  1066. XA_ATOM, 32, PropModeReplace,
  1067. (uchar *) &string, 1);
  1068. xev.property = xsre->property;
  1069. } else if (xsre->target == sel.xtarget || xsre->target == XA_STRING) {
  1070. /*
  1071. * xith XA_STRING non ascii characters may be incorrect in the
  1072. * requestor. It is not our problem, use utf8.
  1073. */
  1074. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  1075. if (xsre->selection == XA_PRIMARY) {
  1076. seltext = sel.primary;
  1077. } else if (xsre->selection == clipboard) {
  1078. seltext = sel.clipboard;
  1079. } else {
  1080. fprintf(stderr,
  1081. "Unhandled clipboard selection 0x%lx\n",
  1082. xsre->selection);
  1083. return;
  1084. }
  1085. if (seltext != NULL) {
  1086. XChangeProperty(xsre->display, xsre->requestor,
  1087. xsre->property, xsre->target,
  1088. 8, PropModeReplace,
  1089. (uchar *)seltext, strlen(seltext));
  1090. xev.property = xsre->property;
  1091. }
  1092. }
  1093. /* all done, send a notification to the listener */
  1094. if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
  1095. fprintf(stderr, "Error sending SelectionNotify event\n");
  1096. }
  1097. void
  1098. xsetsel(char *str, Time t)
  1099. {
  1100. free(sel.primary);
  1101. sel.primary = str;
  1102. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
  1103. if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
  1104. selclear(0);
  1105. }
  1106. void
  1107. brelease(XEvent *e)
  1108. {
  1109. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  1110. mousereport(e);
  1111. return;
  1112. }
  1113. if (e->xbutton.button == Button2) {
  1114. selpaste(NULL);
  1115. } else if (e->xbutton.button == Button1) {
  1116. if (sel.mode == SEL_READY) {
  1117. getbuttoninfo(e);
  1118. selcopy(e->xbutton.time);
  1119. } else
  1120. selclear(NULL);
  1121. sel.mode = SEL_IDLE;
  1122. tsetdirt(sel.nb.y, sel.ne.y);
  1123. }
  1124. }
  1125. void
  1126. bmotion(XEvent *e)
  1127. {
  1128. int oldey, oldex, oldsby, oldsey;
  1129. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  1130. mousereport(e);
  1131. return;
  1132. }
  1133. if (!sel.mode)
  1134. return;
  1135. sel.mode = SEL_READY;
  1136. oldey = sel.oe.y;
  1137. oldex = sel.oe.x;
  1138. oldsby = sel.nb.y;
  1139. oldsey = sel.ne.y;
  1140. getbuttoninfo(e);
  1141. if (oldey != sel.oe.y || oldex != sel.oe.x)
  1142. tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
  1143. }
  1144. void
  1145. die(const char *errstr, ...)
  1146. {
  1147. va_list ap;
  1148. va_start(ap, errstr);
  1149. vfprintf(stderr, errstr, ap);
  1150. va_end(ap);
  1151. exit(1);
  1152. }
  1153. void
  1154. execsh(void)
  1155. {
  1156. char **args, *sh, *prog;
  1157. const struct passwd *pw;
  1158. char buf[sizeof(long) * 8 + 1];
  1159. errno = 0;
  1160. if ((pw = getpwuid(getuid())) == NULL) {
  1161. if (errno)
  1162. die("getpwuid:%s\n", strerror(errno));
  1163. else
  1164. die("who are you?\n");
  1165. }
  1166. if ((sh = getenv("SHELL")) == NULL)
  1167. sh = (pw->pw_shell[0]) ? pw->pw_shell : shell;
  1168. if (opt_cmd)
  1169. prog = opt_cmd[0];
  1170. else if (utmp)
  1171. prog = utmp;
  1172. else
  1173. prog = sh;
  1174. args = (opt_cmd) ? opt_cmd : (char *[]) {prog, NULL};
  1175. snprintf(buf, sizeof(buf), "%lu", xw.win);
  1176. unsetenv("COLUMNS");
  1177. unsetenv("LINES");
  1178. unsetenv("TERMCAP");
  1179. setenv("LOGNAME", pw->pw_name, 1);
  1180. setenv("USER", pw->pw_name, 1);
  1181. setenv("SHELL", sh, 1);
  1182. setenv("HOME", pw->pw_dir, 1);
  1183. setenv("TERM", termname, 1);
  1184. setenv("WINDOWID", buf, 1);
  1185. signal(SIGCHLD, SIG_DFL);
  1186. signal(SIGHUP, SIG_DFL);
  1187. signal(SIGINT, SIG_DFL);
  1188. signal(SIGQUIT, SIG_DFL);
  1189. signal(SIGTERM, SIG_DFL);
  1190. signal(SIGALRM, SIG_DFL);
  1191. execvp(prog, args);
  1192. _exit(1);
  1193. }
  1194. void
  1195. sigchld(int a)
  1196. {
  1197. int stat;
  1198. pid_t p;
  1199. if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
  1200. die("Waiting for pid %hd failed: %s\n", pid, strerror(errno));
  1201. if (pid != p)
  1202. return;
  1203. if (!WIFEXITED(stat) || WEXITSTATUS(stat))
  1204. die("child finished with error '%d'\n", stat);
  1205. exit(0);
  1206. }
  1207. void
  1208. stty(void)
  1209. {
  1210. char cmd[_POSIX_ARG_MAX], **p, *q, *s;
  1211. size_t n, siz;
  1212. if ((n = strlen(stty_args)) > sizeof(cmd)-1)
  1213. die("incorrect stty parameters\n");
  1214. memcpy(cmd, stty_args, n);
  1215. q = cmd + n;
  1216. siz = sizeof(cmd) - n;
  1217. for (p = opt_cmd; p && (s = *p); ++p) {
  1218. if ((n = strlen(s)) > siz-1)
  1219. die("stty parameter length too long\n");
  1220. *q++ = ' ';
  1221. q = memcpy(q, s, n);
  1222. q += n;
  1223. siz-= n + 1;
  1224. }
  1225. *q = '\0';
  1226. if (system(cmd) != 0)
  1227. perror("Couldn't call stty");
  1228. }
  1229. void
  1230. ttynew(void)
  1231. {
  1232. int m, s;
  1233. struct winsize w = {term.row, term.col, 0, 0};
  1234. if (opt_io) {
  1235. term.mode |= MODE_PRINT;
  1236. iofd = (!strcmp(opt_io, "-")) ?
  1237. 1 : open(opt_io, O_WRONLY | O_CREAT, 0666);
  1238. if (iofd < 0) {
  1239. fprintf(stderr, "Error opening %s:%s\n",
  1240. opt_io, strerror(errno));
  1241. }
  1242. }
  1243. if (opt_line) {
  1244. if ((cmdfd = open(opt_line, O_RDWR)) < 0)
  1245. die("open line failed: %s\n", strerror(errno));
  1246. close(0);
  1247. dup(cmdfd);
  1248. stty();
  1249. return;
  1250. }
  1251. /* seems to work fine on linux, openbsd and freebsd */
  1252. if (openpty(&m, &s, NULL, NULL, &w) < 0)
  1253. die("openpty failed: %s\n", strerror(errno));
  1254. switch (pid = fork()) {
  1255. case -1:
  1256. die("fork failed\n");
  1257. break;
  1258. case 0:
  1259. close(iofd);
  1260. setsid(); /* create a new process group */
  1261. dup2(s, 0);
  1262. dup2(s, 1);
  1263. dup2(s, 2);
  1264. if (ioctl(s, TIOCSCTTY, NULL) < 0)
  1265. die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
  1266. close(s);
  1267. close(m);
  1268. execsh();
  1269. break;
  1270. default:
  1271. close(s);
  1272. cmdfd = m;
  1273. signal(SIGCHLD, sigchld);
  1274. break;
  1275. }
  1276. }
  1277. void
  1278. ttyread(void)
  1279. {
  1280. static char buf[BUFSIZ];
  1281. static int buflen = 0;
  1282. char *ptr;
  1283. int charsize; /* size of utf8 char in bytes */
  1284. Rune unicodep;
  1285. int ret;
  1286. /* append read bytes to unprocessed bytes */
  1287. if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  1288. die("Couldn't read from shell: %s\n", strerror(errno));
  1289. /* process every complete utf8 char */
  1290. buflen += ret;
  1291. ptr = buf;
  1292. while ((charsize = utf8decode(ptr, &unicodep, buflen))) {
  1293. tputc(unicodep);
  1294. ptr += charsize;
  1295. buflen -= charsize;
  1296. }
  1297. /* keep any uncomplete utf8 char for the next call */
  1298. memmove(buf, ptr, buflen);
  1299. }
  1300. void
  1301. ttywrite(const char *s, size_t n)
  1302. {
  1303. fd_set wfd;
  1304. struct timespec tv;
  1305. ssize_t r;
  1306. /*
  1307. * Remember that we are using a pty, which might be a modem line.
  1308. * Writing too much will clog the line. That's why we are doing this
  1309. * dance.
  1310. * FIXME: Migrate the world to Plan 9.
  1311. */
  1312. while (n > 0) {
  1313. FD_ZERO(&wfd);
  1314. FD_SET(cmdfd, &wfd);
  1315. tv.tv_sec = 0;
  1316. tv.tv_nsec = 0;
  1317. /* Check if we can write. */
  1318. if (pselect(cmdfd+1, NULL, &wfd, NULL, &tv, NULL) < 0) {
  1319. if (errno == EINTR)
  1320. continue;
  1321. die("select failed: %s\n", strerror(errno));
  1322. }
  1323. if(!FD_ISSET(cmdfd, &wfd)) {
  1324. /* No, then free some buffer space. */
  1325. ttyread();
  1326. } else {
  1327. /*
  1328. * Only write 256 bytes at maximum. This seems to be a
  1329. * reasonable value for a serial line. Bigger values
  1330. * might clog the I/O.
  1331. */
  1332. r = write(cmdfd, s, (n < 256)? n : 256);
  1333. if (r < 0) {
  1334. die("write error on tty: %s\n",
  1335. strerror(errno));
  1336. }
  1337. if (r < n) {
  1338. /*
  1339. * We weren't able to write out everything.
  1340. * This means the buffer is getting full
  1341. * again. Empty it.
  1342. */
  1343. if (n < 256)
  1344. ttyread();
  1345. n -= r;
  1346. s += r;
  1347. } else {
  1348. /* All bytes have been written. */
  1349. break;
  1350. }
  1351. }
  1352. }
  1353. }
  1354. void
  1355. ttysend(char *s, size_t n)
  1356. {
  1357. int len;
  1358. Rune u;
  1359. ttywrite(s, n);
  1360. if (IS_SET(MODE_ECHO))
  1361. while ((len = utf8decode(s, &u, n)) > 0) {
  1362. techo(u);
  1363. n -= len;
  1364. s += len;
  1365. }
  1366. }
  1367. void
  1368. ttyresize(void)
  1369. {
  1370. struct winsize w;
  1371. w.ws_row = term.row;
  1372. w.ws_col = term.col;
  1373. w.ws_xpixel = xw.tw;
  1374. w.ws_ypixel = xw.th;
  1375. if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  1376. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  1377. }
  1378. int
  1379. tattrset(int attr)
  1380. {
  1381. int i, j;
  1382. for (i = 0; i < term.row-1; i++) {
  1383. for (j = 0; j < term.col-1; j++) {
  1384. if (term.line[i][j].mode & attr)
  1385. return 1;
  1386. }
  1387. }
  1388. return 0;
  1389. }
  1390. void
  1391. tsetdirt(int top, int bot)
  1392. {
  1393. int i;
  1394. LIMIT(top, 0, term.row-1);
  1395. LIMIT(bot, 0, term.row-1);
  1396. for (i = top; i <= bot; i++)
  1397. term.dirty[i] = 1;
  1398. }
  1399. void
  1400. tsetdirtattr(int attr)
  1401. {
  1402. int i, j;
  1403. for (i = 0; i < term.row-1; i++) {
  1404. for (j = 0; j < term.col-1; j++) {
  1405. if (term.line[i][j].mode & attr) {
  1406. tsetdirt(i, i);
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. }
  1412. void
  1413. tfulldirt(void)
  1414. {
  1415. tsetdirt(0, term.row-1);
  1416. }
  1417. void
  1418. tcursor(int mode)
  1419. {
  1420. static TCursor c[2];
  1421. int alt = IS_SET(MODE_ALTSCREEN);
  1422. if (mode == CURSOR_SAVE) {
  1423. c[alt] = term.c;
  1424. } else if (mode == CURSOR_LOAD) {
  1425. term.c = c[alt];
  1426. tmoveto(c[alt].x, c[alt].y);
  1427. }
  1428. }
  1429. void
  1430. treset(void)
  1431. {
  1432. uint i;
  1433. term.c = (TCursor){{
  1434. .mode = ATTR_NULL,
  1435. .fg = defaultfg,
  1436. .bg = defaultbg
  1437. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  1438. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1439. for (i = tabspaces; i < term.col; i += tabspaces)
  1440. term.tabs[i] = 1;
  1441. term.top = 0;
  1442. term.bot = term.row - 1;
  1443. term.mode = MODE_WRAP;
  1444. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  1445. term.charset = 0;
  1446. for (i = 0; i < 2; i++) {
  1447. tmoveto(0, 0);
  1448. tcursor(CURSOR_SAVE);
  1449. tclearregion(0, 0, term.col-1, term.row-1);
  1450. tswapscreen();
  1451. }
  1452. }
  1453. void
  1454. tnew(int col, int row)
  1455. {
  1456. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  1457. tresize(col, row);
  1458. term.numlock = 1;
  1459. treset();
  1460. }
  1461. void
  1462. tswapscreen(void)
  1463. {
  1464. Line *tmp = term.line;
  1465. term.line = term.alt;
  1466. term.alt = tmp;
  1467. term.mode ^= MODE_ALTSCREEN;
  1468. tfulldirt();
  1469. }
  1470. void
  1471. tscrolldown(int orig, int n)
  1472. {
  1473. int i;
  1474. Line temp;
  1475. LIMIT(n, 0, term.bot-orig+1);
  1476. tsetdirt(orig, term.bot-n);
  1477. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  1478. for (i = term.bot; i >= orig+n; i--) {
  1479. temp = term.line[i];
  1480. term.line[i] = term.line[i-n];
  1481. term.line[i-n] = temp;
  1482. }
  1483. selscroll(orig, n);
  1484. }
  1485. void
  1486. tscrollup(int orig, int n)
  1487. {
  1488. int i;
  1489. Line temp;
  1490. LIMIT(n, 0, term.bot-orig+1);
  1491. tclearregion(0, orig, term.col-1, orig+n-1);
  1492. tsetdirt(orig+n, term.bot);
  1493. for (i = orig; i <= term.bot-n; i++) {
  1494. temp = term.line[i];
  1495. term.line[i] = term.line[i+n];
  1496. term.line[i+n] = temp;
  1497. }
  1498. selscroll(orig, -n);
  1499. }
  1500. void
  1501. selscroll(int orig, int n)
  1502. {
  1503. if (sel.ob.x == -1)
  1504. return;
  1505. if (BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
  1506. if ((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
  1507. selclear(NULL);
  1508. return;
  1509. }
  1510. if (sel.type == SEL_RECTANGULAR) {
  1511. if (sel.ob.y < term.top)
  1512. sel.ob.y = term.top;
  1513. if (sel.oe.y > term.bot)
  1514. sel.oe.y = term.bot;
  1515. } else {
  1516. if (sel.ob.y < term.top) {
  1517. sel.ob.y = term.top;
  1518. sel.ob.x = 0;
  1519. }
  1520. if (sel.oe.y > term.bot) {
  1521. sel.oe.y = term.bot;
  1522. sel.oe.x = term.col;
  1523. }
  1524. }
  1525. selnormalize();
  1526. }
  1527. }
  1528. void
  1529. tnewline(int first_col)
  1530. {
  1531. int y = term.c.y;
  1532. if (y == term.bot) {
  1533. tscrollup(term.top, 1);
  1534. } else {
  1535. y++;
  1536. }
  1537. tmoveto(first_col ? 0 : term.c.x, y);
  1538. }
  1539. void
  1540. csiparse(void)
  1541. {
  1542. char *p = csiescseq.buf, *np;
  1543. long int v;
  1544. csiescseq.narg = 0;
  1545. if (*p == '?') {
  1546. csiescseq.priv = 1;
  1547. p++;
  1548. }
  1549. csiescseq.buf[csiescseq.len] = '\0';
  1550. while (p < csiescseq.buf+csiescseq.len) {
  1551. np = NULL;
  1552. v = strtol(p, &np, 10);
  1553. if (np == p)
  1554. v = 0;
  1555. if (v == LONG_MAX || v == LONG_MIN)
  1556. v = -1;
  1557. csiescseq.arg[csiescseq.narg++] = v;
  1558. p = np;
  1559. if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1560. break;
  1561. p++;
  1562. }
  1563. csiescseq.mode[0] = *p++;
  1564. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  1565. }
  1566. /* for absolute user moves, when decom is set */
  1567. void
  1568. tmoveato(int x, int y)
  1569. {
  1570. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1571. }
  1572. void
  1573. tmoveto(int x, int y)
  1574. {
  1575. int miny, maxy;
  1576. if (term.c.state & CURSOR_ORIGIN) {
  1577. miny = term.top;
  1578. maxy = term.bot;
  1579. } else {
  1580. miny = 0;
  1581. maxy = term.row - 1;
  1582. }
  1583. term.c.state &= ~CURSOR_WRAPNEXT;
  1584. term.c.x = LIMIT(x, 0, term.col-1);
  1585. term.c.y = LIMIT(y, miny, maxy);
  1586. }
  1587. void
  1588. tsetchar(Rune u, Glyph *attr, int x, int y)
  1589. {
  1590. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1591. "", "", "", "", "", "", "", /* A - G */
  1592. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1593. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1594. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1595. "", "", "", "", "", "", "°", "±", /* ` - g */
  1596. "", "", "", "", "", "", "", "", /* h - o */
  1597. "", "", "", "", "", "", "", "", /* p - w */
  1598. "", "", "", "π", "", "£", "·", /* x - ~ */
  1599. };
  1600. /*
  1601. * The table is proudly stolen from rxvt.
  1602. */
  1603. if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
  1604. BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
  1605. utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
  1606. if (term.line[y][x].mode & ATTR_WIDE) {
  1607. if (x+1 < term.col) {
  1608. term.line[y][x+1].u = ' ';
  1609. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1610. }
  1611. } else if (term.line[y][x].mode & ATTR_WDUMMY) {
  1612. term.line[y][x-1].u = ' ';
  1613. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1614. }
  1615. term.dirty[y] = 1;
  1616. term.line[y][x] = *attr;
  1617. term.line[y][x].u = u;
  1618. }
  1619. void
  1620. tclearregion(int x1, int y1, int x2, int y2)
  1621. {
  1622. int x, y, temp;
  1623. Glyph *gp;
  1624. if (x1 > x2)
  1625. temp = x1, x1 = x2, x2 = temp;
  1626. if (y1 > y2)
  1627. temp = y1, y1 = y2, y2 = temp;
  1628. LIMIT(x1, 0, term.col-1);
  1629. LIMIT(x2, 0, term.col-1);
  1630. LIMIT(y1, 0, term.row-1);
  1631. LIMIT(y2, 0, term.row-1);
  1632. for (y = y1; y <= y2; y++) {
  1633. term.dirty[y] = 1;
  1634. for (x = x1; x <= x2; x++) {
  1635. gp = &term.line[y][x];
  1636. if (selected(x, y))
  1637. selclear(NULL);
  1638. gp->fg = term.c.attr.fg;
  1639. gp->bg = term.c.attr.bg;
  1640. gp->mode = 0;
  1641. gp->u = ' ';
  1642. }
  1643. }
  1644. }
  1645. void
  1646. tdeletechar(int n)
  1647. {
  1648. int dst, src, size;
  1649. Glyph *line;
  1650. LIMIT(n, 0, term.col - term.c.x);
  1651. dst = term.c.x;
  1652. src = term.c.x + n;
  1653. size = term.col - src;
  1654. line = term.line[term.c.y];
  1655. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1656. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1657. }
  1658. void
  1659. tinsertblank(int n)
  1660. {
  1661. int dst, src, size;
  1662. Glyph *line;
  1663. LIMIT(n, 0, term.col - term.c.x);
  1664. dst = term.c.x + n;
  1665. src = term.c.x;
  1666. size = term.col - dst;
  1667. line = term.line[term.c.y];
  1668. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1669. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1670. }
  1671. void
  1672. tinsertblankline(int n)
  1673. {
  1674. if (BETWEEN(term.c.y, term.top, term.bot))
  1675. tscrolldown(term.c.y, n);
  1676. }
  1677. void
  1678. tdeleteline(int n)
  1679. {
  1680. if (BETWEEN(term.c.y, term.top, term.bot))
  1681. tscrollup(term.c.y, n);
  1682. }
  1683. int32_t
  1684. tdefcolor(int *attr, int *npar, int l)
  1685. {
  1686. int32_t idx = -1;
  1687. uint r, g, b;
  1688. switch (attr[*npar + 1]) {
  1689. case 2: /* direct color in RGB space */
  1690. if (*npar + 4 >= l) {
  1691. fprintf(stderr,
  1692. "erresc(38): Incorrect number of parameters (%d)\n",
  1693. *npar);
  1694. break;
  1695. }
  1696. r = attr[*npar + 2];
  1697. g = attr[*npar + 3];
  1698. b = attr[*npar + 4];
  1699. *npar += 4;
  1700. if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1701. fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
  1702. r, g, b);
  1703. else
  1704. idx = TRUECOLOR(r, g, b);
  1705. break;
  1706. case 5: /* indexed color */
  1707. if (*npar + 2 >= l) {
  1708. fprintf(stderr,
  1709. "erresc(38): Incorrect number of parameters (%d)\n",
  1710. *npar);
  1711. break;
  1712. }
  1713. *npar += 2;
  1714. if (!BETWEEN(attr[*npar], 0, 255))
  1715. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1716. else
  1717. idx = attr[*npar];
  1718. break;
  1719. case 0: /* implemented defined (only foreground) */
  1720. case 1: /* transparent */
  1721. case 3: /* direct color in CMY space */
  1722. case 4: /* direct color in CMYK space */
  1723. default:
  1724. fprintf(stderr,
  1725. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1726. break;
  1727. }
  1728. return idx;
  1729. }
  1730. void
  1731. tsetattr(int *attr, int l)
  1732. {
  1733. int i;
  1734. int32_t idx;
  1735. for (i = 0; i < l; i++) {
  1736. switch (attr[i]) {
  1737. case 0:
  1738. term.c.attr.mode &= ~(
  1739. ATTR_BOLD |
  1740. ATTR_FAINT |
  1741. ATTR_ITALIC |
  1742. ATTR_UNDERLINE |
  1743. ATTR_BLINK |
  1744. ATTR_REVERSE |
  1745. ATTR_INVISIBLE |
  1746. ATTR_STRUCK );
  1747. term.c.attr.fg = defaultfg;
  1748. term.c.attr.bg = defaultbg;
  1749. break;
  1750. case 1:
  1751. term.c.attr.mode |= ATTR_BOLD;
  1752. break;
  1753. case 2:
  1754. term.c.attr.mode |= ATTR_FAINT;
  1755. break;
  1756. case 3:
  1757. term.c.attr.mode |= ATTR_ITALIC;
  1758. break;
  1759. case 4:
  1760. term.c.attr.mode |= ATTR_UNDERLINE;
  1761. break;
  1762. case 5: /* slow blink */
  1763. /* FALLTHROUGH */
  1764. case 6: /* rapid blink */
  1765. term.c.attr.mode |= ATTR_BLINK;
  1766. break;
  1767. case 7:
  1768. term.c.attr.mode |= ATTR_REVERSE;
  1769. break;
  1770. case 8:
  1771. term.c.attr.mode |= ATTR_INVISIBLE;
  1772. break;
  1773. case 9:
  1774. term.c.attr.mode |= ATTR_STRUCK;
  1775. break;
  1776. case 22:
  1777. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1778. break;
  1779. case 23:
  1780. term.c.attr.mode &= ~ATTR_ITALIC;
  1781. break;
  1782. case 24:
  1783. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1784. break;
  1785. case 25:
  1786. term.c.attr.mode &= ~ATTR_BLINK;
  1787. break;
  1788. case 27:
  1789. term.c.attr.mode &= ~ATTR_REVERSE;
  1790. break;
  1791. case 28:
  1792. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1793. break;
  1794. case 29:
  1795. term.c.attr.mode &= ~ATTR_STRUCK;
  1796. break;
  1797. case 38:
  1798. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1799. term.c.attr.fg = idx;
  1800. break;
  1801. case 39:
  1802. term.c.attr.fg = defaultfg;
  1803. break;
  1804. case 48:
  1805. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1806. term.c.attr.bg = idx;
  1807. break;
  1808. case 49:
  1809. term.c.attr.bg = defaultbg;
  1810. break;
  1811. default:
  1812. if (BETWEEN(attr[i], 30, 37)) {
  1813. term.c.attr.fg = attr[i] - 30;
  1814. } else if (BETWEEN(attr[i], 40, 47)) {
  1815. term.c.attr.bg = attr[i] - 40;
  1816. } else if (BETWEEN(attr[i], 90, 97)) {
  1817. term.c.attr.fg = attr[i] - 90 + 8;
  1818. } else if (BETWEEN(attr[i], 100, 107)) {
  1819. term.c.attr.bg = attr[i] - 100 + 8;
  1820. } else {
  1821. fprintf(stderr,
  1822. "erresc(default): gfx attr %d unknown\n",
  1823. attr[i]), csidump();
  1824. }
  1825. break;
  1826. }
  1827. }
  1828. }
  1829. void
  1830. tsetscroll(int t, int b)
  1831. {
  1832. int temp;
  1833. LIMIT(t, 0, term.row-1);
  1834. LIMIT(b, 0, term.row-1);
  1835. if (t > b) {
  1836. temp = t;
  1837. t = b;
  1838. b = temp;
  1839. }
  1840. term.top = t;
  1841. term.bot = b;
  1842. }
  1843. void
  1844. tsetmode(int priv, int set, int *args, int narg)
  1845. {
  1846. int *lim, mode;
  1847. int alt;
  1848. for (lim = args + narg; args < lim; ++args) {
  1849. if (priv) {
  1850. switch (*args) {
  1851. case 1: /* DECCKM -- Cursor key */
  1852. MODBIT(term.mode, set, MODE_APPCURSOR);
  1853. break;
  1854. case 5: /* DECSCNM -- Reverse video */
  1855. mode = term.mode;
  1856. MODBIT(term.mode, set, MODE_REVERSE);
  1857. if (mode != term.mode)
  1858. redraw();
  1859. break;
  1860. case 6: /* DECOM -- Origin */
  1861. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1862. tmoveato(0, 0);
  1863. break;
  1864. case 7: /* DECAWM -- Auto wrap */
  1865. MODBIT(term.mode, set, MODE_WRAP);
  1866. break;
  1867. case 0: /* Error (IGNORED) */
  1868. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1869. case 3: /* DECCOLM -- Column (IGNORED) */
  1870. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1871. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1872. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1873. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1874. case 42: /* DECNRCM -- National characters (IGNORED) */
  1875. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1876. break;
  1877. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1878. MODBIT(term.mode, !set, MODE_HIDE);
  1879. break;
  1880. case 9: /* X10 mouse compatibility mode */
  1881. xsetpointermotion(0);
  1882. MODBIT(term.mode, 0, MODE_MOUSE);
  1883. MODBIT(term.mode, set, MODE_MOUSEX10);
  1884. break;
  1885. case 1000: /* 1000: report button press */
  1886. xsetpointermotion(0);
  1887. MODBIT(term.mode, 0, MODE_MOUSE);
  1888. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1889. break;
  1890. case 1002: /* 1002: report motion on button press */
  1891. xsetpointermotion(0);
  1892. MODBIT(term.mode, 0, MODE_MOUSE);
  1893. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1894. break;
  1895. case 1003: /* 1003: enable all mouse motions */
  1896. xsetpointermotion(set);
  1897. MODBIT(term.mode, 0, MODE_MOUSE);
  1898. MODBIT(term.mode, set, MODE_MOUSEMANY);
  1899. break;
  1900. case 1004: /* 1004: send focus events to tty */
  1901. MODBIT(term.mode, set, MODE_FOCUS);
  1902. break;
  1903. case 1006: /* 1006: extended reporting mode */
  1904. MODBIT(term.mode, set, MODE_MOUSESGR);
  1905. break;
  1906. case 1034:
  1907. MODBIT(term.mode, set, MODE_8BIT);
  1908. break;
  1909. case 1049: /* swap screen & set/restore cursor as xterm */
  1910. if (!allowaltscreen)
  1911. break;
  1912. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1913. /* FALLTHROUGH */
  1914. case 47: /* swap screen */
  1915. case 1047:
  1916. if (!allowaltscreen)
  1917. break;
  1918. alt = IS_SET(MODE_ALTSCREEN);
  1919. if (alt) {
  1920. tclearregion(0, 0, term.col-1,
  1921. term.row-1);
  1922. }
  1923. if (set ^ alt) /* set is always 1 or 0 */
  1924. tswapscreen();
  1925. if (*args != 1049)
  1926. break;
  1927. /* FALLTHROUGH */
  1928. case 1048:
  1929. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1930. break;
  1931. case 2004: /* 2004: bracketed paste mode */
  1932. MODBIT(term.mode, set, MODE_BRCKTPASTE);
  1933. break;
  1934. /* Not implemented mouse modes. See comments there. */
  1935. case 1001: /* mouse highlight mode; can hang the
  1936. terminal by design when implemented. */
  1937. case 1005: /* UTF-8 mouse mode; will confuse
  1938. applications not supporting UTF-8
  1939. and luit. */
  1940. case 1015: /* urxvt mangled mouse mode; incompatible
  1941. and can be mistaken for other control
  1942. codes. */
  1943. default:
  1944. fprintf(stderr,
  1945. "erresc: unknown private set/reset mode %d\n",
  1946. *args);
  1947. break;
  1948. }
  1949. } else {
  1950. switch (*args) {
  1951. case 0: /* Error (IGNORED) */
  1952. break;
  1953. case 2: /* KAM -- keyboard action */
  1954. MODBIT(term.mode, set, MODE_KBDLOCK);
  1955. break;
  1956. case 4: /* IRM -- Insertion-replacement */
  1957. MODBIT(term.mode, set, MODE_INSERT);
  1958. break;
  1959. case 12: /* SRM -- Send/Receive */
  1960. MODBIT(term.mode, !set, MODE_ECHO);
  1961. break;
  1962. case 20: /* LNM -- Linefeed/new line */
  1963. MODBIT(term.mode, set, MODE_CRLF);
  1964. break;
  1965. default:
  1966. fprintf(stderr,
  1967. "erresc: unknown set/reset mode %d\n",
  1968. *args);
  1969. break;
  1970. }
  1971. }
  1972. }
  1973. }
  1974. void
  1975. csihandle(void)
  1976. {
  1977. char buf[40];
  1978. int len;
  1979. switch (csiescseq.mode[0]) {
  1980. default:
  1981. unknown:
  1982. fprintf(stderr, "erresc: unknown csi ");
  1983. csidump();
  1984. /* die(""); */
  1985. break;
  1986. case '@': /* ICH -- Insert <n> blank char */
  1987. DEFAULT(csiescseq.arg[0], 1);
  1988. tinsertblank(csiescseq.arg[0]);
  1989. break;
  1990. case 'A': /* CUU -- Cursor <n> Up */
  1991. DEFAULT(csiescseq.arg[0], 1);
  1992. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1993. break;
  1994. case 'B': /* CUD -- Cursor <n> Down */
  1995. case 'e': /* VPR --Cursor <n> Down */
  1996. DEFAULT(csiescseq.arg[0], 1);
  1997. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1998. break;
  1999. case 'i': /* MC -- Media Copy */
  2000. switch (csiescseq.arg[0]) {
  2001. case 0:
  2002. tdump();
  2003. break;
  2004. case 1:
  2005. tdumpline(term.c.y);
  2006. break;
  2007. case 2:
  2008. tdumpsel();
  2009. break;
  2010. case 4:
  2011. term.mode &= ~MODE_PRINT;
  2012. break;
  2013. case 5:
  2014. term.mode |= MODE_PRINT;
  2015. break;
  2016. }
  2017. break;
  2018. case 'c': /* DA -- Device Attributes */
  2019. if (csiescseq.arg[0] == 0)
  2020. ttywrite(vtiden, sizeof(vtiden) - 1);
  2021. break;
  2022. case 'C': /* CUF -- Cursor <n> Forward */
  2023. case 'a': /* HPR -- Cursor <n> Forward */
  2024. DEFAULT(csiescseq.arg[0], 1);
  2025. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  2026. break;
  2027. case 'D': /* CUB -- Cursor <n> Backward */
  2028. DEFAULT(csiescseq.arg[0], 1);
  2029. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  2030. break;
  2031. case 'E': /* CNL -- Cursor <n> Down and first col */
  2032. DEFAULT(csiescseq.arg[0], 1);
  2033. tmoveto(0, term.c.y+csiescseq.arg[0]);
  2034. break;
  2035. case 'F': /* CPL -- Cursor <n> Up and first col */
  2036. DEFAULT(csiescseq.arg[0], 1);
  2037. tmoveto(0, term.c.y-csiescseq.arg[0]);
  2038. break;
  2039. case 'g': /* TBC -- Tabulation clear */
  2040. switch (csiescseq.arg[0]) {
  2041. case 0: /* clear current tab stop */
  2042. term.tabs[term.c.x] = 0;
  2043. break;
  2044. case 3: /* clear all the tabs */
  2045. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  2046. break;
  2047. default:
  2048. goto unknown;
  2049. }
  2050. break;
  2051. case 'G': /* CHA -- Move to <col> */
  2052. case '`': /* HPA */
  2053. DEFAULT(csiescseq.arg[0], 1);
  2054. tmoveto(csiescseq.arg[0]-1, term.c.y);
  2055. break;
  2056. case 'H': /* CUP -- Move to <row> <col> */
  2057. case 'f': /* HVP */
  2058. DEFAULT(csiescseq.arg[0], 1);
  2059. DEFAULT(csiescseq.arg[1], 1);
  2060. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  2061. break;
  2062. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  2063. DEFAULT(csiescseq.arg[0], 1);
  2064. tputtab(csiescseq.arg[0]);
  2065. break;
  2066. case 'J': /* ED -- Clear screen */
  2067. selclear(NULL);
  2068. switch (csiescseq.arg[0]) {
  2069. case 0: /* below */
  2070. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  2071. if (term.c.y < term.row-1) {
  2072. tclearregion(0, term.c.y+1, term.col-1,
  2073. term.row-1);
  2074. }
  2075. break;
  2076. case 1: /* above */
  2077. if (term.c.y > 1)
  2078. tclearregion(0, 0, term.col-1, term.c.y-1);
  2079. tclearregion(0, term.c.y, term.c.x, term.c.y);
  2080. break;
  2081. case 2: /* all */
  2082. tclearregion(0, 0, term.col-1, term.row-1);
  2083. break;
  2084. default:
  2085. goto unknown;
  2086. }
  2087. break;
  2088. case 'K': /* EL -- Clear line */
  2089. switch (csiescseq.arg[0]) {
  2090. case 0: /* right */
  2091. tclearregion(term.c.x, term.c.y, term.col-1,
  2092. term.c.y);
  2093. break;
  2094. case 1: /* left */
  2095. tclearregion(0, term.c.y, term.c.x, term.c.y);
  2096. break;
  2097. case 2: /* all */
  2098. tclearregion(0, term.c.y, term.col-1, term.c.y);
  2099. break;
  2100. }
  2101. break;
  2102. case 'S': /* SU -- Scroll <n> line up */
  2103. DEFAULT(csiescseq.arg[0], 1);
  2104. tscrollup(term.top, csiescseq.arg[0]);
  2105. break;
  2106. case 'T': /* SD -- Scroll <n> line down */
  2107. DEFAULT(csiescseq.arg[0], 1);
  2108. tscrolldown(term.top, csiescseq.arg[0]);
  2109. break;
  2110. case 'L': /* IL -- Insert <n> blank lines */
  2111. DEFAULT(csiescseq.arg[0], 1);
  2112. tinsertblankline(csiescseq.arg[0]);
  2113. break;
  2114. case 'l': /* RM -- Reset Mode */
  2115. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  2116. break;
  2117. case 'M': /* DL -- Delete <n> lines */
  2118. DEFAULT(csiescseq.arg[0], 1);
  2119. tdeleteline(csiescseq.arg[0]);
  2120. break;
  2121. case 'X': /* ECH -- Erase <n> char */
  2122. DEFAULT(csiescseq.arg[0], 1);
  2123. tclearregion(term.c.x, term.c.y,
  2124. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  2125. break;
  2126. case 'P': /* DCH -- Delete <n> char */
  2127. DEFAULT(csiescseq.arg[0], 1);
  2128. tdeletechar(csiescseq.arg[0]);
  2129. break;
  2130. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  2131. DEFAULT(csiescseq.arg[0], 1);
  2132. tputtab(-csiescseq.arg[0]);
  2133. break;
  2134. case 'd': /* VPA -- Move to <row> */
  2135. DEFAULT(csiescseq.arg[0], 1);
  2136. tmoveato(term.c.x, csiescseq.arg[0]-1);
  2137. break;
  2138. case 'h': /* SM -- Set terminal mode */
  2139. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  2140. break;
  2141. case 'm': /* SGR -- Terminal attribute (color) */
  2142. tsetattr(csiescseq.arg, csiescseq.narg);
  2143. break;
  2144. case 'n': /* DSR – Device Status Report (cursor position) */
  2145. if (csiescseq.arg[0] == 6) {
  2146. len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
  2147. term.c.y+1, term.c.x+1);
  2148. ttywrite(buf, len);
  2149. }
  2150. break;
  2151. case 'r': /* DECSTBM -- Set Scrolling Region */
  2152. if (csiescseq.priv) {
  2153. goto unknown;
  2154. } else {
  2155. DEFAULT(csiescseq.arg[0], 1);
  2156. DEFAULT(csiescseq.arg[1], term.row);
  2157. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  2158. tmoveato(0, 0);
  2159. }
  2160. break;
  2161. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  2162. tcursor(CURSOR_SAVE);
  2163. break;
  2164. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  2165. tcursor(CURSOR_LOAD);
  2166. break;
  2167. case ' ':
  2168. switch (csiescseq.mode[1]) {
  2169. case 'q': /* DECSCUSR -- Set Cursor Style */
  2170. DEFAULT(csiescseq.arg[0], 1);
  2171. if (!BETWEEN(csiescseq.arg[0], 0, 6)) {
  2172. goto unknown;
  2173. }
  2174. xw.cursor = csiescseq.arg[0];
  2175. break;
  2176. default:
  2177. goto unknown;
  2178. }
  2179. break;
  2180. }
  2181. }
  2182. void
  2183. csidump(void)
  2184. {
  2185. int i;
  2186. uint c;
  2187. printf("ESC[");
  2188. for (i = 0; i < csiescseq.len; i++) {
  2189. c = csiescseq.buf[i] & 0xff;
  2190. if (isprint(c)) {
  2191. putchar(c);
  2192. } else if (c == '\n') {
  2193. printf("(\\n)");
  2194. } else if (c == '\r') {
  2195. printf("(\\r)");
  2196. } else if (c == 0x1b) {
  2197. printf("(\\e)");
  2198. } else {
  2199. printf("(%02x)", c);
  2200. }
  2201. }
  2202. putchar('\n');
  2203. }
  2204. void
  2205. csireset(void)
  2206. {
  2207. memset(&csiescseq, 0, sizeof(csiescseq));
  2208. }
  2209. void
  2210. strhandle(void)
  2211. {
  2212. char *p = NULL;
  2213. int j, narg, par;
  2214. term.esc &= ~(ESC_STR_END|ESC_STR);
  2215. strparse();
  2216. par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
  2217. switch (strescseq.type) {
  2218. case ']': /* OSC -- Operating System Command */
  2219. switch (par) {
  2220. case 0:
  2221. case 1:
  2222. case 2:
  2223. if (narg > 1)
  2224. xsettitle(strescseq.args[1]);
  2225. return;
  2226. case 4: /* color set */
  2227. if (narg < 3)
  2228. break;
  2229. p = strescseq.args[2];
  2230. /* FALLTHROUGH */
  2231. case 104: /* color reset, here p = NULL */
  2232. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  2233. if (xsetcolorname(j, p)) {
  2234. fprintf(stderr, "erresc: invalid color %s\n", p);
  2235. } else {
  2236. /*
  2237. * TODO if defaultbg color is changed, borders
  2238. * are dirty
  2239. */
  2240. redraw();
  2241. }
  2242. return;
  2243. }
  2244. break;
  2245. case 'k': /* old title set compatibility */
  2246. xsettitle(strescseq.args[0]);
  2247. return;
  2248. case 'P': /* DCS -- Device Control String */
  2249. case '_': /* APC -- Application Program Command */
  2250. case '^': /* PM -- Privacy Message */
  2251. return;
  2252. }
  2253. fprintf(stderr, "erresc: unknown str ");
  2254. strdump();
  2255. }
  2256. void
  2257. strparse(void)
  2258. {
  2259. int c;
  2260. char *p = strescseq.buf;
  2261. strescseq.narg = 0;
  2262. strescseq.buf[strescseq.len] = '\0';
  2263. if (*p == '\0')
  2264. return;
  2265. while (strescseq.narg < STR_ARG_SIZ) {
  2266. strescseq.args[strescseq.narg++] = p;
  2267. while ((c = *p) != ';' && c != '\0')
  2268. ++p;
  2269. if (c == '\0')
  2270. return;
  2271. *p++ = '\0';
  2272. }
  2273. }
  2274. void
  2275. strdump(void)
  2276. {
  2277. int i;
  2278. uint c;
  2279. printf("ESC%c", strescseq.type);
  2280. for (i = 0; i < strescseq.len; i++) {
  2281. c = strescseq.buf[i] & 0xff;
  2282. if (c == '\0') {
  2283. return;
  2284. } else if (isprint(c)) {
  2285. putchar(c);
  2286. } else if (c == '\n') {
  2287. printf("(\\n)");
  2288. } else if (c == '\r') {
  2289. printf("(\\r)");
  2290. } else if (c == 0x1b) {
  2291. printf("(\\e)");
  2292. } else {
  2293. printf("(%02x)", c);
  2294. }
  2295. }
  2296. printf("ESC\\\n");
  2297. }
  2298. void
  2299. strreset(void)
  2300. {
  2301. memset(&strescseq, 0, sizeof(strescseq));
  2302. }
  2303. void
  2304. tprinter(char *s, size_t len)
  2305. {
  2306. if (iofd != -1 && xwrite(iofd, s, len) < 0) {
  2307. fprintf(stderr, "Error writing in %s:%s\n",
  2308. opt_io, strerror(errno));
  2309. close(iofd);
  2310. iofd = -1;
  2311. }
  2312. }
  2313. void
  2314. toggleprinter(const Arg *arg)
  2315. {
  2316. term.mode ^= MODE_PRINT;
  2317. }
  2318. void
  2319. printscreen(const Arg *arg)
  2320. {
  2321. tdump();
  2322. }
  2323. void
  2324. printsel(const Arg *arg)
  2325. {
  2326. tdumpsel();
  2327. }
  2328. void
  2329. tdumpsel(void)
  2330. {
  2331. char *ptr;
  2332. if ((ptr = getsel())) {
  2333. tprinter(ptr, strlen(ptr));
  2334. free(ptr);
  2335. }
  2336. }
  2337. void
  2338. tdumpline(int n)
  2339. {
  2340. char buf[UTF_SIZ];
  2341. Glyph *bp, *end;
  2342. bp = &term.line[n][0];
  2343. end = &bp[MIN(tlinelen(n), term.col) - 1];
  2344. if (bp != end || bp->u != ' ') {
  2345. for ( ;bp <= end; ++bp)
  2346. tprinter(buf, utf8encode(bp->u, buf));
  2347. }
  2348. tprinter("\n", 1);
  2349. }
  2350. void
  2351. tdump(void)
  2352. {
  2353. int i;
  2354. for (i = 0; i < term.row; ++i)
  2355. tdumpline(i);
  2356. }
  2357. void
  2358. tputtab(int n)
  2359. {
  2360. uint x = term.c.x;
  2361. if (n > 0) {
  2362. while (x < term.col && n--)
  2363. for (++x; x < term.col && !term.tabs[x]; ++x)
  2364. /* nothing */ ;
  2365. } else if (n < 0) {
  2366. while (x > 0 && n++)
  2367. for (--x; x > 0 && !term.tabs[x]; --x)
  2368. /* nothing */ ;
  2369. }
  2370. term.c.x = LIMIT(x, 0, term.col-1);
  2371. }
  2372. void
  2373. techo(Rune u)
  2374. {
  2375. if (ISCONTROL(u)) { /* control code */
  2376. if (u & 0x80) {
  2377. u &= 0x7f;
  2378. tputc('^');
  2379. tputc('[');
  2380. } else if (u != '\n' && u != '\r' && u != '\t') {
  2381. u ^= 0x40;
  2382. tputc('^');
  2383. }
  2384. }
  2385. tputc(u);
  2386. }
  2387. void
  2388. tdeftran(char ascii)
  2389. {
  2390. static char cs[] = "0B";
  2391. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  2392. char *p;
  2393. if ((p = strchr(cs, ascii)) == NULL) {
  2394. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  2395. } else {
  2396. term.trantbl[term.icharset] = vcs[p - cs];
  2397. }
  2398. }
  2399. void
  2400. tdectest(char c)
  2401. {
  2402. int x, y;
  2403. if (c == '8') { /* DEC screen alignment test. */
  2404. for (x = 0; x < term.col; ++x) {
  2405. for (y = 0; y < term.row; ++y)
  2406. tsetchar('E', &term.c.attr, x, y);
  2407. }
  2408. }
  2409. }
  2410. void
  2411. tstrsequence(uchar c)
  2412. {
  2413. switch (c) {
  2414. case 0x90: /* DCS -- Device Control String */
  2415. c = 'P';
  2416. break;
  2417. case 0x9f: /* APC -- Application Program Command */
  2418. c = '_';
  2419. break;
  2420. case 0x9e: /* PM -- Privacy Message */
  2421. c = '^';
  2422. break;
  2423. case 0x9d: /* OSC -- Operating System Command */
  2424. c = ']';
  2425. break;
  2426. }
  2427. strreset();
  2428. strescseq.type = c;
  2429. term.esc |= ESC_STR;
  2430. }
  2431. void
  2432. tcontrolcode(uchar ascii)
  2433. {
  2434. switch (ascii) {
  2435. case '\t': /* HT */
  2436. tputtab(1);
  2437. return;
  2438. case '\b': /* BS */
  2439. tmoveto(term.c.x-1, term.c.y);
  2440. return;
  2441. case '\r': /* CR */
  2442. tmoveto(0, term.c.y);
  2443. return;
  2444. case '\f': /* LF */
  2445. case '\v': /* VT */
  2446. case '\n': /* LF */
  2447. /* go to first col if the mode is set */
  2448. tnewline(IS_SET(MODE_CRLF));
  2449. return;
  2450. case '\a': /* BEL */
  2451. if (term.esc & ESC_STR_END) {
  2452. /* backwards compatibility to xterm */
  2453. strhandle();
  2454. } else {
  2455. if (!(xw.state & WIN_FOCUSED))
  2456. xseturgency(1);
  2457. if (bellvolume)
  2458. XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
  2459. }
  2460. break;
  2461. case '\033': /* ESC */
  2462. csireset();
  2463. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  2464. term.esc |= ESC_START;
  2465. return;
  2466. case '\016': /* SO (LS1 -- Locking shift 1) */
  2467. case '\017': /* SI (LS0 -- Locking shift 0) */
  2468. term.charset = 1 - (ascii - '\016');
  2469. return;
  2470. case '\032': /* SUB */
  2471. tsetchar('?', &term.c.attr, term.c.x, term.c.y);
  2472. case '\030': /* CAN */
  2473. csireset();
  2474. break;
  2475. case '\005': /* ENQ (IGNORED) */
  2476. case '\000': /* NUL (IGNORED) */
  2477. case '\021': /* XON (IGNORED) */
  2478. case '\023': /* XOFF (IGNORED) */
  2479. case 0177: /* DEL (IGNORED) */
  2480. return;
  2481. case 0x84: /* TODO: IND */
  2482. break;
  2483. case 0x85: /* NEL -- Next line */
  2484. tnewline(1); /* always go to first col */
  2485. break;
  2486. case 0x88: /* HTS -- Horizontal tab stop */
  2487. term.tabs[term.c.x] = 1;
  2488. break;
  2489. case 0x8d: /* TODO: RI */
  2490. case 0x8e: /* TODO: SS2 */
  2491. case 0x8f: /* TODO: SS3 */
  2492. case 0x98: /* TODO: SOS */
  2493. break;
  2494. case 0x9a: /* DECID -- Identify Terminal */
  2495. ttywrite(vtiden, sizeof(vtiden) - 1);
  2496. break;
  2497. case 0x9b: /* TODO: CSI */
  2498. case 0x9c: /* TODO: ST */
  2499. break;
  2500. case 0x90: /* DCS -- Device Control String */
  2501. case 0x9f: /* APC -- Application Program Command */
  2502. case 0x9e: /* PM -- Privacy Message */
  2503. case 0x9d: /* OSC -- Operating System Command */
  2504. tstrsequence(ascii);
  2505. return;
  2506. }
  2507. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  2508. term.esc &= ~(ESC_STR_END|ESC_STR);
  2509. }
  2510. /*
  2511. * returns 1 when the sequence is finished and it hasn't to read
  2512. * more characters for this sequence, otherwise 0
  2513. */
  2514. int
  2515. eschandle(uchar ascii)
  2516. {
  2517. switch (ascii) {
  2518. case '[':
  2519. term.esc |= ESC_CSI;
  2520. return 0;
  2521. case '#':
  2522. term.esc |= ESC_TEST;
  2523. return 0;
  2524. case 'P': /* DCS -- Device Control String */
  2525. case '_': /* APC -- Application Program Command */
  2526. case '^': /* PM -- Privacy Message */
  2527. case ']': /* OSC -- Operating System Command */
  2528. case 'k': /* old title set compatibility */
  2529. tstrsequence(ascii);
  2530. return 0;
  2531. case 'n': /* LS2 -- Locking shift 2 */
  2532. case 'o': /* LS3 -- Locking shift 3 */
  2533. term.charset = 2 + (ascii - 'n');
  2534. break;
  2535. case '(': /* GZD4 -- set primary charset G0 */
  2536. case ')': /* G1D4 -- set secondary charset G1 */
  2537. case '*': /* G2D4 -- set tertiary charset G2 */
  2538. case '+': /* G3D4 -- set quaternary charset G3 */
  2539. term.icharset = ascii - '(';
  2540. term.esc |= ESC_ALTCHARSET;
  2541. return 0;
  2542. case 'D': /* IND -- Linefeed */
  2543. if (term.c.y == term.bot) {
  2544. tscrollup(term.top, 1);
  2545. } else {
  2546. tmoveto(term.c.x, term.c.y+1);
  2547. }
  2548. break;
  2549. case 'E': /* NEL -- Next line */
  2550. tnewline(1); /* always go to first col */
  2551. break;
  2552. case 'H': /* HTS -- Horizontal tab stop */
  2553. term.tabs[term.c.x] = 1;
  2554. break;
  2555. case 'M': /* RI -- Reverse index */
  2556. if (term.c.y == term.top) {
  2557. tscrolldown(term.top, 1);
  2558. } else {
  2559. tmoveto(term.c.x, term.c.y-1);
  2560. }
  2561. break;
  2562. case 'Z': /* DECID -- Identify Terminal */
  2563. ttywrite(vtiden, sizeof(vtiden) - 1);
  2564. break;
  2565. case 'c': /* RIS -- Reset to inital state */
  2566. treset();
  2567. xresettitle();
  2568. xloadcols();
  2569. break;
  2570. case '=': /* DECPAM -- Application keypad */
  2571. term.mode |= MODE_APPKEYPAD;
  2572. break;
  2573. case '>': /* DECPNM -- Normal keypad */
  2574. term.mode &= ~MODE_APPKEYPAD;
  2575. break;
  2576. case '7': /* DECSC -- Save Cursor */
  2577. tcursor(CURSOR_SAVE);
  2578. break;
  2579. case '8': /* DECRC -- Restore Cursor */
  2580. tcursor(CURSOR_LOAD);
  2581. break;
  2582. case '\\': /* ST -- String Terminator */
  2583. if (term.esc & ESC_STR_END)
  2584. strhandle();
  2585. break;
  2586. default:
  2587. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2588. (uchar) ascii, isprint(ascii)? ascii:'.');
  2589. break;
  2590. }
  2591. return 1;
  2592. }
  2593. void
  2594. tputc(Rune u)
  2595. {
  2596. char c[UTF_SIZ];
  2597. int control;
  2598. int width, len;
  2599. Glyph *gp;
  2600. control = ISCONTROL(u);
  2601. len = utf8encode(u, c);
  2602. if (!control && (width = wcwidth(u)) == -1) {
  2603. memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
  2604. width = 1;
  2605. }
  2606. if (IS_SET(MODE_PRINT))
  2607. tprinter(c, len);
  2608. /*
  2609. * STR sequence must be checked before anything else
  2610. * because it uses all following characters until it
  2611. * receives a ESC, a SUB, a ST or any other C1 control
  2612. * character.
  2613. */
  2614. if (term.esc & ESC_STR) {
  2615. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2616. ISCONTROLC1(u)) {
  2617. term.esc &= ~(ESC_START|ESC_STR);
  2618. term.esc |= ESC_STR_END;
  2619. } else if (strescseq.len + len < sizeof(strescseq.buf) - 1) {
  2620. memmove(&strescseq.buf[strescseq.len], c, len);
  2621. strescseq.len += len;
  2622. return;
  2623. } else {
  2624. /*
  2625. * Here is a bug in terminals. If the user never sends
  2626. * some code to stop the str or esc command, then st
  2627. * will stop responding. But this is better than
  2628. * silently failing with unknown characters. At least
  2629. * then users will report back.
  2630. *
  2631. * In the case users ever get fixed, here is the code:
  2632. */
  2633. /*
  2634. * term.esc = 0;
  2635. * strhandle();
  2636. */
  2637. return;
  2638. }
  2639. }
  2640. /*
  2641. * Actions of control codes must be performed as soon they arrive
  2642. * because they can be embedded inside a control sequence, and
  2643. * they must not cause conflicts with sequences.
  2644. */
  2645. if (control) {
  2646. tcontrolcode(u);
  2647. /*
  2648. * control codes are not shown ever
  2649. */
  2650. return;
  2651. } else if (term.esc & ESC_START) {
  2652. if (term.esc & ESC_CSI) {
  2653. csiescseq.buf[csiescseq.len++] = u;
  2654. if (BETWEEN(u, 0x40, 0x7E)
  2655. || csiescseq.len >= \
  2656. sizeof(csiescseq.buf)-1) {
  2657. term.esc = 0;
  2658. csiparse();
  2659. csihandle();
  2660. }
  2661. return;
  2662. } else if (term.esc & ESC_ALTCHARSET) {
  2663. tdeftran(u);
  2664. } else if (term.esc & ESC_TEST) {
  2665. tdectest(u);
  2666. } else {
  2667. if (!eschandle(u))
  2668. return;
  2669. /* sequence already finished */
  2670. }
  2671. term.esc = 0;
  2672. /*
  2673. * All characters which form part of a sequence are not
  2674. * printed
  2675. */
  2676. return;
  2677. }
  2678. if (sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
  2679. selclear(NULL);
  2680. gp = &term.line[term.c.y][term.c.x];
  2681. if (IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2682. gp->mode |= ATTR_WRAP;
  2683. tnewline(1);
  2684. gp = &term.line[term.c.y][term.c.x];
  2685. }
  2686. if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2687. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2688. if (term.c.x+width > term.col) {
  2689. tnewline(1);
  2690. gp = &term.line[term.c.y][term.c.x];
  2691. }
  2692. tsetchar(u, &term.c.attr, term.c.x, term.c.y);
  2693. if (width == 2) {
  2694. gp->mode |= ATTR_WIDE;
  2695. if (term.c.x+1 < term.col) {
  2696. gp[1].u = '\0';
  2697. gp[1].mode = ATTR_WDUMMY;
  2698. }
  2699. }
  2700. if (term.c.x+width < term.col) {
  2701. tmoveto(term.c.x+width, term.c.y);
  2702. } else {
  2703. term.c.state |= CURSOR_WRAPNEXT;
  2704. }
  2705. }
  2706. void
  2707. tresize(int col, int row)
  2708. {
  2709. int i;
  2710. int minrow = MIN(row, term.row);
  2711. int mincol = MIN(col, term.col);
  2712. int *bp;
  2713. TCursor c;
  2714. if (col < 1 || row < 1) {
  2715. fprintf(stderr,
  2716. "tresize: error resizing to %dx%d\n", col, row);
  2717. return;
  2718. }
  2719. /*
  2720. * slide screen to keep cursor where we expect it -
  2721. * tscrollup would work here, but we can optimize to
  2722. * memmove because we're freeing the earlier lines
  2723. */
  2724. for (i = 0; i <= term.c.y - row; i++) {
  2725. free(term.line[i]);
  2726. free(term.alt[i]);
  2727. }
  2728. /* ensure that both src and dst are not NULL */
  2729. if (i > 0) {
  2730. memmove(term.line, term.line + i, row * sizeof(Line));
  2731. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2732. }
  2733. for (i += row; i < term.row; i++) {
  2734. free(term.line[i]);
  2735. free(term.alt[i]);
  2736. }
  2737. /* resize to new width */
  2738. term.specbuf = xrealloc(term.specbuf, col * sizeof(XftGlyphFontSpec));
  2739. /* resize to new height */
  2740. term.line = xrealloc(term.line, row * sizeof(Line));
  2741. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2742. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2743. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2744. /* resize each row to new width, zero-pad if needed */
  2745. for (i = 0; i < minrow; i++) {
  2746. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2747. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2748. }
  2749. /* allocate any new rows */
  2750. for (/* i == minrow */; i < row; i++) {
  2751. term.line[i] = xmalloc(col * sizeof(Glyph));
  2752. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2753. }
  2754. if (col > term.col) {
  2755. bp = term.tabs + term.col;
  2756. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2757. while (--bp > term.tabs && !*bp)
  2758. /* nothing */ ;
  2759. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2760. *bp = 1;
  2761. }
  2762. /* update terminal size */
  2763. term.col = col;
  2764. term.row = row;
  2765. /* reset scrolling region */
  2766. tsetscroll(0, row-1);
  2767. /* make use of the LIMIT in tmoveto */
  2768. tmoveto(term.c.x, term.c.y);
  2769. /* Clearing both screens (it makes dirty all lines) */
  2770. c = term.c;
  2771. for (i = 0; i < 2; i++) {
  2772. if (mincol < col && 0 < minrow) {
  2773. tclearregion(mincol, 0, col - 1, minrow - 1);
  2774. }
  2775. if (0 < col && minrow < row) {
  2776. tclearregion(0, minrow, col - 1, row - 1);
  2777. }
  2778. tswapscreen();
  2779. tcursor(CURSOR_LOAD);
  2780. }
  2781. term.c = c;
  2782. }
  2783. void
  2784. xresize(int col, int row)
  2785. {
  2786. xw.tw = MAX(1, col * xw.cw);
  2787. xw.th = MAX(1, row * xw.ch);
  2788. XFreePixmap(xw.dpy, xw.buf);
  2789. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2790. DefaultDepth(xw.dpy, xw.scr));
  2791. XftDrawChange(xw.draw, xw.buf);
  2792. xclear(0, 0, xw.w, xw.h);
  2793. }
  2794. ushort
  2795. sixd_to_16bit(int x)
  2796. {
  2797. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  2798. }
  2799. int
  2800. xloadcolor(int i, const char *name, Color *ncolor)
  2801. {
  2802. XRenderColor color = { .alpha = 0xffff };
  2803. if (!name) {
  2804. if (BETWEEN(i, 16, 255)) { /* 256 color */
  2805. if (i < 6*6*6+16) { /* same colors as xterm */
  2806. color.red = sixd_to_16bit( ((i-16)/36)%6 );
  2807. color.green = sixd_to_16bit( ((i-16)/6) %6 );
  2808. color.blue = sixd_to_16bit( ((i-16)/1) %6 );
  2809. } else { /* greyscale */
  2810. color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
  2811. color.green = color.blue = color.red;
  2812. }
  2813. return XftColorAllocValue(xw.dpy, xw.vis,
  2814. xw.cmap, &color, ncolor);
  2815. } else
  2816. name = colorname[i];
  2817. }
  2818. return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
  2819. }
  2820. void
  2821. xloadcols(void)
  2822. {
  2823. int i;
  2824. static int loaded;
  2825. Color *cp;
  2826. if (loaded) {
  2827. for (cp = dc.col; cp < &dc.col[LEN(dc.col)]; ++cp)
  2828. XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
  2829. }
  2830. for (i = 0; i < LEN(dc.col); i++)
  2831. if (!xloadcolor(i, NULL, &dc.col[i])) {
  2832. if (colorname[i])
  2833. die("Could not allocate color '%s'\n", colorname[i]);
  2834. else
  2835. die("Could not allocate color %d\n", i);
  2836. }
  2837. loaded = 1;
  2838. }
  2839. int
  2840. xsetcolorname(int x, const char *name)
  2841. {
  2842. Color ncolor;
  2843. if (!BETWEEN(x, 0, LEN(dc.col)))
  2844. return 1;
  2845. if (!xloadcolor(x, name, &ncolor))
  2846. return 1;
  2847. XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
  2848. dc.col[x] = ncolor;
  2849. return 0;
  2850. }
  2851. void
  2852. xtermclear(int col1, int row1, int col2, int row2)
  2853. {
  2854. XftDrawRect(xw.draw,
  2855. &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
  2856. borderpx + col1 * xw.cw,
  2857. borderpx + row1 * xw.ch,
  2858. (col2-col1+1) * xw.cw,
  2859. (row2-row1+1) * xw.ch);
  2860. }
  2861. /*
  2862. * Absolute coordinates.
  2863. */
  2864. void
  2865. xclear(int x1, int y1, int x2, int y2)
  2866. {
  2867. XftDrawRect(xw.draw,
  2868. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  2869. x1, y1, x2-x1, y2-y1);
  2870. }
  2871. void
  2872. xhints(void)
  2873. {
  2874. XClassHint class = {opt_class ? opt_class : termname, termname};
  2875. XWMHints wm = {.flags = InputHint, .input = 1};
  2876. XSizeHints *sizeh = NULL;
  2877. sizeh = XAllocSizeHints();
  2878. sizeh->flags = PSize | PResizeInc | PBaseSize;
  2879. sizeh->height = xw.h;
  2880. sizeh->width = xw.w;
  2881. sizeh->height_inc = xw.ch;
  2882. sizeh->width_inc = xw.cw;
  2883. sizeh->base_height = 2 * borderpx;
  2884. sizeh->base_width = 2 * borderpx;
  2885. if (xw.isfixed) {
  2886. sizeh->flags |= PMaxSize | PMinSize;
  2887. sizeh->min_width = sizeh->max_width = xw.w;
  2888. sizeh->min_height = sizeh->max_height = xw.h;
  2889. }
  2890. if (xw.gm & (XValue|YValue)) {
  2891. sizeh->flags |= USPosition | PWinGravity;
  2892. sizeh->x = xw.l;
  2893. sizeh->y = xw.t;
  2894. sizeh->win_gravity = xgeommasktogravity(xw.gm);
  2895. }
  2896. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
  2897. &class);
  2898. XFree(sizeh);
  2899. }
  2900. int
  2901. xgeommasktogravity(int mask)
  2902. {
  2903. switch (mask & (XNegative|YNegative)) {
  2904. case 0:
  2905. return NorthWestGravity;
  2906. case XNegative:
  2907. return NorthEastGravity;
  2908. case YNegative:
  2909. return SouthWestGravity;
  2910. }
  2911. return SouthEastGravity;
  2912. }
  2913. int
  2914. xloadfont(Font *f, FcPattern *pattern)
  2915. {
  2916. FcPattern *match;
  2917. FcResult result;
  2918. match = FcFontMatch(NULL, pattern, &result);
  2919. if (!match)
  2920. return 1;
  2921. if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  2922. FcPatternDestroy(match);
  2923. return 1;
  2924. }
  2925. f->set = NULL;
  2926. f->pattern = FcPatternDuplicate(pattern);
  2927. f->ascent = f->match->ascent;
  2928. f->descent = f->match->descent;
  2929. f->lbearing = 0;
  2930. f->rbearing = f->match->max_advance_width;
  2931. f->height = f->ascent + f->descent;
  2932. f->width = f->lbearing + f->rbearing;
  2933. return 0;
  2934. }
  2935. void
  2936. xloadfonts(char *fontstr, double fontsize)
  2937. {
  2938. FcPattern *pattern;
  2939. double fontval;
  2940. float ceilf(float);
  2941. if (fontstr[0] == '-') {
  2942. pattern = XftXlfdParse(fontstr, False, False);
  2943. } else {
  2944. pattern = FcNameParse((FcChar8 *)fontstr);
  2945. }
  2946. if (!pattern)
  2947. die("st: can't open font %s\n", fontstr);
  2948. if (fontsize > 1) {
  2949. FcPatternDel(pattern, FC_PIXEL_SIZE);
  2950. FcPatternDel(pattern, FC_SIZE);
  2951. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  2952. usedfontsize = fontsize;
  2953. } else {
  2954. if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  2955. FcResultMatch) {
  2956. usedfontsize = fontval;
  2957. } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
  2958. FcResultMatch) {
  2959. usedfontsize = -1;
  2960. } else {
  2961. /*
  2962. * Default font size is 12, if none given. This is to
  2963. * have a known usedfontsize value.
  2964. */
  2965. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  2966. usedfontsize = 12;
  2967. }
  2968. defaultfontsize = usedfontsize;
  2969. }
  2970. FcConfigSubstitute(0, pattern, FcMatchPattern);
  2971. FcDefaultSubstitute(pattern);
  2972. if (xloadfont(&dc.font, pattern))
  2973. die("st: can't open font %s\n", fontstr);
  2974. if (usedfontsize < 0) {
  2975. FcPatternGetDouble(dc.font.match->pattern,
  2976. FC_PIXEL_SIZE, 0, &fontval);
  2977. usedfontsize = fontval;
  2978. if (fontsize == 0)
  2979. defaultfontsize = fontval;
  2980. }
  2981. /* Setting character width and height. */
  2982. xw.cw = ceilf(dc.font.width * cwscale);
  2983. xw.ch = ceilf(dc.font.height * chscale);
  2984. FcPatternDel(pattern, FC_SLANT);
  2985. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  2986. if (xloadfont(&dc.ifont, pattern))
  2987. die("st: can't open font %s\n", fontstr);
  2988. FcPatternDel(pattern, FC_WEIGHT);
  2989. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  2990. if (xloadfont(&dc.ibfont, pattern))
  2991. die("st: can't open font %s\n", fontstr);
  2992. FcPatternDel(pattern, FC_SLANT);
  2993. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  2994. if (xloadfont(&dc.bfont, pattern))
  2995. die("st: can't open font %s\n", fontstr);
  2996. FcPatternDestroy(pattern);
  2997. }
  2998. void
  2999. xunloadfont(Font *f)
  3000. {
  3001. XftFontClose(xw.dpy, f->match);
  3002. FcPatternDestroy(f->pattern);
  3003. if (f->set)
  3004. FcFontSetDestroy(f->set);
  3005. }
  3006. void
  3007. xunloadfonts(void)
  3008. {
  3009. /* Free the loaded fonts in the font cache. */
  3010. while (frclen > 0)
  3011. XftFontClose(xw.dpy, frc[--frclen].font);
  3012. xunloadfont(&dc.font);
  3013. xunloadfont(&dc.bfont);
  3014. xunloadfont(&dc.ifont);
  3015. xunloadfont(&dc.ibfont);
  3016. }
  3017. void
  3018. xzoom(const Arg *arg)
  3019. {
  3020. Arg larg;
  3021. larg.f = usedfontsize + arg->f;
  3022. xzoomabs(&larg);
  3023. }
  3024. void
  3025. xzoomabs(const Arg *arg)
  3026. {
  3027. xunloadfonts();
  3028. xloadfonts(usedfont, arg->f);
  3029. cresize(0, 0);
  3030. redraw();
  3031. xhints();
  3032. }
  3033. void
  3034. xzoomreset(const Arg *arg)
  3035. {
  3036. Arg larg;
  3037. if (defaultfontsize > 0) {
  3038. larg.f = defaultfontsize;
  3039. xzoomabs(&larg);
  3040. }
  3041. }
  3042. void
  3043. xinit(void)
  3044. {
  3045. XGCValues gcvalues;
  3046. Cursor cursor;
  3047. Window parent;
  3048. pid_t thispid = getpid();
  3049. XColor xmousefg, xmousebg;
  3050. if (!(xw.dpy = XOpenDisplay(NULL)))
  3051. die("Can't open display\n");
  3052. xw.scr = XDefaultScreen(xw.dpy);
  3053. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  3054. /* font */
  3055. if (!FcInit())
  3056. die("Could not init fontconfig.\n");
  3057. usedfont = (opt_font == NULL)? font : opt_font;
  3058. xloadfonts(usedfont, 0);
  3059. /* colors */
  3060. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  3061. xloadcols();
  3062. /* adjust fixed window geometry */
  3063. xw.w = 2 * borderpx + term.col * xw.cw;
  3064. xw.h = 2 * borderpx + term.row * xw.ch;
  3065. if (xw.gm & XNegative)
  3066. xw.l += DisplayWidth(xw.dpy, xw.scr) - xw.w - 2;
  3067. if (xw.gm & YNegative)
  3068. xw.t += DisplayWidth(xw.dpy, xw.scr) - xw.h - 2;
  3069. /* Events */
  3070. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  3071. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  3072. xw.attrs.bit_gravity = NorthWestGravity;
  3073. xw.attrs.event_mask = FocusChangeMask | KeyPressMask
  3074. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  3075. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  3076. xw.attrs.colormap = xw.cmap;
  3077. if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
  3078. parent = XRootWindow(xw.dpy, xw.scr);
  3079. xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
  3080. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  3081. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  3082. | CWEventMask | CWColormap, &xw.attrs);
  3083. memset(&gcvalues, 0, sizeof(gcvalues));
  3084. gcvalues.graphics_exposures = False;
  3085. dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
  3086. &gcvalues);
  3087. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  3088. DefaultDepth(xw.dpy, xw.scr));
  3089. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  3090. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  3091. /* Xft rendering context */
  3092. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  3093. /* input methods */
  3094. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  3095. XSetLocaleModifiers("@im=local");
  3096. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  3097. XSetLocaleModifiers("@im=");
  3098. if ((xw.xim = XOpenIM(xw.dpy,
  3099. NULL, NULL, NULL)) == NULL) {
  3100. die("XOpenIM failed. Could not open input"
  3101. " device.\n");
  3102. }
  3103. }
  3104. }
  3105. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  3106. | XIMStatusNothing, XNClientWindow, xw.win,
  3107. XNFocusWindow, xw.win, NULL);
  3108. if (xw.xic == NULL)
  3109. die("XCreateIC failed. Could not obtain input method.\n");
  3110. /* white cursor, black outline */
  3111. cursor = XCreateFontCursor(xw.dpy, mouseshape);
  3112. XDefineCursor(xw.dpy, xw.win, cursor);
  3113. if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
  3114. xmousefg.red = 0xffff;
  3115. xmousefg.green = 0xffff;
  3116. xmousefg.blue = 0xffff;
  3117. }
  3118. if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
  3119. xmousebg.red = 0x0000;
  3120. xmousebg.green = 0x0000;
  3121. xmousebg.blue = 0x0000;
  3122. }
  3123. XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
  3124. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  3125. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  3126. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  3127. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  3128. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  3129. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  3130. PropModeReplace, (uchar *)&thispid, 1);
  3131. xresettitle();
  3132. XMapWindow(xw.dpy, xw.win);
  3133. xhints();
  3134. XSync(xw.dpy, False);
  3135. }
  3136. int
  3137. xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
  3138. {
  3139. float winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch, xp, yp;
  3140. ushort mode, prevmode = USHRT_MAX;
  3141. Font *font = &dc.font;
  3142. int frcflags = FRC_NORMAL;
  3143. float runewidth = xw.cw;
  3144. Rune rune;
  3145. FT_UInt glyphidx;
  3146. FcResult fcres;
  3147. FcPattern *fcpattern, *fontpattern;
  3148. FcFontSet *fcsets[] = { NULL };
  3149. FcCharSet *fccharset;
  3150. int i, f, numspecs = 0;
  3151. for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
  3152. /* Fetch rune and mode for current glyph. */
  3153. rune = glyphs[i].u;
  3154. mode = glyphs[i].mode;
  3155. /* Skip dummy wide-character spacing. */
  3156. if (mode == ATTR_WDUMMY)
  3157. continue;
  3158. /* Determine font for glyph if different from previous glyph. */
  3159. if (prevmode != mode) {
  3160. prevmode = mode;
  3161. font = &dc.font;
  3162. frcflags = FRC_NORMAL;
  3163. runewidth = xw.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
  3164. if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
  3165. font = &dc.ibfont;
  3166. frcflags = FRC_ITALICBOLD;
  3167. } else if (mode & ATTR_ITALIC) {
  3168. font = &dc.ifont;
  3169. frcflags = FRC_ITALIC;
  3170. } else if (mode & ATTR_BOLD) {
  3171. font = &dc.bfont;
  3172. frcflags = FRC_BOLD;
  3173. }
  3174. yp = winy + font->ascent;
  3175. }
  3176. /* Lookup character index with default font. */
  3177. glyphidx = XftCharIndex(xw.dpy, font->match, rune);
  3178. if (glyphidx) {
  3179. specs[numspecs].font = font->match;
  3180. specs[numspecs].glyph = glyphidx;
  3181. specs[numspecs].x = (short)xp;
  3182. specs[numspecs].y = (short)yp;
  3183. xp += runewidth;
  3184. numspecs++;
  3185. continue;
  3186. }
  3187. /* Fallback on font cache, search the font cache for match. */
  3188. for (f = 0; f < frclen; f++) {
  3189. glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
  3190. /* Everything correct. */
  3191. if (glyphidx && frc[f].flags == frcflags)
  3192. break;
  3193. /* We got a default font for a not found glyph. */
  3194. if (!glyphidx && frc[f].flags == frcflags
  3195. && frc[f].unicodep == rune) {
  3196. break;
  3197. }
  3198. }
  3199. /* Nothing was found. Use fontconfig to find matching font. */
  3200. if (f >= frclen) {
  3201. if (!font->set)
  3202. font->set = FcFontSort(0, font->pattern,
  3203. 1, 0, &fcres);
  3204. fcsets[0] = font->set;
  3205. /*
  3206. * Nothing was found in the cache. Now use
  3207. * some dozen of Fontconfig calls to get the
  3208. * font for one single character.
  3209. *
  3210. * Xft and fontconfig are design failures.
  3211. */
  3212. fcpattern = FcPatternDuplicate(font->pattern);
  3213. fccharset = FcCharSetCreate();
  3214. FcCharSetAddChar(fccharset, rune);
  3215. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  3216. fccharset);
  3217. FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
  3218. FcConfigSubstitute(0, fcpattern,
  3219. FcMatchPattern);
  3220. FcDefaultSubstitute(fcpattern);
  3221. fontpattern = FcFontSetMatch(0, fcsets, 1,
  3222. fcpattern, &fcres);
  3223. /*
  3224. * Overwrite or create the new cache entry.
  3225. */
  3226. if (frclen >= LEN(frc)) {
  3227. frclen = LEN(frc) - 1;
  3228. XftFontClose(xw.dpy, frc[frclen].font);
  3229. frc[frclen].unicodep = 0;
  3230. }
  3231. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  3232. fontpattern);
  3233. frc[frclen].flags = frcflags;
  3234. frc[frclen].unicodep = rune;
  3235. glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
  3236. f = frclen;
  3237. frclen++;
  3238. FcPatternDestroy(fcpattern);
  3239. FcCharSetDestroy(fccharset);
  3240. }
  3241. specs[numspecs].font = frc[f].font;
  3242. specs[numspecs].glyph = glyphidx;
  3243. specs[numspecs].x = (short)xp;
  3244. specs[numspecs].y = (short)(winy + frc[f].font->ascent);
  3245. xp += runewidth;
  3246. numspecs++;
  3247. }
  3248. return numspecs;
  3249. }
  3250. void
  3251. xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
  3252. {
  3253. int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
  3254. int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
  3255. width = charlen * xw.cw;
  3256. Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  3257. XRenderColor colfg, colbg;
  3258. XRectangle r;
  3259. /* Determine foreground and background colors based on mode. */
  3260. if (base.fg == defaultfg) {
  3261. if (base.mode & ATTR_ITALIC)
  3262. base.fg = defaultitalic;
  3263. else if ((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD))
  3264. base.fg = defaultitalic;
  3265. else if (base.mode & ATTR_UNDERLINE)
  3266. base.fg = defaultunderline;
  3267. }
  3268. if (IS_TRUECOL(base.fg)) {
  3269. colfg.alpha = 0xffff;
  3270. colfg.red = TRUERED(base.fg);
  3271. colfg.green = TRUEGREEN(base.fg);
  3272. colfg.blue = TRUEBLUE(base.fg);
  3273. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  3274. fg = &truefg;
  3275. } else {
  3276. fg = &dc.col[base.fg];
  3277. }
  3278. if (IS_TRUECOL(base.bg)) {
  3279. colbg.alpha = 0xffff;
  3280. colbg.green = TRUEGREEN(base.bg);
  3281. colbg.red = TRUERED(base.bg);
  3282. colbg.blue = TRUEBLUE(base.bg);
  3283. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  3284. bg = &truebg;
  3285. } else {
  3286. bg = &dc.col[base.bg];
  3287. }
  3288. /* Change basic system colors [0-7] to bright system colors [8-15] */
  3289. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
  3290. fg = &dc.col[base.fg + 8];
  3291. if (IS_SET(MODE_REVERSE)) {
  3292. if (fg == &dc.col[defaultfg]) {
  3293. fg = &dc.col[defaultbg];
  3294. } else {
  3295. colfg.red = ~fg->color.red;
  3296. colfg.green = ~fg->color.green;
  3297. colfg.blue = ~fg->color.blue;
  3298. colfg.alpha = fg->color.alpha;
  3299. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
  3300. &revfg);
  3301. fg = &revfg;
  3302. }
  3303. if (bg == &dc.col[defaultbg]) {
  3304. bg = &dc.col[defaultfg];
  3305. } else {
  3306. colbg.red = ~bg->color.red;
  3307. colbg.green = ~bg->color.green;
  3308. colbg.blue = ~bg->color.blue;
  3309. colbg.alpha = bg->color.alpha;
  3310. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
  3311. &revbg);
  3312. bg = &revbg;
  3313. }
  3314. }
  3315. if (base.mode & ATTR_REVERSE) {
  3316. temp = fg;
  3317. fg = bg;
  3318. bg = temp;
  3319. }
  3320. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
  3321. colfg.red = fg->color.red / 2;
  3322. colfg.green = fg->color.green / 2;
  3323. colfg.blue = fg->color.blue / 2;
  3324. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  3325. fg = &revfg;
  3326. }
  3327. if (base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
  3328. fg = bg;
  3329. if (base.mode & ATTR_INVISIBLE)
  3330. fg = bg;
  3331. /* Intelligent cleaning up of the borders. */
  3332. if (x == 0) {
  3333. xclear(0, (y == 0)? 0 : winy, borderpx,
  3334. winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
  3335. }
  3336. if (x + charlen >= term.col) {
  3337. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  3338. ((y >= term.row-1)? xw.h : (winy + xw.ch)));
  3339. }
  3340. if (y == 0)
  3341. xclear(winx, 0, winx + width, borderpx);
  3342. if (y == term.row-1)
  3343. xclear(winx, winy + xw.ch, winx + width, xw.h);
  3344. /* Clean up the region we want to draw to. */
  3345. XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
  3346. /* Set the clip region because Xft is sometimes dirty. */
  3347. r.x = 0;
  3348. r.y = 0;
  3349. r.height = xw.ch;
  3350. r.width = width;
  3351. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  3352. /* Render the glyphs. */
  3353. XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
  3354. /* Render underline and strikethrough. */
  3355. if (base.mode & ATTR_UNDERLINE) {
  3356. XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
  3357. width, 1);
  3358. }
  3359. if (base.mode & ATTR_STRUCK) {
  3360. XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
  3361. width, 1);
  3362. }
  3363. /* Reset clip to none. */
  3364. XftDrawSetClip(xw.draw, 0);
  3365. }
  3366. void
  3367. xdrawglyph(Glyph g, int x, int y)
  3368. {
  3369. int numspecs;
  3370. XftGlyphFontSpec spec;
  3371. numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
  3372. xdrawglyphfontspecs(&spec, g, numspecs, x, y);
  3373. }
  3374. void
  3375. xdrawcursor(void)
  3376. {
  3377. static int oldx = 0, oldy = 0;
  3378. int curx;
  3379. Glyph g = {' ', ATTR_NULL, defaultbg, defaultcs};
  3380. LIMIT(oldx, 0, term.col-1);
  3381. LIMIT(oldy, 0, term.row-1);
  3382. curx = term.c.x;
  3383. /* adjust position if in dummy */
  3384. if (term.line[oldy][oldx].mode & ATTR_WDUMMY)
  3385. oldx--;
  3386. if (term.line[term.c.y][curx].mode & ATTR_WDUMMY)
  3387. curx--;
  3388. g.u = term.line[term.c.y][term.c.x].u;
  3389. /* remove the old cursor */
  3390. xdrawglyph(term.line[oldy][oldx], oldx, oldy);
  3391. if (IS_SET(MODE_HIDE))
  3392. return;
  3393. /* draw the new one */
  3394. if (xw.state & WIN_FOCUSED) {
  3395. switch (xw.cursor) {
  3396. case 0: /* Blinking Block */
  3397. case 1: /* Blinking Block (Default) */
  3398. case 2: /* Steady Block */
  3399. if (IS_SET(MODE_REVERSE)) {
  3400. g.mode |= ATTR_REVERSE;
  3401. g.fg = defaultcs;
  3402. g.bg = defaultfg;
  3403. }
  3404. g.mode |= term.line[term.c.y][curx].mode & ATTR_WIDE;
  3405. xdrawglyph(g, term.c.x, term.c.y);
  3406. break;
  3407. case 3: /* Blinking Underline */
  3408. case 4: /* Steady Underline */
  3409. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3410. borderpx + curx * xw.cw,
  3411. borderpx + (term.c.y + 1) * xw.ch - cursorthickness,
  3412. xw.cw, cursorthickness);
  3413. break;
  3414. case 5: /* Blinking bar */
  3415. case 6: /* Steady bar */
  3416. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3417. borderpx + curx * xw.cw,
  3418. borderpx + term.c.y * xw.ch,
  3419. cursorthickness, xw.ch);
  3420. break;
  3421. }
  3422. } else {
  3423. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3424. borderpx + curx * xw.cw,
  3425. borderpx + term.c.y * xw.ch,
  3426. xw.cw - 1, 1);
  3427. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3428. borderpx + curx * xw.cw,
  3429. borderpx + term.c.y * xw.ch,
  3430. 1, xw.ch - 1);
  3431. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3432. borderpx + (curx + 1) * xw.cw - 1,
  3433. borderpx + term.c.y * xw.ch,
  3434. 1, xw.ch - 1);
  3435. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3436. borderpx + curx * xw.cw,
  3437. borderpx + (term.c.y + 1) * xw.ch - 1,
  3438. xw.cw, 1);
  3439. }
  3440. oldx = curx, oldy = term.c.y;
  3441. }
  3442. void
  3443. xsettitle(char *p)
  3444. {
  3445. XTextProperty prop;
  3446. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  3447. &prop);
  3448. XSetWMName(xw.dpy, xw.win, &prop);
  3449. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  3450. XFree(prop.value);
  3451. }
  3452. void
  3453. xresettitle(void)
  3454. {
  3455. xsettitle(opt_title ? opt_title : "st");
  3456. }
  3457. void
  3458. redraw(void)
  3459. {
  3460. tfulldirt();
  3461. draw();
  3462. }
  3463. void
  3464. draw(void)
  3465. {
  3466. drawregion(0, 0, term.col, term.row);
  3467. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
  3468. xw.h, 0, 0);
  3469. XSetForeground(xw.dpy, dc.gc,
  3470. dc.col[IS_SET(MODE_REVERSE)?
  3471. defaultfg : defaultbg].pixel);
  3472. }
  3473. void
  3474. drawregion(int x1, int y1, int x2, int y2)
  3475. {
  3476. int i, x, y, ox, numspecs;
  3477. Glyph base, new;
  3478. XftGlyphFontSpec* specs;
  3479. int ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN);
  3480. if (!(xw.state & WIN_VISIBLE))
  3481. return;
  3482. for (y = y1; y < y2; y++) {
  3483. if (!term.dirty[y])
  3484. continue;
  3485. xtermclear(0, y, term.col, y);
  3486. term.dirty[y] = 0;
  3487. specs = term.specbuf;
  3488. numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y);
  3489. i = ox = 0;
  3490. for (x = x1; x < x2 && i < numspecs; x++) {
  3491. new = term.line[y][x];
  3492. if (new.mode == ATTR_WDUMMY)
  3493. continue;
  3494. if (ena_sel && selected(x, y))
  3495. new.mode ^= ATTR_REVERSE;
  3496. if (i > 0 && ATTRCMP(base, new)) {
  3497. xdrawglyphfontspecs(specs, base, i, ox, y);
  3498. specs += i;
  3499. numspecs -= i;
  3500. i = 0;
  3501. }
  3502. if (i == 0) {
  3503. ox = x;
  3504. base = new;
  3505. }
  3506. i++;
  3507. }
  3508. if (i > 0)
  3509. xdrawglyphfontspecs(specs, base, i, ox, y);
  3510. }
  3511. xdrawcursor();
  3512. }
  3513. void
  3514. expose(XEvent *ev)
  3515. {
  3516. redraw();
  3517. }
  3518. void
  3519. visibility(XEvent *ev)
  3520. {
  3521. XVisibilityEvent *e = &ev->xvisibility;
  3522. MODBIT(xw.state, e->state != VisibilityFullyObscured, WIN_VISIBLE);
  3523. }
  3524. void
  3525. unmap(XEvent *ev)
  3526. {
  3527. xw.state &= ~WIN_VISIBLE;
  3528. }
  3529. void
  3530. xsetpointermotion(int set)
  3531. {
  3532. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  3533. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  3534. }
  3535. void
  3536. xseturgency(int add)
  3537. {
  3538. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  3539. MODBIT(h->flags, add, XUrgencyHint);
  3540. XSetWMHints(xw.dpy, xw.win, h);
  3541. XFree(h);
  3542. }
  3543. void
  3544. focus(XEvent *ev)
  3545. {
  3546. XFocusChangeEvent *e = &ev->xfocus;
  3547. if (e->mode == NotifyGrab)
  3548. return;
  3549. if (ev->type == FocusIn) {
  3550. XSetICFocus(xw.xic);
  3551. xw.state |= WIN_FOCUSED;
  3552. xseturgency(0);
  3553. if (IS_SET(MODE_FOCUS))
  3554. ttywrite("\033[I", 3);
  3555. } else {
  3556. XUnsetICFocus(xw.xic);
  3557. xw.state &= ~WIN_FOCUSED;
  3558. if (IS_SET(MODE_FOCUS))
  3559. ttywrite("\033[O", 3);
  3560. }
  3561. }
  3562. int
  3563. match(uint mask, uint state)
  3564. {
  3565. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  3566. }
  3567. void
  3568. numlock(const Arg *dummy)
  3569. {
  3570. term.numlock ^= 1;
  3571. }
  3572. char*
  3573. kmap(KeySym k, uint state)
  3574. {
  3575. Key *kp;
  3576. int i;
  3577. /* Check for mapped keys out of X11 function keys. */
  3578. for (i = 0; i < LEN(mappedkeys); i++) {
  3579. if (mappedkeys[i] == k)
  3580. break;
  3581. }
  3582. if (i == LEN(mappedkeys)) {
  3583. if ((k & 0xFFFF) < 0xFD00)
  3584. return NULL;
  3585. }
  3586. for (kp = key; kp < key + LEN(key); kp++) {
  3587. if (kp->k != k)
  3588. continue;
  3589. if (!match(kp->mask, state))
  3590. continue;
  3591. if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  3592. continue;
  3593. if (term.numlock && kp->appkey == 2)
  3594. continue;
  3595. if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  3596. continue;
  3597. if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
  3598. continue;
  3599. return kp->s;
  3600. }
  3601. return NULL;
  3602. }
  3603. void
  3604. kpress(XEvent *ev)
  3605. {
  3606. XKeyEvent *e = &ev->xkey;
  3607. KeySym ksym;
  3608. char buf[32], *customkey;
  3609. int len;
  3610. Rune c;
  3611. Status status;
  3612. Shortcut *bp;
  3613. if (IS_SET(MODE_KBDLOCK))
  3614. return;
  3615. len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
  3616. /* 1. shortcuts */
  3617. for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  3618. if (ksym == bp->keysym && match(bp->mod, e->state)) {
  3619. bp->func(&(bp->arg));
  3620. return;
  3621. }
  3622. }
  3623. /* 2. custom keys from config.h */
  3624. if ((customkey = kmap(ksym, e->state))) {
  3625. ttysend(customkey, strlen(customkey));
  3626. return;
  3627. }
  3628. /* 3. composed string from input method */
  3629. if (len == 0)
  3630. return;
  3631. if (len == 1 && e->state & Mod1Mask) {
  3632. if (IS_SET(MODE_8BIT)) {
  3633. if (*buf < 0177) {
  3634. c = *buf | 0x80;
  3635. len = utf8encode(c, buf);
  3636. }
  3637. } else {
  3638. buf[1] = buf[0];
  3639. buf[0] = '\033';
  3640. len = 2;
  3641. }
  3642. }
  3643. ttysend(buf, len);
  3644. }
  3645. void
  3646. cmessage(XEvent *e)
  3647. {
  3648. /*
  3649. * See xembed specs
  3650. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  3651. */
  3652. if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  3653. if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  3654. xw.state |= WIN_FOCUSED;
  3655. xseturgency(0);
  3656. } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  3657. xw.state &= ~WIN_FOCUSED;
  3658. }
  3659. } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
  3660. /* Send SIGHUP to shell */
  3661. kill(pid, SIGHUP);
  3662. exit(0);
  3663. }
  3664. }
  3665. void
  3666. cresize(int width, int height)
  3667. {
  3668. int col, row;
  3669. if (width != 0)
  3670. xw.w = width;
  3671. if (height != 0)
  3672. xw.h = height;
  3673. col = (xw.w - 2 * borderpx) / xw.cw;
  3674. row = (xw.h - 2 * borderpx) / xw.ch;
  3675. tresize(col, row);
  3676. xresize(col, row);
  3677. ttyresize();
  3678. }
  3679. void
  3680. resize(XEvent *e)
  3681. {
  3682. if (e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  3683. return;
  3684. cresize(e->xconfigure.width, e->xconfigure.height);
  3685. }
  3686. void
  3687. run(void)
  3688. {
  3689. XEvent ev;
  3690. int w = xw.w, h = xw.h;
  3691. fd_set rfd;
  3692. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  3693. struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
  3694. long deltatime;
  3695. /* Waiting for window mapping */
  3696. do {
  3697. XNextEvent(xw.dpy, &ev);
  3698. /*
  3699. * This XFilterEvent call is required because of XOpenIM. It
  3700. * does filter out the key event and some client message for
  3701. * the input method too.
  3702. */
  3703. if (XFilterEvent(&ev, None))
  3704. continue;
  3705. if (ev.type == ConfigureNotify) {
  3706. w = ev.xconfigure.width;
  3707. h = ev.xconfigure.height;
  3708. }
  3709. } while (ev.type != MapNotify);
  3710. ttynew();
  3711. cresize(w, h);
  3712. clock_gettime(CLOCK_MONOTONIC, &last);
  3713. lastblink = last;
  3714. for (xev = actionfps;;) {
  3715. FD_ZERO(&rfd);
  3716. FD_SET(cmdfd, &rfd);
  3717. FD_SET(xfd, &rfd);
  3718. if (pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
  3719. if (errno == EINTR)
  3720. continue;
  3721. die("select failed: %s\n", strerror(errno));
  3722. }
  3723. if (FD_ISSET(cmdfd, &rfd)) {
  3724. ttyread();
  3725. if (blinktimeout) {
  3726. blinkset = tattrset(ATTR_BLINK);
  3727. if (!blinkset)
  3728. MODBIT(term.mode, 0, MODE_BLINK);
  3729. }
  3730. }
  3731. if (FD_ISSET(xfd, &rfd))
  3732. xev = actionfps;
  3733. clock_gettime(CLOCK_MONOTONIC, &now);
  3734. drawtimeout.tv_sec = 0;
  3735. drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
  3736. tv = &drawtimeout;
  3737. dodraw = 0;
  3738. if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  3739. tsetdirtattr(ATTR_BLINK);
  3740. term.mode ^= MODE_BLINK;
  3741. lastblink = now;
  3742. dodraw = 1;
  3743. }
  3744. deltatime = TIMEDIFF(now, last);
  3745. if (deltatime > 1000 / (xev ? xfps : actionfps)) {
  3746. dodraw = 1;
  3747. last = now;
  3748. }
  3749. if (dodraw) {
  3750. while (XPending(xw.dpy)) {
  3751. XNextEvent(xw.dpy, &ev);
  3752. if (XFilterEvent(&ev, None))
  3753. continue;
  3754. if (handler[ev.type])
  3755. (handler[ev.type])(&ev);
  3756. }
  3757. draw();
  3758. XFlush(xw.dpy);
  3759. if (xev && !FD_ISSET(xfd, &rfd))
  3760. xev--;
  3761. if (!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  3762. if (blinkset) {
  3763. if (TIMEDIFF(now, lastblink) \
  3764. > blinktimeout) {
  3765. drawtimeout.tv_nsec = 1000;
  3766. } else {
  3767. drawtimeout.tv_nsec = (1E6 * \
  3768. (blinktimeout - \
  3769. TIMEDIFF(now,
  3770. lastblink)));
  3771. }
  3772. drawtimeout.tv_sec = \
  3773. drawtimeout.tv_nsec / 1E9;
  3774. drawtimeout.tv_nsec %= (long)1E9;
  3775. } else {
  3776. tv = NULL;
  3777. }
  3778. }
  3779. }
  3780. }
  3781. }
  3782. void
  3783. usage(void)
  3784. {
  3785. die("%s " VERSION " (c) 2010-2015 st engineers\n"
  3786. "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
  3787. " [-i] [-t title] [-T title] [-w windowid] [-e command ...]"
  3788. " [command ...]\n"
  3789. " st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
  3790. " [-i] [-t title] [-T title] [-w windowid] [-l line]"
  3791. " [stty_args ...]\n",
  3792. argv0);
  3793. }
  3794. int
  3795. main(int argc, char *argv[])
  3796. {
  3797. uint cols = 80, rows = 24;
  3798. xw.l = xw.t = 0;
  3799. xw.isfixed = False;
  3800. xw.cursor = 0;
  3801. ARGBEGIN {
  3802. case 'a':
  3803. allowaltscreen = 0;
  3804. break;
  3805. case 'c':
  3806. opt_class = EARGF(usage());
  3807. break;
  3808. case 'e':
  3809. if (argc > 0)
  3810. --argc, ++argv;
  3811. goto run;
  3812. case 'f':
  3813. opt_font = EARGF(usage());
  3814. break;
  3815. case 'g':
  3816. xw.gm = XParseGeometry(EARGF(usage()),
  3817. &xw.l, &xw.t, &cols, &rows);
  3818. break;
  3819. case 'i':
  3820. xw.isfixed = 1;
  3821. break;
  3822. case 'o':
  3823. opt_io = EARGF(usage());
  3824. break;
  3825. case 'l':
  3826. opt_line = EARGF(usage());
  3827. break;
  3828. case 't':
  3829. case 'T':
  3830. opt_title = EARGF(usage());
  3831. break;
  3832. case 'w':
  3833. opt_embed = EARGF(usage());
  3834. break;
  3835. case 'v':
  3836. default:
  3837. usage();
  3838. } ARGEND;
  3839. run:
  3840. if (argc > 0) {
  3841. /* eat all remaining arguments */
  3842. opt_cmd = argv;
  3843. if (!opt_title && !opt_line)
  3844. opt_title = basename(xstrdup(argv[0]));
  3845. }
  3846. setlocale(LC_CTYPE, "");
  3847. XSetLocaleModifiers("");
  3848. tnew(MAX(cols, 1), MAX(rows, 1));
  3849. xinit();
  3850. selinit();
  3851. run();
  3852. return 0;
  3853. }