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.

4375 lines
94 KiB

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