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.

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