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.

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