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.

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