Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4267 lines
92 KiB

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