Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions adjust-grub-theme/adjust_grub_theme_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -135,9 +135,14 @@ func (s *su) TestGetFontSize() {
}

func (s *su) TestGetScreenSizeFromGrubParams() {
grubParamsFilePath := "testdata/grub"
require.FileExists(s.T(), grubParamsFilePath)
w, h, err := getScreenSizeFromGrubParams(grubParamsFilePath)
require.FileExists(s.T(), "testdata/grub")
require.FileExists(s.T(), "testdata/grub.d/00_test")
require.FileExists(s.T(), "testdata/grub.d/99_local")
w, h, err := getScreenSizeFromGrubParams([]string{
"testdata/grub",
"testdata/grub.d/00_test",
"testdata/grub.d/99_local",
})
assert.NoError(s.T(), err)
assert.Equal(s.T(), 1024, w)
assert.Equal(s.T(), 768, h)
Expand Down Expand Up @@ -201,7 +206,7 @@ func (s *su) TestAdjustThemeNormal() {
defer func() {
_ = os.RemoveAll(optThemeOutputDir)
}()
err = adjustThemeNormal()
err = adjustThemeNormalV20()
assert.Equal(s.T(), nil, err)

}
Expand Down
122 changes: 113 additions & 9 deletions adjust-grub-theme/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -162,11 +162,8 @@ func getFontSize(screenWidth int, screenHeight int) int {
return round(y)
}

func getScreenSizeFromGrubParams(grubParamsFilePath string) (w, h int, err error) {
params, err := loadGrubParams(grubParamsFilePath)
if err != nil {
return
}
func getScreenSizeFromGrubParams(grubParamsFilePaths []string) (w, h int, err error) {
params := loadGrubParams(grubParamsFilePaths)

w, h, err = parseResolution(getGfxMode(params))
if err != nil {
Expand Down Expand Up @@ -306,13 +303,13 @@ func adjustTheme() {
if optFallbackOnly {
return
}
err = adjustThemeNormal()
err = adjustThemeNormalV25()
if err != nil {
logger.Fatal(err)
}
}

func adjustThemeNormal() error {
func adjustThemeNormalV20() error {
themeInputDir := filepath.Join(optThemeInputDir, themeNameNormal)
themeOutputDir := filepath.Join(optThemeOutputDir, themeNameNormal)

Expand Down Expand Up @@ -409,6 +406,100 @@ func adjustThemeNormal() error {
return err
}

func adjustThemeNormalV25() error {
themeInputDir := filepath.Join(optThemeInputDir, themeNameNormal)
themeOutputDir := filepath.Join(optThemeOutputDir, themeNameNormal)

cleanupThemeOutputDir(themeOutputDir)
err := os.MkdirAll(themeOutputDir, 0755)
if err != nil {
return err
}
copyThemeFiles(themeInputDir, themeOutputDir)

bgImg, themeBgImg, err := loadV25BackgroundImage()
if err != nil {
return err
}

err = saveJpeg(bgImg, filepath.Join(themeOutputDir, "background.jpg"))
if err != nil {
return err
}
if themeBgImg != nil {
err = saveJpeg(themeBgImg, filepath.Join(themeOutputDir, "background_in_theme.jpg"))
if err != nil {
return err
}
} else {
_, err = copyFile(filepath.Join(themeOutputDir, "background.jpg"),
filepath.Join(themeOutputDir, "background_in_theme.jpg"))
if err != nil {
return err
}
}

themeFile := filepath.Join(themeInputDir, "theme.txt.tpl")
theme, err := tt.ParseThemeFile(themeFile)
if err != nil {
return err
}

bootMenu := theme.FindComponentByType(tt.ComponentTypeBootMenu)
if bootMenu != nil {
adjustBootMenuV25(bootMenu, optScreenWidth, optScreenHeight)
}

for _, comp := range theme.Components {
if comp.Type == tt.ComponentTypeLabel {
adjustLabelText(comp)
}
}

themeOutput := filepath.Join(themeOutputDir, "theme.txt")
themeOutputFh, err := os.Create(themeOutput)
if err != nil {
return err
}
defer func() {
_ = themeOutputFh.Close()
}()
bw := bufio.NewWriter(themeOutputFh)
// write head
_, err = fmt.Fprintf(bw, "#version:%d\n", VERSION)
if err != nil {
return err
}
_, err = fmt.Fprintf(bw, "#lang:%s\n", optLang)
if err != nil {
return err
}

var inputDir string
inputDir, err = filepath.Abs(themeInputDir)
if err != nil {
logger.Warning(err)
inputDir = themeInputDir
}

_, err = fmt.Fprintf(bw, "#themeInputDir:%s\n", inputDir)
if err != nil {
return err
}

_, err = fmt.Fprintln(bw, "#head end")
if err != nil {
return err
}

_, err = theme.WriteTo(bw)
if err != nil {
return err
}
err = bw.Flush()
return err
}

func adjustThemeFallback() error {
themeInputDir := filepath.Join(optThemeInputDir, themeNameFallback)
themeOutputDir := filepath.Join(optThemeOutputDir, themeNameFallback)
Expand Down Expand Up @@ -585,7 +676,10 @@ func main() {

if optScreenWidth == 0 || optScreenHeight == 0 {
var err error
optScreenWidth, optScreenHeight, err = getScreenSizeFromGrubParams(grubParamsFile)
optScreenWidth, optScreenHeight, err = getScreenSizeFromGrubParams([]string{
grubParamsFile,
ddeGrubParamsFile,
})
if err != nil {
logger.Debug(err)
optScreenWidth = 1024
Expand Down Expand Up @@ -984,6 +1078,16 @@ func adjustBootMenu(themeOutputDir string, comp *tt.Component, vars map[string]f
adjustScrollbarThumbPixmapStyle(scrollbarThumbR)
}

func adjustBootMenuV25(comp *tt.Component, width, height int) {
if width == 1024 && height == 768 {
// halfWidthPercent represents half of the boot menu width percentage.
// The boot menu is centered, so width = halfWidthPercent * 2, left = 50% - halfWidthPercent,
halfWidthPercent := 22
comp.SetProp("width", tt.RelNum(halfWidthPercent*2))
comp.SetProp("left", tt.RelNum(50-halfWidthPercent))
}
}

const (
orientationHorizontal = 0
orientationVertical = 1
Expand Down
2 changes: 2 additions & 0 deletions adjust-grub-theme/testdata/grub.d/00_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test config for GRUB_GFXMODE override
GRUB_GFXMODE=1920x1080
4 changes: 4 additions & 0 deletions adjust-grub-theme/testdata/grub.d/99_local
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Local settings - highest priority
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
GRUB_GFXMODE=1024x768
52 changes: 37 additions & 15 deletions adjust-grub-theme/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -153,6 +153,7 @@ func decodeShellValue(in string) string {
const defaultGrubGfxMode = "auto"
const grubGfxMode = "GRUB_GFXMODE"
const grubParamsFile = "/etc/default/grub"
const ddeGrubParamsFile = "/etc/default/grub.d/11_dde.cfg"

func getGfxMode(params map[string]string) (val string) {
val = decodeShellValue(params[grubGfxMode])
Expand All @@ -162,31 +163,52 @@ func getGfxMode(params map[string]string) (val string) {
return
}

func loadGrubParams(grubParamsFilePath string) (map[string]string, error) {
func loadGrubParams(grubParamsFilePaths []string) map[string]string {
params := make(map[string]string)
f, err := os.Open(grubParamsFilePath)

// First read the main configuration file
for _, grubParamsFilePath := range grubParamsFilePaths {
if err := readGrubParamsFile(grubParamsFilePath, params); err != nil {
logger.Warningf("Failed to read grub params file %s: %v", grubParamsFilePath, err)
}
}

return params
}

func readGrubParamsFile(filePath string, params map[string]string) error {
f, err := os.Open(filePath)
if err != nil {
return params, err
if os.IsNotExist(err) {
return nil
}
return err
}
defer func() {
_ = f.Close()
}()

r := kv.NewReader(f)
r.TrimSpace = kv.TrimLeadingTailingSpace
r.Comment = '#'
for {
pair, err := r.Read()
if err != nil {
break
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comment lines
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Find the position of the first equal sign
eqIdx := strings.Index(line, "=")
if eqIdx == -1 {
// Not in key=value format, skip
continue
}
if pair.Key == "" {
key := strings.TrimSpace(line[:eqIdx])
value := strings.TrimSpace(line[eqIdx+1:])
if key == "" {
continue
}
params[pair.Key] = pair.Value
params[key] = value
}

return params, nil
return scanner.Err()
}

type InvalidResolutionError struct {
Expand Down
4 changes: 2 additions & 2 deletions adjust-grub-theme/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

package main

const VERSION int = 18
const VERSION int = 19
12 changes: 11 additions & 1 deletion grub_theme/themetxt/theme.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -264,6 +264,16 @@ func (t *Theme) Dump() {
}
}

// FindComponentByType finds the first component by component type
func (t *Theme) FindComponentByType(compType string) *Component {
for _, comp := range t.Components {
if comp.Type == compType {
return comp
}
}
return nil
}

func (t *Theme) WriteTo(w io.Writer) (n int64, err error) {
for _, prop := range t.Props {
var pn int
Expand Down
1 change: 1 addition & 0 deletions misc/data/grub-themes/deepin-fallback/theme.txt.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ terminal-border: "0"
item_height = 18
item_spacing = 25
selected_item_pixmap_style = "selected_item_*.png"
icon_width = 0
}

# Show a countdown message using the label component
Expand Down
Binary file added misc/data/grub-themes/deepin-v20/terminal_box_c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading