diff --git a/.clang-format b/.clang-format index d456f16..19acc21 100644 --- a/.clang-format +++ b/.clang-format @@ -2,3 +2,7 @@ BasedOnStyle: Chromium IndentWidth: 2 SpacesBeforeTrailingComments: 1 AllowShortIfStatementsOnASingleLine: WithoutElse + +AlignAfterOpenBracket: Align +BinPackParameters: true +AllowAllParametersOfDeclarationOnNextLine: true diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..9fa2b22 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,47 @@ +Checks: > + clang-analyzer-*, + -clang-analyzer-security.insecureAPI.str*, + -clang-analyzer-security.insecureAPI.mem*, + -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, + bugprone-*, + -bugprone-easily-swappable-parameters, + -bugprone-signal-handler, + cert-*, + -cert-msc54-cpp, + -cert-sig30-c, + modernize-*, + -modernize-macro-to-enum, + performance-*, + portability-*, + readability-*, + -readability-braces-around-statements, + +WarningsAsErrors: '' +HeaderFilterRegex: '.*' +FormatStyle: file + +CheckOptions: + - key: readability-magic-numbers.IgnoredIntegerValues + value: '0;1;2;3;10;255' + # - key: readability-magic-numbers.IgnoredValues + # value: '0;1;2;3;10;255' + - key: readability-identifier-naming.VariableCase + value: lower_case + - key: readability-identifier-naming.ConstantParameterCase + value: UPPER_CASE + + - key: readability-identifier-naming.ConstantCase + value: "UPPER_CASE" + + - key: readability-identifier-length.VariableThreshold + value: '2' + - key: readability-identifier-length.ParameterThreshold + value: '2' + - key: readability-identifier-length.LocalConstantThreshold + value: '2' + - key: readability-identifier-length.MemberThreshold + value: '2' + - key: readability-identifier-length.MinimumParameterNameLength + value: '2' + - key: readability-identifier-length.MinimumVariableNameLength + value: '2' diff --git a/.github/workflows/check-and-build.yml b/.github/workflows/check-and-build.yml index 80bcc75..36d1ed0 100644 --- a/.github/workflows/check-and-build.yml +++ b/.github/workflows/check-and-build.yml @@ -34,7 +34,7 @@ jobs: - run: find . -type f -name '*.sh' -not -path './assets/pkg/aur/*/src/*' | xargs shellcheck clangcheck: - name: Chang Check + name: Clang Check runs-on: ubuntu-24.04 steps: diff --git a/.gitignore b/.gitignore index d24feeb..097f96e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ dist/ /compile_commands.json .cache/ +# just in case, i dont wanna rebase ever again +valgrind-out.txt # nix build result result diff --git a/Makefile b/Makefile index 379e9be..a542b1e 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,10 @@ ALLFLAGS=$(CFLAGS) -I$(IDIR) LIBS=-lpam -_DEPS = util.h ui.h config.h auth.h efield.h keys.h users.h sessions.h chvt.h +_DEPS = log.h util.h ui.h ui_state.h config.h desktop.h auth.h ofield.h efield.h keys.h users.h sessions.h chvt.h macros.h DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) -_OBJ = main.o util.o ui.o config.o auth.o efield.o users.o sessions.o chvt.o +_OBJ = main.o log.o util.o ui.o ui_state.o config.o desktop.o auth.o ofield.o efield.o users.o sessions.o chvt.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) $(ODIR)/%.o: $(CDIR)/%.c $(DEPS) @@ -72,3 +72,9 @@ install-service-openrc: install-service-s6: rsync -a --no-owner --no-group ./assets/services/s6/. /etc/s6/sv/lidm @printf '\x1b[1m%s\x1b[0m\n\n' " don't forget to run 's6-service add default lidm' and 's6-db-reload'" + +pre-commit: + codespell + find . -type f -name '*.sh' -not -path './assets/pkg/aur/*/src/*' | xargs shellcheck + clang-format -i $$(git ls-files "*.c" "*.h") + clang-tidy -p . $$(git ls-files "*.c" "*.h") diff --git a/README.md b/README.md index 5042d5b..dbbc413 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ # LiDM -LiDM is a really light UI portion a unix [login manager](https://en.wikipedia.org/wiki/Login_manager) made in C, highly customizable and held together by hopes and prayers ๐Ÿ™. +LiDM is a really light unix [login manager](https://en.wikipedia.org/wiki/Login_manager) made in C, highly customizable and held together by hopes and prayers ๐Ÿ™. -LiDM is like any [X Display Manager](https://en.wikipedia.org/wiki/X_display_manager) you have seen such as SDDM or GDM but without using X org graphics, instead being a purely [text based interface](https://en.wikipedia.org/wiki/Text-based_user_interface). +LiDM is like any [Display Manager](https://en.wikipedia.org/wiki/X_display_manager) you have seen such as SDDM or GDM but without using any X.org graphics at all. Instead being a purely [text based interface](https://en.wikipedia.org/wiki/Text-based_user_interface) inside your terminal/TTY. ![demo gif](assets/media/lidm.gif) @@ -16,43 +16,27 @@ LiDM is like any [X Display Manager](https://en.wikipedia.org/wiki/X_display_man ## Features -* Builds **FAST**. - * `a32e4a5`: - * `2.830s`: laptop, -O3, -j2, `AMD E-450 APU with Radeon(tm) HD Graphics` - * `0.172s`: desktop, -O3, -j12, `AMD Ryzen 5 5600G with Radeon Graphics` - * `663427e`: - * `0.085s`: desktop, -O0, `AMD Ryzen 5 5600G with Radeon Graphics` - * `0.009s`: desktop, -O0, CC=tcc, `AMD Ryzen 5 5600G with Radeon Graphics` -* Works everywhere you can get gcc to compile. -* Fast and possibly efficient. +* Simple as C, you only need a C compiler and standard unix libraries to build this. * Fully customizable, from strings, including action keys, to colors (I hope you know ansi escape codes) * Automatically detects xorg and wayland sessions, plus allowing to launch the default user shell (if enabled in config) * Starts with many init systems (systemd, dinit, runit, openrc and s6). -## WIP - -* Desktop's file `TryExec` key. -* Save last selection. -* Show/hide passwd switch. -* Long sessions, strings, usernames, passwords... they will just overflow or fuck your terminal, I know it and I don't know if I'll fix it. -* UTF characters or any multi-byte character, not yet supported properly, everything treats characters as a single byte, some chars might work or not depending on the context, but it's not designed to. - # Index -- [LiDM](#lidm) - - [Features](#features) - - [WIP](#wip) -- [Index](#index) -- [Ideology](#ideology) -- [Usage](#usage) - - [Arguments](#arguments) - - [Program](#program) -- [Requirements](#requirements) -- [Installation](#installation) -- [Configuring](#configuring) -- [Contributing](#contributing) -- [Backstory](#backstory) -- [Contributors](#contributors) +* [LiDM](#lidm) + * [Features](#features) + * [WIP](#wip) +* [Index](#index) +* [Ideology](#ideology) +* [Usage](#usage) + * [Arguments](#arguments) + * [Program](#program) +* [Requirements](#requirements) +* [Installation](#installation) +* [Configuring](#configuring) +* [Contributing](#contributing) +* [Inspiration](#inspiration) +* [Contributors](#contributors) # Ideology @@ -66,66 +50,73 @@ We all know that the most important thing in a project is the ideology of the au ### Arguments -If a single argument is provided (don't even do `--` or standard parsing...), it passes that argument to `chvt` on startup, used (at least) by most service files. +If a single argument is provided (don't even do `--` or standard unix parsing...), it switches to said tty number at startup. Used (at least) by most service files. ### Program -On top of pure intuition: +Base (mostly intuitive): -* You can change focus of session/user/passwd with up/down arrows. -* In case arrow keys do nothing on the focused input (Either is empty text or doesn't have more options), it tries to change session and if there's only one session it changes user. -* Typing anything will allow to put a custom user or shell command too, you can use arrow keys to move. -* ESC and then left/right arrows will force to change the option of the focused input, useful if you're editing the current input and arrow keys just move. -* Editing a predefined option on a user or a shell session, will put you in edit mode preserving the original value, other cases start from scratch. +* Use arrow keys to navigate the inputs and type over any of them to override the default value. +* Enter will just attempt to login. +* If you are focused on an edited input, horizontal arrow keys will attempt to move across the text just as expected. + +On top of that: + +* Using the horizontal arrow keys if the focused input is not in text mode or the movement would overflow the input. It will try to change in such direction the option of session or the user. +* Pressing ESC and then horizontal arrows will force to change the option of the focused input even if it's in edit mode. +* Editing an option on a user or a shell session will put you in edit mode appending after the original value. # Requirements -* A computer with unix based system. -* That system should have the resources necessary for this program to make sense (Sessions, users...). -* A compiler (optional, you can compile by hand, but I doubt you want to see the code). * Make (Also optional, but does things automatically, make sure `gcc` and `mkdir -p` work as expected). -* PAM, used for user authentication, just what `login` or `su` use internally. Don't worry, it's surely pre-installed. +* A compiler like `cc`, `gcc` or `clang`. Make sure to use the desired `CC=` on the `make` command. +* PAM library, used for user authentication, just what `login` or `su` use internally. Don't worry, it's surely pre-installed. # Installation -Check the [installation guide](./INSTALL.md) to use your preferred installation source. +Check the [installation guide](INSTALL.md) to use your preferred installation source. # Configuring -Copy any `.ini` file from [`themes/`](./themes/) (`default.ini` will always be updated) to `/etc/lidm.ini` and/or configure it to your liking. Also, don't place empty lines (for now). You can also set `LIDM_CONF` environment variable to specify a config path. +Copy any `.ini` file from [`themes/`](./themes/) (`default.ini` will always be updated) to `/etc/lidm.ini` and/or configure it to your liking. You can also set `LIDM_CONF` environment variable to specify a config path. -Configured colors are just gonna be put inside `\x1b[...m`, ofc you can add an 'm' to break this and this can f\*ck up really bad or even make some nice UI effect possible, you should also be able to embed the `\x1b` byte in the config as I won't parse escape codes, I think that the parser is just gonna grab anything in the config file from the space after the `=` (yes, I'ma enforce that space, get good taste if you don't like it) until the newline, you can put any abomination in there. But please, keep in mind this might break easily. +The format attempts to mimic the TOML format. Escape sequences like `\x1b` are allowed as well as comments and empty lines. -The default fg style should disable decorators set up in other elements (cursive, underline... it's just adding 20 to the number btw, so if cursive is 4 (iirc), disabling it is 24). +Colors are gonna be put inside `\x1b[...m`, if you don't know what this is check [an ANSI table](https://gist.github.com/JBlond/2fea43a3049b38287e5e9cefc87b2124). Mind that `\x1b` is the same as `\e`, `\033` and several other representations. + +> [!NOTE] +> The default `fg` style should disable decorators set up in other elements (cursive, underline...). It's just adding 20 to the number, so if an underline is 4, disabling it is done with 24. > [!TIP] > If you don't like seeing an element, you can change the fg color of it to be the same as the bg, making it invisible. # Contributing -If you want to contribute check the [CONTRIBUTING.md](docs/CONTRIBUTING.md) +If you want to contribute check the [contribution guide](docs/CONTRIBUTING.md). -# Backstory +# Inspiration -Summer travelling to visit family with an old laptop that barely supports x86\_64, and ly recently added some avx2 instructions I think (invalid op codes), manually building (any previous commit too) didn't work because of something in the `build.zig` file, so out of boredom I decided to craft up my own simple display manager on the only language this thing can handle... **C** (I hate this and reserve the right for the rust rewrite, actually solid). +This project was started after facing some issues building [ly](https://github.com/fairyglade/ly) on an ancient laptop, the UI is heavily inspired by it. -I spedrun it in roughly 3 days and I'm bad af in C, so this is spaghetti code on **another** level. I think it doesn't do almost anything unsafe, I mean, I didn't check allocations and it's capable of reallocating memory until your username uses all memory, crashing the system due to a off-by-one error, but pretty consistent otherwise (probably). +For this reason the project's philosophy is to be simple and minimal, such that even prehistoric hardware is capable of running it. -The name is just ly but changing "y" with "i", that had a reason but forgot it, (maybe the i in *simple*), so I remembered this sh\*tty laptop with a lid, this thing is also a display manager (dm, ly command is also `ly-dm`), so just did lidm due to all that. +I forgot what exactly the name came from, but it surely was a mix of a few things so: + +* Obviously it's inspired by `ly`. `ly-dm` leads to "lydm". +* Wow make "lydm" simple with a "y" โ†’ "i" transformation. +* Associate it with the "i" in s**i**mple and other display managers like **Li**ghtDM. +* And the **la**ptop this project started in has a **lid**. # Contributors [![GitHub Contributors](https://contrib.rocks/image?repo=javalsai/lidm\&max=20)](https://github.com/javalsai/lidm/graphs/contributors) -* KillerTofus, [made the AUR package](https://github.com/javalsai/lidm/pull/2)! Saved me from reading the Arch Wiki ๐Ÿ’€. -* DeaDvey, the most awesomest of all, did some pretty HARDCORE gramer checking. (and trolling, he wrote that, 33 commits of just readme changes ffs) -* grialion, made a simple C implementation of `chvt` instead of insecurely relying on `kbd utils`'s command. -* cerealexperiments\_, found a missing newline (had the guts to read the source code, crazy ik) -* ChatGPT, in times of slow laptops where pages take ages to load, a single tab connected to a bunch of burning cloud GPUs feeding corporate hype is all you need to get quick answers for your questions, as long as you know how to filter AI crap ofc. -* [My lack of gf](https://www.instagram.com/reel/C8sa3Gltmtq), can't imagine this project being possible if somebody actually cared about me daily. +[killertofus](https://github.com/killertofus), [deadvey](https://github.com/deadvey), [grialion](https://github.com/grialion/), cerealexperiments\_, [antiz96](https://github.com/Antiz96), [rmntgx](https://github.com/rmntgx) and [more...](https://github.com/javalsai/lidm/graphs/contributors) ---- +With their big or small contributions, every commit has helped in its own way and encouraged me to keep putting my soul into this. -๐ŸŒŸ Finally, consider starring this repo or... ๐Ÿ”ช +*** -![star-history](https://api.star-history.com/svg?repos=javalsai/lidm&type=Date) +๐ŸŒŸ Finally, consider starring this repo [or...](https://www.reddit.com/r/github/comments/1l2mchg/is_this_allowed) ๐Ÿ”ช + +![star-history](https://api.star-history.com/svg?repos=javalsai/lidm\&type=Date) diff --git a/assets/man/lidm-config.5 b/assets/man/lidm-config.5 index 6641924..8f25ac2 100644 --- a/assets/man/lidm-config.5 +++ b/assets/man/lidm-config.5 @@ -10,76 +10,96 @@ lidm-config \- Configuration file syntax for lidm \fB\fI/etc/lidm.ini\fP +.SH THEMES +There are themes you can take as reference or simply copy in \fBthemes/\fP, no need to tackle the config yourself. It's good to give this a read however. + .SH DESCRIPTION -The \fI/etc/lidm.ini\fP file specifies all the configuration for lidm, including theme colors. +The \fI/etc/lidm.ini\fP file specifies all the configuration for lidm, including theme colors. It's also possible to override this path with the \fILIDM_CONF\fP environment variable. -The config parser is very primitive still, so the file only consists of \fBkey/value\fP pairs separated by \fB' = '\fP (yes, the surrounding spaces are necessary). It will also not warn if the config is invalid. +The configuration aims to mimic a toml syntax but it's not 100% compatible with it. It's grouped in tables and takes key/value pairs parsed in different modes depndending on the type. Comments and empty lines are allowed as well as surrounding whitespace at the edges of the line and the equals sign. -You can't escape characters with \fB'\\'\fP, but the program reads until the end of line, so you can put any raw bytes there. +Values can be of these types: +.TP +\fBSTRING\fP +Needs to be quoted with double quotes, allows basic character escaping with a backslash as well as bytes of the form \fB\\xHH\fP, please don't put a nullbyte. +.TP +\fBBOOL\fP +Either \fBtrue\fP or \fBfalse\fP, no quoting. +.TP +\fBKEY\fP +Represents a keyboard key, must be unquoted, possible values are enumerated in \fBinclude/keys.h\fP. +.TP +\fBSTRING_ARRAY\fP +A list of strings, especified as a normal string, but reassigning to the same variable will append to that list. .SH KEYS -Similar keys are grouped together to keep this as short as possible. +In case anything here is outdated, all the config structure is defined in \fBinclude/config.h\fP, you can also find the default values there. This reference is separated by tables. -.SS Colors -All keys under this section are always wrapped inside ansi sequences (\fB\\x1b[...m\fP). +.SS colors +All these values are of type \fBSTRING\fP. .TP -\fBcolors.bd, colors.fg, colors.err\fP +\fBbd, fg, err\fP Background, foreground and error escape sequences. \fB'fg'\fP is also used as reset sequence, so it must remove effects used in other keys (such as bold, underline...) \fBWITHOUT\fP using the \fB'0'\fP sequence, as that would remove the background color. .TP -\fBcolors.s_wayland, colors.s_xorg, colors.s_shell\fP +\fBs_wayland, s_xorg, s_shell\fP Coloring for sessions of such types (Wayland, X.org, Shells) .TP -.TP -\fBcolors.e_hostname, colors.e_date, colors.e_box\fP +\fBe_hostname, e_date, e_box\fP Coloring for the hostname, date and box elements. .TP -\fBcolors.e_header\fP +\fB.e_header\fP Coloring for heading elements (left column) .TP -\fBcolors.e_user, colors.e_passwd, colors.e_badpasswd\fP +\fBe_user, e_passwd, e_badpasswd\fP Coloring for the user element, password and bad padssword. .TP -\fBcolors.e_key\fP +\fBe_key\fP Coloring for key elements (eg: F1, F2, CTRL...) -.SS -Single characters used for some elements (can be longer than a character, but it will likely break UI) +.SS chars +Single characters used for some elements (could be longer than a character, but it will likely break UI code assumptions), these are also of type \fBSTRING\fP. .TP -\fBchars.hb, chars.vb\fP +\fBhb, vb\fP Character for the horizontal bar (hb) and vertical bar (vb). .TP -\fBchars.ctl, chars.ctr, chars.cbl, chars.cbr\fP +\fBctl, ctr, cbl, cbr\fP Characters for the corners of the box (ctl = corner top left, cbr = corner bottom right) -.SS Functions +.SS functions +All these are of type \fBKEY\fP. .TP -\fBfunctions.poweroff, functions.reboot, functions.refresh\fP +\fBpoweroff, reboot, refresh\fP Function key to use for such action. -.SS String -Strings to use for some elements. +.SS strings +Display strings to use for some elements. .TP -\fBstrings.f_poweroff, strings.f_reboot, strings.f_refresh\fP -Text displayed to name such functions. +\fBf_poweroff, f_reboot, f_refresh\fP +Text displayed to name such functions at the bottom of the screen. .TP -\fBstrings.e_user, strings.e_passwd\fP +\fBe_user, e_passwd\fP Text to display for these element headers. .TP -\fBstrings.s_wayland, strings.s_xorg, strings.s_shell\fP +\fBs_wayland, s_xorg, s_shell\fP Text to display as the header for such sessions. +.TP +\fBopts_pre, opts_post\fP +To be used for items with multiple options on the left and right sides \fB"< "\fP and \fB" >"\fP. -.SS Behavior +.SS behavior +Other miscellaneous behavior strings, of mixed types .TP -\fBbehavior.include_defshell\fP -"true"/"false" (invalid input defaults to false), if true, includes the user default shell as a session to launch +\fBinclude_defshell\fP +Type \fBBOOL\fP, specifies if the default user shel should be included as a session. .TP -\fBbehavior.source\fP -Specify paths to source on login, simple KEY=VALUE format with comments (#) or empty'ish lines, quoting or escape sequences not supported yet. It is NOT an array, but you rather assign to it multiple times. +\fBsource, user_source\fP +Types \fBSTRING_ARRAY\fP. Specify paths to source on login if they exist, simple KEY=VALUE format with comments (#) or empty'ish lines, quoting or escape sequences not supported yet. \fBuser_source\fP paths are relative to user home. .TP -\fBbehavior.user_source\fP -Same as \fIbehavior.source\fP but relative to user home (if present). +\fBtimefmt\fP +Specify the time format string to be displayed. Check \fBstrftime (3)\fP to know the possible formatting variables. .SH "SEE ALSO" .BR lidm (1) +.BR strftime (3) diff --git a/assets/man/lidm.1 b/assets/man/lidm.1 index b4c161a..dd341e5 100644 --- a/assets/man/lidm.1 +++ b/assets/man/lidm.1 @@ -23,6 +23,9 @@ TTY to switch to, does not mean that it will run in it. .TP \fBLIDM_CONF\fP Specify the config to use other than \fI/etc/lidm.ini\fP +.TP +\fBLIDM_LOG\fP +Optionally, a path to output misc logs and potential silent failures. .SH FILES .TP diff --git a/assets/media/lidm.gif b/assets/media/lidm.gif index 0d4e892..562243b 100644 Binary files a/assets/media/lidm.gif and b/assets/media/lidm.gif differ diff --git a/assets/services/README.md b/assets/services/README.md index e99db11..f978157 100644 --- a/assets/services/README.md +++ b/assets/services/README.md @@ -8,6 +8,9 @@ There's make scripts to automatically copy the service files to the proper locat The manual steps for installation are: +> [!IMPORTANT] +> If you think this should be packaged too, there's [a discussion](https://github.com/javalsai/lidm/discussions/39) to talk about this. + ## Systemd * Copy `systemd.service` to `/etc/systemd/system/lidm.service` @@ -35,5 +38,5 @@ The manual steps for installation are: * Add the service with `s6-service add default lidm` * Reload the database with `s6-db-reload` (you might have to run this every time the service file changes) -> \[!WARNING] +> [!WARNING] > Make sure to disable any other service that might run on tty7, such us lightdm or most display managers out there. diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index f1a0c38..0f4ae69 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -2,38 +2,46 @@ Contributions are welcome! Here's how you can help: -- [Contributing code](#code) -- [Reporting issues](#issues) +* [Contributing code](#code) +* [Reporting issues](#issues) +* [Other](#other) ## Code -For small fixes or incremental improvements simply fork the repo and follow the process below. For larger changes submit an [RFC:](RFC.md) +For small fixes or incremental improvements simply fork the repo and follow the process below. + 1. [Fork](https://help.github.com/articles/fork-a-repo/) the repository and [clone](https://help.github.com/articles/cloning-a-repository/) your fork. -2. Start coding! - - Configure clangd LSP by generating `compile_commands.json` (e.g. `bear -- make` or `compiledb make`) - - Implement your feature. - - Check your code works as expected. - - Run the code formatter: `clang-format -i $(git ls-files "*.c" "*.h")` +2. Start coding! (it might be helpful to read a [quide on the project structure and conventions](structure.md) before this) + * Configure clangd LSP by generating `compile_commands.json` (e.g. `bear -- make` or `compiledb make`) + * Implement your feature. + * Check your code works as expected. + * Run the code formatter: `clang-format -i $(git ls-files "*.c" "*.h")` + * Run the code linter: `clang-tidy -p . $(git ls-files "*.c" "*.h")`. Some checks are pretty pedantic, feel free to ignore or debate some of the rules. + * If you prefer, you can run `make pre-commit` to run several code style checks like the avobe along a few extra stuff. 3. Commit your changes to a new branch (not `master`, one change per branch) and push it: - - Commit messages should: - - Header line: explain the commit in one line (use the imperative) - - Be descriptive. - - Have a first line with less than *80 characters* and have a second line that is *empty* if you want to add a description. + * Commit messages should: + * Header line: explain the commit in one line (use the imperative) ("feat" for features, "fix", "style", "chore", "docs", etc) + * Be descriptive. + * Don't make the title too long and add commit descriptions if you think the changes need it. + * e.g. "feat: add support for X", "fix(config): config parser segfaulting", "docs(typo): fix a typo in README.md" 4. Once you are happy with your changes, submit a pull request. - - Open the pull request. - - Add a short description explaining briefly what you've done (or if it's a work-in-progress - what you need to do) + * Open the pull request. + * Add a short description explaining what you've done (or if it's a work-in-progress - what you need to do) ## Issues 1. Do a quick search on GitHub to check if the issue has already been reported. 2. [Open an issue](https://github.com//javalsai/lidm/issues/new) and describe the issue you are having - you could include: - - Screenshots. - - Ways to reproduce the issue. - - Your lidm version. - - Your platform (e.g. arch linux or Ubuntu 15.04 x64) + * Screenshots. + * Ways to reproduce the issue. + * Your lidm version. + * Your platform (e.g. arch linux or Ubuntu 15.04 x64) and init system if you know. After reporting you should aim to answer questions or clarifications as this helps pinpoint the cause of the issue. +## Other + +If you are unsure what category your contribution falls under, feel free to [open an issue](https://github.com//javalsai/lidm/issues/new) or [a discussion](https://github.com//javalsai/lidm/discussions/new) to talk about it. diff --git a/docs/structure.md b/docs/structure.md new file mode 100644 index 0000000..32c4a3d --- /dev/null +++ b/docs/structure.md @@ -0,0 +1,89 @@ +# Introduction + +This file aims to explain the basic project structure and some code conventions +and patterns used for contributors. If you plan on contributing a considerable +amount of code, please read this thoroughly to keep the project behavior +consistent. + +Even if your changes are not very related to the code, this guide can help to +understand how they can be related to the rest of the project and what side +changes it can require. + +# Structure + +The file structure is very simple, you have `.c` files in `src/` with their +header `.h` counterpart in `include/`, they are listed in the `Makefile` and +built all together on the same layer. + +Each file contains functions aimed to provide some specialized behavior, they +tend to follow a common prefix (like `vec_` for vector functions) to avoid name +collisions with themselves. + +## Important Files + +The `main.c` of course provides the entry point of the program, then each file +has special functionality, but a special one is `util.h`, is linked with almost +every file and provides some shared utilities to dealing with UTF-8, dynamic +Vectors and other stuff. + +`log.c` is also an important file as it provides logging utilities for +debugging, there's no need to deal with it's initialization, it behaves just +like standard printing utilities but integrated into the configured logfile. + +# Debugging + +The log module can be easily used by just setting up the `LIDM_LOG` +environmental variable to the logs output path. + +# Header Files + +But what if you create a new file? It's important that the definitions are only +evaluated once by the compiler, even if they are included by several files. For +this, you can use this simple trick (assume this is for a file named +`mylib.h`): + +```h +#ifndef MYLIBH_ +#define MYLIBH_ + +// library contents +// ... + +#endif +``` + +It's also a good idea to include brief comments above functions if it's not +evident what they do and name all parameters. + +# Nullability Checks + +Nullability checks are not really enforced in the code but it's never a bad +idea to include them, however, `gcc` doesn't support them under certain +conditions. + +For this you can use `NULLABLE`, `NNULLABLE` and `UNULLABLE` macros from +`"macros.h"` to conditionally include `_Nullable`, `_Nonnull` and +`_Null-unspecified` respectively. + +# Handling & Support + +Every function should properly handle allocation failures (avoid them with +stack arrays where possible), support UTF-8 strings, use the `log` module +instead of printing to stdout (messing with the state of the ui) and free all +lost memory (if it can be lost before reaching the final stage where it execs +another program, I'm fine with not freeing memory still reachable at the end). + +# Code format, style and linting + +Be reasonable and follow `clang-format` and `clang-tidy` rules. `clang-tidy` +can be quite pedantic with the current primitive rules, so feel free to use +`LINTIGNORE` or discuss if said rule should be enforced in first place. + +Also avoid grammar typos and for shell scripts use `shellcheck`, you can run +this checks with `make pre-commit` if you have the required tools. + +# Documentation + +Please, also check if there's documentation related to the feature/changes you +are implementing, could be on markdown or manpages, if there is and you have +some spare time, it's really helpful to update it; although not enforced. diff --git a/include/auth.h b/include/auth.h index c3ef186..2464a86 100644 --- a/include/auth.h +++ b/include/auth.h @@ -1,15 +1,12 @@ -#ifndef _AUTHH_ -#define _AUTHH_ +#ifndef AUTHH_ +#define AUTHH_ #include #include "config.h" #include "sessions.h" -bool launch(char* user, - char* passwd, - struct session session, - void (*cb)(void), - struct behavior* behavior); +bool launch(char* user, char* passwd, struct session session, void (*cb)(void), + struct config* config); #endif diff --git a/include/chvt.h b/include/chvt.h index a39af33..8883ada 100644 --- a/include/chvt.h +++ b/include/chvt.h @@ -1,5 +1,5 @@ -#ifndef _CHVTH_ -#define _CHVTH_ +#ifndef CHVTH_ +#define CHVTH_ #include #include diff --git a/include/config.h b/include/config.h index 5ad3fd0..108923b 100644 --- a/include/config.h +++ b/include/config.h @@ -1,83 +1,163 @@ -#ifndef _CONFIGH_ -#define _CONFIGH_ +#ifndef CONFIGH_ +#define CONFIGH_ #include -#include +#include #include "keys.h" +#include "macros.h" #include "util.h" +enum introspection_type { + STRING, + BOOL, + KEY, + STRING_ARRAY, +}; +static const char* NNULLABLE const INTROS_TYS_NAMES[] = { + [STRING] = "STRING", + [BOOL] = "BOOL", + [KEY] = "KEY", + [STRING_ARRAY] = "STRING_ARRAY", +}; + +struct introspection_item { + char* NNULLABLE name; + size_t offset; + enum introspection_type typ; +}; + +#define INTROS_ITEM(key, table, ty) \ + { \ + .name = #key, \ + .offset = offsetof(table, key), \ + .typ = (ty), \ + } + +#define STRUCT_BUILDER(cty, name, ty, def, tname) cty name; +#define DEFAULT_BUILDER(cty, name, ty, def, tname) .name = (def), +#define INTROS_BUILDER(cty, name, ty, def, tname) \ + INTROS_ITEM(name, struct table_##tname, ty), + +#define BUILD_TABLE(table, TABLE) \ + struct table_##table { \ + TABLE(STRUCT_BUILDER, table) \ + } +#define BUILD_DEFAULT(utable, table, TABLE) \ + static const struct table_##table DEFAULT_TABLE_##utable = { \ + TABLE(DEFAULT_BUILDER, table)} +#define BUILD_INTROS(utable, table, TABLE) \ + static const struct introspection_item INTROS_TABLE_##utable[] = { \ + TABLE(INTROS_BUILDER, table)} + +#define BUILD(table, utable, TABLE) \ + BUILD_TABLE(table, TABLE); \ + BUILD_DEFAULT(utable, table, TABLE); \ + BUILD_INTROS(utable, table, TABLE); + // should be ansi escape codes under \x1b[...m // if not prepared accordingly, it might break -struct theme_colors { - char* bg; - char* fg; - char* err; - char* s_wayland; - char* s_xorg; - char* s_shell; - char* e_hostname; - char* e_date; - char* e_box; - char* e_header; - char* e_user; - char* e_passwd; - char* e_badpasswd; - char* e_key; -}; +#define TABLE_COLORS(F, name) \ + F(char* NNULLABLE, bg, STRING, "48;2;38;28;28", name) \ + F(char* NNULLABLE, fg, STRING, "22;24;38;2;245;245;245", name) \ + F(char* NNULLABLE, err, STRING, "1;31", name) \ + F(char* NNULLABLE, s_wayland, STRING, "38;2;255;174;66", name) \ + F(char* NNULLABLE, s_xorg, STRING, "38;2;37;175;255", name) \ + F(char* NNULLABLE, s_shell, STRING, "38;2;34;140;34", name) \ + F(char* NNULLABLE, e_hostname, STRING, "38;2;255;64;64", name) \ + F(char* NNULLABLE, e_date, STRING, "38;2;144;144;144", name) \ + F(char* NNULLABLE, e_box, STRING, "38;2;122;122;122", name) \ + F(char* NNULLABLE, e_header, STRING, "4;38;2;0;255;0", name) \ + F(char* NNULLABLE, e_user, STRING, "36", name) \ + F(char* NNULLABLE, e_passwd, STRING, "4;38;2;245;245;205", name) \ + F(char* NNULLABLE, e_badpasswd, STRING, "3;4;31", name) \ + F(char* NNULLABLE, e_key, STRING, "38;2;255;174;66", name) + +BUILD(colors, COLORS, TABLE_COLORS); // even if they're multiple bytes long // they should only take up 1 char size on display -struct theme_chars { - char* hb; - char* vb; +#define TABLE_CHARS(F, name) \ + F(char* NNULLABLE, hb, STRING, "โ”€", name) \ + F(char* NNULLABLE, vb, STRING, "โ”‚", name) \ + F(char* NNULLABLE, ctl, STRING, "โ”Œ", name) \ + F(char* NNULLABLE, ctr, STRING, "โ”", name) \ + F(char* NNULLABLE, cbl, STRING, "โ””", name) \ + F(char* NNULLABLE, cbr, STRING, "โ”˜", name) - char* ctl; - char* ctr; - char* cbl; - char* cbr; -}; +BUILD(chars, CHARS, TABLE_CHARS); -struct theme { - struct theme_colors colors; - struct theme_chars chars; -}; +#define TABLE_FUNCTIONS(F, name) \ + F(enum keys, poweroff, KEY, F1, name) \ + F(enum keys, reboot, KEY, F2, name) \ + F(enum keys, refresh, KEY, F5, name) -struct functions { - enum keys poweroff; - enum keys reboot; - enum keys refresh; -}; +BUILD(functions, FUNCTIONS, TABLE_FUNCTIONS); -struct strings { - char* f_poweroff; - char* f_reboot; - char* f_refresh; - char* e_user; - char* e_passwd; - char* s_wayland; - char* s_xorg; - char* s_shell; -}; +#define TABLE_STRINGS(F, name) \ + F(char* NNULLABLE, f_poweroff, STRING, "poweroff", name) \ + F(char* NNULLABLE, f_reboot, STRING, "reboot", name) \ + F(char* NNULLABLE, f_refresh, STRING, "refresh", name) \ + F(char* NNULLABLE, e_user, STRING, "user", name) \ + F(char* NNULLABLE, e_passwd, STRING, "password", name) \ + F(char* NNULLABLE, s_wayland, STRING, "wayland", name) \ + F(char* NNULLABLE, s_xorg, STRING, "xorg", name) \ + F(char* NNULLABLE, s_shell, STRING, "shell", name) \ + F(char* NNULLABLE, opts_pre, STRING, "< ", name) \ + F(char* NNULLABLE, opts_post, STRING, " >", name) -struct behavior { - bool include_defshell; - struct Vector source; - struct Vector user_source; -}; +BUILD(strings, STRINGS, TABLE_STRINGS); +#define NULL_VEC \ + (struct Vector) { \ + 0, 0, NULL \ + } +#define TABLE_BEHAVIOR(F, name) \ + F(bool, include_defshell, BOOL, true, name) \ + F(struct Vector, source, STRING_ARRAY, NULL_VEC, name) \ + F(struct Vector, user_source, STRING_ARRAY, NULL_VEC, name) \ + F(char* NNULLABLE, timefmt, STRING, "%X %x", name) + +BUILD(behavior, BEHAVIOR, TABLE_BEHAVIOR); + +//// CONFIG struct config { - struct theme theme; - struct functions functions; - struct strings strings; - struct behavior behavior; + struct table_colors colors; + struct table_chars chars; + struct table_functions functions; + struct table_strings strings; + struct table_behavior behavior; }; -bool line_parser( - FILE* fd, - ssize_t* blksize, - u_char (*cb)(char* key, - char* value)); // might use this for parsing .desktop files too -struct config* parse_config(char* path); +static const struct config DEFAULT_CONFIG = { + .colors = DEFAULT_TABLE_COLORS, + .chars = DEFAULT_TABLE_CHARS, + .functions = DEFAULT_TABLE_FUNCTIONS, + .strings = DEFAULT_TABLE_STRINGS, + .behavior = DEFAULT_TABLE_BEHAVIOR, +}; + +struct introspection_table { + char* NNULLABLE tname; + size_t offset; + const struct introspection_item* NNULLABLE intros; + size_t len; +}; + +static const struct introspection_table CONFIG_INSTROSPECTION[] = { + {"colors", offsetof(struct config, colors), INTROS_TABLE_COLORS, + sizeof(INTROS_TABLE_COLORS) / sizeof(INTROS_TABLE_COLORS[0])}, + {"chars", offsetof(struct config, chars), INTROS_TABLE_CHARS, + sizeof(INTROS_TABLE_CHARS) / sizeof(INTROS_TABLE_CHARS[0])}, + {"functions", offsetof(struct config, functions), INTROS_TABLE_FUNCTIONS, + sizeof(INTROS_TABLE_FUNCTIONS) / sizeof(INTROS_TABLE_FUNCTIONS[0])}, + {"strings", offsetof(struct config, strings), INTROS_TABLE_STRINGS, + sizeof(INTROS_TABLE_STRINGS) / sizeof(INTROS_TABLE_STRINGS[0])}, + {"behavior", offsetof(struct config, behavior), INTROS_TABLE_BEHAVIOR, + sizeof(INTROS_TABLE_BEHAVIOR) / sizeof(INTROS_TABLE_BEHAVIOR[0])}, +}; + +//// FUNCTIONS +int parse_config(struct config* NNULLABLE config, char* NNULLABLE path); #endif diff --git a/include/desktop.h b/include/desktop.h new file mode 100644 index 0000000..bfd829e --- /dev/null +++ b/include/desktop.h @@ -0,0 +1,20 @@ +#ifndef DESKTOPH_ +#define DESKTOPH_ + +#include +#include + +#include "macros.h" + +struct status { + bool finish; + int ret; +}; + +int read_desktop(FILE* NNULLABLE fd, void* UNULLABLE ctx, + struct status (*NNULLABLE cb)(void* UNULLABLE ctx, + char* NULLABLE table, + char* NNULLABLE key, + char* NNULLABLE value)); + +#endif diff --git a/include/efield.h b/include/efield.h index ed03c32..b792339 100644 --- a/include/efield.h +++ b/include/efield.h @@ -1,18 +1,18 @@ -#ifndef _EFIELDH_ -#define _EFIELDH_ +#ifndef EFIELDH_ +#define EFIELDH_ #include #include +// holds also the max string buffer in itself, not dynamic struct editable_field { - u_char length; u_char pos; char content[255]; }; -struct editable_field field_new(char*); -void field_trim(struct editable_field*, u_char); -void field_update(struct editable_field*, char*); -bool field_seek(struct editable_field*, char); +struct editable_field efield_new(char* content); +void efield_trim(struct editable_field* self, u_char pos); +void efield_update(struct editable_field* self, char* update); +bool efield_seek(struct editable_field* self, char seek); #endif diff --git a/include/keys.h b/include/keys.h index 51aaacc..8b37764 100644 --- a/include/keys.h +++ b/include/keys.h @@ -1,5 +1,5 @@ -#ifndef _KEYSH_ -#define _KEYSH_ +#ifndef KEYSH_ +#define KEYSH_ #include @@ -34,7 +34,7 @@ enum keys { PAGE_DOWN, }; -static const char* const key_names[] = { +static const char* const KEY_NAMES[] = { [ESC] = "ESC", [F1] = "F1", [F2] = "F2", @@ -70,7 +70,7 @@ struct key_mapping { const char* sequences[3]; }; -static const struct key_mapping key_mappings[] = { +static const struct key_mapping KEY_MAPPINGS[] = { {ESC, {"\x1b", NULL}}, {F1, {"\x1bOP", "\x1b[[A", NULL}}, {F2, {"\x1bOQ", "\x1b[[B", NULL}}, diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..226b48b --- /dev/null +++ b/include/log.h @@ -0,0 +1,12 @@ +#ifndef LOGH_ +#define LOGH_ + +#include + +void log_init(FILE* fd); +void log_puts(const char* msg); +void log_printf(const char* fmt, ...); +// NOLINTNEXTLINE(readability-identifier-length) +void log_perror(const char* s); + +#endif diff --git a/include/macros.h b/include/macros.h new file mode 100644 index 0000000..c549cf1 --- /dev/null +++ b/include/macros.h @@ -0,0 +1,25 @@ +#ifndef MACROSH_ +#define MACROSH_ + +// Do we just replace the compiler with clang?? +#if defined(__clang__) +#define NULLABLE _Nullable +#else +#define NULLABLE +#endif + +#if defined(__clang__) +#define NNULLABLE _Nonnull +#else +#define NNULLABLE +#endif + +#if defined(__clang__) +#define UNULLABLE _Null_unspecified +#else +#define UNULLABLE +#endif + +#define LEN(X) (sizeof(X) / sizeof((X)[0])) + +#endif diff --git a/include/ofield.h b/include/ofield.h new file mode 100644 index 0000000..1d2c608 --- /dev/null +++ b/include/ofield.h @@ -0,0 +1,23 @@ +#ifndef OFIELD_ +#define OFIELD_ + +#include +#include "efield.h" + +// related vector is external, indexing at 1, 0 means empty and hence points to +// the editable_field +struct opts_field { + size_t opts; + size_t current_opt; + struct editable_field efield; +}; + +struct opts_field ofield_new(size_t opts); +void ofield_toedit(struct opts_field* self, char* init); +void ofield_kbd_type(struct opts_field* self, char* typed, char* empty_default); +bool ofield_opts_seek(struct opts_field* self, char seek); +bool ofield_seek(struct opts_field* self, char seek); + +u_char ofield_display_cursor_col(struct opts_field* self, u_char maxlen); + +#endif diff --git a/include/sessions.h b/include/sessions.h index 108c0bc..d10eaee 100644 --- a/include/sessions.h +++ b/include/sessions.h @@ -1,8 +1,9 @@ -#ifndef _SESSIONSH_ -#define _SESSIONSH_ +#ifndef SESSIONSH_ +#define SESSIONSH_ #include +#include "macros.h" #include "util.h" enum session_type { @@ -12,9 +13,9 @@ enum session_type { }; struct session { - char* name; - char* exec; - char* tryexec; + char* NNULLABLE name; + char* NNULLABLE exec; + char* NULLABLE tryexec; enum session_type type; }; diff --git a/include/ui.h b/include/ui.h index a7f7bec..d37df95 100644 --- a/include/ui.h +++ b/include/ui.h @@ -1,12 +1,55 @@ -#ifndef _UIH_ -#define _UIH_ +#ifndef UIH_ +#define UIH_ #include "config.h" +#include "ofield.h" #include "util.h" -void setup(struct config); +// [box_start] +// โ†“ +// 0 [โ”Œ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +// 1 โ”‚ โ”‚ +// 2 โ”‚ the-art 2025-06-20 21:40:44 โ”‚ +// 3 โ”‚ | โ”‚ +// 4 โ”‚ | โ”‚ +// 5 โ”‚ xorg < Default > | โ”‚ +// 6 โ”‚ | โ”‚ +// 7 โ”‚ user javalsai | โ”‚ +// 8 โ”‚ | โ”‚ +// 9 โ”‚ password ________________________________ โ”‚ +// 10 โ”‚ | | โ”‚ +// 11 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +// 01234567890123456789012345678901234567890123456789 +// 00000000001111111111222222222233333333334444444444 +// |--| | โ†‘ |--|[BOX_HMARGIN] +// [BOX_HMARGIN] [VALUES_COL] | +// |---|[VALUES_SEPR] | +// |--------------------------------|[VALUE_MAXLEN] + +#define HEAD_ROW 2 +#define SESSION_ROW 5 +#define USER_ROW 7 +#define PASSWD_ROW 9 + +#define BOX_WIDTH 50 +#define BOX_HEIGHT 12 +#define BOX_HMARGIN 2 + +#define VALUES_COL 15 +#define VALUES_SEPR 3 +#define VALUE_MAXLEN (BOX_WIDTH - VALUES_COL + 1 - BOX_HMARGIN - 2) + +enum input { SESSION, USER, PASSWD }; +extern const u_char INPUTS_N; + +void setup(struct config* config); int load(struct Vector* users, struct Vector* sessions); -void print_err(const char*); -void print_errno(const char*); +void print_err(const char* /*msg*/); +void print_errno(const char* /*descr*/); + +void ui_update_field(enum input focused_input); +void ui_update_ffield(); +void ui_update_ofield(struct opts_field* self); +void ui_update_cursor_focus(); #endif diff --git a/include/ui_state.h b/include/ui_state.h new file mode 100644 index 0000000..52e6df4 --- /dev/null +++ b/include/ui_state.h @@ -0,0 +1,29 @@ +#ifndef UISTATEH_ +#define UISTATEH_ + +#include + +#include "macros.h" +#include "ui.h" + +extern enum input focused_input; + +extern struct opts_field of_session; +extern struct opts_field of_user; +extern struct opts_field of_passwd; + +extern struct Vector* UNULLABLE gusers; +extern struct Vector* UNULLABLE gsessions; + +struct opts_field* NNULLABLE get_opts_field(enum input from); +struct opts_field* NNULLABLE get_opts_ffield(); + +struct user st_user(); +struct session st_session(bool include_defshell); + +void st_ch_focus(char direction); +void st_ch_of_opts(char direction); +void st_ch_ef_col(char direction); +void st_kbd_type(char* NNULLABLE text, bool cfg_include_defshell); + +#endif diff --git a/include/users.h b/include/users.h index 0a2eda0..7e818fb 100644 --- a/include/users.h +++ b/include/users.h @@ -1,5 +1,5 @@ -#ifndef _USERSH_ -#define _USERSH_ +#ifndef USERSH_ +#define USERSH_ #include diff --git a/include/util.h b/include/util.h index 5965a31..126533c 100644 --- a/include/util.h +++ b/include/util.h @@ -1,17 +1,24 @@ -#ifndef _UTILH_ -#define _UTILH_ +#ifndef UTILH_ +#define UTILH_ #include +#include #include #include #include #include "keys.h" -enum keys find_keyname(char*); -enum keys find_ansi(char*); -void read_press(u_char*, char*); -void strcln(char** dest, const char* source); +int find_keyname(enum keys* at, const char* name); +enum keys find_ansi(const char* seq); +void read_press(u_char* length, char* out); + +bool utf8_iscont(char byte); +size_t utf8len(const char* str); +size_t utf8len_until(const char* str, const char* until); +const char* utf8back(const char* str); +const char* utf8seek(const char* str); +const char* utf8seekn(const char* str, size_t n); struct Vector { uint32_t length; @@ -19,15 +26,15 @@ struct Vector { void** pages; }; -struct Vector vec_new(); -int vec_resize(struct Vector*, size_t size); -int vec_reserve(struct Vector*, size_t size); -int vec_reserve_exact(struct Vector*, size_t size); -int vec_push(struct Vector*, void* item); -void vec_free(struct Vector*); -void vec_clear(struct Vector*); -void vec_reset(struct Vector*); -void* vec_pop(struct Vector*); // won't free it, nor shrink vec list space -void* vec_get(struct Vector*, size_t index); +extern const struct Vector VEC_NEW; +int vec_resize(struct Vector* self, size_t size); +int vec_reserve(struct Vector* self, size_t size); +int vec_reserve_exact(struct Vector* self, size_t size); +int vec_push(struct Vector* self, void* item); +void vec_free(struct Vector* self); +void vec_clear(struct Vector* self); +void vec_reset(struct Vector* self); +void* vec_pop(struct Vector* self); // won't free it, nor shrink vec list space +void* vec_get(struct Vector* self, size_t index); #endif diff --git a/src/auth.c b/src/auth.c index 01bdaa2..f4d1312 100644 --- a/src/auth.c +++ b/src/auth.c @@ -15,12 +15,13 @@ #include "unistd.h" #include "util.h" -int pam_conversation(int num_msg, - const struct pam_message** msg, - struct pam_response** resp, - void* appdata_ptr) { +int pam_conversation(int num_msg, const struct pam_message** msg, + struct pam_response** resp, void* appdata_ptr) { struct pam_response* reply = (struct pam_response*)malloc(sizeof(struct pam_response) * num_msg); + if (!reply) { + return PAM_BUF_ERR; + } for (size_t i = 0; i < num_msg; i++) { reply[i].resp = NULL; reply[i].resp_retcode = 0; @@ -91,13 +92,11 @@ void sourceFileTry(char* file) { } if (line) free(line); - fclose(file2source); + (void)fclose(file2source); } -void moarEnv(char* user, - struct session session, - struct passwd* pw, - struct behavior* behavior) { +void moarEnv(char* user, struct session session, struct passwd* pw, + struct config* config) { if (chdir(pw->pw_dir) == -1) print_errno("can't chdir to user home"); setenv("HOME", pw->pw_dir, true); @@ -109,31 +108,30 @@ void moarEnv(char* user, // PATH? - char* xdg_session_type; + char* xdg_session_type = "unknown"; if (session.type == SHELL) xdg_session_type = "tty"; if (session.type == XORG) xdg_session_type = "x11"; if (session.type == WAYLAND) xdg_session_type = "wayland"; setenv("XDG_SESSION_TYPE", xdg_session_type, true); printf("\n\n\n\n\x1b[1m"); - for (size_t i = 0; i < behavior->source.length; i++) { + for (size_t i = 0; i < config->behavior.source.length; i++) { /* printf("DEBUG(source)!!!! %d %s\n", i, (char*)vec_get(&behavior->source, * i)); */ - sourceFileTry((char*)vec_get(&behavior->source, i)); + sourceFileTry((char*)vec_get(&config->behavior.source, i)); } /* printf("\n"); */ if (pw->pw_dir) { uint home_len = strlen(pw->pw_dir); - for (size_t i = 0; i < behavior->user_source.length; i++) { - char* file2sourcepath = (char*)vec_get(&behavior->user_source, i); - char* newbuf = - malloc(home_len + strlen(file2sourcepath) + 2); // nullbyte and slash - if (newbuf == NULL) continue; // can't bother - strcpy(newbuf, pw->pw_dir); + for (size_t i = 0; i < config->behavior.user_source.length; i++) { + char* file2sourcepath = (char*)vec_get(&config->behavior.user_source, i); + size_t newbuf_len = home_len + strlen(file2sourcepath) + 2; + char* newbuf = malloc(newbuf_len); // nullbyte and slash + if (newbuf == NULL) continue; // can't bother + memcpy(newbuf, pw->pw_dir, newbuf_len); newbuf[home_len] = '/'; // assume pw_dir doesn't start with '/' :P - strcpy(&newbuf[home_len + 1], file2sourcepath); + memcpy(&newbuf[home_len + 1], file2sourcepath, newbuf_len - home_len); - /* printf("DEBUG(user_source)!!!! %d %s\n", i, newbuf); */ sourceFileTry(newbuf); free(newbuf); } @@ -150,11 +148,9 @@ void moarEnv(char* user, /*setenv("XDG_SEAT", "seat0", true);*/ } -bool launch(char* user, - char* passwd, - struct session session, - void (*cb)(void), - struct behavior* behavior) { +// NOLINTBEGIN(readability-function-cognitive-complexity) +bool launch(char* user, char* passwd, struct session session, void (*cb)(void), + struct config* config) { struct passwd* pw = getpwnam(user); if (pw == NULL) { print_err("could not get user info"); @@ -176,9 +172,10 @@ bool launch(char* user, uint pid = fork(); if (pid == 0) { // child - char* TERM = NULL; - char* _GETTERM = getenv("TERM"); - if (_GETTERM != NULL) strcln(&TERM, _GETTERM); + char* term = NULL; + char* getterm = getenv("TERM"); + // TODO: handle malloc error + if (getterm != NULL) term = strdup(getterm); if (clearenv() != 0) { print_errno("clearenv"); _exit(EXIT_FAILURE); @@ -194,13 +191,13 @@ bool launch(char* user, } // FIXME: path hotfix putenv("PATH=/bin:/usr/bin"); - if (TERM != NULL) { - setenv("TERM", TERM, true); - free(TERM); + if (term != NULL) { + setenv("TERM", term, true); + free(term); } - free(envlist); - moarEnv(user, session, pw, behavior); + free((void*)envlist); + moarEnv(user, session, pw, config); // TODO: chown stdin to user // does it inherit stdin from parent and @@ -228,29 +225,30 @@ bool launch(char* user, // TODO: these will be different due to TryExec // and, Exec/TryExec might contain spaces as args printf("\x1b[0m"); + // NOLINTNEXTLINE(bugprone-branch-clone) if (session.type == SHELL) { clear_screen(); - fflush(stdout); + (void)fflush(stdout); execlp(session.exec, session.exec, NULL); } else if (session.type == XORG || session.type == WAYLAND) { clear_screen(); - fflush(stdout); + (void)fflush(stdout); execlp(session.exec, session.exec, NULL); } perror("execl error"); - fprintf(stderr, "failure calling session\n"); + (void)fputs("failure calling session\n", stderr); } else { - waitpid(pid, NULL, 0); + __pid_t child_pid = (__pid_t)pid; + waitpid(child_pid, NULL, 0); pam_setcred(pamh, PAM_DELETE_CRED); pam_close_session(pamh, 0); pam_end(pamh, PAM_SUCCESS); - if (*reach_session == false) { - return false; - } else - exit(0); + if (*reach_session == false) return false; + exit(0); } return true; } +// NOLINTEND(readability-function-cognitive-complexity) diff --git a/src/chvt.c b/src/chvt.c index a0c11bb..513f026 100644 --- a/src/chvt.c +++ b/src/chvt.c @@ -11,6 +11,7 @@ static char* vterms[] = {"/dev/tty", "/dev/tty0", "/dev/vc/0", "/dev/systty", int chvt_str(char* str) { char* err; errno = 0; + // NOLINTNEXTLINE(readability-identifier-length) long i = strtol(str, &err, 10); if (errno) { perror("strol"); @@ -23,13 +24,16 @@ int chvt_str(char* str) { } int chvt(int n) { - fprintf(stderr, "activating vt %d\n", n); + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) + (void)fprintf(stderr, "activating vt %d\n", n); + // NOLINTNEXTLINE(readability-identifier-length) char c = 0; for (size_t i = 0; i < sizeof(vterms) / sizeof(vterms[0]); i++) { int fd = open(vterms[i], O_RDWR); if (fd >= 0 && isatty(fd) && ioctl(fd, KDGKBTYPE, &c) == 0 && c < 3) { if (ioctl(fd, VT_ACTIVATE, n) < 0 || ioctl(fd, VT_WAITACTIVE, n) < 0) { - fprintf(stderr, "Couldn't activate vt %d\n", n); + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) + (void)fprintf(stderr, "Couldn't activate vt %d\n", n); return -1; } return 0; @@ -37,6 +41,7 @@ int chvt(int n) { close(fd); } - fprintf(stderr, "Couldn't get a file descriptor referring to the console.\n"); + (void)fputs("Couldn't get a file descriptor referring to the console.\n", + stderr); return -1; } diff --git a/src/config.c b/src/config.c index d4857b3..7619804 100644 --- a/src/config.c +++ b/src/config.c @@ -1,166 +1,240 @@ +#include +#include +#include +#include #include #include +#include #include "config.h" +#include "desktop.h" +#include "log.h" #include "util.h" -// Alr so ima explain the bitfield returned by `cb` a bit -// 4 bits: -// 0b0001: break out of parsing (returning true) -// 0b0010: free the value -// 0b0100: free the key -// 0b1000: break out of parsing (returning false) -// -// This would return true if everything goes fine, false otherwise (malloc -// error, broke parsing, etc) -bool line_parser(FILE* fd, - ssize_t* blksize, - u_char (*cb)(char* key, char* value)) { - size_t opt_size = 4096; - if (blksize != NULL) opt_size = *blksize; +#define UPPER_HALF_BYTE 4 +int parse_hex(char* _at, char x1, char x2) { + // make linter happy + u_char* at = (u_char*)_at; - while (true) { - size_t alloc_size = opt_size; - char* buf = malloc(alloc_size); - if (buf == NULL) return false; - ssize_t read_size = getline(&buf, &alloc_size, fd); - if (read_size == -1) { - free(buf); + *at = 0; + + if ('0' <= x1 && x1 <= '9') { + *at += (x1 - '0') << UPPER_HALF_BYTE; + } else if ('A' <= x1 && x1 <= 'F') { + *at += (x1 - 'A' + 10) << UPPER_HALF_BYTE; + } else if ('a' <= x1 && x1 <= 'f') { + *at += (x1 - 'a' + 10) << UPPER_HALF_BYTE; + } else { + return -1; + } + + if ('0' <= x2 && x2 <= '9') { + *at += (x2 - '0'); + } else if ('A' <= x2 && x2 <= 'F') { + *at += (x2 - 'A' + 10); + } else if ('a' <= x2 && x2 <= 'f') { + *at += (x2 - 'a' + 10); + } else { + return -1; + } + + return 0; +} + +struct parser_error { + const char* NULLABLE msg; + size_t col; +}; + +#define FAIL(str) \ + return (struct parser_error) { \ + str, p - raw \ + } +#define NOFAIL return (struct parser_error){NULL, 0} +struct parser_error parse_str_inplace(char* raw) { + // reader pointer + char* p = raw; // NOLINT(readability-identifier-length) + if (*p != '"') FAIL("Strign not quoted"); + p++; + + // writer iterator, by nature will always be under the reader + size_t i = 0; // NOLINT(readability-identifier-length) + while (*p != '\0') { + if (*p == '"') { + if (p[1] == '\0') { + raw[i] = '\0'; + NOFAIL; + } + FAIL("String finished but there's content after"); + } + if (*p == '\\') { + p++; + if (*p == '\0') break; + switch (*p) { + case '\\': + raw[i++] = '\\'; + break; + case 't': + raw[i++] = '\t'; + break; + case 'n': + raw[i++] = '\n'; + break; + case '\"': + raw[i++] = '\"'; + break; + case '\'': + raw[i++] = '\''; + break; + case 'x': + if (p[1] == '\0' || p[2] == '\0') goto unfinished; + if (parse_hex(&raw[i++], p[1], p[2]) != 0) + FAIL("Invalid hex escape sequence"); + p += 2; + break; + default: + FAIL("Invalid escape variant"); + } + } else + raw[i++] = *p; + + p++; + } + +unfinished: + FAIL("Unfinished string"); +} +#undef FAIL +#undef NOFAIL + +#define FAIL(str) return (struct parser_error){str, 0} +#define NOFAIL return (struct parser_error){NULL, 0} +union typ_ptr { + char** string; + bool* boolean; + enum keys* key; + struct Vector* vec; + uintptr_t ptr; +}; +struct parser_error parse_key(enum introspection_type typ, union typ_ptr at, + char* key, size_t offset) { + char* aux_str = NULL; + struct parser_error aux_err; + + switch (typ) { + case STRING: + aux_str = strdup(key); + if (!aux_str) FAIL("allocation failure"); + aux_err = parse_str_inplace(aux_str); + if (!aux_err.msg) { + free(aux_str); + return aux_err; + } + // FIXME: it be broken, prob the ptr arithmetic or smth, we mem leak + // instead ๐Ÿ˜Ž If the ptr is not the default it means it was prev + // allocated, we should free if (*(char**)((uintptr_t)(&default_config) + + // offset) != *at.string) { + // free(*at.string); + // } + *at.string = aux_str; break; - } - - uint read; - char* key = malloc(read_size); - if (key == NULL) { - free(buf); - return false; - } - char* value = malloc(read_size); - if (value == NULL) { - free(buf); - return false; - } - if ((read = sscanf(buf, "%[^ ] = %[^\n]\n", key, value)) != 0) { - u_char ret = cb(key, value); - if (ret & 0b0100) free(key); - if (ret & 0b0010) free(value); - if (ret & 0b1000) { - free(buf); - return false; + case BOOL: + if (strcmp(key, "true") == 0) + *at.boolean = true; + else if (strcmp(key, "false") == 0) + *at.boolean = false; + else { + FAIL("Invalid key value, wasn't 'true' nor 'false'"); } - if (ret & 0b0001) { - free(buf); - break; + break; + case KEY: + // NOLINTNEXTLINE(performance-no-int-to-ptr) + if (find_keyname(at.key, key) != 0) { + FAIL("Keyboard KEY name not found"); } - } - free(buf); + break; + case STRING_ARRAY: + aux_str = strdup(key); + if (!aux_str) FAIL("allocation failure"); + aux_err = parse_str_inplace(aux_str); + if (!aux_err.msg) { + free(aux_str); + return aux_err; + } + vec_push(at.vec, aux_str); + break; } - return true; + NOFAIL; } +#undef FAIL +#undef NOFAIL -struct config* __config; -// Yanderedev code (wanna fix this with a table or smth) -u_char config_line_handler(char* k, char* v) { - if (strcmp(k, "colors.bg") == 0) - __config->theme.colors.bg = v; - else if (strcmp(k, "colors.fg") == 0) - __config->theme.colors.fg = v; - else if (strcmp(k, "colors.err") == 0) - __config->theme.colors.err = v; - else if (strcmp(k, "colors.s_wayland") == 0) - __config->theme.colors.s_wayland = v; - else if (strcmp(k, "colors.s_xorg") == 0) - __config->theme.colors.s_xorg = v; - else if (strcmp(k, "colors.s_shell") == 0) - __config->theme.colors.s_shell = v; - else if (strcmp(k, "colors.e_hostname") == 0) - __config->theme.colors.e_hostname = v; - else if (strcmp(k, "colors.e_date") == 0) - __config->theme.colors.e_date = v; - else if (strcmp(k, "colors.e_box") == 0) - __config->theme.colors.e_box = v; - else if (strcmp(k, "colors.e_header") == 0) - __config->theme.colors.e_header = v; - else if (strcmp(k, "colors.e_user") == 0) - __config->theme.colors.e_user = v; - else if (strcmp(k, "colors.e_passwd") == 0) - __config->theme.colors.e_passwd = v; - else if (strcmp(k, "colors.e_badpasswd") == 0) - __config->theme.colors.e_badpasswd = v; - else if (strcmp(k, "colors.e_key") == 0) - __config->theme.colors.e_key = v; - else if (strcmp(k, "chars.hb") == 0) - __config->theme.chars.hb = v; - else if (strcmp(k, "chars.vb") == 0) - __config->theme.chars.vb = v; - else if (strcmp(k, "chars.ctl") == 0) - __config->theme.chars.ctl = v; - else if (strcmp(k, "chars.ctr") == 0) - __config->theme.chars.ctr = v; - else if (strcmp(k, "chars.cbl") == 0) - __config->theme.chars.cbl = v; - else if (strcmp(k, "chars.cbr") == 0) - __config->theme.chars.cbr = v; - else if (strcmp(k, "functions.poweroff") == 0) { - __config->functions.poweroff = find_keyname(v); - return 0b0110; - } else if (strcmp(k, "functions.reboot") == 0) { - __config->functions.reboot = find_keyname(v); - return 0b0110; - } else if (strcmp(k, "functions.refresh") == 0) { - __config->functions.refresh = find_keyname(v); - return 0b0110; - } else if (strcmp(k, "strings.f_poweroff") == 0) - __config->strings.f_poweroff = v; - else if (strcmp(k, "strings.f_reboot") == 0) - __config->strings.f_reboot = v; - else if (strcmp(k, "strings.f_refresh") == 0) - __config->strings.f_refresh = v; - else if (strcmp(k, "strings.e_user") == 0) - __config->strings.e_user = v; - else if (strcmp(k, "strings.e_passwd") == 0) - __config->strings.e_passwd = v; - else if (strcmp(k, "strings.s_wayland") == 0) - __config->strings.s_wayland = v; - else if (strcmp(k, "strings.s_xorg") == 0) - __config->strings.s_xorg = v; - else if (strcmp(k, "strings.s_shell") == 0) - __config->strings.s_shell = v; - else if (strcmp(k, "behavior.include_defshell") == 0) { - __config->behavior.include_defshell = strcmp(v, "true") == 0; - return 0b0110; - } else if (strcmp(k, "behavior.source") == 0) - vec_push(&__config->behavior.source, v); - else if (strcmp(k, "behavior.user_source") == 0) - vec_push(&__config->behavior.user_source, v); - else - return 0b1111; +// NOLINTBEGIN(readability-identifier-length,readability-function-cognitive-complexity,readability-identifier-length) +struct status config_line_handler(void* _config, char* table, char* k, + char* v) { + struct config* config = (struct config*)_config; + struct status ret = {.finish = false}; - return 0b0100; + const struct introspection_table* this_intros_table = NULL; + for (size_t i = 0; i < LEN(CONFIG_INSTROSPECTION); i++) { + if (table == NULL) { + if (table != CONFIG_INSTROSPECTION[i].tname) continue; + } else if (strcmp(CONFIG_INSTROSPECTION[i].tname, table) != 0) + continue; + this_intros_table = &CONFIG_INSTROSPECTION[i]; + break; + } + if (this_intros_table == NULL) { + log_printf("[E] table '%s' is not part of the config\n", table); + return ret; + } + + const struct introspection_item* this_intros_key = NULL; + for (size_t i = 0; i < this_intros_table->len; i++) { + if (strcmp(this_intros_table->intros[i].name, k) != 0) continue; + this_intros_key = &this_intros_table->intros[i]; + break; + } + if (this_intros_key == NULL) { + log_printf("[E] key '%s' is not part of the table '%s' in config\n", k, + table); + return ret; + } + + size_t offset = this_intros_table->offset + this_intros_key->offset; + union typ_ptr k_addr = {.ptr = (uintptr_t)config + offset}; + + struct parser_error err = parse_key(this_intros_key->typ, k_addr, v, offset); + if (err.msg != NULL) { + log_printf("[E] cfg parser, failed to parse [%s.%s] (%s): %s\n", table, k, + INTROS_TYS_NAMES[this_intros_key->typ], err.msg); + log_printf("%s\n%*c^\n", v, err.col); + return ret; + } + + log_printf("[I] cfg parsed [%s.%s]\n", table, k); + + return ret; } +// NOLINTEND(readability-identifier-length,readability-function-cognitive-complexity,readability-identifier-length) -struct config* parse_config(char* path) { - struct stat sb; +int parse_config(struct config* NNULLABLE config, char* NNULLABLE path) { FILE* fd = fopen(path, "r"); - if (fd == NULL || (stat(path, &sb) == -1)) { + if (fd == NULL) { perror("fopen"); - fprintf(stderr, - "Please place a config file at /etc/lidm.ini or set the LIDM_CONF " - "env variable"); - return NULL; + (void)fputs( + "Please place a config file at /etc/lidm.ini or set the LIDM_CONF " + "env variable", + stderr); + return -1; } - __config = malloc(sizeof(struct config)); - __config->behavior.source = vec_new(); - __config->behavior.user_source = vec_new(); - - if (__config == NULL) return NULL; - bool ret = line_parser(fd, (ssize_t*)&sb.st_blksize, config_line_handler); + bool ret = read_desktop(fd, config, config_line_handler); + (void)fclose(fd); if (!ret) { - free(__config); - return NULL; + return -1; } - return __config; + return 0; } diff --git a/src/desktop.c b/src/desktop.c new file mode 100644 index 0000000..04c96c0 --- /dev/null +++ b/src/desktop.c @@ -0,0 +1,85 @@ +// NOLINTBEGIN(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,readability-function-cognitive-complexity) +#include +#include +#include +#include + +#include "desktop.h" + +char* trim_str(char* str) { + while (*str == ' ' || *str == '\t') + str++; + + size_t i = strlen(str); // NOLINT(readability-identifier-length) + while (i > 0) { + if (str[i - 1] != ' ' && str[i - 1] != '\t' && str[i - 1] != '\n') break; + i--; + } + str[i] = '\0'; + + return str; +} + +int read_desktop(FILE* fd, void* ctx, + struct status (*cb)(void* ctx, char* table, char* key, + char* value)) { + char* table_name = NULL; + + bool ret = -1; + char* buf = NULL; + size_t alloc_size = 0; + size_t read_size; + while ((read_size = getline(&buf, &alloc_size, fd)) > 0) { + ret = 0; + + char* buf_start = trim_str(buf); + size_t indent_size = buf_start - buf; + + if (read_size - indent_size < 1) continue; + if (*buf_start == '#') continue; + + if (*buf_start == '[' && buf_start[strlen(buf_start) - 1] == ']') { + if (table_name != NULL) free(table_name); + buf_start[strlen(buf_start) - 1] = '\0'; + table_name = strdup(buf_start + 1); + if (table_name == NULL) { + ret = -1; + break; + } + } else { + // Find '=' + size_t eq_idx = 0; + while (buf_start[eq_idx] != '\0') { + if (buf_start[eq_idx] == '=') break; + eq_idx++; + } + // impossible with a min len of 1 (empty line) + if (eq_idx == 0) continue; + // Check its not end + if (buf_start[eq_idx] != '=') { + ret = -1; + break; + } + + // Key & Value + char* key = buf_start; + buf_start[eq_idx] = '\0'; // the equal + key = trim_str(key); + char* value = &buf_start[eq_idx + 1]; + buf_start[read_size - 1] = '\0'; // the newline + value = trim_str(value); + + // Callback + struct status cb_ret = cb(ctx, table_name, key, value); + if (cb_ret.finish) { + ret = cb_ret.ret; + break; + } + } + } + + if (table_name != NULL) free(table_name); + if (buf != NULL) free(buf); + return ret; +} +// NOLINTEND(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,readability-function-cognitive-complexity) diff --git a/src/efield.c b/src/efield.c index d04eaac..2327c72 100644 --- a/src/efield.c +++ b/src/efield.c @@ -2,72 +2,83 @@ #include "efield.h" #include "ui.h" +#include "util.h" -struct editable_field field_new(char* content) { - struct editable_field __efield; +// NOLINTBEGIN(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) + +struct editable_field efield_new(char* content) { + struct editable_field efield; if (content != NULL) { - __efield.length = __efield.pos = strlen(content); - memcpy(__efield.content, content, __efield.length); + efield.pos = strlen(content); + memcpy(efield.content, content, strlen(content) + 1); } else { - field_trim(&__efield, 0); + efield_trim(&efield, 0); } - __efield.content[__efield.length] = '\0'; - return __efield; + + return efield; } -void field_trim(struct editable_field* field, u_char pos) { - field->length = field->pos = pos; - field->content[field->length] = '\0'; +void efield_trim(struct editable_field* self, u_char pos) { + self->pos = pos; + self->content[pos + 1] = '\0'; } -void field_update(struct editable_field* field, char* update) { +#define BACKSPACE_CODE 127 +void efield_update(struct editable_field* self, char* update) { u_char insert_len = strlen(update); if (insert_len == 0) return; - if (field->pos > field->length) field->pos = field->length; // WTF + if (self->pos > strlen(self->content)) + self->pos = strlen(self->content); // WTF tho + if (insert_len == 1) { // backspace - if (*update == 127) { - if (field->pos == 0) return; - if (field->pos < field->length) { - memmove(&field->content[field->pos - 1], &field->content[field->pos], - field->length - field->pos); - } - (field->pos)--; - (field->length)--; - field->content[field->length] = '\0'; + if (*update == BACKSPACE_CODE) { + if (self->pos == 0) return; + char* curr = &self->content[self->pos]; + char* prev = (char*)utf8back(curr); + memmove(prev, curr, strlen(self->content) - self->pos + 1); + + self->pos -= curr - prev; return; } + // TODO: Del } // append - if (field->length + field->pos >= 255) { + if (strlen(update) + self->pos + 1 >= 255) { print_err("field too long"); } - if (field->pos < field->length) { - // move with immediate buffer - memmove(&field->content[field->pos + insert_len], - &field->content[field->pos], field->length - field->pos); - } - memcpy(&field->content[field->pos], update, insert_len); - field->pos += insert_len; - field->length += insert_len; - field->content[field->length] = '\0'; + // move the after pos, including nullbyte + memmove(&self->content[self->pos + insert_len], &self->content[self->pos], + strlen(self->content) - self->pos + 1); + memcpy(&self->content[self->pos], update, insert_len); + + self->pos += insert_len; } // returns bool depending if it was able to "use" the seek -bool field_seek(struct editable_field* field, char seek) { - if (field->length == 0) return false; +bool efield_seek(struct editable_field* self, char seek) { + if (*self->content == '\0') return false; + if (seek == 0) return false; - if (seek < 0 && -seek > field->pos) - field->pos = 0; - else if (seek > 0 && 255 - field->pos < seek) - field->pos = 255; - else - field->pos += seek; + u_char count = seek < 0 ? -seek : seek; + char* ptr = &self->content[self->pos]; + char* start = ptr; - if (field->pos > field->length) field->pos = field->length; + while (count-- > 0) { + if (seek < 0) { + if (ptr == self->content) break; + ptr = (char*)utf8back(ptr); + } else { + if (*ptr == '\0') break; + ptr = (char*)utf8seek(ptr); + } + } - return true; + self->pos = (u_char)(ptr - self->content); + return ptr != start; } + +// NOLINTEND(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..bd473e2 --- /dev/null +++ b/src/log.c @@ -0,0 +1,38 @@ +#include +#include +#include + +#include "log.h" + +static FILE* logger_out = NULL; + +void log_init(FILE* fd) { + if (logger_out) (void)fclose(logger_out); + logger_out = fd; +} + +void log_puts(const char* msg) { + if (!logger_out) return; + (void)fputs(msg, logger_out); +} + +void log_printf(const char* fmt, ...) { + if (!logger_out) return; + + va_list args; + va_start(args, fmt); + + (void)vfprintf(logger_out, fmt, args); + + va_end(args); +} + +// NOLINTNEXTLINE(readability-identifier-length) +void log_perror(const char* s) { + if (!logger_out) return; + + if (s) + (void)fprintf(logger_out, "%s: %s", s, strerror(errno)); + else + (void)fprintf(logger_out, "%s", strerror(errno)); +} diff --git a/src/main.c b/src/main.c index 2096782..3ec6f92 100644 --- a/src/main.c +++ b/src/main.c @@ -1,27 +1,43 @@ #include #include +#include #include #include #include #include "chvt.h" #include "config.h" +#include "log.h" #include "sessions.h" #include "ui.h" #include "users.h" #include "util.h" int main(int argc, char* argv[]) { + // Logger + char* log_output = getenv("LIDM_LOG"); + if (log_output) { + FILE* log_fd = fopen(log_output, "w"); + if (!log_fd) { + perror("fopen"); + (void)fputs("failed to open logfile in write mode", stderr); + return -1; + } + + log_init(log_fd); + } + + // Chvt if (argc == 2) chvt_str(argv[1]); + struct config config = DEFAULT_CONFIG; char* conf_override = getenv("LIDM_CONF"); - struct config* config = - parse_config(conf_override == NULL ? "/etc/lidm.ini" : conf_override); - if (config == NULL) { - fprintf(stderr, "error parsing config\n"); + char* conf_path = conf_override ? conf_override : "/etc/lidm.ini"; + if (parse_config(&config, conf_path) != 0) { + (void)fputs("error parsing config\n", stderr); return 1; } - setup(*config); + setup(&config); struct Vector users = get_human_users(); struct Vector sessions = get_avaliable_sessions(); diff --git a/src/ofield.c b/src/ofield.c new file mode 100644 index 0000000..0914412 --- /dev/null +++ b/src/ofield.c @@ -0,0 +1,77 @@ +#include + +#include "config.h" +#include "efield.h" +#include "ofield.h" +#include "ui.h" +#include "util.h" + +struct opts_field ofield_new(size_t opts) { + if (opts == 0) { + return (struct opts_field){ + .opts = 0, + .current_opt = 0, + .efield = efield_new(""), + }; + } + + return (struct opts_field){ + .opts = opts, + .current_opt = 1, + }; +} + +void ofield_toedit(struct opts_field* self, char* init) { + self->current_opt = 0; + self->efield = efield_new(init); +} + +void ofield_kbd_type(struct opts_field* self, char* typed, + char* empty_default) { + if (self->current_opt != 0) ofield_toedit(self, empty_default); + efield_update(&self->efield, typed); +} + +bool ofield_opts_seek(struct opts_field* self, char seek) { + // no options or (a single option but its selected instead of on edit) + if (self->opts == 0 || (self->opts == 1 && self->current_opt != 0)) + return false; + + self->current_opt = + 1 + ((self->current_opt - 1 + seek + self->opts) % self->opts); + ui_update_ofield(self); + return true; +} + +bool ofield_seek(struct opts_field* self, char seek) { + if (self->current_opt == 0) { + if (efield_seek(&self->efield, seek)) { + return true; + } + } + + if (self->opts == 0) return false; + ofield_opts_seek(self, seek); + return true; +} + +u_char ofield_display_cursor_col(struct opts_field* self, u_char maxlen) { + if (self->current_opt == 0) { + u_char display_len = utf8len(self->efield.content); + u_char pos = utf8len_until(self->efield.content, + &self->efield.content[self->efield.pos]); + + if (display_len > maxlen) { + if (pos < maxlen / 2) { + return pos; + } + if (display_len - pos < maxlen / 2) { + return maxlen - (display_len - pos); + } + return maxlen / 2; + } + + return pos; + } + return 0; +} diff --git a/src/sessions.c b/src/sessions.c index f4e6a12..f8f09b3 100644 --- a/src/sessions.c +++ b/src/sessions.c @@ -7,6 +7,8 @@ #include #include +#include "desktop.h" +#include "log.h" #include "sessions.h" #include "util.h" @@ -14,123 +16,114 @@ struct source_dir { enum session_type type; char* dir; }; -static const struct source_dir sources[] = { +static const struct source_dir SOURCES[] = { {XORG, "/usr/share/xsessions"}, {WAYLAND, "/usr/share/wayland-sessions"}, }; -static struct session __new_session(enum session_type type, - char* name, - const char* exec, - const char* tryexec) { - struct session __session; - __session.type = type; - strcln(&__session.name, name); - strcln(&__session.exec, exec); - strcln(&__session.tryexec, tryexec); - - return __session; -} - static struct Vector* cb_sessions = NULL; -// NOTE: commented printf's here would be nice to have debug logs if I ever -// implement it -static enum session_type session_type; -static int fn(const char* fpath, const struct stat* sb, int typeflag) { - if (sb == NULL || !S_ISREG(sb->st_mode)) return 0; +struct ctx_typ { + char* NULLABLE name; + char* NULLABLE exec; + char* NULLABLE tryexec; +}; +struct status cb(void* _ctx, char* NULLABLE table, char* key, char* value) { + struct ctx_typ* ctx = (struct ctx_typ*)_ctx; + struct status ret; + ret.finish = false; + + if (table == NULL) return ret; + if (strcmp(table, "Desktop Entry") != 0) return ret; + + char** NULLABLE copy_at = NULL; + if (strcmp(key, "Name") == 0) { + if (ctx->name == NULL) copy_at = &ctx->name; + } else if (strcmp(key, "Exec") == 0) { + if (ctx->exec == NULL) copy_at = &ctx->exec; + } else if (strcmp(key, "TryExec") == 0) { + if (ctx->tryexec == NULL) copy_at = &ctx->tryexec; + } + + if (copy_at != NULL) { + *copy_at = strdup(value); + if (*copy_at == NULL) { + log_perror("strdup"); + log_puts("[E] failed to allocate memory"); + ret.finish = true; + ret.ret = -1; + } + } + + if (ctx->name != NULL && ctx->exec != NULL && ctx->tryexec != NULL) { + ret.finish = true; + ret.ret = 0; + } + + return ret; +} + +// also, always return 0 or we will break parsing and we don't want a bad +// desktop file to break all possible sessions +static enum session_type session_type; +// NOLINTNEXTLINE(readability-function-cognitive-complexity) +static int fn(const char* fpath, const struct stat* sb, int typeflag) { + // guessing symlink behavior + // - FTW_PHYS if set doesn't follow symlinks, so ftw() has no flags and it + // follows symlinks, we should never get to handle that + if (typeflag != FTW_F) return 0; + log_printf("[I] found file %s\n", fpath); + + struct ctx_typ ctx = { + .name = NULL, + .exec = NULL, + .tryexec = NULL, + }; - /*printf("gonna open %s\n", fpath);*/ FILE* fd = fopen(fpath, "r"); if (fd == NULL) { - perror("fopen"); - fprintf(stderr, "error opening file (r) '%s'\n", fpath); + log_perror("fopen"); + log_printf("[E] error opening file '%s' for read\n", fpath); return 0; } - u_char found = 0; - size_t alloc_size = sb->st_blksize; - - char* name_buf = NULL; - char* exec_buf = NULL; - char* tryexec_buf = NULL; - // This should be made a specific function - // Emm, if anything goes wrong just free the inner loop and `break;` fd and - // the rest is handled after - while (true) { - char* buf = malloc(sb->st_blksize); - ssize_t read_size = getline(&buf, &alloc_size, fd); - if (read_size == -1) { - free(buf); - break; - } - - uint read; - char* key = malloc(read_size + sizeof(char)); - if (key == NULL) { - free(buf); - // TODO: more sophisticated error handling?? - break; - } - char* value = malloc(read_size + sizeof(char)); - if (value == NULL) { - free(buf); - free(key); - // TODO: more sophisticated error handling?? - break; - } - value[0] = '\0'; // I'm not sure if sscanf would null this string out - if ((read = sscanf(buf, "%[^=]=%[^\n]\n", key, value)) != 0) { - if (strcmp(key, "Name") == 0) { - found &= 0b001; - name_buf = realloc(value, strlen(value) + sizeof(char)); - } else if (strcmp(key, "Exec") == 0) { - found &= 0b010; - exec_buf = realloc(value, strlen(value) + sizeof(char)); - } else if (strcmp(key, "TryExec") == 0) { - found &= 0b100; - tryexec_buf = realloc(value, strlen(value) + sizeof(char)); - } else { - free(value); - } - } else { - free(value); - } - free(key); - free(buf); - if (found == 0b111) break; + int ret = read_desktop(fd, &ctx, &cb); + if (ret < 0) { // any error + log_printf("[E] format error parsing %s", fpath); + return 0; } - /*printf("\nend parsing...\n");*/ - // Generic handling of exit - - fclose(fd); + (void)fclose(fd); // just add this to the list - if (name_buf != NULL && exec_buf != NULL) { - struct session* session_i = malloc(sizeof(struct session)); - *session_i = __new_session(session_type, name_buf, exec_buf, - tryexec_buf == NULL ? "" : tryexec_buf); - vec_push(cb_sessions, session_i); - } + if (ctx.name != NULL && ctx.exec != NULL) { + struct session* this_session = malloc(sizeof(struct session)); + if (this_session == NULL) return 0; - if (name_buf != NULL) free(name_buf); - if (exec_buf != NULL) free(exec_buf); - if (tryexec_buf != NULL) free(tryexec_buf); + *this_session = (struct session){ + .name = ctx.name, + .exec = ctx.exec, + .tryexec = ctx.tryexec, + .type = session_type, + }; + + vec_push(cb_sessions, this_session); + } return 0; } // This code is designed to be run purely single threaded +#define LIKELY_BOUND_SESSIONS 8 struct Vector get_avaliable_sessions() { - struct Vector sessions = vec_new(); - vec_reserve(&sessions, 8); + struct Vector sessions = VEC_NEW; + vec_reserve(&sessions, LIKELY_BOUND_SESSIONS); cb_sessions = &sessions; - for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) { - /*printf("recurring into %s\n", sources[i].dir);*/ - session_type = sources[i].type; - ftw(sources[i].dir, &fn, 1); + for (size_t i = 0; i < (sizeof(SOURCES) / sizeof(SOURCES[0])); i++) { + log_printf("[I] parsing into %s\n", SOURCES[i].dir); + session_type = SOURCES[i].type; + ftw(SOURCES[i].dir, &fn, 1); } cb_sessions = NULL; diff --git a/src/ui.c b/src/ui.c index d4a552f..eb2c463 100644 --- a/src/ui.c +++ b/src/ui.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -19,55 +20,51 @@ #include "auth.h" #include "efield.h" #include "keys.h" +#include "ofield.h" #include "sessions.h" #include "ui.h" +#include "ui_state.h" #include "users.h" #include "util.h" +const u_char INPUTS_N = 3; + static void print_box(); static void print_footer(); static void restore_all(); -static void signal_handler(int); - -const uint boxw = 50; -const uint boxh = 12; +static void signal_handler(int code); struct uint_point { uint x; uint y; }; -static void print_session(struct uint_point, struct session, bool); -static void print_user(struct uint_point, struct user, bool); -static void print_passwd(struct uint_point, uint, bool); - -enum input { SESSION, USER, PASSWD }; -static u_char inputs_n = 3; +static void print_session(struct uint_point origin, struct session session, + bool multiple); +static void print_user(struct uint_point origin, struct user user, + bool multiple); +static void print_passwd(struct uint_point origin, uint length, bool err); // ansi resource: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 static struct termios orig_term; static struct termios term; static struct winsize window; -static struct theme theme; -static struct functions functions; -static struct strings strings; -static struct behavior behavior; -void setup(struct config __config) { +#define INNER_BOX_OUT_MARGIN 2 +struct config* g_config = NULL; +void setup(struct config* config) { + g_config = config; ioctl(STDOUT_FILENO, TIOCGWINSZ, &window); + // at least // 2 padding top and bottom for footer and vertical compensation // 2 padding left & right to not overflow footer width - if (window.ws_row < boxh + 4 || window.ws_col < boxw + 4) { - fprintf(stderr, "\x1b[1;31mScreen too small\x1b[0m\n"); + if (window.ws_row < BOX_HEIGHT + INNER_BOX_OUT_MARGIN * 2 || + window.ws_col < BOX_WIDTH + INNER_BOX_OUT_MARGIN * 2) { + (void)fprintf(stderr, "\x1b[1;31mScreen too small\x1b[0m\n"); exit(1); } - theme = __config.theme; - functions = __config.functions; - strings = __config.strings; - behavior = __config.behavior; - tcgetattr(STDOUT_FILENO, &orig_term); term = orig_term; // save term // "stty" attrs @@ -76,294 +73,157 @@ void setup(struct config __config) { // save cursor pos, save screen, set color and reset screen // (applying color to all screen) - printf("\x1b[s\x1b[?47h\x1b[%s;%sm\x1b[2J", theme.colors.bg, theme.colors.fg); + printf("\x1b[s\x1b[?47h\x1b[%s;%sm\x1b[2J", g_config->colors.bg, + g_config->colors.fg); print_footer(); - atexit(restore_all); - signal(SIGINT, signal_handler); + (void)atexit(restore_all); + (void)signal(SIGINT, signal_handler); } static struct uint_point box_start() { - struct uint_point __start; - __start.x = (window.ws_col - boxw) / 2 + 1; - __start.y = (window.ws_row - boxh) / 2 + 1; - return __start; + return (struct uint_point){ + .x = ((window.ws_col - BOX_WIDTH) / 2) + 1, // looks better + .y = ((window.ws_row - BOX_HEIGHT) / 2), // leave more space under + }; } -static char* fmt_time() { - time_t t = time(NULL); - struct tm tm = *localtime(&t); +#define STRFTIME_PREALLOC 64 +#define TM_YEAR_EPOCH 1900 +static char* fmt_time(const char* fmt) { + time_t tme = time(NULL); + struct tm tm = *localtime(&tme); - size_t bsize = - snprintf(NULL, 0, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, - tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec) + - 1; - char* buf = malloc(bsize); - snprintf(buf, bsize, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, - tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - return buf; -} + size_t alloc_size = STRFTIME_PREALLOC; + char* buf = malloc(alloc_size); + if (!buf) return NULL; + while (true) { + if (strftime(buf, alloc_size, fmt, &tm) != 0) return buf; -// TODO: handle buffers longer than the buffer (cut str to the end, change -// cursor pos...) should just overlap for now - -// ugh, this represent a field which might have options -// opts is the amount of other options possible (0 will behave as a passwd) -// aaaand (it's an abstract idea, letme think), also holds the status of a -// custom content, like custom launch command or user or smth -struct opt_field { - uint opts; - uint current_opt; // 0 is edit mode btw - struct editable_field efield; -}; -void print_ofield(struct opt_field* focused_input); - -static struct opt_field ofield_new(uint opts) { - struct opt_field __field; - __field.opts = opts; - __field.current_opt = 1; - if (opts == 0) { - __field.current_opt = 0; - __field.efield = field_new(""); - } - return __field; -} -static void ofield_toedit(struct opt_field* ofield, char* init) { - ofield->current_opt = 0; - ofield->efield = field_new(init); -} -static void ofield_type(struct opt_field* ofield, char* new, char* startstr) { - if (ofield->current_opt != 0) ofield_toedit(ofield, startstr); - field_update(&ofield->efield, new); -} -// true if it changed anything, single opt fields return false -static bool ofield_opt_seek(struct opt_field* ofield, char seek) { - // TODO: think this - if (ofield->opts == 0 || (ofield->opts == 1 && ofield->current_opt != 0)) - return false; - - ofield->current_opt = - 1 + ((ofield->current_opt - 1 + seek + ofield->opts) % ofield->opts); - - print_ofield(ofield); - return true; -} -// true in case it was able to "use" the seek (a empty only editable field -// wouldn't) -static bool ofield_seek(struct opt_field* ofield, char seek) { - if (ofield->current_opt == 0) { - if (field_seek(&ofield->efield, seek)) { - return true; + alloc_size *= 2; + char* nbuf = realloc(buf, alloc_size); + if (!nbuf) { + free(buf); + return NULL; } + buf = nbuf; } - - if (ofield->opts == 0) return false; - - ofield_opt_seek(ofield, seek); - - return true; } -static u_char ofield_max_displ_pos(struct opt_field* ofield) { - // TODO: set max cursor pos too - // keep in mind that also have to keep in mind scrolling and ughhh, mentally - // blocked, but this is complex - if (ofield->current_opt == 0) - return ofield->efield.pos; - else - return 0; -} - -enum input focused_input = PASSWD; -struct opt_field of_session; -struct opt_field of_user; -struct opt_field of_passwd; - -struct Vector* gusers; -struct Vector* gsessions; - -// not *that* OF tho -struct opt_field* get_of(enum input from) { - if (from == SESSION) return &of_session; - if (from == USER) return &of_user; - if (from == PASSWD) return &of_passwd; - return NULL; -} - -void ffield_cursor_focus() { +void ui_update_cursor_focus() { struct uint_point bstart = box_start(); u_char line = bstart.y; - u_char row = bstart.x + 15; + u_char col = bstart.x + VALUES_COL; + + struct opts_field* ofield = get_opts_ffield(); + u_char maxlen = VALUE_MAXLEN; + if (ofield->opts > 1) { + maxlen -= utf8len(g_config->strings.opts_pre) + + utf8len(g_config->strings.opts_post); + } + col += ofield_display_cursor_col(ofield, maxlen); + if (ofield->opts > 1) col += utf8len(g_config->strings.opts_pre); // rows in here quite bodged if (focused_input == SESSION) { - line += 5; - row += (of_session.opts > 1) * 2; + line += SESSION_ROW; } else if (focused_input == USER) { - line += 7; - row += (of_user.opts > 1) * 2; + line += USER_ROW; } else if (focused_input == PASSWD) - line += 9; + line += PASSWD_ROW; - struct opt_field* ofield = get_of(focused_input); - row += ofield->current_opt == 0 ? ofield_max_displ_pos(ofield) : 0; - - printf("\x1b[%d;%dH", line, row); - fflush(stdout); + (void)printf("\x1b[%d;%dH", line, col); + (void)fflush(stdout); } -struct user get_current_user() { - if (of_user.current_opt != 0) - return *(struct user *)vec_get(gusers, of_user.current_opt - 1); - else { - struct user custom_user; - custom_user.shell = "/usr/bin/bash"; - custom_user.username = custom_user.display_name = of_user.efield.content; - return custom_user; - } -} - -struct session get_current_session() { - if (of_session.current_opt != 0) { - // this is for the default user shell :P, not the greatest - // implementation but I want to get his done - if (behavior.include_defshell && - of_session.current_opt == gsessions->length + 1) { - struct session shell_session; - shell_session.type = SHELL; - shell_session.exec = shell_session.name = get_current_user().shell; - return shell_session; - } else - return *(struct session *)vec_get(gsessions, of_session.current_opt - 1); - } else { - struct session custom_session; - custom_session.type = SHELL; - custom_session.name = custom_session.exec = of_session.efield.content; - return custom_session; - } -} - -void print_field(enum input focused_input) { +void ui_update_field(enum input focused_input) { struct uint_point origin = box_start(); if (focused_input == PASSWD) { - print_passwd(origin, of_passwd.efield.length, false); + print_passwd(origin, utf8len(of_passwd.efield.content), false); } else if (focused_input == SESSION) { - print_session(origin, get_current_session(), of_session.opts > 1); + print_session(origin, st_session(g_config->behavior.include_defshell), + of_session.opts > 1); } else if (focused_input == USER) { - print_user(origin, get_current_user(), of_user.opts > 1); - print_field(SESSION); + print_user(origin, st_user(), of_user.opts > 1); + ui_update_field(SESSION); } - ffield_cursor_focus(); + ui_update_cursor_focus(); } -void print_ffield() { - print_field(focused_input); +void ui_update_ffield() { + ui_update_field(focused_input); } -void print_ofield(struct opt_field* ofield) { + +void ui_update_ofield(struct opts_field* NNULLABLE self) { enum input input; - if (ofield == &of_session) + if (self == &of_session) input = SESSION; - else if (ofield == &of_user) + else if (self == &of_user) input = USER; - else if (ofield == &of_passwd) + else if (self == &of_passwd) input = PASSWD; else return; - print_field(input); -} - -// true = forward, false = backward -void ffield_move(bool direction) { - if (direction) - focused_input = (focused_input + 1 + inputs_n) % inputs_n; - else - focused_input = (focused_input - 1 + inputs_n) % inputs_n; - - ffield_cursor_focus(); -} - -// tf I'm doing -void ffield_change_opt(bool direction) { - struct opt_field* ffield = get_of(focused_input); - if (focused_input == PASSWD) ffield = &of_session; - if (!ofield_opt_seek(ffield, direction ? 1 : -1)) { - if (focused_input == PASSWD || focused_input == SESSION) - ofield_opt_seek(&of_user, direction ? 1 : -1); - else - ofield_opt_seek(&of_session, direction ? 1 : -1); - } -} -void ffield_change_pos(bool direction) { - struct opt_field* ffield = get_of(focused_input); - if (!ofield_seek(ffield, direction ? 1 : -1)) - if (!ofield_opt_seek(&of_session, direction ? 1 : -1)) - ofield_opt_seek(&of_user, direction ? 1 : -1); - - ffield_cursor_focus(); -} - -void ffield_type(char* text) { - struct opt_field* field = get_of(focused_input); - char* start = ""; - if (focused_input == USER && of_user.current_opt != 0) - start = get_current_user().username; - if (focused_input == SESSION && of_session.current_opt != 0 && - get_current_session().type == SHELL) - start = get_current_session().exec; - - ofield_type(field, text, start); - print_ffield(); + ui_update_field(input); } static char* unknown_str = "unknown"; +// NOLINTNEXTLINE(readability-function-cognitive-complexity) int load(struct Vector* users, struct Vector* sessions) { /// SETUP gusers = users; gsessions = sessions; // hostnames larger won't render properly - char* hostname = malloc(16); - if (gethostname(hostname, 16) != 0) { - free(hostname); + const u_char HOSTNAME_SIZE = VALUES_COL - VALUES_SEPR - BOX_HMARGIN; + char hostname_buf[HOSTNAME_SIZE]; + char* hostname = hostname_buf; + if (gethostname(hostname_buf, HOSTNAME_SIZE) != 0) { hostname = unknown_str; } else { - hostname[15] = '\0'; + // Ig "successful completion" doesn't contemplate truncation case, so need + // to append the unspecified nullbyte + + // char* hidx = + // (char*)utf8back(&hostname[VALUES_COL - VALUES_SEPR - BOX_HMARGIN - + // 1]); + // *hidx = '\0'; } - of_session = ofield_new(sessions->length + behavior.include_defshell); + of_session = + ofield_new(sessions->length + g_config->behavior.include_defshell); of_user = ofield_new(users->length); of_passwd = ofield_new(0); /// PRINTING - const struct uint_point boxstart = box_start(); + const struct uint_point BOXSTART = box_start(); // printf box print_box(); // put hostname - printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2, - boxstart.x + 12 - (uint)strlen(hostname), theme.colors.e_hostname, - hostname, theme.colors.fg); - if (hostname != unknown_str) free(hostname); + printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, + BOXSTART.x + VALUES_COL - VALUES_SEPR - (uint)utf8len(hostname), + g_config->colors.e_hostname, hostname, g_config->colors.fg); // put date - char* fmtd_time = fmt_time(); - printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2, - boxstart.x + boxw - 3 - (uint)strlen(fmtd_time), theme.colors.e_date, - fmtd_time, theme.colors.fg); + char* fmtd_time = fmt_time(g_config->behavior.timefmt); + printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, + BOXSTART.x + BOX_WIDTH - 1 - BOX_HMARGIN - (uint)utf8len(fmtd_time), + g_config->colors.e_date, fmtd_time, g_config->colors.fg); free(fmtd_time); - print_field(SESSION); - print_field(USER); - print_field(PASSWD); - ffield_cursor_focus(); + ui_update_field(SESSION); + ui_update_field(USER); + ui_update_field(PASSWD); + ui_update_cursor_focus(); /// INTERACTIVE u_char len; - char seq[256]; + char seq[0xff]; uint esc = 0; while (true) { read_press(&len, seq); @@ -372,201 +232,237 @@ int load(struct Vector* users, struct Vector* sessions) { if (ansi_code != -1) { if (ansi_code == ESC) { esc = 2; - } else if (ansi_code == functions.refresh) { + } else if (ansi_code == g_config->functions.refresh) { restore_all(); return 0; - } else if (ansi_code == functions.reboot) { + } else if (ansi_code == g_config->functions.reboot) { restore_all(); reboot(RB_AUTOBOOT); exit(0); - } else if (ansi_code == functions.poweroff) { + } else if (ansi_code == g_config->functions.poweroff) { restore_all(); reboot(RB_POWER_OFF); exit(0); } else if (ansi_code == A_UP || ansi_code == A_DOWN) { - ffield_move(ansi_code == A_DOWN); + st_ch_focus(ansi_code == A_DOWN ? 1 : -1); } else if (ansi_code == A_RIGHT || ansi_code == A_LEFT) { if (esc) - ffield_change_opt(ansi_code == A_RIGHT); + st_ch_of_opts(ansi_code == A_RIGHT ? 1 : -1); else - ffield_change_pos(ansi_code == A_RIGHT); + st_ch_ef_col(ansi_code == A_RIGHT ? 1 : -1); } } } else { if (len == 1 && *seq == '\n') { - if (!launch(get_current_user().username, of_passwd.efield.content, - get_current_session(), &restore_all, &behavior)) { - print_passwd(box_start(), of_passwd.efield.length, true); - ffield_cursor_focus(); + if (!launch(st_user().username, of_passwd.efield.content, + st_session(g_config->behavior.include_defshell), + &restore_all, g_config)) { + print_passwd(box_start(), utf8len(of_passwd.efield.content), true); + ui_update_cursor_focus(); } } else - ffield_type(seq); + st_kbd_type(seq, g_config->behavior.include_defshell); } if (esc != 0) esc--; } } -static char* line_cleaner = NULL; -static void clean_line(struct uint_point origin, uint line) { - if (line_cleaner == NULL) { - line_cleaner = malloc((boxw - 2) * sizeof(char) + 1); - memset(line_cleaner, 32, boxw - 2); - line_cleaner[boxw - 2] = 0; - } - printf("\x1b[%d;%dH", origin.y + line, origin.x + 1); - printf("%s", line_cleaner); +void clean_line(struct uint_point origin, uint line) { + // - outline + nullbyte + static char line_cleaner[BOX_WIDTH - 2 + 1] = { + [0 ... BOX_WIDTH - 2 - 1] = ' ', [BOX_WIDTH - 2] = '\0'}; + printf("\x1b[%d;%dH%s", origin.y + line, origin.x + 1, line_cleaner); } -// TODO: session_len > 32 -static void print_session(struct uint_point origin, - struct session session, - bool multiple) { - clean_line(origin, 5); - const char* session_type; +u_char get_render_pos_offset(struct opts_field* self, u_char maxlen) { + if (self->current_opt != 0) return 0; + + u_char pos = utf8len_until(self->efield.content, + &self->efield.content[self->efield.pos]); + return pos - ofield_display_cursor_col(self, maxlen); +} + +void print_session(struct uint_point origin, struct session session, + bool multiple) { + clean_line(origin, SESSION_ROW); + + const char* NNULLABLE session_type; if (session.type == XORG) { - session_type = strings.s_xorg; + session_type = g_config->strings.s_xorg; } else if (session.type == WAYLAND) { - session_type = strings.s_wayland; + session_type = g_config->strings.s_wayland; + } else if (session.type == SHELL) { + session_type = g_config->strings.s_shell; } else { - session_type = strings.s_shell; + __builtin_unreachable(); } - printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm", - (ulong)(origin.x + 11 - strlen(session_type)), theme.colors.e_header, - session_type, theme.colors.fg); + + // already in the box, - 1 bcs no need to step over margin, same reasoning in + // other places + printf( + "\r\x1b[%luC\x1b[%sm%s\x1b[%sm", + (ulong)(origin.x + VALUES_COL - VALUES_SEPR - utf8len(session_type) - 1), + g_config->colors.e_header, session_type, g_config->colors.fg); char* session_color; if (session.type == XORG) { - session_color = theme.colors.s_xorg; + session_color = g_config->colors.s_xorg; } else if (session.type == WAYLAND) { - session_color = theme.colors.s_wayland; + session_color = g_config->colors.s_wayland; } else { - session_color = theme.colors.s_shell; + session_color = g_config->colors.s_shell; } + char* toprint = session.name; if (multiple) { - printf("\r\x1b[%dC< \x1b[%sm%s\x1b[%sm >", origin.x + 14, session_color, - session.name, theme.colors.fg); + u_char maxlen = VALUE_MAXLEN - utf8len(g_config->strings.opts_pre) - + utf8len(g_config->strings.opts_post); + toprint += get_render_pos_offset(&of_session, maxlen); + size_t printlen = utf8seekn(toprint, maxlen) - toprint; + + printf("\r\x1b[%dC%s\x1b[%sm%.*s\x1b[%sm%s", origin.x + VALUES_COL - 1, + g_config->strings.opts_pre, session_color, (int)printlen, toprint, + g_config->colors.fg, g_config->strings.opts_post); } else { - printf("\r\x1b[%dC\x1b[%sm%s\x1b[%sm", origin.x + 14, session_color, - session.name, theme.colors.fg); + toprint += get_render_pos_offset(&of_session, VALUE_MAXLEN); + size_t printlen = utf8seekn(toprint, VALUE_MAXLEN) - toprint; + printf("\r\x1b[%dC\x1b[%sm%.*s\x1b[%sm", origin.x + VALUES_COL - 1, + session_color, (int)printlen, toprint, g_config->colors.fg); } } -// TODO: user_len > 32 -static void print_user(struct uint_point origin, - struct user user, - bool multiple) { - clean_line(origin, 7); +void print_user(struct uint_point origin, struct user user, bool multiple) { + clean_line(origin, USER_ROW); printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm", - (ulong)(origin.x + 11 - strlen(strings.e_user)), theme.colors.e_header, - strings.e_user, theme.colors.fg); + (ulong)(origin.x + VALUES_COL - VALUES_SEPR - + utf8len(g_config->strings.e_user) - 1), + g_config->colors.e_header, g_config->strings.e_user, + g_config->colors.fg); - char* user_color = theme.colors.e_user; + char* user_color = g_config->colors.e_user; + char* toprint = user.display_name; if (multiple) { - printf("\r\x1b[%dC< \x1b[%sm%s\x1b[%sm >", origin.x + 14, user_color, - user.display_name, theme.colors.fg); + u_char maxlen = VALUE_MAXLEN - utf8len(g_config->strings.opts_pre) - + utf8len(g_config->strings.opts_post); + toprint += get_render_pos_offset(&of_session, maxlen); + size_t printlen = utf8seekn(toprint, maxlen) - toprint; + + printf("\r\x1b[%dC< \x1b[%sm%.*s\x1b[%sm >", origin.x + VALUES_COL - 1, + user_color, (int)printlen, toprint, g_config->colors.fg); } else { - printf("\r\x1b[%dC\x1b[%sm%s\x1b[%sm", origin.x + 14, user_color, - user.display_name, theme.colors.fg); + toprint += get_render_pos_offset(&of_user, VALUE_MAXLEN); + size_t printlen = utf8seekn(toprint, VALUE_MAXLEN) - toprint; + printf("\r\x1b[%dC\x1b[%sm%.*s\x1b[%sm", origin.x + VALUES_COL - 1, + user_color, (int)printlen, toprint, g_config->colors.fg); } } -static char passwd_prompt[33]; -// TODO: passwd_len > 32 -static void print_passwd(struct uint_point origin, uint length, bool err) { - clean_line(origin, 9); +void print_passwd(struct uint_point origin, uint length, bool err) { + char passwd_prompt[VALUE_MAXLEN + 1]; + clean_line(origin, PASSWD_ROW); printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm", - (ulong)(origin.x + 11 - strlen(strings.e_passwd)), - theme.colors.e_header, strings.e_passwd, theme.colors.fg); + (ulong)(origin.x + VALUES_COL - VALUES_SEPR - + utf8len(g_config->strings.e_passwd) - 1), + g_config->colors.e_header, g_config->strings.e_passwd, + g_config->colors.fg); char* pass_color; if (err) - pass_color = theme.colors.e_badpasswd; + pass_color = g_config->colors.e_badpasswd; else - pass_color = theme.colors.e_passwd; + pass_color = g_config->colors.e_passwd; - ulong prompt_len = sizeof(passwd_prompt); - ulong actual_len = length > prompt_len ? prompt_len : length; - memset(passwd_prompt, ' ', prompt_len); + ulong actual_len = length > VALUE_MAXLEN ? VALUE_MAXLEN : length; + memset(passwd_prompt, ' ', VALUE_MAXLEN); memset(passwd_prompt, '*', actual_len); - passwd_prompt[32] = 0; + passwd_prompt[VALUE_MAXLEN] = '\0'; - printf("\r\x1b[%dC\x1b[%sm", origin.x + 14, pass_color); + printf("\r\x1b[%dC\x1b[%sm", origin.x + VALUES_COL - 1, pass_color); printf("%s", passwd_prompt); - printf("\x1b[%sm", theme.colors.fg); + printf("\x1b[%sm", g_config->colors.fg); } -static void print_empty_row(uint w, uint n, char* edge1, char* edge2) { +static void print_empty_row(uint wid, uint n, char* edge1, char* edge2) { for (size_t i = 0; i < n; i++) { - printf("%s\x1b[%dC%s\x1b[%dD\x1b[1B", edge1, w, edge2, w + 2); + printf("%s\x1b[%dC%s\x1b[%dD\x1b[1B", edge1, wid, edge2, wid + 2); } } -static void print_row(uint w, uint n, char* edge1, char* edge2, char* filler) { +static void print_row(uint wid, uint n, char* edge1, char* edge2, + char* filler) { for (size_t i = 0; i < n; i++) { printf("%s", edge1); - for (size_t i = 0; i < w; i++) { + for (size_t i = 0; i < wid; i++) { printf("%s", filler); } - printf("%s\x1b[%dD\x1b[1B", edge2, w + 2); + printf("%s\x1b[%dD\x1b[1B", edge2, wid + 2); } } static void print_box() { - // TODO: check min sizes - const struct uint_point bstart = box_start(); + const struct uint_point BSTART = box_start(); - printf("\x1b[%d;%dH\x1b[%sm", bstart.y, bstart.x, theme.colors.e_box); - fflush(stdout); - print_row(boxw - 2, 1, theme.chars.ctl, theme.chars.ctr, theme.chars.hb); - print_empty_row(boxw - 2, boxh - 2, theme.chars.vb, theme.chars.vb); - print_row(boxw - 2, 1, theme.chars.cbl, theme.chars.cbr, theme.chars.hb); - printf("\x1b[%sm", theme.colors.fg); - fflush(stdout); + printf("\x1b[%d;%dH\x1b[%sm", BSTART.y, BSTART.x, g_config->colors.e_box); + print_row(BOX_WIDTH - 2, 1, g_config->chars.ctl, g_config->chars.ctr, + g_config->chars.hb); + print_empty_row(BOX_WIDTH - 2, BOX_HEIGHT - 2, g_config->chars.vb, + g_config->chars.vb); + print_row(BOX_WIDTH - 2, 1, g_config->chars.cbl, g_config->chars.cbr, + g_config->chars.hb); + printf("\x1b[%sm", g_config->colors.fg); + (void)fflush(stdout); } static void print_footer() { - size_t bsize = snprintf(NULL, 0, "%s %s %s %s %s %s", strings.f_poweroff, - key_names[functions.poweroff], strings.f_reboot, - key_names[functions.reboot], strings.f_refresh, - key_names[functions.refresh]); + size_t bsize = utf8len(g_config->strings.f_poweroff) + + utf8len(KEY_NAMES[g_config->functions.poweroff]) + + utf8len(g_config->strings.f_reboot) + + utf8len(KEY_NAMES[g_config->functions.reboot]) + + utf8len(g_config->strings.f_refresh) + + utf8len(KEY_NAMES[g_config->functions.refresh]); + + bsize += 2 * 2 + // 2 wide separators between 3 items + 3 * 1; // 3 thin separators inside every item uint row = window.ws_row - 1; uint col = window.ws_col - 2 - bsize; printf( "\x1b[%3$d;%4$dH%8$s \x1b[%1$sm%5$s\x1b[%2$sm %9$s " "\x1b[%1$sm%6$s\x1b[%2$sm %10$s \x1b[%1$sm%7$s\x1b[%2$sm", - theme.colors.e_key, theme.colors.fg, row, col, - key_names[functions.poweroff], key_names[functions.reboot], - key_names[functions.refresh], strings.f_poweroff, strings.f_reboot, - strings.f_refresh); - fflush(stdout); + g_config->colors.e_key, g_config->colors.fg, row, col, + KEY_NAMES[g_config->functions.poweroff], + KEY_NAMES[g_config->functions.reboot], + KEY_NAMES[g_config->functions.refresh], g_config->strings.f_poweroff, + g_config->strings.f_reboot, g_config->strings.f_refresh); + (void)fflush(stdout); } void print_err(const char* msg) { struct uint_point origin = box_start(); - fprintf(stderr, "\x1b[%d;%dH%s(%d): %s", origin.y - 1, origin.x, msg, errno, - strerror(errno)); + (void)fprintf(stderr, "\x1b[%d;%dH%s(%d): %s", origin.y - 1, origin.x, msg, + errno, strerror(errno)); } void print_errno(const char* descr) { struct uint_point origin = box_start(); if (descr == NULL) - fprintf(stderr, "\x1b[%d;%dH\x1b[%smunknown error(%d): %s", origin.y - 1, - origin.x, theme.colors.err, errno, strerror(errno)); + (void)fprintf(stderr, "\x1b[%d;%dH\x1b[%smunknown error(%d): %s", + origin.y - 1, origin.x, g_config->colors.err, errno, + strerror(errno)); else { - fprintf(stderr, "\x1b[%d;%dH\x1b[%sm%s(%d): %s", origin.y - 1, origin.x, - theme.colors.err, descr, errno, strerror(errno)); + (void)fprintf(stderr, "\x1b[%d;%dH\x1b[%sm%s(%d): %s", origin.y - 1, + origin.x, g_config->colors.err, descr, errno, + strerror(errno)); } } void restore_all() { // restore cursor pos, restore screen and show cursor - printf("\x1b[u\x1b[?47l\x1b[?25h"); - fflush(stdout); + (void)printf("\x1b[u\x1b[?47l\x1b[?25h"); + (void)fflush(stdout); tcsetattr(STDOUT_FILENO, TCSANOW, &orig_term); } diff --git a/src/ui_state.c b/src/ui_state.c new file mode 100644 index 0000000..18e1b09 --- /dev/null +++ b/src/ui_state.c @@ -0,0 +1,93 @@ +#include "ui_state.h" +#include "ofield.h" +#include "sessions.h" +#include "ui.h" +#include "users.h" + +enum input focused_input = PASSWD; + +struct Vector* gusers; +struct Vector* gsessions; + +struct opts_field of_session; +struct opts_field of_user; +struct opts_field of_passwd; + +struct opts_field* NNULLABLE get_opts_field(enum input from) { + if (from == SESSION) return &of_session; + if (from == USER) return &of_user; + if (from == PASSWD) return &of_passwd; + __builtin_unreachable(); +} + +struct opts_field* NNULLABLE get_opts_ffield() { + return get_opts_field(focused_input); +} + +struct user st_user() { + if (of_user.current_opt != 0) + return *(struct user*)vec_get(gusers, of_user.current_opt - 1); + + struct user custom_user; + custom_user.shell = "/usr/bin/bash"; + custom_user.username = custom_user.display_name = of_user.efield.content; + return custom_user; +} +struct session st_session(bool include_defshell) { + if (of_session.current_opt != 0) { + // this is for the default user shell :P, not the greatest + // implementation but I want to get his done + if (include_defshell && of_session.current_opt == gsessions->length + 1) { + struct session shell_session; + shell_session.type = SHELL; + shell_session.exec = shell_session.name = st_user().shell; + return shell_session; + } + + return *(struct session*)vec_get(gsessions, of_session.current_opt - 1); + } + + struct session custom_session; + custom_session.type = SHELL; + custom_session.name = custom_session.exec = of_session.efield.content; + return custom_session; +} + +void st_ch_focus(char direction) { + focused_input = (focused_input + direction) % INPUTS_N; + ui_update_cursor_focus(); +} + +void st_ch_of_opts(char direction) { + struct opts_field* ffield = get_opts_ffield(); + if (focused_input == PASSWD) ffield = &of_session; + if (!ofield_opts_seek(ffield, direction)) { + if (focused_input == PASSWD || focused_input == SESSION) + ofield_opts_seek(&of_user, direction); + else + ofield_opts_seek(&of_session, direction); + } +} + +void st_ch_ef_col(char direction) { + struct opts_field* ffield = get_opts_ffield(); + if (!ofield_seek(ffield, direction)) + if (!ofield_opts_seek(&of_session, direction)) + ofield_opts_seek(&of_user, direction); + + ui_update_cursor_focus(); + ui_update_ffield(); +} + +void st_kbd_type(char* text, bool cfg_include_defshell) { + struct opts_field* field = get_opts_ffield(); + char* start = ""; + if (focused_input == USER && of_user.current_opt != 0) + start = st_user().username; + if (focused_input == SESSION && of_session.current_opt != 0 && + st_session(cfg_include_defshell).type == SHELL) + start = st_session(cfg_include_defshell).exec; + + ofield_kbd_type(field, text, start); + ui_update_ffield(); +} diff --git a/src/users.c b/src/users.c index fcb9903..f3724ba 100644 --- a/src/users.c +++ b/src/users.c @@ -1,36 +1,64 @@ #include #include -#include #include #include #include +#include "log.h" +#include "macros.h" #include "users.h" #include "util.h" -static struct user __new_user(struct passwd* p) { - struct user __user; - strcln(&__user.shell, p->pw_shell); - strcln(&__user.username, p->pw_name); - if (p->pw_gecos[0] == '\0') - __user.display_name = __user.username; - else - strcln(&__user.display_name, p->pw_gecos); +// NOLINTNEXTLINE(readability-identifier-length) +int build_user(struct user* NNULLABLE user, struct passwd* p) { + char* shell = strdup(p->pw_shell); + char* username = strdup(p->pw_name); + char* displayname; + if (strlen(p->pw_gecos) == 0) + displayname = username; + else { + displayname = strdup(p->pw_gecos); + } + if (!shell || !username || !displayname) { + free(shell); + free(username); + if (displayname != username) free(displayname); + // actually tidy struggles with the line i have avobe + // NOLINTNEXTLINE(clang-analyzer-unix.Malloc) + return -1; + } - return __user; + *user = (struct user){ + .shell = shell, + .username = username, + .display_name = displayname, + }; + + return 0; } // This code is designed to be run purely single threaded +#define LIKELY_BOUND_USERS 4 struct Vector get_human_users() { - struct Vector users = vec_new(); - vec_reserve(&users, 4); + struct Vector users = VEC_NEW; + vec_reserve(&users, LIKELY_BOUND_USERS); - struct passwd* pwd; + struct passwd* NULLABLE pwd; while ((pwd = getpwent()) != NULL) { - if (!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0)) continue; + log_printf("[I] handling user %s\n", pwd->pw_name); + // `- 1` bcs sizeof counts the nullbyte + if (pwd->pw_dir && + strncmp(pwd->pw_dir, "/home/", sizeof("/home/") - 1) != 0) + continue; + log_printf("[I] found %s\n", pwd->pw_name); struct user* user_i = malloc(sizeof(struct user)); - *user_i = __new_user(pwd); + if (user_i == NULL) continue; // malloc error + + if (build_user(user_i, pwd) != 0) { + log_printf("[E] failed to allocate allocate memory for %s\n", + pwd->pw_name); + } vec_push(&users, user_i); } diff --git a/src/util.c b/src/util.c index d23c741..7701f9f 100644 --- a/src/util.c +++ b/src/util.c @@ -11,23 +11,20 @@ static int selret_magic(); -void strcln(char** dest, const char* source) { - *dest = malloc(strlen(source) + sizeof(char)); - strcpy(*dest, source); -} - -enum keys find_keyname(char* name) { - for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) { - if (strcmp(key_names[i], name) == 0) return (enum keys)i; +int find_keyname(enum keys* at, const char* name) { + for (size_t i = 0; i < LEN(KEY_MAPPINGS); i++) { + if (strcmp(KEY_NAMES[i], name) == 0) { + *at = (enum keys)i; + return 0; + } } - perror("key not found"); - exit(1); + return -1; } -enum keys find_ansi(char* seq) { - for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) { - struct key_mapping mapping = key_mappings[i]; +enum keys find_ansi(const char* seq) { + for (size_t i = 0; i < LEN(KEY_MAPPINGS); i++) { + struct key_mapping mapping = KEY_MAPPINGS[i]; for (size_t j = 0; mapping.sequences[j] != NULL; j++) { if (strcmp(mapping.sequences[j], seq) == 0) { return (enum keys)i; @@ -37,7 +34,6 @@ enum keys find_ansi(char* seq) { return -1; } -// https://stackoverflow.com/a/48040042 void read_press(u_char* length, char* out) { *length = 0; @@ -57,6 +53,7 @@ void read_press(u_char* length, char* out) { } } +// https://stackoverflow.com/a/48040042 static int selret_magic() { fd_set set; struct timeval timeout; @@ -67,77 +64,123 @@ static int selret_magic() { return select(1, &set, NULL, NULL, &timeout); } -// Vector shii -struct Vector vec_new() { - struct Vector vec; - vec_reset(&vec); - return vec; +// UTF-8 shii +#define UTF8_CONT_MSK 0b11000000 +#define UTF8_CONT_VAL 0b10000000 +bool utf8_iscont(char byte) { + return (byte & UTF8_CONT_MSK) == UTF8_CONT_VAL; } -int vec_resize(struct Vector* vec, size_t size) { - void** new_location = realloc(vec->pages, size * sizeof(void*)); +size_t utf8len(const char* str) { + size_t len = 0; + while (*str != '\0') { + if (!utf8_iscont(*(str++))) len++; + } + + return len; +} + +size_t utf8len_until(const char* str, const char* until) { + size_t len = 0; + while (str < until) { + if (!utf8_iscont(*(str++))) len++; + } + + return len; +} + +const char* utf8back(const char* str) { + while (utf8_iscont(*(--str))) { + } + return str; +} +const char* utf8seek(const char* str) { + while (utf8_iscont(*(++str))) { + } + return str; +} + +const char* utf8seekn(const char* str, size_t n) { + while (n > 0 && *str != '\0') { + str = utf8seek(str); + n--; + } + return str; +} + +// Vector shii +const struct Vector VEC_NEW = { + .length = 0, + .capacity = 0, + .pages = NULL, +}; + +int vec_resize(struct Vector* self, size_t size) { + void** new_location = + (void**)realloc((void*)self->pages, size * sizeof(void*)); if (new_location != NULL) { - if (vec->length > size) vec->length = size; - vec->capacity = size; - vec->pages = new_location; + if (self->length > size) self->length = size; + self->capacity = size; + self->pages = new_location; } else { return -1; } return 0; } -int vec_reserve(struct Vector* vec, size_t size) { - uint32_t new_capacity = vec->capacity; - while (vec->length + size > new_capacity) { +int vec_reserve(struct Vector* self, size_t size) { + uint32_t new_capacity = self->capacity; + while (self->length + size > new_capacity) { new_capacity = new_capacity + (new_capacity >> 1) + 1; // cap * 1.5 + 1; 0 1 2 4 7 11... } - return vec_resize(vec, new_capacity); + return vec_resize(self, new_capacity); } -int vec_reserve_exact(struct Vector* vec, size_t size) { - uint32_t needed_capacity = vec->length + size; - if (vec->capacity < needed_capacity) { - return vec_resize(vec, needed_capacity); - } else { - return 0; +int vec_reserve_exact(struct Vector* self, size_t size) { + uint32_t needed_capacity = self->length + size; + if (self->capacity < needed_capacity) { + return vec_resize(self, needed_capacity); } -} - -int vec_push(struct Vector* vec, void* item) { - int res_ret = vec_reserve(vec, 1); - if (res_ret != 0) return res_ret; - - vec->pages[vec->length++] = item; return 0; } -void vec_free(struct Vector* vec) { - while (vec->length > 0) - free(vec->pages[--vec->length]); +int vec_push(struct Vector* self, void* item) { + int res_ret = vec_reserve(self, 1); + if (res_ret != 0) return res_ret; - vec_clear(vec); + self->pages[self->length++] = item; + return 0; } -void vec_clear(struct Vector* vec) { - free(vec->pages); - vec_reset(vec); +void vec_free(struct Vector* self) { + while (self->length > 0) + free(self->pages[--self->length]); + + vec_clear(self); } -void vec_reset(struct Vector* vec) { - vec->length = 0; - vec->capacity = 0; - vec->pages = NULL; +void vec_clear(struct Vector* self) { + free((void*)self->pages); + vec_reset(self); } -void* vec_pop(struct Vector* vec) { - if (vec->length == 0) return NULL; - - return vec->pages[--vec->length]; +void vec_reset(struct Vector* self) { + *self = (struct Vector){ + .length = 0, + .capacity = 0, + .pages = NULL, + }; } -void* vec_get(struct Vector* vec, size_t index) { - if (index >= vec->length) return NULL; +void* vec_pop(struct Vector* self) { + if (self->length == 0) return NULL; - return vec->pages[index]; + return self->pages[--self->length]; +} + +void* vec_get(struct Vector* self, size_t index) { + if (index >= self->length) return NULL; + + return self->pages[index]; } diff --git a/themes/README.md b/themes/README.md index ad54063..93955bb 100644 --- a/themes/README.md +++ b/themes/README.md @@ -16,6 +16,14 @@ ![kanagawa-wave.ini theme](./screenshots/kanagawa-wave.png) +## minimal.ini + +![minimal.ini theme](./screenshots/minimal.png) + +> [!WARNING] +> This theme **heavily** relies on terminal rendering. It's just to show +> customizability, a TTY won't render this properly + ## nature.ini ![nature.ini theme](./screenshots/nature.png) diff --git a/themes/cherry.ini b/themes/cherry.ini index 4d014c3..9e047dd 100644 --- a/themes/cherry.ini +++ b/themes/cherry.ini @@ -1,35 +1,47 @@ -colors.bg = 48;2;77;33;55 -colors.fg = 22;3;24;38;2;245;245;245 -colors.err = 1;31 -colors.s_wayland = 38;2;255;174;66 -colors.s_xorg = 38;2;37;175;255 -colors.s_shell = 32 -colors.e_hostname = 1;23;38;5;197 -colors.e_date = 31 -colors.e_box = 31 -colors.e_header = 1;4;38;5;204 -colors.e_user = 38;5;51 -colors.e_passwd = 4;2;38;5;203 -colors.e_badpasswd = 4;31 -colors.e_key = 1;23;38;5;197 -chars.hb = โ”€ -chars.vb = โ”‚ -chars.ctl = โ”Œ -chars.ctr = โ” -chars.cbl = โ”” -chars.cbr = โ”˜ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +bg = "48;2;77;33;55" +fg = "22;3;24;38;2;245;245;245" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +s_shell = "32" +e_hostname = "1;23;38;5;197" +e_date = "31" +e_box = "31" +e_header = "1;4;38;5;204" +e_user = "38;5;51" +e_passwd = "4;2;38;5;203" +e_badpasswd = "4;31" +e_key = "1;23;38;5;197" + +[chars] +# hb = "โ”€" +# vb = "โ”‚" +# ctl = "โ”Œ" +# ctr = "โ”" +# cbl = "โ””" +# cbr = "โ”˜" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +# f_poweroff = "poweroff" +# f_reboot = "reboot" +# f_refresh = "refresh" +# e_user = "user" +# e_passwd = "password" +# s_wayland = "wayland" +# s_xorg = "xorg" +# s_shell = "shell" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +# timefmt = "%X %x" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/default.ini b/themes/default.ini index ff4727d..0853f10 100644 --- a/themes/default.ini +++ b/themes/default.ini @@ -1,35 +1,47 @@ -colors.bg = 48;2;38;28;28 -colors.fg = 22;24;38;2;245;245;245 -colors.err = 1;31 -colors.s_wayland = 38;2;255;174;66 -colors.s_xorg = 38;2;37;175;255 -colors.s_shell = 38;2;34;140;34 -colors.e_hostname = 38;2;255;64;64 -colors.e_date = 38;2;144;144;144 -colors.e_box = 38;2;122;122;122 -colors.e_header = 4;38;2;0;255;0 -colors.e_user = 36 -colors.e_passwd = 4;38;2;245;245;205 -colors.e_badpasswd = 3;4;31 -colors.e_key = 38;2;255;174;66 -chars.hb = โ”€ -chars.vb = โ”‚ -chars.ctl = โ”Œ -chars.ctr = โ” -chars.cbl = โ”” -chars.cbr = โ”˜ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +# bg = "48;2;38;28;28" +# fg = "22;24;38;2;245;245;245" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +# s_shell = "38;2;34;140;34" +# e_hostname = "38;2;255;64;64" +# e_date = "38;2;144;144;144" +# e_box = "38;2;122;122;122" +# e_header = "4;38;2;0;255;0" +# e_user = "36" +# e_passwd = "4;38;2;245;245;205" +# e_badpasswd = "3;4;31" +# e_key = "38;2;255;174;66" + +[chars] +# hb = "โ”€" +# vb = "โ”‚" +# ctl = "โ”Œ" +# ctr = "โ”" +# cbl = "โ””" +# cbr = "โ”˜" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +# f_poweroff = "poweroff" +# f_reboot = "reboot" +# f_refresh = "refresh" +# e_user = "user" +# e_passwd = "password" +# s_wayland = "wayland" +# s_xorg = "xorg" +# s_shell = "shell" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +# timefmt = "%X %x" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/kanagawa-dragon.ini b/themes/kanagawa-dragon.ini index 8dfb715..b1426ea 100644 --- a/themes/kanagawa-dragon.ini +++ b/themes/kanagawa-dragon.ini @@ -1,35 +1,47 @@ -colors.bg = 48;2;24;22;22 -colors.fg = 22;24;38;2;185;185;185 -colors.err = 1;31 -colors.s_wayland = 38;2;255;174;66 -colors.s_xorg = 38;2;37;175;255 -colors.s_shell = 38;2;34;140;34 -colors.e_hostname = 38;2;208;189;156 -colors.e_date = 38;2;144;144;144 -colors.e_box = 38;2;122;122;122 -colors.e_header = 4;38;2;126;146;178 -colors.e_user = 38;2;148;89;84 -colors.e_passwd = 4;38;2;245;245;205 -colors.e_badpasswd = 3;4;31 -colors.e_key = 38;2;255;174;66 -chars.hb = โ”€ -chars.vb = โ”‚ -chars.ctl = โ”Œ -chars.ctr = โ” -chars.cbl = โ”” -chars.cbr = โ”˜ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +bg = "48;2;24;22;22" +fg = "22;24;38;2;185;185;185" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +# s_shell = "38;2;34;140;34" +e_hostname = "38;2;208;189;156" +e_date = "38;2;144;144;144" +# e_box = "38;2;122;122;122" +e_header = "4;38;2;126;146;178" +e_user = "38;2;148;89;84" +# e_passwd = "4;38;2;245;245;205" +# e_badpasswd = "3;4;31" +# e_key = "38;2;255;174;66" + +[chars] +# hb = "โ”€" +# vb = "โ”‚" +# ctl = "โ”Œ" +# ctr = "โ”" +# cbl = "โ””" +# cbr = "โ”˜" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +# f_poweroff = "poweroff" +# f_reboot = "reboot" +# f_refresh = "refresh" +# e_user = "user" +# e_passwd = "password" +# s_wayland = "wayland" +# s_xorg = "xorg" +# s_shell = "shell" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +timefmt = "%Y-%m-%d %H:%M:%S" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/kanagawa-wave.ini b/themes/kanagawa-wave.ini index 93368e1..617b635 100644 --- a/themes/kanagawa-wave.ini +++ b/themes/kanagawa-wave.ini @@ -1,35 +1,47 @@ -colors.bg = 48;2;28;28;36 -colors.fg = 22;24;38;2;168;168;168 -colors.err = 1;31 -colors.s_wayland = 38;2;255;174;66 -colors.s_xorg = 38;2;37;175;255 -colors.s_shell = 38;2;34;140;34 -colors.e_hostname = 38;2;196;165;112 -colors.e_date = 38;2;144;144;144 -colors.e_box = 38;2;122;122;122 -colors.e_header = 4;38;2;114;133;162 -colors.e_user = 38;2;211;137;88 -colors.e_passwd = 4;38;2;245;245;205 -colors.e_badpasswd = 3;4;31 -colors.e_key = 38;2;255;174;66 -chars.hb = โ”€ -chars.vb = โ”‚ -chars.ctl = โ”Œ -chars.ctr = โ” -chars.cbl = โ”” -chars.cbr = โ”˜ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +bg = "48;2;28;28;36" +fg = "22;24;38;2;168;168;168" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +# s_shell = "38;2;34;140;34" +e_hostname = "38;2;196;165;112" +e_date = "38;2;144;144;144" +# e_box = "38;2;122;122;122" +e_header = "4;38;2;114;133;162" +e_user = "38;2;211;137;88" +# e_passwd = "4;38;2;245;245;205" +# e_badpasswd = "3;4;31" +# e_key = "38;2;255;174;66" + +[chars] +# hb = "โ”€" +# vb = "โ”‚" +# ctl = "โ”Œ" +# ctr = "โ”" +# cbl = "โ””" +# cbr = "โ”˜" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +# f_poweroff = "poweroff" +# f_reboot = "reboot" +# f_refresh = "refresh" +# e_user = "user" +# e_passwd = "password" +# s_wayland = "wayland" +# s_xorg = "xorg" +# s_shell = "shell" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +timefmt = "%Y-%m-%d %H:%M:%S" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/minimal.ini b/themes/minimal.ini new file mode 100644 index 0000000..d823179 --- /dev/null +++ b/themes/minimal.ini @@ -0,0 +1,48 @@ +[colors] +# bg = "48;2;38;28;28" +# fg = "22;24;38;2;245;245;245" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +# s_shell = "38;2;34;140;34" +# e_hostname = "38;2;255;64;64" +# e_date = "38;2;144;144;144" +# e_box = "38;2;122;122;122" +e_header = "38;2;0;255;0" +# e_user = "36" +# e_passwd = "4;38;2;245;245;205" +# e_badpasswd = "3;4;31" +# e_key = "38;2;255;174;66" + +[chars] +hb = " " +vb = " " +ctl = " " +ctr = " " +cbl = " " +cbr = " " + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +# EXCLUSIVE TO NERD FONTS +[strings] +f_poweroff = "\xef\x80\x91 " +f_reboot = "\xee\xab\x92 " +f_refresh = "\xf3\xb0\x91\x93 " +e_user = "\xee\xb4\xb5" +e_passwd = "\xef\x80\xa3" +s_wayland = "W" +s_xorg = "X" +s_shell = "$" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +timefmt = "\xef\x80\x97 %X \xef\x84\xb3 %x" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/nature.ini b/themes/nature.ini index 7e6ee04..1158f64 100644 --- a/themes/nature.ini +++ b/themes/nature.ini @@ -1,35 +1,47 @@ -colors.bg = 48;2;15;22;15 -colors.fg = 22;23;24;38;2;245;245;245 -colors.err = 1;31 -colors.s_wayland = 38;2;255;174;66 -colors.s_xorg = 38;2;37;175;255 -colors.s_shell = 32 -colors.e_hostname = 38;5;28 -colors.e_date = 32 -colors.e_box = 32 -colors.e_header = 1;4;32 -colors.e_user = 38;5;51 -colors.e_passwd = 4;2;38;5;83 -colors.e_badpasswd = 3;4;31 -colors.e_key = 32 -chars.hb = โ”€ -chars.vb = โ”‚ -chars.ctl = โ”Œ -chars.ctr = โ” -chars.cbl = โ”” -chars.cbr = โ”˜ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +bg = "48;2;15;22;15" +fg = "22;23;24;38;2;245;245;245" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +s_shell = "32" +e_hostname = "38;5;28" +e_date = "32" +e_box = "32" +e_header = "1;4;32" +e_user = "38;5;51" +e_passwd = "4;2;38;5;83" +e_badpasswd = "3;4;31" +e_key = "32" + +[chars] +# hb = "โ”€" +# vb = "โ”‚" +# ctl = "โ”Œ" +# ctr = "โ”" +# cbl = "โ””" +# cbr = "โ”˜" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +# f_poweroff = "poweroff" +# f_reboot = "reboot" +# f_refresh = "refresh" +# e_user = "user" +# e_passwd = "password" +# s_wayland = "wayland" +# s_xorg = "xorg" +# s_shell = "shell" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +timefmt = "%Y-%m-%d %H:%M:%S" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/nord.ini b/themes/nord.ini index 07d832e..a7b9bc7 100644 --- a/themes/nord.ini +++ b/themes/nord.ini @@ -1,35 +1,47 @@ -colors.bg = 48;2;19;19;22 -colors.fg = 22;24;38;2;245;245;245 -colors.err = 1;31 -colors.s_wayland = 38;2;255;174;66 -colors.s_xorg = 38;2;37;175;255 -colors.s_shell = 32 -colors.e_hostname = 34 -colors.e_date = 38;2;66;66;88 -colors.e_box = 38;2;122;122;122 -colors.e_header = 1;4;36 -colors.e_user = 38;5;51 -colors.e_passwd = 4;2;38;5;80 -colors.e_badpasswd = 3;4;31 -colors.e_key = 34 -chars.hb = โ”€ -chars.vb = โ”‚ -chars.ctl = โ”Œ -chars.ctr = โ” -chars.cbl = โ”” -chars.cbr = โ”˜ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +bg = "48;2;19;19;22" +fg = "22;24;38;2;245;245;245" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +s_shell = "32" +e_hostname = "34" +e_date = "38;2;66;66;88" +# e_box = "38;2;122;122;122" +e_header = "1;4;36" +e_user = "38;5;51" +e_passwd = "4;2;38;5;80" +# e_badpasswd = "3;4;31" +e_key = "34" + +[chars] +# hb = "โ”€" +# vb = "โ”‚" +# ctl = "โ”Œ" +# ctr = "โ”" +# cbl = "โ””" +# cbr = "โ”˜" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +# f_poweroff = "poweroff" +# f_reboot = "reboot" +# f_refresh = "refresh" +# e_user = "user" +# e_passwd = "password" +# s_wayland = "wayland" +# s_xorg = "xorg" +# s_shell = "shell" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +timefmt = "%Y-%m-%d %H:%M:%S" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/nothing.ini b/themes/nothing.ini index ade26d6..1580af1 100644 --- a/themes/nothing.ini +++ b/themes/nothing.ini @@ -1,35 +1,49 @@ -colors.bg = - -colors.fg = 24;39m[?25l[- -colors.err = - -colors.s_wayland = - -colors.s_xorg = - -colors.s_shell = - -colors.e_hostname = - -colors.e_date = 30 -colors.e_box = - -colors.e_header = 30 -colors.e_user = - -colors.e_passwd = 24;30 -colors.e_badpasswd = - -colors.e_key = - -chars.hb = - -chars.vb = - -chars.ctl = - -chars.ctr = - -chars.cbl = - -chars.cbr = - -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +# the '-' makes '\x1b[-m' which usually does nothing +bg = "-" +# you can even break out of the '\x1b...m' context like do, I wouldn't though +fg = "24;39m\x1b[?25l\x1b[-" +err = "-" +s_wayland = "-" +s_xorg = "-" +s_shell = "-" +e_hostname = "-" +e_date = "30" +e_box = "-" +e_header = "30" +e_user = "-" +e_passwd = "24;30" +e_badpasswd = "-" +e_key = "-" + +[chars] +hb = " " +vb = " " +ctl = " " +ctr = " " +cbl = " " +cbr = " " + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +# f_poweroff = "poweroff" +# f_reboot = "reboot" +# f_refresh = "refresh" +e_user = "" +e_passwd = "" +s_wayland = "" +s_xorg = "" +s_shell = "" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +timefmt = " " +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/old-blue.ini b/themes/old-blue.ini index 9ac2e88..cea1cf0 100644 --- a/themes/old-blue.ini +++ b/themes/old-blue.ini @@ -1,35 +1,47 @@ -colors.bg = 48;2;0;0;255 -colors.fg = 22;24;39;48;2;0;0;255 -colors.err = 1;31 -colors.s_wayland = 38;2;255;174;66 -colors.s_xorg = 38;2;37;175;255 -colors.s_shell = 38;2;34;140;34 -colors.e_hostname = 38;2;255;64;64 -colors.e_date = 38;2;144;144;144 -colors.e_box = 34 -colors.e_header = 4;38;2;0;255;0 -colors.e_user = 36 -colors.e_passwd = 4;38;2;245;245;205 -colors.e_badpasswd = 3;4;31 -colors.e_key = 1;31;48;2;255;174;66 -chars.hb = โ–ˆ -chars.vb = โ–ˆ -chars.ctl = โ–ˆ -chars.ctr = โ–ˆ -chars.cbl = โ–ˆ -chars.cbr = โ–ˆ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = powewoff -strings.f_reboot = rewoot -strings.f_refresh = rewresh -strings.e_user = wuser -strings.e_passwd = passwd -strings.s_wayland = waywand -strings.s_xorg = xworg -strings.s_shell = swell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +bg = "48;2;0;0;255" +fg = "22;24;39;48;2;0;0;255" +# err = "1;31" +# s_wayland = "38;2;255;174;66" +# s_xorg = "38;2;37;175;255" +# s_shell = "38;2;34;140;34" +# e_hostname = "38;2;255;64;64" +e_date = "38;2;144;144;144" +e_box = "34" +e_header = "4;38;2;0;255;0" +e_user = "36" +e_passwd = "4;38;2;245;245;205" +# e_badpasswd = "3;4;31" +e_key = "1;31;48;2;255;174;66" + +[chars] +hb = "โ–ˆ" +vb = "โ–ˆ" +ctl = "โ–ˆ" +ctr = "โ–ˆ" +cbl = "โ–ˆ" +cbr = "โ–ˆ" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +f_poweroff = "powewoff" +f_reboot = "rewoot" +f_refresh = "rewresh" +e_user = "wuser" +e_passwd = "passwd" +s_wayland = "waywand" +s_xorg = "xworg" +s_shell = "swell" +# opts_pre = "< " +# opts_post = " >" + +[behavior] +# include_defshell = true +timefmt = "%Y-%m-%d %H:%M:%S" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/screenshots/cherry.png b/themes/screenshots/cherry.png index ca47c17..e64f8eb 100644 Binary files a/themes/screenshots/cherry.png and b/themes/screenshots/cherry.png differ diff --git a/themes/screenshots/default.png b/themes/screenshots/default.png index b732fe7..fbd4127 100644 Binary files a/themes/screenshots/default.png and b/themes/screenshots/default.png differ diff --git a/themes/screenshots/kanagawa-dragon.png b/themes/screenshots/kanagawa-dragon.png index e816900..89d5ddc 100644 Binary files a/themes/screenshots/kanagawa-dragon.png and b/themes/screenshots/kanagawa-dragon.png differ diff --git a/themes/screenshots/kanagawa-wave.png b/themes/screenshots/kanagawa-wave.png index ac8dbf9..3d16a9b 100644 Binary files a/themes/screenshots/kanagawa-wave.png and b/themes/screenshots/kanagawa-wave.png differ diff --git a/themes/screenshots/minimal.png b/themes/screenshots/minimal.png new file mode 100644 index 0000000..24d82ec Binary files /dev/null and b/themes/screenshots/minimal.png differ diff --git a/themes/screenshots/nature.png b/themes/screenshots/nature.png index 1c3210b..9de460b 100644 Binary files a/themes/screenshots/nature.png and b/themes/screenshots/nature.png differ diff --git a/themes/screenshots/nord.png b/themes/screenshots/nord.png index 52274bd..9977487 100644 Binary files a/themes/screenshots/nord.png and b/themes/screenshots/nord.png differ diff --git a/themes/screenshots/nothing.png b/themes/screenshots/nothing.png index 25cbb63..7308489 100644 Binary files a/themes/screenshots/nothing.png and b/themes/screenshots/nothing.png differ diff --git a/themes/screenshots/old-blue.png b/themes/screenshots/old-blue.png index 4e307d4..868e60b 100644 Binary files a/themes/screenshots/old-blue.png and b/themes/screenshots/old-blue.png differ diff --git a/themes/screenshots/tasteless.png b/themes/screenshots/tasteless.png index 3f0902c..d4c866c 100644 Binary files a/themes/screenshots/tasteless.png and b/themes/screenshots/tasteless.png differ diff --git a/themes/take-screenshots.sh b/themes/take-screenshots.sh new file mode 100755 index 0000000..e1db83d --- /dev/null +++ b/themes/take-screenshots.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +set -e + +MYSELF=$(realpath "$0") +MYDIR=$(dirname "$MYSELF") + +COLS=60 +ROWS=35 + +if [[ -z "$IM_FLOATING" ]]; then + if [[ -z "$LIDM_SESSIONS_N" ]]; then + echo "Please set LIDM_SESSIONS_N" >&2; + exit 1; + fi + + # 12 pts โ‰ˆ 16 px + exec hyprctl dispatch exec \ + "[float; size $((COLS*16)) $((ROWS*16))]" \ + "kitty --override font_size=12.0 --override background_opacity=1 --override cursor_trail=0 --override cursor_shape=beam --override cursor_blink_interval=0 bash -c 'cd \"$PWD\" && IM_FLOATING=1 LIDM_PATH=\"$LIDM_PATH\" LIDM_SCR_TTY=\"$(tty)\" LIDM_SESSIONS_N=\"$LIDM_SESSIONS_N\" \"$MYSELF\"'" +fi + +LIDM_PATH=${LIDM_PATH:-$(command which lidm)} +echo "Using '$LIDM_PATH'" +[[ -e "$LIDM_PATH" ]] || { + echo "'$LIDM_PATH' is not executable" >&2; + sleep 2; + exit 1; +} + +notify-send "also make sure this is not in the way either" +echo "Press enter when the window is clearly visible and nothing in the way" +echo "ONLY use this script if you are on hyprland and a \"normal\" kitty config" +echo "AND make sure you have rg, jq, grim, gifski and maybe a few more" + +exec 2>"$LIDM_SCR_TTY" +echo "This should show on the original terminal" > "$LIDM_SCR_TTY" +echo "And this too" >&2 + +read -r + +BG=$(rg '^background ' ~/.config/kitty/kitty.conf | cut -d'#' -f2) +printf '\033]4;0;rgb:%s\007' "${BG:0:2}/${BG:2:2}/${BG:4:2}" + +PRAD=$(hyprctl getoption decoration:rounding | rg int | cut -d' ' -f2) +hyprctl keyword decoration:rounding 0 + +tty=$(tty) +for theme in "$MYDIR"/*.ini; do + LIDM_CONF="$theme" "$LIDM_PATH" <"$tty" & + LIDM_PID=$! + + sleep .2 + for n in $(seq "$LIDM_SESSIONS_N"); do + GEOMETRY=$( + hyprctl -j activewindow | \ + jq -r '(.at[0]|tostring) + "," + (.at[1]|tostring) + " " + (.size[0]|tostring) + "x" + (.size[1]|tostring)' + ) + grim -g "$GEOMETRY" - > "$MYDIR/screenshots/$(basename "$theme" | cut -d. -f1)-$n.png" + notify-send "$n taken" + wtype -k right + sleep .5 + done + kill -15 $LIDM_PID + sleep .5 +done + +gifski \ + -Q 95 --fps 4 \ + -W $((COLS*32)) \ + -o "$MYDIR/../assets/media/lidm.gif" \ + "$MYDIR"/screenshots/*.png &> "$LIDM_SCR_TTY" + +for screenshot in "$MYDIR"/screenshots/*"-1.png"; do + # shellcheck disable=SC2001 + mv "$screenshot" "$(sed 's/-[0-9]\.png/.png/' <<<"$screenshot")" + +done +rm "$MYDIR"/screenshots/*"-"[0-9].png + +printf '\033]104\007' +hyprctl keyword decoration:rounding "$PRAD" diff --git a/themes/tasteless.ini b/themes/tasteless.ini index f56617a..de40376 100644 --- a/themes/tasteless.ini +++ b/themes/tasteless.ini @@ -1,35 +1,48 @@ -colors.bg = - -colors.fg = 24 -colors.err = - -colors.s_wayland = - -colors.s_xorg = - -colors.s_shell = - -colors.e_hostname = - -colors.e_date = - -colors.e_box = - -colors.e_header = - -colors.e_user = - -colors.e_passwd = - -colors.e_badpasswd = - -colors.e_key = - -chars.hb = โ”€ -chars.vb = โ”‚ -chars.ctl = โ”Œ -chars.ctr = โ” -chars.cbl = โ”” -chars.cbr = โ”˜ -functions.poweroff = F1 -functions.reboot = F2 -functions.refresh = F5 -strings.f_poweroff = poweroff -strings.f_reboot = reboot -strings.f_refresh = refresh -strings.e_user = user -strings.e_passwd = password -strings.s_wayland = wayland -strings.s_xorg = xorg -strings.s_shell = shell -behavior.include_defshell = true -behavior.source = /etc/lidm.env -behavior.source = /etc/locale.conf -behavior.user_source = .lidm.env +[colors] +# the '-' makes '\x1b[-m' which usually does nothing +bg = "-" +fg = "24" +err = "-" +s_wayland = "-" +s_xorg = "-" +s_shell = "-" +e_hostname = "-" +e_date = "-" +e_box = "-" +e_header = "-" +e_user = "-" +e_passwd = "-" +e_badpasswd = "-" +e_key = "-" + +[chars] +# hb = "โ”€" +# vb = "โ”‚" +# ctl = "โ”Œ" +# ctr = "โ”" +# cbl = "โ””" +# cbr = "โ”˜" + +[functions] +# poweroff = F1 +# reboot = F2 +# refresh = F5 + +[strings] +f_poweroff = "off" +f_reboot = "reb" +f_refresh = "ref" +e_user = "user" +e_passwd = "pass" +s_wayland = "wayl" +s_xorg = "xorg" +s_shell = "sh" +opts_pre = "ยท " +opts_post = " " + +[behavior] +# include_defshell = true +timefmt = "%Y-%m-%d %H:%M:%S" +source = "/etc/lidm.env" +source = "/etc/locale.conf" +user_source = ".lidm.env" diff --git a/themes/view.sh b/themes/view.sh new file mode 100755 index 0000000..5e91d01 --- /dev/null +++ b/themes/view.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -e + +MYSELF=$(realpath "$0") +MYDIR=$(dirname "$MYSELF") + +LIDM_PATH=${LIDM_PATH:-$(command which lidm)} +echo "Using '$LIDM_PATH'" +[[ -e "$LIDM_PATH" ]] || { echo "'$LIDM_PATH' is not executable" >&2; exit 1; } + +echo "Press \`Ctrl + C\` once you are done viewing the theme" +sleep 3 + +for theme in "$MYDIR/"*.ini; do + LIDM_CONF="$theme" "$LIDM_PATH" || : + echo "That was '$(basename "$theme")'" + sleep 2 || : +done diff --git a/valgrind-out.txt b/valgrind-out.txt deleted file mode 100644 index b0fc291..0000000 --- a/valgrind-out.txt +++ /dev/null @@ -1,286 +0,0 @@ -==12748== Memcheck, a memory error detector -==12748== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. -==12748== Using Valgrind-3.25.1-4441567fbe-20250520 and LibVEX; rerun with -h for copyright info -==12748== Command: ./lidm -==12748== Parent PID: 12397 -==12748== ---12748-- ---12748-- Valgrind options: ---12748-- --leak-check=full ---12748-- --show-leak-kinds=all ---12748-- --track-origins=yes ---12748-- --verbose ---12748-- --log-file=valgrind-out.txt ---12748-- Contents of /proc/version: ---12748-- Linux version 6.14.9-zen1-1-zen (linux-zen@artixlinux) (gcc (GCC) 15.1.1 20250425, GNU ld (GNU Binutils) 2.44.0) #1 ZEN SMP PREEMPT_DYNAMIC Fri, 30 May 2025 04:52:33 +0000 ---12748-- ---12748-- Arch and hwcaps: AMD64, LittleEndian, amd64-cx16-lzcnt-rdtscp-sse3-ssse3-avx-avx2-bmi-f16c-rdrand-rdseed-fma ---12748-- Page sizes: currently 4096, max supported 4096 ---12748-- Valgrind library directory: /usr/lib/valgrind ---12748-- Reading syms from /media/javalsai/coding/projects/lidm/lidm ---12748-- Reading syms from /usr/lib/ld-linux-x86-64.so.2 -==12748== Downloading debug info for /usr/lib/ld-linux-x86-64.so.2... ---12748-- Considering /home/javalsai/.cache/debuginfod_client/39fb3280aa247d1eb6d4193322edf8bcdc2e597e/debuginfo .. ---12748-- .. CRC is valid -==12748== Successfully downloaded debug file for /usr/lib/ld-linux-x86-64.so.2 ---12748-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux ---12748-- object doesn't have a dynamic symbol table ---12748-- Scheduler: using generic scheduler lock implementation. ---12748-- Reading suppressions file: /usr/lib/valgrind/default.supp -==12748== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-12748-by-javalsai-on-??? -==12748== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-12748-by-javalsai-on-??? -==12748== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-12748-by-javalsai-on-??? -==12748== -==12748== TO CONTROL THIS PROCESS USING vgdb (which you probably -==12748== don't want to do, unless you know exactly what you're doing, -==12748== or are doing some strange experiment): -==12748== /usr/lib/valgrind/../../bin/vgdb --pid=12748 ...command... -==12748== -==12748== TO DEBUG THIS PROCESS USING GDB: start GDB like this -==12748== /path/to/gdb ./lidm -==12748== and then give GDB the following command -==12748== target remote | /usr/lib/valgrind/../../bin/vgdb --pid=12748 -==12748== --pid is optional if only one valgrind process is running -==12748== -==12748== Downloading debug info for /usr/lib/valgrind/memcheck-amd64-linux... -==12748== Server query failed: No such file or directory ---12748-- REDIR: 0x403ca60 (ld-linux-x86-64.so.2:strlen) redirected to 0x58234897 (vgPlain_amd64_linux_REDIR_FOR_strlen) ---12748-- REDIR: 0x403b420 (ld-linux-x86-64.so.2:strcmp) redirected to 0x582348b1 (vgPlain_amd64_linux_REDIR_FOR_strcmp) ---12748-- REDIR: 0x403b1f0 (ld-linux-x86-64.so.2:index) redirected to 0x582348ea (vgPlain_amd64_linux_REDIR_FOR_index) ---12748-- REDIR: 0x403a580 (ld-linux-x86-64.so.2:bcmp) redirected to 0x582348cc (vgPlain_amd64_linux_REDIR_FOR_memcmp) ---12748-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so ---12748-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so -==12748== WARNING: new redirection conflicts with existing -- ignoring it ---12748-- old: 0x0403b420 (strcmp ) R-> (0000.0) 0x582348b1 vgPlain_amd64_linux_REDIR_FOR_strcmp -==12748== Downloading debug info for /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so... -==12748== Server query failed: No such file or directory ---12748-- new: 0x0403b420 (strcmp ) R-> (2016.0) 0x048662f0 strcmp -==12748== WARNING: new redirection conflicts with existing -- ignoring it ---12748-- old: 0x0403ca60 (strlen ) R-> (0000.0) 0x58234897 vgPlain_amd64_linux_REDIR_FOR_strlen ---12748-- new: 0x0403ca60 (strlen ) R-> (2007.0) 0x04864e80 strlen ---12748-- REDIR: 0x403a850 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x486aa80 (mempcpy) ---12748-- Reading syms from /usr/lib/libpam.so.0.85.1 ---12748-- Reading syms from /usr/lib/libc.so.6 -==12748== Downloading debug info for /usr/lib/libc.so.6... ---12748-- Considering /home/javalsai/.cache/debuginfod_client/a4845db211e7b42c7176f9be21b8301e13e7d985/debuginfo .. ---12748-- .. CRC is valid -==12748== Successfully downloaded debug file for /usr/lib/libc.so.6 ---12748-- Reading syms from /usr/lib/libaudit.so.1.0.0 ---12748-- Reading syms from /usr/lib/libcap-ng.so.0.0.0 ---12748-- REDIR: 0x403cc00 (ld-linux-x86-64.so.2:strncmp) redirected to 0x4865a50 (strncmp) -==12748== Downloading debug info for /usr/lib/valgrind/vgpreload_core-amd64-linux.so... -==12748== Server query failed: No such file or directory ---12748-- REDIR: 0x4977e60 (libc.so.6:strncasecmp) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4974fa0 (libc.so.6:mempcpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x498b650 (libc.so.6:wcpncpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4975340 (libc.so.6:stpcpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49ec410 (libc.so.6:__memmove_chk) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49991b0 (libc.so.6:wmemcmp) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4975f50 (libc.so.6:strcat) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4974650 (libc.so.6:memcpy@@GLIBC_2.14) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x498d490 (libc.so.6:wcsncpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49ec2f0 (libc.so.6:__memcpy_chk) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) -==12748== WARNING: new redirection conflicts with existing -- ignoring it ---12748-- old: 0x04a30710 (__memcpy_chk_avx_una) R-> (2024.0) 0x0486a460 __memmove_chk ---12748-- new: 0x04a30710 (__memcpy_chk_avx_una) R-> (2030.0) 0x0486ab90 __memcpy_chk ---12748-- REDIR: 0x498cc00 (libc.so.6:wcslen) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4975150 (libc.so.6:memset) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x498d4f0 (libc.so.6:wcsnlen) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4978150 (libc.so.6:strnlen) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49781e0 (libc.so.6:strpbrk) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49760d0 (libc.so.6:strcmp) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49750c0 (libc.so.6:memrchr) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x498ba20 (libc.so.6:wcschr) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4975fc0 (libc.so.6:index) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4978210 (libc.so.6:rindex) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x498bad0 (libc.so.6:wcscmp) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49753b0 (libc.so.6:stpncpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4999130 (libc.so.6:wmemchr) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4978000 (libc.so.6:strncmp) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4975410 (libc.so.6:strcasecmp) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4977390 (libc.so.6:strcspn) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x498c9f0 (libc.so.6:wcscpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4977f00 (libc.so.6:strncasecmp_l) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4976040 (libc.so.6:strchrnul) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4974560 (libc.so.6:bcmp) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4977320 (libc.so.6:strcpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49754b0 (libc.so.6:strcasecmp_l) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4977dd0 (libc.so.6:strlen) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x49780a0 (libc.so.6:strncpy) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4974e80 (libc.so.6:memmove) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) -==12748== Preferring higher priority redirection: ---12748-- old: 0x04a30740 (__memcpy_avx_unalign) R-> (2018.0) 0x048679a0 __memcpy_avx_unaligned_erms ---12748-- new: 0x04a30740 (__memcpy_avx_unalign) R-> (2018.1) 0x04869730 memmove ---12748-- REDIR: 0x49744e0 (libc.so.6:memchr) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) ---12748-- REDIR: 0x4978400 (libc.so.6:strspn) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) -==12748== WARNING: new redirection conflicts with existing -- ignoring it ---12748-- old: 0x04a30710 (__memcpy_chk_avx_una) R-> (2024.0) 0x0486a460 __memmove_chk ---12748-- new: 0x04a30710 (__memcpy_chk_avx_una) R-> (2030.0) 0x0486ab90 __memcpy_chk ---12748-- REDIR: 0x4978cf0 (libc.so.6:strstr) redirected to 0x48501a0 (_vgnU_ifunc_wrapper) -==12748== WARNING: new redirection conflicts with existing -- ignoring it ---12748-- old: 0x04a30710 (__memcpy_chk_avx_una) R-> (2024.0) 0x0486a460 __memmove_chk ---12748-- new: 0x04a30710 (__memcpy_chk_avx_una) R-> (2030.0) 0x0486ab90 __memcpy_chk ---12748-- REDIR: 0x4a35370 (libc.so.6:__strrchr_avx2) redirected to 0x48645e0 (rindex) ---12748-- REDIR: 0x4a334c0 (libc.so.6:__strlen_avx2) redirected to 0x4864d60 (strlen) ---12748-- REDIR: 0x4a345a0 (libc.so.6:__strncmp_avx2) redirected to 0x4865820 (strncmp) ---12748-- REDIR: 0x4970550 (libc.so.6:malloc) redirected to 0x485b730 (malloc) ---12748-- REDIR: 0x4a328e0 (libc.so.6:__strchr_avx2) redirected to 0x4864860 (index) ---12748-- REDIR: 0x4a2fd40 (libc.so.6:__memchr_avx2) redirected to 0x48663b0 (memchr) ---12748-- REDIR: 0x4a30740 (libc.so.6:__memcpy_avx_unaligned_erms) redirected to 0x4869730 (memmove) ---12748-- REDIR: 0x4a30710 (libc.so.6:__memcpy_chk_avx_unaligned_erms) redirected to 0x486a460 (__memmove_chk) ---12748-- REDIR: 0x4a32d10 (libc.so.6:__strcmp_avx2) redirected to 0x4866180 (strcmp) ---12748-- REDIR: 0x4a32b20 (libc.so.6:__strchrnul_avx2) redirected to 0x486a510 (strchrnul) ---12748-- REDIR: 0x4a30700 (libc.so.6:__mempcpy_avx_unaligned_erms) redirected to 0x486a640 (mempcpy) ---12748-- REDIR: 0x4970c70 (libc.so.6:free) redirected to 0x485e880 (free) ---12748-- REDIR: 0x4970fd0 (libc.so.6:realloc) redirected to 0x4862dd0 (realloc) ---12748-- REDIR: 0x4a31140 (libc.so.6:__memset_avx2_unaligned_erms) redirected to 0x4869600 (memset) ---12748-- REDIR: 0x4a33640 (libc.so.6:__strncasecmp_avx2) redirected to 0x4865ba0 (strncasecmp) ---12748-- REDIR: 0x4a31450 (libc.so.6:__stpcpy_avx2) redirected to 0x4868e60 (stpcpy) ---12748-- REDIR: 0x4a2ffe0 (libc.so.6:__memcmp_avx2_movbe) redirected to 0x4868d20 (bcmp) -==12748== -==12748== HEAP SUMMARY: -==12748== in use at exit: 1,981 bytes in 54 blocks -==12748== total heap usage: 120 allocs, 66 frees, 124,222 bytes allocated -==12748== -==12748== Searching for pointers to 54 not-freed blocks -==12748== Checked 147,504 bytes -==12748== -==12748== 9 bytes in 1 blocks are still reachable in loss record 1 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x49773DE: strdup (strdup.c:42) -==12748== by 0x400C352: build_user (users.c:15) -==12748== by 0x400C4E5: get_human_users (users.c:60) -==12748== by 0x4008715: main (main.c:42) -==12748== -==12748== 9 bytes in 1 blocks are still reachable in loss record 2 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x49773DE: strdup (strdup.c:42) -==12748== by 0x400C365: build_user (users.c:16) -==12748== by 0x400C4E5: get_human_users (users.c:60) -==12748== by 0x4008715: main (main.c:42) -==12748== -==12748== 24 bytes in 1 blocks are still reachable in loss record 3 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x400C4C5: get_human_users (users.c:57) -==12748== by 0x4008715: main (main.c:42) -==12748== -==12748== 24 bytes in 2 blocks are still reachable in loss record 4 of 14 -==12748== at 0x4862E4F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x4008C23: vec_resize (util.c:75) -==12748== by 0x4008CC6: vec_reserve (util.c:92) -==12748== by 0x4008D2F: vec_push (util.c:105) -==12748== by 0x400AE99: parse_key (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x400AFD1: config_line_handler (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x400B3E0: read_desktop (desktop.c:73) -==12748== by 0x400B155: parse_config (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x40086D3: main (main.c:36) -==12748== -==12748== 32 bytes in 1 blocks are still reachable in loss record 5 of 14 -==12748== at 0x4862E4F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x4008C23: vec_resize (util.c:75) -==12748== by 0x4008CC6: vec_reserve (util.c:92) -==12748== by 0x4008D2F: vec_push (util.c:105) -==12748== by 0x400C51A: get_human_users (users.c:64) -==12748== by 0x4008715: main (main.c:42) -==12748== -==12748== 47 bytes in 3 blocks are still reachable in loss record 6 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x49773DE: strdup (strdup.c:42) -==12748== by 0x400AE6C: parse_key (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x400AFD1: config_line_handler (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x400B3E0: read_desktop (desktop.c:73) -==12748== by 0x400B155: parse_config (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x40086D3: main (main.c:36) -==12748== -==12748== 49 bytes in 1 blocks are still reachable in loss record 7 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x400A123: clean_line (ui.c:408) -==12748== by 0x400A1CB: print_session (ui.c:419) -==12748== by 0x4009837: print_field (ui.c:246) -==12748== by 0x4009E61: load (ui.c:354) -==12748== by 0x400874F: main (main.c:45) -==12748== -==12748== 88 bytes in 1 blocks are still reachable in loss record 8 of 14 -==12748== at 0x4862E4F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x4008C23: vec_resize (util.c:75) -==12748== by 0x4008CC6: vec_reserve (util.c:92) -==12748== by 0x4008D2F: vec_push (util.c:105) -==12748== by 0x400C84E: fn (sessions.c:111) -==12748== by 0x49D1425: process_entry.isra.0 (ftw.c:472) -==12748== by 0x49D18C2: ftw_dir (ftw.c:551) -==12748== by 0x49D215F: ftw_startup (ftw.c:771) -==12748== by 0x400C931: get_avaliable_sessions (sessions.c:127) -==12748== by 0x4008728: main (main.c:43) -==12748== -==12748== 103 bytes in 8 blocks are still reachable in loss record 9 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x49773DE: strdup (strdup.c:42) -==12748== by 0x400C656: cb (sessions.c:50) -==12748== by 0x400B3E0: read_desktop (desktop.c:73) -==12748== by 0x400C7A0: fn (sessions.c:91) -==12748== by 0x49D1425: process_entry.isra.0 (ftw.c:472) -==12748== by 0x49D18C2: ftw_dir (ftw.c:551) -==12748== by 0x49D215F: ftw_startup (ftw.c:771) -==12748== by 0x400C931: get_avaliable_sessions (sessions.c:127) -==12748== by 0x4008728: main (main.c:43) -==12748== -==12748== 128 bytes in 4 blocks are still reachable in loss record 10 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x400C7F6: fn (sessions.c:101) -==12748== by 0x49D1425: process_entry.isra.0 (ftw.c:472) -==12748== by 0x49D18C2: ftw_dir (ftw.c:551) -==12748== by 0x49D215F: ftw_startup (ftw.c:771) -==12748== by 0x400C931: get_avaliable_sessions (sessions.c:127) -==12748== by 0x4008728: main (main.c:43) -==12748== -==12748== 176 bytes in 1 blocks are still reachable in loss record 11 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x4A043B2: nss_files_global_allocate (nss_files_data.c:38) -==12748== by 0x49D503B: __libc_allocate_once_slow (allocate_once.c:28) -==12748== by 0x4A0446C: allocate_once (allocate_once.h:90) -==12748== by 0x4A0446C: __nss_files_data_get (nss_files_data.c:54) -==12748== by 0x4A044E2: __nss_files_data_open (nss_files_data.c:98) -==12748== by 0x4A19400: _nss_files_getpwent_r (files-XXX.c:171) -==12748== by 0x4A02D8D: __nss_getent_r (getnssent_r.c:194) -==12748== by 0x4A121D0: getpwent_r@@GLIBC_2.2.5 (getXXent_r.c:159) -==12748== by 0x4A02928: __nss_getent (getnssent.c:36) -==12748== by 0x4A11FD3: getpwent (getXXent.c:89) -==12748== by 0x400C51F: get_human_users (users.c:49) -==12748== by 0x4008715: main (main.c:42) -==12748== -==12748== 348 bytes in 28 blocks are still reachable in loss record 12 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x49773DE: strdup (strdup.c:42) -==12748== by 0x400AEC7: parse_key (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x400AFD1: config_line_handler (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x400B3E0: read_desktop (desktop.c:73) -==12748== by 0x400B155: parse_config (in /media/javalsai/coding/projects/lidm/lidm) -==12748== by 0x40086D3: main (main.c:36) -==12748== -==12748== 472 bytes in 1 blocks are still reachable in loss record 13 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x494B6FF: __fopen_internal (iofopen.c:65) -==12748== by 0x40085E8: main (main.c:20) -==12748== -==12748== 472 bytes in 1 blocks are still reachable in loss record 14 of 14 -==12748== at 0x485B7A8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) -==12748== by 0x494B6FF: __fopen_internal (iofopen.c:65) -==12748== by 0x4A046E1: __nss_files_fopen (nss_files_fopen.c:27) -==12748== by 0x4A0451A: __nss_files_data_internal_open (nss_files_data.c:83) -==12748== by 0x4A0451A: __nss_files_data_open (nss_files_data.c:106) -==12748== by 0x4A19400: _nss_files_getpwent_r (files-XXX.c:171) -==12748== by 0x4A02D8D: __nss_getent_r (getnssent_r.c:194) -==12748== by 0x4A121D0: getpwent_r@@GLIBC_2.2.5 (getXXent_r.c:159) -==12748== by 0x4A02928: __nss_getent (getnssent.c:36) -==12748== by 0x4A11FD3: getpwent (getXXent.c:89) -==12748== by 0x400C51F: get_human_users (users.c:49) -==12748== by 0x4008715: main (main.c:42) -==12748== -==12748== LEAK SUMMARY: -==12748== definitely lost: 0 bytes in 0 blocks -==12748== indirectly lost: 0 bytes in 0 blocks -==12748== possibly lost: 0 bytes in 0 blocks -==12748== still reachable: 1,981 bytes in 54 blocks -==12748== suppressed: 0 bytes in 0 blocks -==12748== -==12748== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)