packaging: updated config for HS lifecycle management.
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 if not m:plugin_exists('ivi-resource-manager') and
239    not with_system_controller
240 then
241     resource.method.veto = {
242         function(zone, rset, grant, owners, req_set)
243             return true
244          end
245     }
246 end
247
248 -- test for creating selections
249 mdb.select {
250            name = "audio_owner",
251            table = "audio_playback_owner",
252            columns = {"application_class"},
253            condition = "zone_name = 'driver'"
254 }
255
256 mdb.select {
257            name = "vehicle_speed",
258            table = "amb_vehicle_speed",
259            columns = {"value"},
260            condition = "key = 'VehicleSpeed'"
261 }
262
263 element.lua {
264    name    = "speed2volume",
265    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
266    outputs = {  mdb.table { name = "speedvol",
267                             index = {"zone", "device"},
268                             columns = {{"zone", mdb.string, 16},
269                                        {"device", mdb.string, 16},
270                                        {"value", mdb.floating}},
271                             create = true
272                            }
273              },
274    oldvolume = 0.0,
275    update  = function(self)
276                 speed = self.inputs.speed.single_value
277                 if (speed) then
278                     volume = (speed - 144.0) / 7.0
279                 else
280                     volume = 0.0
281                 end
282                 diff = volume - self.oldvolume
283                 if (diff*diff > self.inputs.param) then
284                     print("*** element "..self.name.." update "..volume)
285                     self.oldvolume = volume
286                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
287                 end
288              end
289 }
290
291 mdb.select {
292     name = "amb_state",
293     table = "amb_state",
294     columns = { "state" },
295     condition = "id = 0"
296 }
297
298 -- Night mode processing chain
299
300 mdb.select {
301     name = "exterior_brightness",
302     table = "amb_exterior_brightness",
303     columns = { "value" },
304     condition = "key = 'ExteriorBrightness'"
305 }
306
307 element.lua {
308     name    = "nightmode",
309     inputs  = { brightness = mdb.select.exterior_brightness },
310     oldmode = -1;
311     outputs = {
312         mdb.table {
313             name = "amb_nightmode",
314             index = { "id" },
315             create = true,
316             columns = {
317                 { "id", mdb.unsigned },
318                 { "night_mode", mdb.unsigned }
319             }
320         }
321     },
322     update = function(self)
323         -- This is a trivial function to calculate night mode. Later, we will
324         -- need a better threshold value and hysteresis to prevent oscillation.
325
326         brightness = self.inputs.brightness.single_value
327
328         if not brightness then
329             return
330         end
331
332         print("*** element "..self.name.." update brightness: "..brightness)
333
334         if brightness > 300 then
335             mode = 0
336         else
337             mode = 1
338         end
339
340         print("*** resulting mode: ".. mode)
341
342         if not (mode == self.oldmode) then
343             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
344         end
345
346         self.oldmode = mode
347     end
348 }
349
350 mdb.select {
351     name = "select_night_mode",
352     table = "amb_nightmode",
353     columns = { "night_mode" },
354     condition = "id = 0"
355 }
356
357 if with_amb then
358     sink.lua {
359         name = "night_mode",
360         inputs = { NightMode = mdb.select.select_night_mode,
361                    amb_state = mdb.select.amb_state },
362         property = "NightMode",
363         type = "b",
364         initiate = builtin.method.amb_initiate,
365         update = builtin.method.amb_update
366     }
367 end
368
369 -- Night mode general handlers
370
371 if with_system_controller then
372     sink.lua {
373         name = "nightmode_homescreen",
374         inputs = { owner = mdb.select.select_night_mode },
375         initiate = function(self)
376                 -- data = mdb.select.select_night_mode.single_value
377                 return true
378             end,
379         update = function(self)
380                 send_night_mode_to_home_screen()
381             end
382     }
383 end
384
385 -- Driving mode processing chain
386
387 element.lua {
388     name    = "drivingmode",
389     inputs  = { speed = mdb.select.vehicle_speed },
390     oldmode = -1;
391     outputs = {
392         mdb.table {
393             name = "amb_drivingmode",
394             index = { "id" },
395             create = true,
396             columns = {
397                 { "id", mdb.unsigned },
398                 { "driving_mode", mdb.unsigned }
399             }
400         }
401     },
402     update = function(self)
403
404         speed = self.inputs.speed.single_value
405
406         if not speed then
407             return
408         end
409
410         if speed == 0 then
411             mode = 0
412         else
413             mode = 1
414         end
415
416         if not (mode == self.oldmode) then
417             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
418         end
419
420         self.oldmode = mode
421     end
422 }
423
424 mdb.select {
425     name = "select_driving_mode",
426     table = "amb_drivingmode",
427     columns = { "driving_mode" },
428     condition = "id = 0"
429 }
430
431 if with_amb then
432     sink.lua {
433         name = "driving_mode",
434         inputs = { DrivingMode = mdb.select.select_driving_mode,
435                    amb_state = mdb.select.amb_state },
436         property = "DrivingMode",
437         type = "u",
438         initiate = builtin.method.amb_initiate,
439         update = builtin.method.amb_update
440     }
441 end
442
443 -- turn signals (left, right)
444
445 mdb.select {
446     name = "winker",
447     table = "amb_turn_signal",
448     columns = { "value" },
449     condition = "key = 'TurnSignal'"
450 }
451
452 -- define three categories
453
454 mdb.select {
455     name = "undefined_applications",
456     table = "aul_applications",
457     columns = { "appid" },
458     condition = "category = '<undefined>'"
459 }
460
461 mdb.select {
462     name = "basic_applications",
463     table = "aul_applications",
464     columns = { "appid" },
465     condition = "category = 'basic'"
466 }
467
468 mdb.select {
469     name = "entertainment_applications",
470     table = "aul_applications",
471     columns = { "appid" },
472     condition = "category = 'entertainment'"
473 }
474
475 function ft(t)
476     -- filter the object garbage out of the tables
477     ret = {}
478
479     for k,v in pairs(t) do
480         if k ~= "userdata" and k ~= "new" then
481             ret[k] = v
482         end
483     end
484
485     return ret
486 end
487
488 function getApplication(appid)
489     local conf = nil
490
491     -- find the correct local application definition
492
493     for k,v in pairs(ft(application)) do
494         if appid == v.appid then
495             conf = v
496             break
497         end
498     end
499
500     return conf
501 end
502
503 function regulateApplications(t, regulation)
504     for k,v in pairs(ft(t)) do
505
506         -- iterate through the undefined and entertainment apps, see if
507         -- they have been overruled in local config
508
509         local conf = getApplication(v.appid)
510
511         if conf and conf.resource_class ~= "player" then
512             -- override, don't disable
513             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
514         else
515             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
516         end
517     end
518     resource.method.recalc("driver")
519 end
520
521 -- regulation (on), use "select_driving_mode"
522
523 sink.lua {
524     name = "driving_regulation",
525     inputs = { owner = mdb.select.select_driving_mode },
526     initiate = function(self)
527         -- local data = mdb.select.select_driving_mode.single_value
528         return true
529     end,
530     update = function(self)
531         local data = mdb.select.select_driving_mode.single_value
532
533         if verbose > 1 then
534             print("Driving mode updated: " .. tostring(data))
535         end
536
537         if not sc then
538             return true
539         end
540
541         -- tell homescreen that driving mode was updated
542         send_driving_mode_to_home_screen()
543
544         regulateApplications(ft(mdb.select.entertainment_applications), data)
545         regulateApplications(ft(mdb.select.undefined_applications), data)
546
547         return true
548     end
549 }
550 --[[
551 sink.lua {
552     name = "regulated_app_change",
553     inputs = { undef = mdb.select.undefined_applications,
554                entertainment = mdb.select.entertainment_applications },
555     initiate = function(self)
556         return true
557     end,
558     update = function(self)
559         local data = mdb.select.select_driving_mode.single_value
560
561         if not sc then
562             return
563         end
564
565         if verbose > 1 then
566             print("regulated application list was changed")
567         end
568
569         regulateApplications(ft(mdb.select.entertainment_applications), data)
570         regulateApplications(ft(mdb.select.undefined_applications), data)
571
572         return true
573     end
574 }
575 --]]
576
577 -- shift position (parking, reverse, other)
578
579 mdb.select {
580     name = "gear_position",
581     table = "amb_gear_position",
582     columns = { "value" },
583     condition = "key = 'GearPosition'"
584 }
585
586 -- cameras (back, front, left, right)
587
588 element.lua {
589     name    = "camera_state",
590     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
591     oldmode = -1;
592     outputs = {
593         mdb.table {
594             name = "target_camera_state",
595             index = { "id" },
596             create = true,
597             columns = {
598                 { "id", mdb.unsigned },
599                 { "front_camera", mdb.unsigned },
600                 { "back_camera", mdb.unsigned },
601                 { "right_camera", mdb.unsigned },
602                 { "left_camera", mdb.unsigned }
603             }
604         }
605     },
606     update = function(self)
607
608         front_camera = 0
609         back_camera = 0
610         right_camera = 0
611         left_camera = 0
612
613         if self.inputs.gear == 128 then
614             back_camera = 1
615         elseif self.inputs.winker == 1 then
616             right_camera = 1
617         elseif self.inputs.winker == 2 then
618             left_camera = 1
619         end
620
621         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
622
623     end
624 }
625
626 -- system controller test setup
627
628 if not with_system_controller then
629    -- ok, we should have 'audio_playback' defined by now
630    m:try_load_plugin('telephony')
631    return
632 end
633
634 m:load_plugin('system-controller')
635
636 window_manager_operation_names = {
637     [1] = "create",
638     [2] = "destroy"
639 }
640
641 function window_manager_operation_name(oper)
642     local name = window_manager_operation_names[oper]
643     if name then return name end
644     return "<unknown " .. tostring(oper) .. ">"
645 end
646
647 window_operation_names = {
648     [1] = "create",
649     [2] = "destroy",
650     [3] = "name_change",
651     [4] = "visible",
652     [5] = "configure",
653     [6] = "active"
654 }
655
656 function window_operation_name(oper)
657     local name = window_operation_names[oper]
658     if name then return name end
659     return "<unknown " .. tostring(oper) .. ">"
660 end
661
662 layer_operation_names = {
663     [1] = "create",
664     [2] = "destroy",
665     [3] = "visible"
666 }
667
668 function layer_operation_name(oper)
669     local name = layer_operation_names[oper]
670     if name then return name end
671     return "<unknown " .. tostring(oper) .. ">"
672 end
673
674 input_manager_operation_names = {
675     [1] = "create",
676     [2] = "destroy",
677     [3] = "ready"
678 }
679
680 function input_manager_operation_name(oper)
681     local name = input_manager_operation_names[oper]
682     if name then return name end
683     return "<unknown " .. tostring(oper) .. ">"
684 end
685
686 input_operation_names = {
687     [1] = "create",
688     [2] = "destroy",
689     [3] = "update"
690 }
691
692 function input_operation_name(oper)
693     local name = input_operation_names[oper]
694     if name then return name end
695     return "<unknown " .. tostring(oper) .. ">"
696 end
697
698 code_operation_names = {
699     [1] = "create",
700     [2] = "destroy",
701     [3] = "state_change"
702 }
703
704 function code_operation_name(oper)
705     local name = code_operation_names[oper]
706     if name then return name end
707     return "<unknown " .. tostring(oper) .. ">"
708 end
709
710 command_names = {
711     [0x00001] = "send_appid",
712     [0x10001] = "create",
713     [0x10002] = "destroy",
714     [0x10003] = "show",
715     [0x10004] = "hide",
716     [0x10005] = "move",
717     [0x10006] = "change_active",
718     [0x10007] = "change_layer",
719     [0x10008] = "change_attr",
720     [0x10009] = "name",
721     [0x10011] = "map_thumb",
722     [0x10012] = "unmap_thumb",
723     [0x10020] = "show layer",
724     [0x10021] = "hide_layer",
725     [0x10022] = "change_layer_attr",
726     [0x20001] = "add_input",
727     [0x20002] = "del_input",
728     [0x20003] = "send_input",
729     [0x40001] = "acquire_res",
730     [0x40002] = "release_res",
731     [0x40003] = "deprive_res",
732     [0x40004] = "waiting_res",
733     [0x40005] = "revert_res",
734     [0x40011] = "create_res",
735     [0x40012] = "destroy_res",
736     [0x50001] = "set_region",
737     [0x50002] = "unset_region",
738     [0x60001] = "change_state"
739 }
740
741 function command_name(command)
742     local name = command_names[command]
743     if name then return name end
744     return "<unknown " .. tostring(command) .. ">"
745 end
746
747 input_layer = {
748    [3] = true, -- input
749    [4] = true, -- touch
750    [5] = true  -- cursor
751 }
752
753 resmgr = resource_manager {
754   screen_event_handler = function(self, ev)
755                              local event = ev.event
756                              local surface = ev.surface
757
758                              if event == "init" then
759                                  if verbose > 0 then
760                                      print("*** init screen resource allocation -- disable all 'player'")
761                                  end
762                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
763                              elseif event == "preallocate" then
764                                  if verbose > 0 then
765                                      print("*** preallocate screen resource "..
766                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
767                                  end
768                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
769                              elseif event == "grant" then
770                                  if verbose > 0 then
771                                     print("*** make visible surface "..surface)
772                                  end
773                                  local a = animation({})
774                                  local r = m:JSON({surface = surface,
775                                                    visible = 1,
776                                                    raise   = 1})
777                                  wmgr:window_request(r,a,0)
778                              elseif event == "revoke" then
779                                  if verbose > 0 then
780                                     print("*** hide surface "..surface)
781                                  end
782                                  local a = animation({})
783                                  local r = m:JSON({surface = ev.surface,
784                                                    visible = 0})
785                                  wmgr:window_request(r,a,0)
786
787                              elseif event == "create" then
788
789                                 if verbose > 0 then
790                                     print("*** screen resource event: " ..
791                                           tostring(ev))
792                                 end
793
794                                 local regulation = mdb.select.select_driving_mode.single_value
795
796                                 if regulation == 1 then
797
798                                     local blacklisted = false
799
800                                     -- applications which have their category set to "entertainment"
801                                     -- or "undefined" are blacklisted, meaning they should be regulated
802
803                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
804                                         if v.appid == ev.appid then
805                                             if verbose > 0 then
806                                                 print(ev.appid .. " was blacklisted (undefined)")
807                                             end
808                                             blacklisted = true
809                                             break
810                                         end
811                                     end
812
813                                     if not blacklisted then
814                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
815                                             if v.appid == ev.appid then
816                                                 if verbose > 0 then
817                                                     print(ev.appid .. " was blacklisted (entertainment)")
818                                                 end
819                                                 blacklisted = true
820                                                 break
821                                             end
822                                         end
823                                     end
824
825                                     -- our local application config, which takes precedence
826                                     local conf = getApplication(ev.appid)
827
828                                     -- disable only non-whitelisted applications
829                                     if not conf or conf.resource_class == "player" then
830                                         if blacklisted then
831                                             if verbose > 0 then
832                                                 print("disabling screen for " .. ev.appid)
833                                             end
834                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
835                                         end
836                                     end
837                                 end
838
839                              elseif event == "destroy" then
840                                if verbose > 0 then
841                                     print("*** screen resource event: " ..
842                                           tostring(ev))
843                                  end
844                              else
845                                  if verbose > 0 then
846                                     print("*** screen resource event: " ..
847                                           tostring(ev))
848                                  end
849                              end
850                          end,
851   audio_event_handler = function(self, ev)
852                              local event = ev.event
853                              local appid = ev.appid
854                              local audioid = ev.audioid
855
856                              if event == "grant" then
857                                  if verbose > 0 then
858                                     print("*** grant audio to "..appid..
859                                           " ("..audioid..") in '" ..
860                                           ev.zone .. "' zone")
861                                  end
862                              elseif event == "revoke" then
863                                  if verbose > 0 then
864                                     print("*** revoke audio from "..appid..
865                                           " ("..audioid..") in '" ..
866                                           ev.zone .. "' zone")
867                                  end
868                              else
869                                  if verbose > 0 then
870                                     print("*** audio resource event: " ..
871                                           tostring(ev))
872                                  end
873                              end
874                         end
875 }
876
877 resclnt = resource_client {}
878
879 wmgr = window_manager {
880   geometry = function(self, w,h, v)
881                   if type(v) == "function" then
882                       return v(w,h)
883                   end
884                   return v
885              end,
886
887   application = function(self, appid)
888                      if appid then
889                          local app = application_lookup(appid)
890                          if not app then
891                              app = application_lookup("default")
892                          end
893                          return app
894                      end
895                      return { privileges = {screen="none", audio="none"} }
896                 end,
897
898   output_order = { 1, 0 },
899
900   outputs = { { name  = "Mid",
901                 id    = 0,
902                 zone  = "driver",
903                 areas = { Full = {
904                               id     = 20,
905                               pos_x  = 0,
906                               pos_y  = 0,
907                               width  = function(w,h) return w end,
908                               height = function(w,h) return h end
909                           },
910                           Left = {
911                               id     = 21,
912                               pos_x  = 0,
913                               pos_y  = 0,
914                               width  = 320,
915                               height = function(w,h) return h end
916                           },
917                           Right = {
918                               id     = 22,
919                               pos_x  = function(w,h) return w-320 end,
920                               pos_y  = 0,
921                               width  = 320,
922                               height = function(w,h) return h end
923                           }
924                         }
925                },
926                { name  = "Center",
927                  id    = 1,
928                  zone  = "driver",
929                  areas = { Status = {
930                               id     = 0,
931                               pos_x  = 0,
932                               pos_y  = 0,
933                               width  = function(w,h) return w end,
934                               height = 64
935                            },
936                            Full = {
937                               id     = 1,
938                               pos_x  = 0,
939                               pos_y  = 64,
940                               width  = function(w,h) return w end,
941                               height = function(w,h) return h-64-128 end
942                            },
943                            Upper = {
944                               id     = 2,
945                               pos_x  = 0,
946                               pos_y  = 64,
947                               width  = function(w,h) return w end,
948                               height = function(w,h) return (h-64-128)/2 end
949                            },
950                            Lower = {
951                               id     = 3,
952                               pos_x  = 0,
953                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
954                               width  = function(w,h) return w end,
955                               height = function(w,h) return (h-64-128)/2 end
956                            },
957                            UpperLeft = {
958                               id     = 4,
959                               pos_x  = 0,
960                               pos_y  = 64,
961                               width  = function(w,h) return w/2 end,
962                               height = function(w,h) return (h-64-128)/2 end
963                            },
964                            UpperRight = {
965                               id     = 5,
966                               pos_x  = function(w,h) return w/2 end,
967                               pos_y  = 64,
968                               width  = function(w,h) return w/2 end,
969                               height = function(w,h) return (h-64-128)/2 end
970                            },
971                            LowerLeft = {
972                               id     = 6,
973                               pos_x  = 0,
974                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
975                               width  = function(w,h) return w/2 end,
976                               height = function(w,h) return (h-64-128)/2 end
977                            },
978                            LowerRight = {
979                               id     = 7,
980                               pos_x  = function(w,h) return w/2 end,
981                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
982                               width  = function(w,h) return w/2 end,
983                               height = function(w,h) return (h-64-128)/2 end
984                            },
985                            SysApp = {
986                               id     = 8,
987                               pos_x  = 0,
988                               pos_y  = 64,
989                               width  = function(w,h) return w end,
990                               height = function(w,h) return h-64-128 end
991                            },
992                            ["SysApp.Left"] = {
993                               id     = 9,
994                               pos_x  = 0,
995                               pos_y  = 64,
996                               width  = function(w,h) return w/2-181 end,
997                               height = function(w,h) return h-64-128 end
998                            },
999                            ["SysApp.Right"] = {
1000                               id     = 10,
1001                               pos_x  = function(w,h) return w/2+181 end,
1002                               pos_y  = 64,
1003                               width  = function(w,h) return w/2-181 end,
1004                               height = function(w,h) return h-64-128 end
1005                            },
1006                            Control = {
1007                               id     = 11,
1008                               pos_x  = 0,
1009                               pos_y  = function(w,h) return h-128 end,
1010                               width  = function(w,h) return w end,
1011                               height = 128
1012                            },
1013                         }
1014               }
1015   },
1016   layers = { {      0, "Background"   , 1 },
1017              {      1, "Application"  , 2 },
1018              {      2, "Softkeyboard" , 4 },
1019              {      3, "HomeScreen"   , 2 },
1020              {      4, "ControlBar"   , 2 },
1021              {      5, "InterruptApp" , 2 },
1022              {      6, "OnScreen"     , 2 },
1023              {    101, "Input"        , 3 },
1024              {    102, "Cursor"       , 5 },
1025              {    103, "Startup"      , 6 },
1026              { 0x1000, "Background"   , 1 },
1027              { 0x2000, "Normal"       , 2 },
1028              { 0x3000, "Fullscreen"   , 2 },
1029              { 0x4000, "InputPanel"   , 3 },
1030              { 0xA000, "Touch"        , 4 },
1031              { 0xB000, "Cursor"       , 5 },
1032              { 0xC000, "Startup"      , 6 }
1033   },
1034
1035
1036   manager_update = function(self, oper)
1037                        if verbose > 0 then
1038                            print("### <== WINDOW MANAGER UPDATE:" ..
1039                                  window_manager_operation_name(oper))
1040                        end
1041                        if oper == 1 then
1042                            local wumask = window_mask { raise   = true,
1043                                                         visible = true,
1044                                                         active  = true }
1045                            local wrmask = window_mask { active  = true }
1046                            local lumask = layer_mask  { visible = true }
1047                            local lrmask = layer_mask  { visible = true }
1048                            local req = m:JSON({
1049                                passthrough_window_update  = wumask:tointeger(),
1050                                passthrough_window_request = wrmask:tointeger(),
1051                                passthrough_layer_update   = lumask:tointeger(),
1052                                passthrough_layer_request  = lrmask:tointeger()
1053                            })
1054                            self:manager_request(req)
1055                        end
1056                    end,
1057
1058   window_update = function(self, oper, win, mask)
1059                       if verbose > 0 then
1060                           print("### <== WINDOW UPDATE oper:" ..
1061                                 window_operation_name(oper) ..
1062                                 " mask: " .. tostring(mask))
1063                           if verbose > 1 then
1064                               print(win)
1065                           end
1066                       end
1067
1068                       local arg = m:JSON({ surface = win.surface,
1069                                            winname = win.name,
1070                       })
1071                       local command = 0
1072
1073                       if oper == 1 then -- create
1074                            local layertype = win.layertype
1075                            if layertype and input_layer[layertype] then
1076                                if verbose > 0 then
1077                                    print("ignoring input panel creation")
1078                                end
1079                                return
1080                            end
1081                            command     = 0x10001
1082                       elseif oper == 2 then -- destroy
1083                            command     = 0x10002
1084                       elseif oper == 3 then  -- namechange
1085                            command     = 0x10009
1086                       elseif oper == 4 or oper == 5 then --visible or configure
1087                            command     = 0x10008
1088                            arg.zone    = win.area
1089                            arg.node    = win.node
1090                            arg.layer   = win.layer
1091                            arg.pos_x   = win.pos_x
1092                            arg.pos_y   = win.pos_y
1093                            arg.width   = win.width
1094                            arg.height  = win.height
1095                            arg.raise   = win.raise
1096                            arg.visible = win.visible
1097                            arg.active  = win.active
1098                       elseif oper == 6 then -- active
1099                            command     = 0x10006
1100                            arg.active  = win.active
1101                       else
1102                            if verbose > 0 then
1103                                print("### nothing to do")
1104                            end
1105                            return
1106                       end
1107
1108                       local msg = m:JSON({ command = command,
1109                                            appid   = win.appid,
1110                                            pid     = win.pid,
1111                                            arg     = arg
1112                       })
1113                       if verbose > 0 then
1114                           print("### <== sending " ..
1115                                 command_name(msg.command) ..
1116                                 " window message to '" .. win.name .. "'")
1117                           if verbose > 1 then
1118                               print(msg)
1119                           end
1120                       end
1121                       sc:send_message(homescreen, msg)
1122
1123                       if oper == 1 then -- create
1124                           local i = input_layer[win.layertype]
1125                           local p = self:application(win.appid)
1126                           local s = p.privileges.screen
1127
1128                           if s == "system" then
1129                               local a = animation({})
1130                               local r = m:JSON({surface = win.surface,
1131                                                 visible = 1,
1132                                                 raise   = 1})
1133                               self:window_request(r,a,0)
1134                           else
1135                               if i then
1136                                   if verbose > 0 then
1137                                       print("do not make resource for " ..
1138                                             "input window")
1139                                   end
1140                               else
1141                                   resclnt:resource_set_create("screen",
1142                                                                "driver",
1143                                                                win.appid,
1144                                                                win.surface)
1145                                   special_screen_sets[win.surface] = true
1146                               end
1147                           end
1148                       elseif oper == 2 then -- destroy
1149                           resclnt:resource_set_destroy("screen", win.surface)
1150                           special_screen_sets[win.surface] = nil
1151                       elseif oper == 6 then -- active
1152                           if win.active then
1153                               local i = input_layer[win.layertype]
1154                               local p = self:application(win.appid)
1155                               local s = p.privileges.screen
1156                               local surface = win.surface
1157                               if not i and s ~= "system" then
1158                                  resclnt:resource_set_acquire("screen",surface)
1159                                  resmgr:window_raise(win.appid, surface, 1)
1160                               end
1161                           end
1162                       end
1163                   end,
1164
1165   layer_update = function(self, oper, layer, mask)
1166                       if verbose > 0 then
1167                           print("### LAYER UPDATE:" ..
1168                                 layer_operation_name(oper) ..
1169                                 " mask: " .. tostring(mask))
1170                           if verbose > 1 then
1171                               print(layer)
1172                           end
1173                       end
1174                       if oper == 3 then -- visible
1175                          local command = 0x10022
1176                          local msg = m:JSON({
1177                                          command = command,
1178                                          appid = "",
1179                                          arg = m:JSON({layer = layer.id,
1180                                                        visible = layer.visible
1181                                          })
1182                                 })
1183                          if verbose > 0 then
1184                             print("### <== sending "..command_name(command)..
1185                                   " layer message")
1186                             if verbose > 1 then
1187                                 print(msg)
1188                             end
1189                          end
1190                          sc:send_message(homescreen, msg)
1191                       else
1192                            if verbose > 0 then
1193                                print("### nothing to do")
1194                            end
1195                       end
1196                  end,
1197
1198   output_update = function(self, oper, out, mask)
1199                       local idx = out.index
1200                       local defidx = self.output_order[idx+1]
1201                       if verbose > 0 then
1202                           print("### OUTPUT UPDATE:" .. oper ..
1203                                 " mask: "..tostring(mask))
1204                       end
1205                       if not defidx then
1206                           return
1207                       end
1208                       print(out)
1209                       local outdef = self.outputs[defidx+1]
1210                       if (oper == 1) then -- create
1211                           if outdef then
1212                               self:output_request(m:JSON({index = idx,
1213                                                           id    = outdef.id,
1214                                                           name  = outdef.name
1215                                                           }))
1216                           end
1217                       elseif (oper == 5) then -- done
1218                           local ads = outdef.areas
1219                           local on = outdef.name
1220                           if ads then
1221                               for name,ad in pairs(ads) do
1222                                   local can = wmgr:canonical_name(on.."."..name)
1223                                   local a = m:JSON({name   = name,
1224                                                     output = out.index})
1225                                   for fld,val in pairs(ad) do
1226                                       a[fld] = self:geometry(out.width,
1227                                                              out.height,
1228                                                              val)
1229                                    end
1230                                    self:area_create(a)
1231                                    resmgr:area_create(area[can], outdef.zone)
1232                               end
1233                           end
1234                       end
1235                   end
1236 }
1237
1238
1239 imgr = input_manager {
1240   inputs = {{ name = "G27 Racing Wheel",
1241               id = 0,
1242               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1243                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1244                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1245                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1246              }}
1247   },
1248
1249   manager_update = function(self, oper)
1250                        if verbose > 0 then
1251                            print("### <== INPUT MANAGER UPDATE:" ..
1252                                  input_manager_operation_name(oper))
1253                        end
1254                    end,
1255
1256   input_update = function(self, oper, inp, mask)
1257                      if verbose > 0 then
1258                          print("### INPUT UPDATE:" ..
1259                                 input_operation_name(oper) ..
1260                                 " mask: " .. tostring(mask))
1261                           if verbose > 1 then
1262                               print(inp)
1263                           end
1264                       end
1265                  end,
1266   code_update  = function(self, oper, code, mask)
1267                      if verbose > 0 then
1268                          print("### CODE UPDATE: mask: " .. tostring(mask))
1269                          if verbose > 1 then
1270                              print(code)
1271                          end
1272                      end
1273                      local msg = m:JSON({ command = 1,
1274                                           appid = "org.tizen.ico.homescreen",
1275                                           arg = m:JSON({ device = code.device,
1276                                                          time = code.time,
1277                                                          input = code.input,
1278                                                          code = code.id,
1279                                                          state = code.state
1280                                            })
1281                      })
1282                      if verbose > 0 then
1283                          print("### <== sending " ..
1284                                command_name(msg.command) ..
1285                                " input message")
1286                          if verbose > 1 then
1287                              print(msg)
1288                          end
1289                      end
1290                      sc:send_message(homescreen, msg)
1291                  end
1292 }
1293
1294 sc = m:get_system_controller()
1295
1296 -- resource sets
1297 sets = {}
1298
1299 -- special screen resource sets
1300 -- TODO: just rewrite screen resource handling to use regular resource API
1301
1302 special_screen_sets = {}
1303
1304 -- user manager
1305 um = m:UserManager()
1306
1307 connected = false
1308 homescreen = ""
1309 onscreen = ""
1310
1311 -- these shoud be before wmgr:connect() is called
1312 if verbose > 0 then
1313    print("====== creating applications ======")
1314 end
1315 application {
1316     appid          = "default",
1317     area           = "Center.Full",
1318     privileges     = { screen = "none", audio = "none" },
1319     resource_class = "player",
1320     screen_priority = 0
1321 }
1322
1323 application {
1324     appid           = "weston",
1325     area            = "Center.Full",
1326     privileges      = { screen = "system", audio = "none" },
1327     resource_class  = "implicit",
1328     screen_priority = 30
1329 }
1330
1331 application {
1332     appid           = "org.tizen.ico.homescreen",
1333     area            = "Center.Full",
1334     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1335     privileges      = { screen = "system", audio = "system" },
1336     resource_class  = "player",
1337     screen_priority = 20
1338 }
1339
1340 application {
1341     appid           = "org.tizen.ico.statusbar",
1342     area            = "Center.Status",
1343     privileges      = { screen = "system", audio = "none" },
1344     resource_class  = "player",
1345     screen_priority = 20
1346 }
1347
1348 application {
1349     appid           = "org.tizen.ico.onscreen",
1350     area            = "Center.Full",
1351     privileges      = { screen = "system", audio = "system" },
1352     resource_class  = "player",
1353     screen_priority = 20
1354 }
1355
1356 application {
1357     appid           = "org.tizen.ico.login",
1358     area            = "Center.Full",
1359     privileges      = { screen = "system", audio = "system" },
1360     resource_class  = "player",
1361     screen_priority = 20
1362 }
1363
1364 application {
1365     appid           = "org.tizen.ico.camera_left",
1366     area            = "Center.SysApp.Left",
1367     privileges      = { screen = "system", audio = "none" },
1368     requisites      = { screen = "blinker_left", audio = "none" },
1369     resource_class  = "player",
1370     screen_priority = 30
1371 }
1372
1373 application {
1374     appid           = "org.tizen.ico.camera_right",
1375     area            = "Center.SysApp.Right",
1376     privileges      = { screen = "system", audio = "none" },
1377     requisites      = { screen = "blinker_right", audio = "none" },
1378     resource_class  = "player",
1379     screen_priority = 30
1380 }
1381
1382 application {
1383     appid           = "net.zmap.navi",
1384     area            = "Center.Full",
1385     privileges      = { screen = "none", audio = "none" },
1386     resource_class  = "navigator",
1387     screen_priority = 30
1388 }
1389
1390 application {
1391     appid           = "GV3ySIINq7.GhostCluster",
1392     area            = "Mid.Full",
1393     privileges      = { screen = "none", audio = "none" },
1394     resource_class  = "system",
1395     screen_priority = 30
1396 }
1397
1398 if sc then
1399     sc.client_handler = function (self, cid, msg)
1400         local command = msg.command
1401         local appid = msg.appid
1402         if verbose > 0 then
1403             print('### ==> client handler:')
1404             if verbose > 1 then
1405                 print(msg)
1406             end
1407         end
1408
1409         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1410         -- disconnection
1411
1412         if command == 0xFFFF then
1413             if verbose > 1 then
1414                 print('client ' .. cid .. ' (' .. msg.appid .. ') disconnected')
1415             end
1416             if msg.appid == homescreen then
1417                 homescreen = ""
1418                 for i,v in pairs(special_screen_sets) do
1419                     resclnt:resource_set_destroy("screen", i)
1420                     special_screen_sets[i] = nil
1421                 end
1422             end
1423             return
1424         end
1425
1426         -- handle the connection to weston
1427
1428         if appid then
1429             if appid == "org.tizen.ico.homescreen" then
1430                 print('Setting homescreen='..appid)
1431                 homescreen = appid
1432                 if command and command == 1 then
1433                     send_driving_mode_to_home_screen()
1434                     send_night_mode_to_home_screen()
1435                 end
1436             elseif appid == "org.tizen.ico.onscreen" then
1437                 onscreen = appid
1438             end
1439
1440             if not connected and appid == "org.tizen.ico.homescreen" then
1441                 print('Trying to connect to weston...')
1442                 connected = wmgr:connect()
1443             end
1444         end
1445     end
1446
1447     sc.generic_handler = function (self, cid, msg)
1448         if verbose > 0 then
1449             print('### ==> generic handler:')
1450             if verbose > 1 then
1451                print(msg)
1452             end
1453         end
1454     end
1455
1456     sc.window_handler = function (self, cid, msg)
1457         if verbose > 0 then
1458             print('### ==> received ' ..
1459                    command_name(msg.command) .. ' message')
1460             if verbose > 1 then
1461                 print(tostring(msg))
1462             end
1463         end
1464
1465         local a = animation({})
1466         local nores = false
1467         if msg.command == 0x10003 then       -- ico SHOW command
1468             local raise_mask = 0x01000000
1469             local lower_mask = 0x02000000
1470             local nores_mask = 0x40000000
1471             local time_mask  = 0x00ffffff
1472             msg.arg.visible = 1
1473             if msg.arg then
1474                 local time = 200
1475                 if  msg.arg.anim_time then
1476                     local t = msg.arg.anim_time
1477                     time = m:AND(t, time_mask)
1478                     nores = not m:AND(t, nores_mask)
1479                     if m:AND(t, raise_mask) then
1480                         msg.arg.raise = 1
1481                     elseif m:AND(t, lower_mask) then
1482                         msg.arg.raise = 0
1483                     end
1484                 end
1485                 if msg.arg.anim_name then
1486                     a.show = { msg.arg.anim_name, time }
1487                     print('time: ' .. tostring(a.show[2]))
1488                 end
1489             end
1490             if not nores then
1491                 local p = wmgr:application(msg.appid)
1492                 local s = p.privileges.screen
1493                 if s == "system" then
1494                     nores = true
1495                     if not msg.arg.raise then
1496                         msg.arg.raise = 1
1497                     end
1498                 end
1499             end
1500             if verbose > 2 then
1501                 print('### ==> SHOW')
1502                 print(tostring(msg.arg))
1503             end
1504             if nores then
1505                 wmgr:window_request(msg.arg, a, 0)
1506             else
1507                 local surface = msg.arg.surface
1508                 resclnt:resource_set_acquire("screen", surface)
1509                 resmgr:window_raise(msg.appid, surface, 1)
1510             end
1511         elseif msg.command == 0x10004 then   -- ico HIDE command
1512             local raise_mask = 0x01000000
1513             local lower_mask = 0x02000000
1514             local nores_mask = 0x40000000
1515             local time_mask  = 0x00ffffff
1516             msg.arg.visible = 0
1517             if msg.arg then
1518                 local time = 200
1519                 if msg.arg.anim_time then
1520                     local t = msg.arg.anim_time
1521                     time = m:AND(t, time_mask)
1522                     nores = not m:AND(t, nores_mask)
1523                 end
1524                 if msg.arg.anim_name then
1525                     a.hide = { msg.arg.anim_name, time }
1526                     print('hide animation time: ' .. tostring(a.hide[2]))
1527                 end
1528             end
1529             if not nores then
1530                 local p = wmgr:application(msg.appid)
1531                 local s = p.privileges.screen
1532                 if s == "system" then
1533                     nores = true
1534                     msg.arg.raise = 0
1535                 end
1536             end
1537             if verbose > 2 then
1538                 print('### ==> HIDE REQUEST')
1539                 print(tostring(msg.arg))
1540             end
1541             if nores then
1542                 wmgr:window_request(msg.arg, a, 0)
1543             else
1544                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1545             end
1546         elseif msg.command == 0x10005 then   -- ico MOVE
1547             if verbose > 2 then
1548                 print('### ==> MOVE REQUEST')
1549                 print(tostring(msg.arg))
1550             end
1551             if msg.arg.zone then
1552                 msg.arg.area = msg.arg.zone
1553             end
1554             wmgr:window_request(msg.arg, a, 0)
1555             -- TODO: handle if area changed
1556         elseif msg.command == 0x10006 then   -- ico ACTIVE
1557             if not msg.arg.active then
1558                 msg.arg.active = 3 -- pointer + keyboard
1559             end
1560             if verbose > 2 then
1561                 print('### ==> ACTIVE REQUEST')
1562                 print(tostring(msg.arg))
1563             end
1564             wmgr:window_request(msg.arg, a, 0)
1565         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1566             if verbose > 2 then
1567                 print('### ==> CHANGE_LAYER REQUEST')
1568                 print(tostring(msg.arg))
1569             end
1570             --[[
1571             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1572                 print("do not change layer for other than cursor or touch")
1573                 return
1574             end
1575             --]]
1576             wmgr:window_request(msg.arg, a, 0)
1577         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1578             local framerate = msg.arg.framerate
1579             if not framerate or framerate < 0 then
1580                 framerate = 0
1581             end
1582             msg.arg.map = 1
1583             if verbose > 2 then
1584                 print('### ==> MAP_THUMB REQUEST')
1585                 print(msg.arg)
1586                 print('framerate: '..framerate)
1587             end
1588             wmgr:window_request(msg.arg, a, framerate)
1589         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1590             msg.arg.map = 0
1591             if verbose > 2 then
1592                 print('### ==> UNMAP_THUMB REQUEST')
1593                 print(msg.arg)
1594             end
1595             wmgr:window_request(msg.arg, a, 0)
1596         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1597             msg.arg.visible = 1
1598             if verbose > 2 then
1599                 print('### ==> SHOW_LAYER REQUEST')
1600                 print(msg.arg)
1601             end
1602             wmgr:layer_request(msg.arg)
1603         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1604             msg.arg.visible = 0
1605             if verbose > 2 then
1606                 print('### ==> HIDE_LAYER REQUEST')
1607                 print(msg.arg)
1608             end
1609             wmgr:layer_request(msg.arg)
1610         end
1611     end
1612
1613     sc.input_handler = function (self, cid, msg)
1614         if verbose > 0 then
1615             print('### ==> input handler: ' .. command_name(msg.command))
1616             if verbose > 1 then
1617                 print(msg)
1618             end
1619         end
1620         if msg.command == 0x20001 then -- add_input
1621             msg.arg.appid = msg.appid
1622             if verbose > 2 then
1623                 print('### ==> ADD_INPUT REQUEST')
1624                 print(tostring(msg.arg))
1625             end
1626             imgr:input_request(msg.arg)
1627         elseif msg.command == 0x20002 then -- del_input
1628             msg.arg.appid = ''
1629             if verbose > 2 then
1630                 print('### ==> DEL_INPUT REQUEST')
1631                 print(tostring(msg.arg))
1632             end
1633             imgr:input_request(msg.arg)
1634         elseif msg.command == 0x20003 then -- send_input
1635         end
1636     end
1637
1638     sc.user_handler = function (self, cid, msg)
1639         if verbose > 0 then
1640             print('### ==> user handler: ' .. command_name(msg.command))
1641             if verbose > 1 then
1642                 print(msg)
1643             end
1644         end
1645
1646         if not um then
1647             print("User Manager not initialized")
1648             return
1649         end
1650
1651         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1652             print("command CHANGE_USER")
1653             if not msg.arg then
1654                 print("invalid message")
1655                 return
1656             end
1657
1658             username = msg.arg.user
1659             passwd = msg.arg.pass
1660
1661             if not username then
1662                 username = ""
1663             end
1664
1665             if not passwd then
1666                 passwd = ""
1667             end
1668
1669             success = um:changeUser(username, passwd)
1670
1671             if not success then
1672                 reply = m.JSON({
1673                     appid = msg.appid,
1674                     command = cmd
1675                 })
1676                 if sc:send_message(msg.appid, reply) then
1677                     print('*** sent authentication failed message')
1678                 else
1679                     print('*** failed to send authentication failed message')
1680                 end
1681             end
1682
1683         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1684             print("command GET_USERLIST")
1685             if not msg.appid then
1686                 print("invalid message")
1687                 return
1688             end
1689
1690             users, currentUser = um:getUserList()
1691
1692             if not users then
1693                 print("failed to get user list")
1694                 return
1695             end
1696
1697             nUsers = 0
1698
1699             for i,v in pairs(users) do
1700                 nUsers = nUsers + 1
1701             end
1702
1703             if not currentUser then
1704                 currentUser = ""
1705             end
1706
1707             if verbose > 1 then
1708                 print("current user: " .. currentUser)
1709                 print("user list:")
1710                 for i,v in pairs(users) do
1711                     print(v)
1712                 end
1713             end
1714
1715             reply = m.JSON({
1716                 appid = msg.appid,
1717                 command = cmd,
1718                 arg = m.JSON({
1719                     user_num = nUsers,
1720                     user_list = users,
1721                     user_login = currentUser
1722                 })
1723             })
1724
1725             if verbose > 1 then
1726                 print("### <== GetUserList reply: " .. tostring(reply))
1727             end
1728
1729             if sc:send_message(msg.appid, reply) then
1730                 print('*** reply OK')
1731             else
1732                 print('*** reply FAILED')
1733             end
1734
1735         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1736             print("command GET_LASTINFO")
1737             if not msg.appid then
1738                 print("invalid message")
1739                 return
1740             end
1741
1742             lastInfo = um:getLastInfo(msg.appid)
1743
1744             if not lastInfo then
1745                 print("failed to get last info for app" .. msg.appid)
1746                 return
1747             end
1748
1749             reply = m.JSON({
1750                 appid = msg.appid,
1751                 command = cmd,
1752                 arg = m.JSON({
1753                     lastinfo = lastinfo
1754                 })
1755             })
1756
1757             if sc:send_message(msg.appid, reply) then
1758                 print('*** reply OK')
1759             else
1760                 print('*** reply FAILED')
1761             end
1762
1763         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1764             print("command SET_LASTINFO")
1765             if not msg.arg or not msg.appid then
1766                 print("invalid message")
1767                 return
1768             end
1769
1770             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1771         end
1772     end
1773
1774     sc.resource_handler = function (self, cid, msg)
1775         if verbose > 0 then
1776             print('### ==> resource handler: ' .. command_name(msg.command))
1777             if verbose > 1 then
1778                 print(msg)
1779             end
1780         end
1781
1782         createResourceSet = function (ctl, client, msg)
1783             cb = function(rset, data)
1784                 print("> resource callback")
1785
1786                 -- type is either basic (0) or interrupt (1)
1787                 requestType = 0
1788                 if msg.res.type then
1789                     requestType = msg.res.type
1790                 end
1791
1792                 if rset.acquired then
1793                     cmd = 0x00040001 -- acquire
1794                 else
1795                     cmd = 0x00040002 -- release
1796                 end
1797
1798                 reply = m.JSON({
1799                         appid = data.client,
1800                         command = cmd,
1801                         res = {
1802                             type = requestType
1803                         }
1804                     })
1805
1806                 if rset.resources.audio_playback then
1807                     reply.res.sound = {
1808                         zone = "driver",
1809                         name = msg.appid,
1810                         adjust = 0,
1811                         -- id = "0"
1812                     }
1813                 end
1814
1815                 if rset.resources.display then
1816                     reply.res.window = {
1817                         zone = "driver",
1818                         name = msg.appid,
1819                         -- id = "0"
1820                     }
1821                 end
1822
1823                 if rset.resources.input then
1824                     reply.res.input = {
1825                         name = msg.appid,
1826                         event = 0
1827                     }
1828                 end
1829                 print("sending message to client: " .. data.client)
1830
1831                 if sc:send_message(data.client, reply) then
1832                     print('*** reply OK')
1833                 else
1834                     print('*** reply FAILED')
1835                 end
1836             end
1837
1838             rset = m:ResourceSet({
1839                     application_class = "player",
1840                     zone = "driver", -- msg.zone ("full")
1841                     callback = cb
1842                 })
1843
1844             rset.data = {
1845                 cid = cid,
1846                 ctl = ctl
1847             }
1848
1849             if msg.res.sound then
1850                 rset:addResource({
1851                         resource_name = "audio_playback"
1852                     })
1853                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1854                 rset.resources.audio_playback.attributes.appid = msg.appid
1855                 print("sound name: " .. msg.res.sound.name)
1856                 print("sound zone:" .. msg.res.sound.zone)
1857                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1858                 if msg.res.sound.id then
1859                     print("sound id: " .. msg.res.sound.id)
1860                 end
1861             end
1862
1863             if msg.res.input then
1864                 rset:addResource({
1865                         resource_name = "input"
1866                     })
1867                 rset.resources.input.attributes.pid = tostring(msg.pid)
1868                 rset.resources.input.attributes.appid = msg.appid
1869                 print("input name: " .. msg.res.sound.name)
1870                 print("input event:" .. tostring(msg.res.input.event))
1871             end
1872
1873             if msg.res.window then
1874                 rset:addResource({
1875                         resource_name = "display"
1876                     })
1877                 rset.resources.display.attributes.pid = tostring(msg.pid)
1878                 rset.resources.display.attributes.appid = msg.appid
1879                 print("display name: " .. msg.res.display.name)
1880                 print("display zone:" .. msg.res.display.zone)
1881                 if msg.res.display.id then
1882                     print("display id: " .. msg.res.display.id)
1883                 end
1884             end
1885
1886             return rset
1887         end
1888
1889         -- parse the message
1890
1891         -- fields common to all messages:
1892         --      msg.command
1893         --      msg.appid
1894         --      msg.pid
1895
1896         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1897             print("command CREATE")
1898
1899             if not sets.cid then
1900                 sets.cid = createResourceSet(self, cid, msg)
1901             end
1902
1903         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1904             print("command DESTROY")
1905
1906             if sets.cid then
1907                 sets.cid:release()
1908             end
1909
1910             sets.cid = nil -- garbage collecting
1911
1912         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1913             print("command ACQUIRE")
1914
1915             if not sets.cid then
1916                 sets.cid = createResourceSet(self, cid, msg)
1917             end
1918
1919             sets.cid:acquire()
1920
1921         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1922             print("command RELEASE")
1923
1924             if sets.cid then
1925                 sets.cid:release()
1926             end
1927
1928         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1929             print("command DEPRIVE")
1930
1931         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1932             print("command WAITING")
1933
1934         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1935             print("command REVERT")
1936         end
1937     end
1938
1939     sc.inputdev_handler = function (self, cid, msg)
1940         if verbose > 0 then
1941             print('*** inputdev handler: ' .. command_name(msg.command))
1942             if verbose > 1 then
1943                 print(msg)
1944             end
1945         end
1946     end
1947 end
1948
1949 function send_driving_mode_to_home_screen()
1950     if homescreen == "" then
1951         return
1952     end
1953
1954     local driving_mode = mdb.select.select_driving_mode.single_value
1955
1956     if not driving_mode then driving_mode = 0 end
1957
1958     local reply = m:JSON({ command = 0x60001,
1959                            arg     = m:JSON({ stateid = 1,
1960                                               state   = driving_mode
1961                                      })
1962                   })
1963
1964     if verbose > 0 then
1965         print("### <== sending " .. command_name(reply.command) .. " message")
1966         if verbose > 1 then
1967             print(reply)
1968         end
1969     end
1970
1971     sc:send_message(homescreen, reply)
1972 end
1973
1974 function send_night_mode_to_home_screen()
1975     if homescreen == "" then
1976         return
1977     end
1978
1979     local night_mode = mdb.select.select_night_mode.single_value
1980
1981     if not night_mode then night_mode = 0 end
1982
1983     local reply = m:JSON({ command = 0x60001,
1984                            arg     = m:JSON({ stateid = 2,
1985                                               state   = night_mode
1986                                      })
1987                   })
1988
1989     if verbose > 0 then
1990         print("### <== sending " .. command_name(reply.command) .. " message")
1991         if verbose > 1 then
1992             print(reply)
1993         end
1994      end
1995
1996      sc:send_message(homescreen, reply)
1997 end
1998
1999 -- we should have 'audio_playback' defined by now
2000 m:try_load_plugin('telephony')