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.

4399 lines
94 KiB

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