Module: Yast::SoundReadRoutinesInclude

Defined in:
../../src/include/sound/read_routines.rb

Instance Method Summary (collapse)

Instance Method Details

- (Hash) extractUniqueKey(comment)

Extacts the unique key and the name of the card from comment in the modules.conf placed before char-major-81-x

Parameters:

  • comment (String)

    before the char-major-81-x

Returns:

  • (Hash)

    example: $[ “unique_key” : string, “name” : string ] or nil



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File '../../src/include/sound/read_routines.rb', line 46

def extractUniqueKey(comment)
  result = nil

  # split to lines
  comment_lines = Builtins.splitstring(comment, "\n")

  # find last line with a ":"
  line_with_uk = ""
  colon_pos_uk = nil
  Builtins.foreach(comment_lines) do |line|
    c_pos = Builtins.findfirstof(line, ":")
    if c_pos != nil
      line_with_uk = line
      colon_pos_uk = c_pos
    end
  end

  # did we find it?
  if colon_pos_uk != nil
    # extract name
    name = Builtins.substring(
      line_with_uk,
      Ops.add(colon_pos_uk, 1),
      Ops.subtract(
        Ops.subtract(Builtins.size(line_with_uk), colon_pos_uk),
        1
      )
    )
    # extract unique key
    start_uk = Builtins.findfirstnotof(line_with_uk, "# \t")
    uk = Builtins.substring(
      line_with_uk,
      start_uk,
      Ops.subtract(colon_pos_uk, start_uk)
    )

    result = { "name" => name, "unique_key" => uk }
  end
  deep_copy(result)
end

- (Object) initialize_sound_read_routines(include_target)



17
18
19
20
21
22
23
24
25
26
27
# File '../../src/include/sound/read_routines.rb', line 17

def initialize_sound_read_routines(include_target)
  Yast.import "UI"
  Yast.import "Sound"
  Yast.import "HWConfig"
  Yast.import "Confirm"
  Yast.import "String"

  Yast.include include_target, "sound/routines.rb"

  textdomain "sound"
end

- (Object) read_card_name_from_alsa(card_id)

tries to determine card model name from audio.alsa agent

Parameters:

  • card_id (Fixnum)

    card id

Returns:

  • card name, “Sound card” on failure



32
33
34
35
36
37
38
39
# File '../../src/include/sound/read_routines.rb', line 32

def read_card_name_from_alsa(card_id)
  pth = Builtins.topath(
    Builtins.sformat(".audio.alsa.cards.%1.name", card_id)
  )
  name = Convert.to_string(SCR.Read(pth))
  return "Sound card" if name == nil
  name
end

- (Object) read_hwconfig



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File '../../src/include/sound/read_routines.rb', line 325

def read_hwconfig
  ret = []

  # get list of all config files
  cfiles = HWConfig.ConfigFiles

  # read module options
  mod_opts = read_save_info
  Builtins.y2milestone("mod_opts: %1", mod_opts)

  cardopts = {}
  ids = {}
  Builtins.foreach(mod_opts) do |op|
    Ops.set(
      cardopts,
      Ops.get_string(op, "alias", ""),
      Ops.get_map(op, "options", {})
    )
    idmap = {}
    if Ops.get_integer(op, "vendor_id", 0) != 0 &&
        Ops.get_integer(op, "device_id", 0) != 0
      Ops.set(idmap, "vendor_id", Ops.get_integer(op, "vendor_id", 0))
      Ops.set(idmap, "device_id", Ops.get_integer(op, "device_id", 0))
    end
    if Ops.get_integer(op, "sub_vendor_id", 0) != 0 &&
        Ops.get_integer(op, "sub_device_id", 0) != 0
      Ops.set(
        idmap,
        "sub_vendor_id",
        Ops.get_integer(op, "sub_vendor_id", 0)
      )
      Ops.set(
        idmap,
        "sub_device_id",
        Ops.get_integer(op, "sub_device_id", 0)
      )
    end
    if Ops.get_string(op, "bus", "") != ""
      Ops.set(idmap, "bus", Ops.get_string(op, "bus", ""))
    end
    Ops.set(ids, Ops.get_string(op, "unique_key", ""), idmap)
  end 


  Builtins.y2milestone("read module options: %1", cardopts)
  Builtins.y2milestone("read IDs: %1", ids)

  # scan each config file - search for sound card config
  Builtins.foreach(cfiles) do |cfile|
    com = HWConfig.GetComment(cfile, "MODULE")
    if com != nil
      coms = Builtins.splitstring(com, "\n")
      sound_card_found = false
      entry = {}

      Builtins.foreach(coms) do |comline|
        if Builtins.regexpmatch(
            comline,
            "^# YaST configured sound card snd-card-[0-9]*"
          )
          _alias = Builtins.regexpsub(
            comline,
            "^# YaST configured sound card (snd-card-[0-9]*)",
            "\\1"
          )

          if _alias != nil
            Ops.set(entry, "hwcfg", cfile)
            Ops.set(entry, "alias", _alias)

            # if the file uses the old config then force update
            if Builtins.regexpmatch(cfile, "^bus-pci-")
              Builtins.y2milestone(
                "detected old configuration file (%1), forced update of the configuration",
                cfile
              )
              Sound.SetModified
            end

            opts = Ops.get(cardopts, _alias, {})

            if Ops.greater_than(Builtins.size(opts), 0)
              Ops.set(entry, "options", opts)
            end

            sound_card_found = true
          end
        elsif Builtins.regexpmatch(comline, "^# ....\\............:.*")
          uniq = Builtins.regexpsub(
            comline,
            "^# (....\\............):(.*)",
            "\\1"
          )
          name = Builtins.regexpsub(
            comline,
            "^# (....\\............):(.*)",
            "\\2"
          )

          if uniq != nil
            Ops.set(entry, "unique_key", uniq)

            # add vendor/device ID
            idmap = Ops.get_map(ids, uniq, {})
            entry = Builtins.union(entry, idmap)
          end

          if name != nil
            Ops.set(entry, "model", name)
          else
            Ops.set(entry, "model", _("Unknown"))
          end
        end
      end 


      m = HWConfig.GetValue(cfile, "MODULE")
      Ops.set(entry, "module", m) if m != nil

      # TODO add bus ID

      if sound_card_found
        Builtins.y2milestone(
          "Found sound card hwconfig (%1): %2",
          cfile,
          entry
        )
        ret = Builtins.add(ret, entry)
      end
    end
  end 


  deep_copy(ret)
