My build of suckless st terminal
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.

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