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

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