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

4330 lines
93 KiB

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