end

- (Array) read_modprobe(mod_path)

reads already saved info from given file

Returns:

  • (Array)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File '../../src/include/sound/read_routines.rb', line 144

def read_modprobe(mod_path)
  save = [] # structure to save

  # path mod_path	= .modprobe_sound;
  #
  # // read from /etc/modprobe/conf if /etc/modprobe.d/50-sound.conf is not present
  # if (SCR::Read(.target.size, "/etc/modprobe.d/50-sound.conf") == -1)
  # {
  #     mod_path			= .modules;
  #     Sound::used_modprobe_conf	= true;
  # }

  mod_alias = Builtins.add(mod_path, "alias")
  mod_options = Builtins.add(mod_path, "options")

  aliases = Convert.convert(
    SCR.Read(mod_alias),
    :from => "any",
    :to   => "list <string>"
  )

  aliases = Builtins.filter(aliases) { |a| is_snd_alias(a) }

  # since parameters in modules options are separated by comas
  # (eg. options mod snd_id=1,2,3, snd_index=1,2,3),
  # we have to count occurences for each module
  position = -1
  mod_occur = {} # alsa modules

  det_cards = []

  # Confirmation: label text (detecting hardware: xxx)
  if !Confirm.Detection(_("Sound Cards"), nil)
    det_cards = []
    Sound.skip_detection = true
  else
    det_cards = Convert.convert(
      SCR.Read(path(".probe.sound")),
      :from => "any",
      :to   => "list <map>"
    )
  end

  Builtins.y2milestone("Detected cards: %1", det_cards)

  # sort the aliases according to the slots option
  aliases = sort_aliases(mod_options, mod_alias, aliases)

  Builtins.y2milestone("Sorted aliases acording to slots: %1", aliases)

  Builtins.foreach(aliases) do |a|
    Builtins.y2milestone("Reading alias %1", a)
    position = Ops.add(position, 1)
    modname = Convert.to_string(SCR.Read(Builtins.add(mod_alias, a)))
    opt_pos = Ops.get_integer(mod_occur, modname, 0)
    mod_occur = Builtins.add(
      mod_occur,
      modname,
      Ops.add(Ops.get_integer(mod_occur, modname, 0), 1)
    )
    opts = Convert.convert(
      SCR.Read(Builtins.add(mod_options, modname)),
      :from => "any",
      :to   => "map <string, string>"
    )
    options = {}
    Builtins.foreach(opts) do |name, val|
      vals = Builtins.splitstring(val, ",")
      if Ops.greater_than(Builtins.size(val), opt_pos)
        options = Builtins.add(
          options,
          name,
          Ops.get_string(vals, opt_pos, "")
        )
      end
    end
    # read card info from the comment string
    comment = Convert.to_string(
      SCR.Read(Builtins.add(Builtins.add(mod_alias, a), "comment"))
    )
    uniq = ""
    model = ""
    res = extractUniqueKey(comment)
    if res != nil
      uniq = Ops.get_string(res, "unique_key", "")
      model = Ops.get_string(res, "name", "")
    else
      # probably system upgrade
      uniq = isa_uniq
      model = read_card_name_from_alsa(position)
    end
    # add index option
    Ops.set(options, "index", Builtins.tostring(position))
    entry = {
      "alias"      => a,
      "module"     => modname,
      "options"    => options,
      "unique_key" => uniq,
      "model"      => model
    }
    Builtins.foreach(det_cards) do |dcard|
      if Ops.get_string(dcard, "unique_key", "") == uniq
        Builtins.y2debug("Found uniq %1: %2", uniq, dcard)
        if Builtins.haskey(dcard, "bus_hwcfg") &&
            Builtins.haskey(dcard, "sysfs_bus_id")
          Ops.set(entry, "bus", Ops.get_string(dcard, "bus_hwcfg", ""))
          Ops.set(
            entry,
            "bus_id",
            Ops.get_string(dcard, "sysfs_bus_id", "")
          )
          Ops.set(
            entry,
            "vendor_id",
            Ops.get_integer(dcard, "vendor_id", 0)
          )
          Ops.set(
            entry,
            "sub_vendor_id",
            Ops.get_integer(dcard, "sub_vendor_id", 0)
          )
          Ops.set(
            entry,
            "device_id",
            Ops.get_integer(dcard, "device_id", 0)
          )
          Ops.set(
            entry,
            "sub_device_id",
            Ops.get_integer(dcard, "sub_device_id", 0)
          )
        end
      end
    end
    Builtins.y2debug("entry: %1", entry)
    save = Builtins.add(save, entry)
  end
  deep_copy(save)
