My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4420 lines
95 KiB

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