end

- (Array) read_save_info

reads already saved info from modules.conf [ $[ “alias”: “snd-card-0”, “module”: “snd-card-emu10k1”, “options”: [“snd_id”:“0”, “snd_index”:“card1”], “unique_key”:“asdf.asdfasdfasdf”, “model”: “Sound Blaster Live!” ], $[…] ]

Examples:

of return value:

Returns:

  • (Array)


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File '../../src/include/sound/read_routines.rb', line 297

def read_save_info
  saved = []

  # First, read from /etc/modprobe.d/50-sound.conf
  if Ops.greater_or_equal(
      SCR.Read(path(".target.size"), "/etc/modprobe.d/50-sound.conf"),
      0
    )
    saved = read_modprobe(path(".modprobe_sound"))
  else
    Builtins.y2milestone("/etc/modprobe.d/50-sound.conf doesn't exist")

    # try /etc/modprobe.conf...
    if Ops.greater_or_equal(
        SCR.Read(path(".target.size"), "/etc/modprobe.conf"),
        0
      )
      saved = read_modprobe(path(".modules"))

      Sound.used_modprobe_conf = true if saved != []
    else
      Builtins.y2milestone("/etc/modprobe.conf doesn't exist")
    end
  end

  deep_copy(saved)
end

- (Object) sort_aliases(config_path, alias_path, aliases)

sort module aliases according to snd slots option



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File '../../src/include/sound/read_routines.rb', line 88

def sort_aliases(config_path, alias_path, aliases)
  aliases = deep_copy(aliases)
  # read 'snd' options
  snd_opts = Convert.convert(
    SCR.Read(Builtins.add(config_path, "snd")),
    :from => "any",
    :to   => "map <string, string>"
  )
  Builtins.y2milestone("snd optionss: %1", snd_opts)

  slots_option = Builtins.splitstring(Ops.get(snd_opts, "slots", ""), ",")
  slots_option = Builtins.maplist(slots_option) do |slotopt|
    String.CutBlanks(slotopt)
  end

  # alias -> driver mapping
  alias_mapping = {}

  # read aliases
  Builtins.foreach(aliases) do |a|
    Builtins.y2milestone("Reading alias %1", a)
    modname = Convert.to_string(SCR.Read(Builtins.add(alias_path, a)))
    alias_mapping = Builtins.add(alias_mapping, a, modname)
  end 


  # driver -> [ aliases ]
  driver_mapping = {}

  # create a reverse mapping
  Builtins.foreach(alias_mapping) do |_alias, driver|
    a_list = Ops.get(driver_mapping, driver, [])
    a_list = Builtins.add(a_list, _alias)
    Ops.set(driver_mapping, driver, a_list)
  end 


  Builtins.y2milestone("Driver mapping: %1", driver_mapping)

  sorted_aliases = []

  # sort aliases
  Builtins.foreach(slots_option) do |slot|
    aliases2 = Ops.get(driver_mapping, slot, [])
    sorted_aliases = Builtins.add(sorted_aliases, Ops.get(aliases2, 0, ""))
    aliases2 = Builtins.remove(aliases2, 0)
    Ops.set(driver_mapping, slot, aliases2)
  end 


  deep_copy(sorted_aliases)
end